Archived : Techno Babble

This is the archived version of my b2evolution code blog.
If you require any help regarding b2evolution then
visit it's support forums

You can find my current blogs here
Code : {@link : WafflesOn}
Personal : {@link : InnerVisions}

« Extra Comment controlComments from registered members only »

Control your children

Posted on 27th Nov 2006 in : Hacks

This is a fix to move a categories children when you move the category. It should work in all 1.8/1.9 versions and is already fixed in 2.0

inc/model/collectons/_category.funcs.php, find this bit of code :-

Code:

/**
* Update a category
*
* This funtion has to handle all needed DB dependencies!
*/
function cat_update(
  $cat_ID,
  $cat_name,
  $cat_parent_ID = 0,
  $cat_blog_ID = '' )
{
  global $DB;
 
  if( $cat_parent_ID == 0 ) $cat_parent_ID = 'NULL';
 
  return $DB->query( "UPDATE T_categories
                        SET cat_name = ".$DB->quote($cat_name).",
                            cat_parent_ID = $cat_parent_ID ".
                            (!empty($cat_blog_ID) ? ", cat_blog_ID = $cat_blog_ID" : '')."
                      WHERE cat_ID = $cat_ID" );
}

and replace it with this :-

Code:

/**
* Update a category
*
* This funtion has to handle all needed DB dependencies!
*/
function cat_update(
  $cat_ID,
  $cat_name,
  $cat_parent_ID = 0,
  $cat_blog_ID = '' )
{
  global $DB;
 
  if ( !empty( $cat_blog_ID ) )
  {// lets move any/all children
    cat_movechildren( $cat_ID, $cat_blog_ID );
  }
 
  if( $cat_parent_ID == 0 ) $cat_parent_ID = 'NULL';
 
  return $DB->query( "UPDATE T_categories
                        SET cat_name = ".$DB->quote($cat_name).",
                            cat_parent_ID = $cat_parent_ID ".
                            (!empty($cat_blog_ID) ? ", cat_blog_ID = $cat_blog_ID" : '')."
                      WHERE cat_ID = $cat_ID" );
}
 
 
/**
* Recursively move a categories children
*/
function cat_movechildren( $cat_ID, $cat_blog_ID )
{
  global $DB;
  $sql = 'select cat_ID from T_categories where cat_parent_ID = '.$cat_ID;
  $results = $DB->get_results( $sql, ARRAY_A );
  if( $results )
  {
    foreach( $results as $record )
    {
      // first lets move the category
      $sql = 'update T_categories
                set cat_blog_ID = '.$cat_blog_ID.'
                where cat_ID = '.$record[ 'cat_ID' ];
      $DB->query( $sql );
      // now lets move any children of this child
      cat_movechildren( $record[ 'cat_ID' ], $cat_blog_ID );
    }
  }
}

¥

No feedback yet

Page archived : 8th Dec 2009
 

X