ultimix
subscription_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_subscription`';
39 
50  var $Database = false;
51  var $DatabaseAlgorithms = false;
52  var $Link = false;
53  var $Security = false;
54  var $SecurityParser = 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->Link = get_package( 'link' , 'last' , __FILE__ );
73  $this->Security = get_package( 'security' , 'last' , __FILE__ );
74  $this->SecurityParser = get_package( 'security::security_parser' , '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 
153  function unsafe_select( $Condition )
154  {
155  try
156  {
157  $this->Database->query_as( DB_OBJECT );
158 
159  return(
160  $this->Database->select(
161  $this->NativeTable.'.*' , $this->NativeTable ,
162  "( $this->AddLimitations ) AND $Condition"
163  )
164  );
165  }
166  catch( Exception $e )
167  {
168  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
169  }
170  }
171 
210  function select( $Start = false , $Limit = false , $Field = false ,
211  $Order = false , $Condition = '1 = 1' )
212  {
213  try
214  {
215  $Condition = $this->DatabaseAlgorithms->select_condition(
216  $Start , $Limit , $Field , $Order , $Condition , $this->NativeTable
217  );
218 
219  return( $this->unsafe_select( $Condition ) );
220  }
221  catch( Exception $e )
222  {
223  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
224  }
225  }
226 
249  function delete( $id , $Options = ' 1 = 1' )
250  {
251  try
252  {
253  $id = $this->Security->get( $id , 'integer_list' );
254 
255  $this->Database->delete( $this->NativeTable , "( $this->AddLimitations ) AND id IN ( $id )" );
256 
257  $this->Database->commit();
258  }
259  catch( Exception $e )
260  {
261  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
262  }
263  }
264 
287  function create( $Record )
288  {
289  try
290  {
291  $Record = $this->SecurityParser->parse_parameters(
292  $Record , 'title:string;description:string;template:string'
293  );
294 
295  list( $Fields , $Values ) = $this->DatabaseAlgorithms->compile_fields_values( $Record );
296 
297  $id = $this->DatabaseAlgorithms->create( $this->NativeTable , $Fields , $Values );
298 
299  $EventManager = get_package( 'event_manager' , 'last' , __FILE__ );
300  $EventManager->trigger_event( 'on_after_create_subscription' , array( 'id' => $id ) );
301 
302  return( $id );
303  }
304  catch( Exception $e )
305  {
306  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
307  }
308  }
309 
332  function update( $id , $Record )
333  {
334  try
335  {
336  $id = $this->Security->get( $id , 'integer_list' );
337  $Record = $this->SecurityParser->parse_parameters(
338  $Record , 'title:string;description:string;template:string' , 'allow_not_set'
339  );
340 
341  list( $Fields , $Values ) = $this->DatabaseAlgorithms->compile_fields_values( $Record );
342 
343  if( isset( $Fields[ 0 ] ) )
344  {
345  $this->Database->update(
346  $this->NativeTable , $Fields , $Values , "( $this->AddLimitations ) AND id IN ( $id )"
347  );
348  $this->Database->commit();
349  }
350  }
351  catch( Exception $e )
352  {
353  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
354  }
355  }
356 
379  function select_list( $id )
380  {
381  try
382  {
383  $id = $this->Security->get( $id , 'integer_list' );
384 
385  return( $this->unsafe_select( $this->NativeTable.".id IN ( $id ) ORDER BY id ASC" ) );
386  }
387  catch( Exception $e )
388  {
389  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
390  }
391  }
392 
415  function subscribe_user( $UserId , $SubscriptionId )
416  {
417  try
418  {
419  $UserId = explode( ',' , $this->Security->get( $UserId , 'integer_list' ) );
420  $SubscriptionId = explode( ',' , $this->Security->get( $SubscriptionId , 'integer_list' ) );
421 
422  foreach( $UserId as $i => $User )
423  {
424  foreach( $SubscriptionId as $j => $Subscription )
425  {
426  $this->Link->create_link( $User , $Subscription , 'user' , 'subscription' , true );
427  }
428  }
429  }
430  catch( Exception $e )
431  {
432  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
433  }
434  }
435 
458  function unsubscribe_user( $UserId , $SubscriptionId )
459  {
460  try
461  {
462  $UserId = explode( ',' , $this->Security->get( $UserId , 'integer_list' ) );
463  $SubscriptionId = explode( ',' , $this->Security->get( $SubscriptionId , 'integer_list' ) );
464 
465  foreach( $UserId as $i => $User )
466  {
467  foreach( $SubscriptionId as $j => $Subscription )
468  {
469  $this->Link->delete_link( $User , $Subscription , 'user' , 'subscription' );
470  }
471  }
472  }
473  catch( Exception $e )
474  {
475  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
476  }
477  }
478 
501  function get_subscriptions_for_user( $UserId )
502  {
503  try
504  {
505  $UserId = $this->Security->get( $UserId , 'integer' );
506 
507  $SubscriptionLinks = $this->Link->get_links( $UserId , false , 'user' , 'subscription' );
508 
509  if( isset( $SubscriptionLinks[ 0 ] ) )
510  {
511  $Subscriptions = implode( ',' , get_field_ex( $SubscriptionLinks , 'object2_id' ) );
512 
513  return( $this->unsafe_select( "id IN ( $Subscriptions )" ) );
514  }
515 
516  return( array() );
517  }
518  catch( Exception $e )
519  {
520  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
521  }
522  }
523  }
524 ?>