ultimix
category_access.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 $NativeTable = '`umx_category`';
39 
50  var $Database = false;
51  var $DatabaseAlgorithms = false;
52  var $Security = false;
53  var $SecurityParser = false;
54 
65  var $Dictionary = false;
66 
77  function __construct()
78  {
79  try
80  {
81  $this->Database = get_package( 'database' , 'last' , __FILE__ );
82  $this->DatabaseAlgorithms = get_package( 'database::database_algorithms' , 'last' , __FILE__ );
83  $this->Security = get_package( 'security' , 'last' , __FILE__ );
84  $this->SecurityParser = get_package( 'security::security_parser' , 'last' , __FILE__ );
85  }
86  catch( Exception $e )
87  {
88  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
89  }
90  }
91 
102  var $AddLimitations = '1 = 1';
103 
122  function set_add_limitations( $theAddLimitation )
123  {
124  try
125  {
126  if( $this->AddLimitations === '1 = 1' )
127  {
128  $this->AddLimitations = $theAddLimitation;
129  }
130  else
131  {
132  throw( new Exception( '"AddLimitations" was already set' ) );
133  }
134  }
135  catch( Exception $e )
136  {
137  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
138  }
139  }
140 
163  function create( $Record )
164  {
165  try
166  {
167  $Script = 'title:string;root_id:integer;direct_category:integer,default_0,allow_not_set;'.
168  'mask:integer,default_0,allow_not_set';
169  $Record = $this->SecurityParser->parse_parameters( $Record , $Script );
170 
171  list( $Fields , $Values ) = $this->DatabaseAlgorithms->compile_fields_values( $Record );
172 
173  $id = $this->DatabaseAlgorithms->create( $this->NativeTable , $Fields , $Values );
174 
175  $EventManager = get_package( 'event_manager' , 'last' , __FILE__ );
176  $EventManager->trigger_event( 'on_after_create_category' , array( 'id' => $id ) );
177 
178  return( $id );
179  }
180  catch( Exception $e )
181  {
182  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
183  }
184  }
185 
208  function update( $id , $Record )
209  {
210  try
211  {
212  $id = $this->Security->get( $id , 'integer_list' );
213 
214  $Script = 'title:string;root_id:integer;direct_category:integer;mask:integer';
215 
216  $Record = $this->SecurityParser->parse_parameters( $Record , $Script , 'allow_not_set' );
217 
218  list( $Fields , $Values ) = $this->DatabaseAlgorithms->compile_fields_values( $Record );
219 
220  if( isset( $Fields[ 0 ] ) )
221  {
222  $Condition = "( $this->AddLimitations ) AND id IN ( $id )";
223 
224  $this->Database->update( $this->NativeTable , $Fields , $Values , $Condition );
225 
226  $this->Database->commit();
227  }
228  }
229  catch( Exception $e )
230  {
231  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
232  }
233  }
234 
257  function select_list( $id )
258  {
259  try
260  {
261  $id = $this->Security->get( $id , 'integer_list' );
262 
263  return( $this->unsafe_select( $this->NativeTable.".id IN ( $id )" ) );
264  }
265  catch( Exception $e )
266  {
267  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
268  }
269  }
270 
289  function delete( $cid )
290  {
291  try
292  {
293  $cid = $this->Security->get( $cid , 'integer_list' );
294 
295  $EventManager = get_package( 'event_manager' , 'last' , __FILE__ );
296  $EventManager->trigger_event( 'on_before_delete_category' , array( 'id' => $cid ) );
297 
298  $this->Database->delete( 'umx_category' , "( $this->AddLimitations ) AND id IN ( $cid )" );
299  $this->Database->commit();
300 
301  $EventManager = get_package( 'event_manager' , 'last' , __FILE__ );
302  $EventManager->trigger_event( 'on_after_delete_category' , array( 'id' => $cid ) );
303  }
304  catch( Exception $e )
305  {
306  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
307  }
308  }
309 
328  function move_up_children_categories( $cid , $NewRootId )
329  {
330  try
331  {
332  $cid = $this->Security->get( $cid , 'integer_list' );
333 
334  $NewRootId = $this->Security->get( $NewRootId , 'integer' );
335 
336  $Condition = "( $this->AddLimitations ) AND root_id IN ( $cid )";
337 
338  $this->Database->update( $this->NativeTable , array( 'root_id' ) , array( $NewRootId ) , $Condition );
339 
340  $this->Database->commit();
341  }
342  catch( Exception $e )
343  {
344  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
345  }
346  }
347 
370  function unsafe_select( $Condition = '1 = 1' )
371  {
372  try
373  {
374  $this->Database->query_as( DB_OBJECT );
375 
376  return( $this->Database->select( '*' , 'umx_category' , "( $this->AddLimitations ) AND $Condition" ) );
377  }
378  catch( Exception $e )
379  {
380  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
381  }
382  }
383 
406  private function get_fetch_parameters( $Condition )
407  {
408  try
409  {
410  $Fields = 'umx_category.id AS cid , umx_category.title AS ctitle , c2.title AS croot_title , '.
411  'c3.title AS cdirect_title , umx_category.mask AS cmask , umx_category.category_name '.
412  'AS category_name';
413 
414  $Tables = 'umx_category , umx_category AS c2 , umx_category AS c3';
415 
416  $Condition = '( ( umx_category.root_id = c2.id AND umx_category.direct_category = c3.id ) OR '.
417  "( umx_category.id = c2.id AND c2.id = c3.id AND umx_category.root_id = 0 ) ) AND $Condition";
418 
419  return( array( $Fields , $Tables , $Condition ) );
420  }
421  catch( Exception $e )
422  {
423  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
424  }
425  }
426 
465  function select( $Start , $Limit , $Field = false , $Order = false , $Condition = '1 = 1' )
466  {
467  try
468  {
469  $Condition = $this->DatabaseAlgorithms->select_condition(
470  $Start , $Limit , $Field , $Order , $Condition , $this->NativeTable
471  );
472 
473  $this->Database->query_as( DB_OBJECT );
474 
475  list( $Fields , $Tables , $Condition ) = $this->get_fetch_parameters( $Condition );
476 
477  return( $this->Database->select( $Fields , $Tables , $Condition ) );
478  }
479  catch( Exception $e )
480  {
481  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
482  }
483  }
484 
507  function get_category_id( $Name )
508  {
509  try
510  {
511  if( $this->Dictionary === false )
512  {
513  $this->Dictionary = $this->unsafe_select( 'category_name IS NOT NULL' );
514  }
515 
516  foreach( $this->Dictionary as $k => $v )
517  {
518  if( get_field( $v , 'category_name' ) == $Name )
519  {
520  return( get_field( $v , 'id' ) );
521  }
522  }
523 
524  throw( new Exception( "Category with name '$Name' was not found" ) );
525  }
526  catch( Exception $e )
527  {
528  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
529  }
530  }
531 
554  function get_children( $id )
555  {
556  try
557  {
558  $id = $this->Security->get( $id , 'integer' );
559 
560  return( $this->unsafe_select( "root_id = $id ORDER BY title" ) );
561  }
562  catch( Exception $e )
563  {
564  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
565  }
566  }
567 
590  function get_children_count( $id )
591  {
592  try
593  {
594  $id = $this->Security->get( $id , 'integer' );
595 
596  return( count( $this->unsafe_select( "root_id = $id ORDER BY title" ) ) );
597  }
598  catch( Exception $e )
599  {
600  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
601  }
602  }
603 
626  function get_siblings( $id )
627  {
628  try
629  {
630  $ids = $this->Security->get( $id , 'integer' );
631 
632  $Siblings = array();
633 
634  while( true )
635  {
636  $Children = $this->unsafe_select( "root_id IN ( $ids ) ORDER BY title" );
637 
638  if( isset( $Children[ 0 ] ) )
639  {
640  $Siblings = array_merge( $Siblings , $Children );
641 
642  $ids = get_field_ex( $Children , 'id' );
643 
644  $ids = implode( ',' , $ids );
645  }
646  else
647  {
648  return( $Siblings );
649  }
650  }
651  }
652  catch( Exception $e )
653  {
654  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
655  }
656  }
657 
680  function get_siblings_ids( $id )
681  {
682  try
683  {
684  $ids = $this->Security->get( $id , 'integer' );
685 
686  $SiblingsIds = array();
687 
688  while( true )
689  {
690  $Children = $this->unsafe_select( "root_id IN ( $ids ) ORDER BY title" );
691 
692  if( isset( $Children[ 0 ] ) )
693  {
694  $ids = get_field_ex( $Children , 'id' );
695 
696  $SiblingsIds = array_merge( $SiblingsIds , $ids );
697 
698  $ids = implode( ',' , $ids );
699  }
700  else
701  {
702  return( $SiblingsIds );
703  }
704  }
705  }
706  catch( Exception $e )
707  {
708  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
709  }
710  }
711 
734  function select_categories_list( $id )
735  {
736  try
737  {
738  $id = $this->Security->get( $id , 'integer' );
739 
740  return( $this->unsafe_select( "direct_category = $id ORDER BY title" ) );
741  }
742  catch( Exception $e )
743  {
744  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
745  }
746  }
747  }
748 
749 ?>