ultimix
link_dictionary.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 $LinkTypesDictionary = false;
39 
50  var $Database = false;
51  var $Security = false;
52 
67  function __construct()
68  {
69  try
70  {
71  $this->Database = get_package( 'database' , 'last' , __FILE__ );
72  $this->Security = get_package( 'security' , 'last' , __FILE__ );
73  }
74  catch( Exception $e )
75  {
76  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
77  }
78  }
79 
106  function get_type_from_cache( $Object1Type , $Object2Type )
107  {
108  try
109  {
110  $Types = array();
111 
112  foreach( $this->LinkTypesDictionary as $k => $v )
113  {
114  if( $Object1Type !== false && $v->object1_type != $Object1Type )
115  {
116  continue;
117  }
118  if( $Object2Type !== false && $v->object2_type != $Object2Type )
119  {
120  continue;
121  }
122 
123  $Types [] = $v->id;
124  }
125  if( isset( $Types[ 0 ] ) )
126  {
127  return( implode( ',' , $Types ) );
128  }
129 
130  return( false );
131  }
132  catch( Exception $e )
133  {
134  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
135  }
136  }
137 
152  function fill_cache()
153  {
154  try
155  {
156  if( $this->LinkTypesDictionary === false )
157  {
158  $this->Database->query_as( DB_OBJECT );
159  $this->LinkTypesDictionary = $this->Database->select( '*' , 'umx_link_dictionary' , "1 = 1" );
160  }
161  }
162  catch( Exception $e )
163  {
164  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
165  }
166  }
167 
194  function get_type_from_db( $Object1Type , $Object2Type )
195  {
196  try
197  {
198  $this->fill_cache();
199 
200  return( $this->get_type_from_cache( $Object1Type , $Object2Type ) );
201  }
202  catch( Exception $e )
203  {
204  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
205  }
206  }
207 
230  function create_link_type( $Object1Type , $Object2Type )
231  {
232  try
233  {
234  $Object1Type = $this->Security->get( $Object1Type , 'string' );
235  $Object2Type = $this->Security->get( $Object2Type , 'string' );
236 
237  $this->Database->lock( array( 'umx_link_dictionary' ) , array( 'WRITE' ) );
238  $this->Database->insert( 'umx_link_dictionary' , 'object1_type , object2_type' ,
239  "'$Object1Type' , '$Object2Type'" );
240  $this->Database->commit();
241  $this->Database->unlock();
242 
243  $this->LinkTypesDictionary = false;
244  }
245  catch( Exception $e )
246  {
247  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
248  }
249  }
250 
277  function get_link_type( $Object1Type , $Object2Type )
278  {
279  try
280  {
281  $LinkType = $this->get_type_from_db( $Object1Type , $Object2Type );
282 
283  if( $LinkType === false )
284  {
285  if( $Object1Type === false || $Object2Type === false )
286  {
287  /* type with this parameters was not found */
288  return( 0 );
289  }
290  else
291  {
292  /* both types defined so we can create new link type */
293  $this->create_link_type( $Object1Type , $Object2Type );
294 
295  return( $this->get_link_type( $Object1Type , $Object2Type ) );
296  }
297  }
298  else
299  {
300  return( $LinkType );
301  }
302  }
303  catch( Exception $e )
304  {
305  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
306  }
307  }
308 
331  function get_link_literal_type( &$Type )
332  {
333  try
334  {
335  $Type = is_array( $Type ) === true ? $Type : array( $Type );
336  $this->fill_cache();
337  $Types = array();
338  foreach( $this->LinkTypesDictionary as $k => $v )
339  {
340  foreach( $Type as $i => $t )
341  {
342  if( $v->id == $t )
343  {
344  $Types [] = array(
345  'object1_type' => $v->object1_type , 'object2_type' => $v->object2_type
346  );
347  }
348  }
349  }
350  if( isset( $Types[ 0 ] ) === false )
351  {
352  throw( new Exception( 'Literal type of the link was not found' ) );
353  }
354  return( $Types );
355  }
356  catch( Exception $e )
357  {
358  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
359  }
360  }
361  }
362 
363 ?>