ultimix
content_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_content`';
39 
50  var $Database = false;
51  var $DatabaseAlgorithms = false;
52  var $Security = false;
53  var $SecurityParser = false;
54  var $UserAccess = false;
55 
66  function __construct()
67  {
68  try
69  {
70  $this->Database = get_package( 'database' , 'last' , __FILE__ );
71  $this->DatabaseAlgorithms = get_package( 'database::database_algorithms' , 'last' , __FILE__ );
72  $this->Security = get_package( 'security' , 'last' , __FILE__ );
73  $this->SecurityParser = get_package( 'security::security_parser' , 'last' , __FILE__ );
74  $this->UserAccess = get_package( 'user::user_access' , 'last' , __FILE__ );
75  }
76  catch( Exception $e )
77  {
78  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
79  }
80  }
81 
92  var $AddLimitations = '1 = 1';
93 
112  function set_add_limitations( $theAddLimitation )
113  {
114  try
115  {
116  if( $this->AddLimitations === '1 = 1' )
117  {
118  $this->AddLimitations = $theAddLimitation;
119  }
120  else
121  {
122  throw( new Exception( '"AddLimitations" was already set' ) );
123  }
124  }
125  catch( Exception $e )
126  {
127  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
128  }
129  }
130 
173  function select( $Start = false , $Limit = false , $Field = false , $Order = false ,
174  $Condition = '1 = 1' , $Options = false )
175  {
176  try
177  {
178  $AddCondition = '';
179  if( $Options !== false && $Options->get_setting( 'category_name' , false ) )
180  {
181  $Category = get_package( 'category::category_algorithms' , 'last' , __FILE__ );
182  $AddCondition = 'AND category IN ( '.implode(
183  ',' , $Category->get_category_ids( $Options->get_setting( 'category_name' ) )
184  ).' )';
185  }
186 
187  $Condition = $this->DatabaseAlgorithms->select_condition(
188  $Start , $Limit , $Field , $Order , $Condition , $this->NativeTable
189  );
190 
191  return( $this->unsafe_select( $Condition ) );
192  }
193  catch( Exception $e )
194  {
195  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
196  }
197  }
198 
217  private function compile_print_content( &$Content )
218  {
219  try
220  {
221  $MainContent = $this->Security->get( get_field( $Content , 'main_content' ) , 'unsafe_string' );
222 
223  $PrintContent = $this->Security->get( get_field( $Content , 'print_content' ) , 'unsafe_string' );
224  set_field(
225  $Content , 'print_content_unsafe' ,
226  strlen( $PrintContent ) === 0 ? $MainContent : $PrintContent
227  );
228 
229  set_field( $Content , 'has_print_content' , strlen( $PrintContent ) === 0 ? 0 : 1 );
230  }
231  catch( Exception $e )
232  {
233  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
234  }
235  }
236 
259  private function compile_selected_content( &$Content )
260  {
261  try
262  {
263  foreach( $Content as $k => $v )
264  {
265  $DemoContent = $this->Security->get( get_field( $v , 'demo_content' ) , 'unsafe_string' );
266  set_field( $Content[ $k ] , 'demo_content_unsafe' , $DemoContent );
267  set_field( $Content[ $k ] , 'has_demo_content' , strlen( $DemoContent ) );
268 
269  $MainContent = $this->Security->get( get_field( $v , 'main_content' ) , 'unsafe_string' );
270  set_field( $Content[ $k ] , 'main_content_unsafe' , $MainContent );
271  set_field( $Content[ $k ] , 'has_main_content' , strlen( $MainContent ) );
272 
273  $this->compile_print_content( $Content[ $k ] );
274  }
275  }
276  catch( Exception $e )
277  {
278  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
279  }
280  }
281 
300  function unsafe_select( $Condition = ' 1 = 1 ' )
301  {
302  try
303  {
304  $this->Database->query_as( DB_OBJECT );
305 
306  $Content = $this->Database->select(
307  $this->NativeTable.'.* , umx_category.title AS category_title , user.login AS author_name' ,
308  $this->NativeTable.' , umx_category , '.$this->UserAccess->NativeTable.' AS user' ,
309  "author = user.id AND ( $this->AddLimitations ) AND umx_category.id = category AND $Condition"
310  );
311 
312  $this->compile_selected_content( $Content );
313 
314  return( $Content );
315  }
316  catch( Exception $e )
317  {
318  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
319  }
320  }
321 
344  function select_list( $id )
345  {
346  try
347  {
348  $id = $this->Security->get( $id , 'integer_list' );
349 
350  return( $this->unsafe_select( $this->NativeTable.".id IN ( $id )" ) );
351  }
352  catch( Exception $e )
353  {
354  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
355  }
356  }
357 
376  function delete( $id )
377  {
378  try
379  {
380  $id = $this->Security->get( $id , 'integer_list' );
381 
382  $this->Database->delete( $this->NativeTable , "( $this->AddLimitations ) AND id IN ( $id )" );
383  }
384  catch( Exception $e )
385  {
386  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
387  }
388  }
389 
412  private function fetch_create_data( &$Record )
413  {
414  try
415  {
416  $Record = $this->SecurityParser->parse_parameters(
417  $Record ,
418  'author:integer;title:string;category:integer;demo_content:string;'.
419  'main_content:string;keywords:string;description:string;print_content:string'
420  );
421 
422  return(
423  $this->DatabaseAlgorithms->compile_fields_values(
425  )
426  );
427  }
428  catch( Exception $e )
429  {
430  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
431  }
432  }
433 
456  function create( &$Record )
457  {
458  try
459  {
460  list( $Fields , $Values ) = $this->fetch_create_data( $Record );
461 
462  $id = $this->DatabaseAlgorithms->create( $this->NativeTable , $Fields , $Values );
463 
464  $EventManager = get_package( 'event_manager' , 'last' , __FILE__ );
465  $EventManager->trigger_event( 'on_after_create_content' , array( 'id' => $id ) );
466 
467  return( $id );
468  }
469  catch( Exception $e )
470  {
471  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
472  }
473  }
474 
497  function select_content_by_category( $CategoryIds )
498  {
499  try
500  {
501  $CategoryIds = $this->Security->get( $CategoryIds , 'integer' );
502 
503  return(
504  $this->unsafe_select(
505  'category IN ( '.implode( ',' , $CategoryIds ).' ) ORDER BY publication_date ASC'
506  )
507  );
508  }
509  catch( Exception $e )
510  {
511  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
512  }
513  }
514 
537  function get_publication_structure( $CategoryIds )
538  {
539  try
540  {
541  $CategoryIds = $this->Security->get( $CategoryIds , 'integer' );
542 
543  return(
544  $this->Database->select(
545  'YEAR( publication_date ) AS publication_year , MONTH( publication_date ) AS publication_month'.
546  ' , COUNT( * ) AS publication_count' ,
547  $this->NativeTable ,
548  'category IN ( '.implode( ',' , $CategoryIds ).
549  ' ) GROUP BY publication_year , publication_month ORDER BY publication_date ASC'
550  )
551  );
552  }
553  catch( Exception $e )
554  {
555  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
556  }
557  }
558 
589  function get_content_for_date( $Year , $Month , $CategoryIds )
590  {
591  try
592  {
593  $Year = $this->Security->get( $Year , 'integer' );
594  $Month = $this->Security->get( $Month , 'integer' );
595  $CategoryIds = $this->Security->get( $CategoryIds , 'integer' );
596 
597  return(
598  $this->unsafe_select(
599  "YEAR( publication_date ) = $Year AND MONTH( publication_date ) = $Month AND ".
600  'category IN ( '.implode( ',' , $CategoryIds ).' ) ORDER BY publication_date ASC'
601  )
602  );
603  }
604  catch( Exception $e )
605  {
606  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
607  }
608  }
609 
628  private function compile_record( &$Record )
629  {
630  try
631  {
632  $Record = $this->SecurityParser->parse_parameters(
633  $Record ,
634  'author:integer;title:string;category:integer;demo_content:string;main_content:string;'.
635  'keywords:string;description:string;print_content:string' , 'allow_not_set'
636  );
637  }
638  catch( Exception $e )
639  {
640  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
641  }
642  }
643 
666  function update( $id , $Record )
667  {
668  try
669  {
670  $id = $this->Security->get( $id , 'integer_list' );
671 
672  $this->compile_record( $Record );
673 
674  list( $Fields , $Values ) = $this->DatabaseAlgorithms->compile_fields_values( $Record );
675 
676  if( isset( $Fields[ 0 ] ) )
677  {
678  /* the modification_date will be changed only if the content was changed */
679  $Fields [] = 'modification_date';
680  $Values [] = 'now()';
681 
682  $Condition = "( $this->AddLimitations ) AND id IN ( $id )";
683  $this->Database->update( $this->NativeTable , $Fields , $Values , $Condition );
684  $this->Database->commit();
685  }
686  }
687  catch( Exception $e )
688  {
689  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
690  }
691  }
692 
715  private function search_fields( $SearchString )
716  {
717  try
718  {
719  $SearchString = $this->Security->get( $SearchString , 'string' );
720 
721  return(
722  "id , CONCAT( title , ' ' , demo_content , ' ' , main_content ) AS record_text ,
723  CHAR_LENGTH( CONCAT( title , ' ' , demo_content , ' ' , main_content ) ) - CHAR_LENGTH(
724  REPLACE( CONCAT( title , ' ' , demo_content , ' ' , main_content ) , '$SearchString' , '' )
725  ) AS relevation ,
726  CONCAT( './content_view.html?content_id=' , id ) AS source_page ,
727  title AS source_page_title"
728  );
729  }
730  catch( Exception $e )
731  {
732  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
733  }
734  }
735 
762  function search( $Start , $SearchString )
763  {
764  try
765  {
766  $Start = $this->Security->get( $Start , 'integer' );
767 
768  $this->Database->query_as( DB_OBJECT );
769 
770  $Records = $this->Database->select(
771  $this->search_fields( $SearchString ) , $this->NativeTable ,
772  "( $this->AddLimitations ) ORDER BY relevation DESC LIMIT $Start , 10"
773  );
774 
775  return( $Records );
776  }
777  catch( Exception $e )
778  {
779  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
780  }
781  }
782  }
783 
784 ?>