ultimix
pmsg_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 $Database = false;
39  var $PmsgAccess = false;
40  var $Security = false;
41  var $UserAlgorithms = false;
42 
53  function __construct()
54  {
55  try
56  {
57  $this->Database = get_package( 'database' , 'last' , __FILE__ );
58  $this->PmsgAccess = get_package( 'pmsg::pmsg_access' , 'last' , __FILE__ );
59  $this->Security = get_package( 'security' , 'last' , __FILE__ );
60  $this->UserAlgorithms = get_package( 'user::user_algorithms' , 'last' , __FILE__ );
61  }
62  catch( Exception $e )
63  {
64  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
65  }
66  }
67 
106  function select_incoming_messages( $Start = false , $Limit = false , $Field = false ,
107  $Order = false , $Condition = false )
108  {
109  try
110  {
111  $Login = $this->UserAlgorithms->get_login();
112 
113  return(
114  $this->PmsgAccess->select(
115  $Start , $Limit , $Field , $Order , "( $Condition ) AND recipient LIKE '$Login' AND deleted = 0"
116  )
117  );
118  }
119  catch( Exception $e )
120  {
121  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
122  }
123  }
124 
163  function select_outgoing_messages( $Start = false , $Limit = false , $Field = false ,
164  $Order = false , $Options = false )
165  {
166  try
167  {
168  $Login = $this->UserAlgorithms->get_login();
169 
170  return(
171  $this->PmsgAccess->select(
172  $Start , $Limit , $Field , $Order ,
173  "author LIKE '$Login' AND NOT ( author LIKE recipient AND deleted IN ( 1 , 2 ) )"
174  )
175  );
176  }
177  catch( Exception $e )
178  {
179  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
180  }
181  }
182 
221  function select_deleted_messages( $Start = false , $Limit = false , $Field = false ,
222  $Order = false , $Options = false )
223  {
224  try
225  {
226  $Login = $this->UserAlgorithms->get_login();
227 
228  return(
229  $this->PmsgAccess->select(
230  $Start , $Limit , $Field , $Order , "recipient LIKE '$Login' AND deleted = 1"
231  )
232  );
233  }
234  catch( Exception $e )
235  {
236  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
237  }
238  }
239 
266  function delete_message_for_user( $id , $Options )
267  {
268  try
269  {
270  $id = $this->Security->get( $id , 'integer_list' );
271 
272  $Login = $this->UserAlgorithms->get_login();
273 
274  $Messages = $this->PmsgAccess->unsafe_select( "recipient LIKE '$Login' AND id IN ( $id )" );
275 
276  $id = implode( ',' , get_field_ex( $Messages , 'id' ) );
277 
278  $this->PmsgAccess->update( $id , array( 'deleted' => 1 ) );
279  }
280  catch( Exception $e )
281  {
282  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
283  }
284  }
285 
308  function cleanup_message_for_user( $id )
309  {
310  try
311  {
312  if( is_array( $id ) )
313  {
314  $id = $this->Security->get( $id , 'integer' );
315  $id = implode( ' , ' , $id );
316  }
317  else
318  {
319  $id = $this->Security->get( $id , 'integer_list' );
320  }
321 
322  $Login = $this->UserAlgorithms->get_login();
323 
324  $Messages = $this->PmsgAccess->unsafe_select( "recipient LIKE '$Login' AND id IN ( $id )" );
325 
326  $id = implode( ',' , get_field_ex( $Messages , 'id' ) );
327 
328  $this->PmsgAccess->update( $id , array( 'deleted' => 2 ) );
329  }
330  catch( Exception $e )
331  {
332  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
333  }
334  }
335 
358  function create( $Record )
359  {
360  try
361  {
362  $Login = $this->UserAlgorithms->get_login();
363  set_field( $Record , 'author' , $Login );
364 
365  $AllRecipients = $this->Security->get( get_field( $Record , 'recipient' ) , 'string' );
366  $AllRecipients = explode( ',' , $AllRecipients );
367 
368  foreach( $AllRecipients as $k => $Recipient )
369  {
370  $Recipient = trim( $Recipient , " \t\r\n," );
371  if( $Recipient != '' )
372  {
373  set_field( $Record , 'recipient' , $Recipient );
374  $this->PmsgAccess->create( $Record );
375  }
376  }
377  }
378  catch( Exception $e )
379  {
380  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
381  }
382  }
383 
403  {
404  try
405  {
406  $Login = $this->UserAlgorithms->get_login();
407  $Count = count( $this->PmsgAccess->unsafe_select( "recipient LIKE '$Login' AND `read` = 0" ) );
408  return( $Count );
409  }
410  catch( Exception $e )
411  {
412  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
413  }
414  }
415 
438  function get_by_id( $id )
439  {
440  try
441  {
442  $id = $this->Security->get( $id , 'integer' );
443 
444  $Records = $this->unsafe_select( $this->NativeTable.".id = $id" );
445 
446  if( count( $Records ) == 0 )
447  {
448  throw( new Exception( 'Record was not found' ) );
449  }
450 
451  return( $Records[ 0 ] );
452  }
453  catch( Exception $e )
454  {
455  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
456  }
457  }
458 
477  function set_read( $id )
478  {
479  try
480  {
481  $id = $this->Security->get( $id , 'integer' );
482  $Login = $this->UserAlgorithms->get_login();
483 
484  $Record = $this->PmsgAccess->unsafe_select( "id = $id AND recipient LIKE '$Login'" );
485  if( count( $Record ) > 0 )
486  {
487  $this->PmsgAccess->update( $id , array( '`read`' => 1 ) );
488  }
489  }
490  catch( Exception $e )
491  {
492  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
493  }
494  }
495  }
496 
497 ?>