ultimix
review_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_review`';
39 
50  var $Database = false;
51  var $DatabaseAlgorithms = false;
52  var $Security = false;
53  var $SecurityParser = false;
54  var $UserAccess = false;
55  var $UserAlgorithms = false;
56 
67  function __construct()
68  {
69  try
70  {
71  $this->Database = get_package( 'database' , 'last' , __FILE__ );
72  $this->DatabaseAlgorithms = get_package( 'database::database_algorithms' , 'last' , __FILE__ );
73  $this->Security = get_package( 'security' , 'last' , __FILE__ );
74  $this->SecurityParser = get_package( 'security::security_parser' , 'last' , __FILE__ );
75  $this->UserAccess = get_package( 'user::user_access' , 'last' , __FILE__ );
76  $this->UserAlgorithms = get_package( 'user::user_algorithms' , 'last' , __FILE__ );
77  }
78  catch( Exception $e )
79  {
80  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
81  }
82  }
83 
94  var $AddLimitations = '1 = 1';
95 
114  function set_add_limitations( $theAddLimitation )
115  {
116  try
117  {
118  if( $this->AddLimitations === '1 = 1' )
119  {
120  $this->AddLimitations = $theAddLimitation;
121  }
122  else
123  {
124  throw( new Exception( '"AddLimitations" was already set' ) );
125  }
126  }
127  catch( Exception $e )
128  {
129  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
130  }
131  }
132 
155  function unsafe_select( $Condition )
156  {
157  try
158  {
159  $this->Database->query_as( DB_OBJECT );
160 
161  return(
162  $this->Database->select(
163  $this->UserAccess->NativeTable.'.* , '.$this->NativeTable.
164  '.id , rank , review , page , creation_date , author' ,
165  $this->NativeTable.' , '.$this->UserAccess->NativeTable ,
166  $this->NativeTable.'.author = '.$this->UserAccess->NativeTable.".id
167  AND ( $this->AddLimitations ) AND $Condition"
168  )
169  );
170  }
171  catch( Exception $e )
172  {
173  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
174  }
175  }
176 
215  function select( $Start = false , $Limit = false , $Field = false ,
216  $Order = false , $Condition = '1 = 1' )
217  {
218  try
219  {
220  $Condition = $this->DatabaseAlgorithms->select_condition(
221  $Start , $Limit , $Field === false ? $this->NativeTable.'.id' : $Field ,
222  $Order , $Condition , $this->NativeTable
223  );
224 
225  return( $this->unsafe_select( $Condition ) );
226  }
227  catch( Exception $e )
228  {
229  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
230  }
231  }
232 
255  function delete( $id , $Options = ' 1 = 1' )
256  {
257  try
258  {
259  $Link = get_package( 'link' , 'last' , __FILE__ );
260  $Link->delete_link( false , $id , false , 'review' );
261 
262  $id = $this->Security->get( $id , 'integer_list' );
263  $this->Database->delete( $this->NativeTable , "( $this->AddLimitations ) AND id IN ( $id )" );
264  $this->Database->commit();
265  }
266  catch( Exception $e )
267  {
268  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
269  }
270  }
271 
294  private function fetch_data( $Record )
295  {
296  try
297  {
298  $MasterLink = $this->SecurityParser->parse_parameters(
299  $Record , 'master_type:command;master_id:integer'
300  );
301 
302  $Record = $this->SecurityParser->parse_parameters(
303  $Record , 'rank:integer,min_-5,max_5;review:string'
304  );
305  set_field( $Record , 'author' , $this->UserAlgorithms->get_id() );
306  set_field( $Record , 'page' , $this->Security->get_srv( 'REQUEST_URI' , 'string' , './index.html' ) );
307 
308  return( array( $MasterLink , $Record ) );
309  }
310  catch( Exception $e )
311  {
312  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
313  }
314  }
315 
338  function create( $Record )
339  {
340  try
341  {
342  list( $MasterLink , $Record ) = $this->fetch_data( $Record );
343 
344  list( $Fields , $Values ) = $this->DatabaseAlgorithms->compile_fields_values( $Record , CREATION_DATE );
345 
346  $id = $this->DatabaseAlgorithms->create( $this->NativeTable , $Fields , $Values );
347 
348  $Link = get_package( 'link' , 'last' , __FILE__ );
349  $Link->create_link(
350  get_field( $MasterLink , 'master_id' ) , $id ,
351  get_field( $MasterLink , 'master_type' ) , 'review'
352  );
353 
354  $EventManager = get_package( 'event_manager' , 'last' , __FILE__ );
355  $EventManager->trigger_event( 'on_after_create_review' , array( 'id' => $id ) );
356 
357  return( $id );
358  }
359  catch( Exception $e )
360  {
361  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
362  }
363  }
364 
387  function update( $id , $Record )
388  {
389  try
390  {
391  $id = $this->Security->get( $id , 'integer_list' );
392  $Record = $this->SecurityParser->parse_parameters(
393  $Record , 'rank:integer;review:string' , 'allow_not_set'
394  );
395 
396  list( $Fields , $Values ) = $this->DatabaseAlgorithms->compile_fields_values( $Record );
397 
398  if( isset( $Fields[ 0 ] ) )
399  {
400  $this->Database->update(
401  $this->NativeTable , $Fields , $Values , "( $this->AddLimitations ) AND id IN ( $id )"
402  );
403  $this->Database->commit();
404  }
405  }
406  catch( Exception $e )
407  {
408  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
409  }
410  }
411 
434  function select_list( $id )
435  {
436  try
437  {
438  $id = $this->Security->get( $id , 'integer_list' );
439 
440  return( $this->unsafe_select( $this->NativeTable.".id IN ( $id ) ORDER BY creation_date ASC" ) );
441  }
442  catch( Exception $e )
443  {
444  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
445  }
446  }
447 
474  function get_total_rank( $Type , $id )
475  {
476  try
477  {
478  $Type = $this->Security->get( $Type , 'command' );
479  $id = $this->Security->get( $id , 'command' );
480 
481  $LinkDictionary = get_package( 'link::link_dictionary' , 'last' , __FILE__ );
482  $LinkType = $LinkDictionary->get_link_type( $Type , 'review' );
483 
484  $Records = $this->Database->select(
485  'SUM( '.$this->NativeTable.'.rank ) AS total_rank' , $this->NativeTable.' , umx_link' ,
486  "umx_link.type = $LinkType AND ".$this->NativeTable.".id = umx_link.object2_id
487  AND umx_link.object1_id = $id AND ( $this->AddLimitations ) AND $Condition"
488  );
489 
490  return( isset( $Records[ 0 ] ) ? get_field( $Records[ 0 ] , 'total_rank' ) : 0 );
491  }
492  catch( Exception $e )
493  {
494  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
495  }
496  }
497  }
498 
499 ?>