Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2007-03-23 11:51:25

feragnoli
Member
From: the hague
Registered: 2005-02-10
Posts: 150

plugin if_category_parent… how to?

hello, I’m trying to make a conditional plugin to output certain content if we’re inside a category or any of its children.

I started from modifying txp:if_category to output a comma-separeted list of children categories from a specified “parent” one.
now I would like to use the built-in if_category in order to parse the content ($thing).

how do I pass the result from the first function (in this case nfe_catlist) to be checked by if_category..?

This is my first function:

nfe_catlist($atts) {
	global $s, $c;
	extract(lAtts(array(
		'active_class' => '',
		'break'        => ',',
		'categories'   => '',
		'class'        => __FUNCTION__,
		'exclude'      => '',
		'label'        => '',
		'labeltag'     => '',
		'parent'       => '',
		'section'      => '',
		'this_section' => 0,
		'type'         => 'article',
		'wraptag'      => '',
	), $atts));
	if ($categories)
	{
		$categories = do_list($categories);
		$categories = join("','", doSlash($categories));
		$rs = safe_rows_start('name, title', 'txp_category', 
			"type = '".doSlash($type)."' and name in ('$categories') order by field(name, '$categories')");
	}
	else
	{
		if ($exclude)
		{
			$exclude = do_list($exclude);
			$exclude = join("','", doSlash($exclude));
			$exclude = "and name not in('$exclude')";
		}
		if ($parent)
		{
			$qs = safe_row('lft, rgt', 'txp_category', "name = '".doSlash($parent)."'");
			if ($qs)
			{
				extract($qs);
				$rs = safe_rows_start('name, title', 'txp_category', 
					"(lft between $lft and $rgt) and type = '".doSlash($type)."' and name != 'default' $exclude order by lft asc");
			}
		}
		else
		{
			$rs = safe_rows_start('name, title', 'txp_category', 
				"type = '$type' and name not in('default','root') $exclude order by name");
		}
	}
	if ($rs)
	{
		$out = array();
		while ($a = nextRow($rs))
		{
			extract($a);
			if ($name)
			{
				$section = ($this_section) ? ( $s == 'default' ? '' : $s ) : $section;
				$out[] = $name;
			}
		}
		if ($out)
		{
			return implode($break, $out);
		}			
	}
}

Last edited by feragnoli (2007-03-26 11:41:13)


what was that again…?

Offline

#2 2007-03-23 12:25:05

ruud
Developer Emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

Re: plugin if_category_parent… how to?

It’s probably easier to call ‘parse’ directly than calling if_category to do this: echo parse($thing);

Your function definition should be nfe_catlist($atts, $thing) instead of nfe_catlist($atts)

Last edited by ruud (2007-03-23 12:26:18)

Offline

#3 2007-03-23 12:41:28

feragnoli
Member
From: the hague
Registered: 2005-02-10
Posts: 150

Re: plugin if_category_parent… how to?

oh, yes. but the nfe_catlist at the moment is just returning a list of values like this:

if 'parent' is defined then return 'parent,child1,child2,…'

at a certain point I have to evaluate that list against the current section, no?

like:

 if ('current') is inside 'parent,child1,child2,… then echo parse($thing);

… sorry for my php ignorance… how do you do that?

thank you very much.


what was that again…?

Offline

#4 2007-03-23 14:22:08

hakjoon
Member
From: Arlington, VA
Registered: 2004-07-29
Posts: 1,634
Website

Re: plugin if_category_parent… how to?

Something like this might work.

$is_child = (in_list($current_cat, nfe_catlist())) ? true : false;
return parse(EvalElse($thing, $is_child));

Shoving is the answer – pusher robot

Offline

#5 2007-03-24 10:22:33

feragnoli
Member
From: the hague
Registered: 2005-02-10
Posts: 150

Re: plugin if_category_parent… how to?

hello hakjoon, thank you.
sorry but I don’t get this.

at this point:

if ($out) { return implode($break, $out); }	 }

nfe_catlist returns the list of categories triggered by the specified attribute parent (<txp:nfe_catlist parent="trees" />)

this:

$is_child = (in_list($current_cat, nfe_catlist())) ? true : false;
return parse(EvalElse($thing, $is_child));

defines wheter current_cat finds itself inside nfe_catlist, if so assigns it to $is_child and finally parses the content $thing, right?

now, at which step should this happen? here (in_list($current_cat, nfe_catlist())) I would call the fucntion nfe_catlist inside itself, right?

what if I evaluate like (follows) this inside the definition of the function?

if ($out) {
$catlist = implode($break, $out);
$is_child = (in_list($current_cat, $catlist)) ? true : false;
return parse(EvalElse($thing, $is_child));
 }

thank you.


what was that again…?

Offline

#6 2007-03-24 13:56:18

ruud
Developer Emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

Re: plugin if_category_parent… how to?

I think your function will end like this:

  $out = array();

  if ($rs)
  {
    while ($a = nextRow($rs))
    {
      extract($a);
      if ($name) $out[] = $name;
    }
  }

  return parse(EvalElse($thing, in_array($out, $current_cat)));
}

Last edited by ruud (2007-03-24 14:03:36)

Offline

#7 2007-03-26 11:36:50

feragnoli
Member
From: the hague
Registered: 2005-02-10
Posts: 150

Re: plugin if_category_parent… how to?

hello, I have a first version of the function in a plugin.
Sure there’s another way to do the same thing, at the moment I’ll stick to this.
It’s here .

Thanks for your help!


what was that again…?

Offline

Board footer

Powered by FluxBB