ultimix
category_algorithms.php
Go to the documentation of this file.
1 <?php
2 
3  /*
4  * This source code is a part of the Ultimix Project.
5  * It is distributed under BSD license. All other third side source code (like tinyMCE) is distributed under
6  * it's own license wich could be found from the corresponding files or sources.
7  * This source code is provided "as is" without any warranties or garanties.
8  *
9  * Have a nice day!
10  *
11  * @url http://ultimix.sorceforge.net
12  *
13  * @author Alexey "gdever" Dodonov
14  */
15 
27 
38  var $CategoryAccess = false;
39  var $Link = false;
40  var $Security = false;
41 
52  function __construct()
53  {
54  try
55  {
56  $this->CategoryAccess = get_package( 'category::category_access' , 'last' , __FILE__ );
57 
58  $this->Link = get_package( 'link' , 'last' , __FILE__ );
59 
60  $this->Security = get_package( 'security' , 'last' , __FILE__ );
61  }
62  catch( Exception $e )
63  {
64  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
65  }
66  }
67 
94  function get_children( $RootId , &$AllItems )
95  {
96  try
97  {
98  $ReturnItems = array();
99 
100  foreach( $AllItems as $k => $v )
101  {
102  if( get_field( $v , 'root_id' ) == $RootId )
103  {
104  $ReturnItems [] = $v;
105  }
106  }
107 
108  return( $ReturnItems );
109  }
110  catch( Exception $e )
111  {
112  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
113  }
114  }
115 
142  function get_previous_items( $AllItems , $LeafId )
143  {
144  try
145  {
146  $ReturnItems = array();
147  $CurrentId = $LeafId;
148  $ItemFound = false;
149 
150  do
151  {
152  $ItemFound = false;
153  foreach( $AllItems as $k => $v )
154  {
155  if( $v->id == $CurrentId )
156  {
157  $ReturnItems [] = $v;
158  $CurrentId = $v->root_id;
159  $ItemFound = true;
160  break;
161  }
162  }
163  }
164  while( $ItemFound );
165 
166  return( array_reverse( isset( $ReturnItems[ 1 ] ) ? $ReturnItems : array() ) );
167  }
168  catch( Exception $e )
169  {
170  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
171  }
172  }
173 
200  function get_object_categories( $MasterId , $MasterType )
201  {
202  try
203  {
204  $Links = $this->Link->get_links( $MasterId , false , $MasterType , 'category' );
205 
206  $CategoryIds = get_field_ex( $Links , 'object2_id' );
207 
208  if( isset( $CategoryIds[ 0 ] ) )
209  {
210  $CategoryIds = implode( ',' , $CategoryIds );
211 
212  return( $this->CategoryAccess->unsafe_select( "id IN ( $CategoryIds )" ) );
213  }
214  else
215  {
216  return( array() );
217  }
218  }
219  catch( Exception $e )
220  {
221  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
222  }
223  }
224 
247  function get_by_id( $id )
248  {
249  try
250  {
251  $id = $this->Security->get( $id , 'integer' );
252 
253  $Users = $this->CategoryAccess->unsafe_select( $this->CategoryAccess->NativeTable.".id = $id" );
254 
255  if( count( $Users ) === 0 || count( $Users ) > 1 )
256  {
257  throw( new Exception( 'Category with id '.$id.' was not found' ) );
258  }
259  else
260  {
261  return( $Users[ 0 ] );
262  }
263  }
264  catch( Exception $e )
265  {
266  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
267  }
268  }
269 
292  function object_exists( $id )
293  {
294  try
295  {
296  $id = $this->Security->get( $id , 'integer' );
297 
298  $Records = $this->CategoryAccess->unsafe_select( $this->CategoryAccess->NativeTable.".id = $id" );
299 
300  return( count( $Records ) === 1 );
301  }
302  catch( Exception $e )
303  {
304  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
305  }
306  }
307 
330  function create( $Record )
331  {
332  try
333  {
334  $id = $this->CategoryAccess->create( $Record );
335 
336  if( get_field( $Record , 'direct_category' , false ) === false )
337  {
338  $RootId = get_field( $Record , 'root_id' );
339 
340  if( $this->object_exists( $RootId ) )
341  {
342  $RootCategory = $this->get_by_id( $RootId );
343  $DirectCategory = get_field( $RootCategory , 'direct_category' );
344  $this->CategoryAccess->update( $id , array( 'direct_category' => $DirectCategory ) );
345  }
346  }
347 
348  return( $id );
349  }
350  catch( Exception $e )
351  {
352  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
353  }
354  }
355 
382  function update_category_title( $cid , $Title )
383  {
384  try
385  {
386  $cid = $this->Security->get( $cid , 'integer' );
387  $Title = $this->Security->get( $Title , 'string' );
388 
389  $this->CategoryAccess->update( $cid , array( 'title' => $Title ) );
390  }
391  catch( Exception $e )
392  {
393  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
394  }
395  }
396 
419  function get_category_ids( $Names )
420  {
421  try
422  {
423  if( $Names == '' )
424  {
425  return( array() );
426  }
427 
428  if( is_string( $Names ) )
429  {
430  $Names = explode( ',' , $Names );
431  }
432 
433  $Ids = array();
434 
435  foreach( $Names as $k => $v )
436  {
437  $Ids [] = $this->CategoryAccess->get_category_id( $v );
438  }
439 
440  return( $Ids );
441  }
442  catch( Exception $e )
443  {
444  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
445  }
446  }
447 
470  function get_children_by_name( $Name )
471  {
472  try
473  {
474  $id = $this->CategoryAccess->get_category_id( $Name );
475 
476  return( $this->CategoryAccess->get_children( $id ) );
477  }
478  catch( Exception $e )
479  {
480  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
481  }
482  }
483 
507  {
508  try
509  {
510  $id = $this->CategoryAccess->get_category_id( $Name );
511 
512  return( $this->CategoryAccess->select_categories_list( $id ) );
513  }
514  catch( Exception $e )
515  {
516  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
517  }
518  }
519  }
520 
521 ?>