CMS  Version 3.9
ConnectableManager Class Reference

Provides a central management class for event handlers and common functionality for the connectable component. More...

Public Member Functions

 ConnectableManager ()
 

Static Public Member Functions

static setDefaults ()
 
static onInitialize ()
 
static registerConnectable ($connectable, $targets)
 Register a connectable class and the list of classes it can connect with. More...
 
static getConnectableFromRequest ()
 Create and populate a Connectable DataItem based on its primary key as passed in on the requesting URL. More...
 
static instantiate ($class, $id)
 Safely instantiate a Connectable. More...
 
static canConnect ($source, $target)
 Determine whether two objects or classes can connect based on the registered connectable map. More...
 
static connectionExists ($source, $target)
 Check whether a connection exists between the given source and target objects. More...
 
static addConnection ($source, $target)
 Add a connection between the given source and target objects. More...
 
static removeConnection ($source, $target)
 Remove any connection that exists between the source and target object (including back-links if they were created). More...
 
static removeConnections ($source, $targetClass)
 Remove any connection that exists between the source and objects of the specified target class (including back-links if they were created). More...
 
static removeAllConnections ($object)
 Remove all the connections to and from the given object. More...
 
static getTargetClasses ($sourceItem)
 Return an array of connectable target classes for the specified item. More...
 
static getConnectedItems ($sourceItem, $targetClass, $constraint="")
 Returns the connected items for the specified source that are of the specified class. More...
 
static getConnectedItemCount ($sourceItem, $targetClass, $constraint="")
 Returns the number of connected items for the specified source that are of the specified class. More...
 
static upgradeComponent ($version)
 

Static Public Attributes

static $connectables = array()
 Connectable type map defining which items can be connected together. More...
 
static $classLookup = array()
 Look-up table of connectable classes from their primary keys. More...
 
static $contextRouter = ""
 

Detailed Description

Provides a central management class for event handlers and common functionality for the connectable component.

Definition at line 25 of file connectable_manager.inc.

Member Function Documentation

◆ addConnection()

static ConnectableManager::addConnection (   $source,
  $target 
)
static

Add a connection between the given source and target objects.

If the target object registered itself as connectable to the source object, then a back-link is automatically inserted.

Parameters
DataItem$sourcethe source object
DataItem$targetthe target object

Definition at line 144 of file connectable_manager.inc.

145  {
147 
148  $sourceDefn = ConnectableManager::$connectables[get_class($source)];
149  $targetDefn = ConnectableManager::$connectables[get_class($target)];
150 
151  $sourceClass = get_class($source);
152  $targetClass = get_class($target);
153 
154  if (!$sourceDefn || !$targetDefn)
155  {
156  throw new FakoliException("Attempt to link an object that is not a registered Connectable");
157  }
158 
159  if (!ConnectableManager::canConnect($sourceClass, $targetClass))
160  {
161  throw new FakoliException("Cannot create a link between a $sourceClass and a $targetClass");
162  }
163 
164  $record = new ConnectionRecord();
165  $record->source_class = $sourceClass;
166  $record->source_id = $source->get($source->getPrimaryKey());
167  $record->target_class = $targetClass;
168  $record->target_id = $target->get($target->getPrimaryKey());
169 
170  $record->save();
171 
172  if (ConnectableManager::canConnect($targetClass, $sourceClass))
173  {
174  // Also insert back-link
175  $record = new ConnectionRecord();
176  $record->source_class = $sourceClass;
177  $record->source_id = $target->get($target->getPrimaryKey());
178  $record->target_class = $targetClass;
179  $record->target_id = $source->get($source->getPrimaryKey());
180 
181  $record->save();
182  }
183  }
static canConnect($source, $target)
Determine whether two objects or classes can connect based on the registered connectable map.
static $connectables
Connectable type map defining which items can be connected together.
static connectionExists($source, $target)
Check whether a connection exists between the given source and target objects.
FakoliException is the base exception class for all Fakoli errors.
Definition: core.inc:53

◆ canConnect()

static ConnectableManager::canConnect (   $source,
  $target 
)
static

Determine whether two objects or classes can connect based on the registered connectable map.

Parameters
mixed$sourceeither a Connectable DataItem or its class name
mixed$targeteither a Connectable DataItem or its class name
Returns
boolean true if the items can be connected, false otherwise

Definition at line 107 of file connectable_manager.inc.

108  {
109  if (is_object($source)) $source = get_class($source);
110  if (is_object($target)) $target = get_class($target);
111 
113 
114  foreach($targets as $t)
115  {
116  if ($t == $target) return true;
117  }
118 
119  return false;
120  }
static getTargetClasses($sourceItem)
Return an array of connectable target classes for the specified item.

◆ ConnectableManager()

ConnectableManager::ConnectableManager ( )

Definition at line 31 of file connectable_manager.inc.

32  {
33 
34  }

◆ connectionExists()

static ConnectableManager::connectionExists (   $source,
  $target 
)
static

Check whether a connection exists between the given source and target objects.

Parameters
DataItem$sourcethe source object
DataItem$targetthe target object

Definition at line 127 of file connectable_manager.inc.

128  {
129  return Query::create(ConnectionRecord, "WHERE source_class=:sc AND source_id=:si AND target_class=:tc AND target_id=:ti")
130  ->bind(":sc", get_class($source),
131  ":si", $source->get($source->getPrimaryKey()),
132  ":tc", get_class($target),
133  ":ti", $target->get($target->getPrimaryKey()))
134  ->exists();
135  }

◆ getConnectableFromRequest()

static ConnectableManager::getConnectableFromRequest ( )
static

Create and populate a Connectable DataItem based on its primary key as passed in on the requesting URL.

Returns
Connectable

Definition at line 63 of file connectable_manager.inc.

64  {
65  foreach (ConnectableManager::$classLookup as $primaryKey => $class)
66  {
67  if (array_key_exists($primaryKey, $_GET))
68  {
69  $id = checkNumeric($_GET[$primaryKey]);
70  $obj = new $class;
71 
72  if ($id) $obj->load($id);
73  return $obj;
74  }
75  }
76 
77  return null;
78  }
static $classLookup
Look-up table of connectable classes from their primary keys.

◆ getConnectedItemCount()

static ConnectableManager::getConnectedItemCount (   $sourceItem,
  $targetClass,
  $constraint = "" 
)
static

Returns the number of connected items for the specified source that are of the specified class.

Parameters
Connectable$sourceItemthe source DataItem (must implement the Connectable interface)
string$targetClassThe class of DataItem Connectable objects that are to be returned
string$constraintadditional constraints to be applied
Exceptions
FakoliException

Definition at line 278 of file connectable_manager.inc.

279  {
280  $query = ConnectableManager::generateConnectedQuery($sourceItem, $targetClass, $constraint);
281  return Query::create($targetClass, $query)
282  ->bind(":sc", get_class($sourceItem), ":si", $sourceItem->get($sourceItem->getPrimaryKey()), ":tc", $targetClass)
283  ->executeValue("COUNT(1)");
284  }
$constraint

◆ getConnectedItems()

static ConnectableManager::getConnectedItems (   $sourceItem,
  $targetClass,
  $constraint = "" 
)
static

Returns the connected items for the specified source that are of the specified class.

Parameters
Connectable$sourceItemthe source DataItem (must implement the Connectable interface)
string$targetClassThe class of DataItem Connectable objects that are to be returned
string$constraintadditional constraints to be applied
Exceptions
FakoliException

Definition at line 262 of file connectable_manager.inc.

263  {
264  $query = ConnectableManager::generateConnectedQuery($sourceItem, $targetClass, $constraint);
265  return Query::create($targetClass, $query)
266  ->bind(":sc", get_class($sourceItem), ":si", $sourceItem->get($sourceItem->getPrimaryKey()), ":tc", $targetClass)
267  ->execute();
268  }

◆ getTargetClasses()

static ConnectableManager::getTargetClasses (   $sourceItem)
static

Return an array of connectable target classes for the specified item.

Parameters
Connectable$sourceItemthe source DataItem (must implement the Connectable interface)
Returns
Array of class names
Exceptions
FakoliException

Definition at line 240 of file connectable_manager.inc.

241  {
242  $sourceClass = is_object($sourceItem) ? get_class($sourceItem) : $sourceItem;
243 
244  if (!array_key_exists($sourceClass, ConnectableManager::$connectables))
245  {
246  throw new FakoliException("$sourceClass is not a registered connectable class");
247  }
248 
249  return ConnectableManager::$connectables[$sourceClass];
250 
251  }

◆ instantiate()

static ConnectableManager::instantiate (   $class,
  $id 
)
static

Safely instantiate a Connectable.

Parameters
string$class
int$id
Exceptions
FakoliException
Returns
Connectable

Definition at line 87 of file connectable_manager.inc.

88  {
89  if (!array_key_exists($class, ConnectableManager::$connectables))
90  {
91  throw new FakoliException(htmlSafe($class)." is not a registered Connectable");
92  }
93 
94  checkNumeric($id);
95 
96  $obj = new $class;
97  if ($id) $obj->load($id);
98  return $obj;
99  }

◆ onInitialize()

static ConnectableManager::onInitialize ( )
static

Definition at line 41 of file connectable_manager.inc.

42  {
43  ComponentManager::fireEvent("RegisterConnectables");
44  }
static fireEvent($event, $parameter=null, $mustBeConsumed=false)
Fire an event to all subscribers as detailed in their manifests.

◆ registerConnectable()

static ConnectableManager::registerConnectable (   $connectable,
  $targets 
)
static

Register a connectable class and the list of classes it can connect with.

Parameters
string$connectablethe connectable class
array$targetsarray of target classes to which it can be connected

Definition at line 51 of file connectable_manager.inc.

52  {
54  $obj = new $connectable;
55  ConnectableManager::$classLookup[$obj->getPrimaryKey()] = $connectable;
56  }

◆ removeAllConnections()

static ConnectableManager::removeAllConnections (   $object)
static

Remove all the connections to and from the given object.

Use this method when the object is being deleted from the database to ensure that you don't end up with stale connections.

Parameters
DataItem$objectthe object for which connections are to be removed

Definition at line 226 of file connectable_manager.inc.

227  {
228  $record = new ConnectionRecord();
229  $objectID = $object->get($object->getPrimaryKey());
230  $class = get_class($object);
231  $record->delete("WHERE (source_class='{$class}' AND source_id={$objectID}) OR (target_class='{$class}' AND target_id={$objectID})");
232  }

◆ removeConnection()

static ConnectableManager::removeConnection (   $source,
  $target 
)
static

Remove any connection that exists between the source and target object (including back-links if they were created).

Parameters
DataItem$sourcethe source object
DataItem$targetthe target object

Definition at line 191 of file connectable_manager.inc.

192  {
193  $record = new ConnectionRecord();
194  $sourceID = checkNumeric($source->get($source->getPrimaryKey()));
195  $targetID = checkNumeric($target->get($target->getPrimaryKey()));
196 
197  $sourceClass = get_class($source);
198  $targetClass = get_class($target);
199 
200  $record->delete("WHERE (source_class='{$sourceClass}' AND source_id={$sourceID} AND target_class='{$targetClass}' AND target_id={$targetID}') " .
201  "OR (source_class='{$targetClass}' AND source_id={$targetID} AND target_class='{$sourceClass}' AND target_id={$sourceID})");
202  }

◆ removeConnections()

static ConnectableManager::removeConnections (   $source,
  $targetClass 
)
static

Remove any connection that exists between the source and objects of the specified target class (including back-links if they were created).

Parameters
DataItem$sourcethe source object
DataItem$targetClassthe target class

Definition at line 210 of file connectable_manager.inc.

211  {
212  $record = new ConnectionRecord();
213  $sourceClass = get_class($source);
214  $sourceID = checkNumeric($source->get($source->getPrimaryKey()));
215 
216  $record->delete("WHERE (source_class='{$sourceClass}' AND source_id={$sourceID} AND target_class='{$targetClass}') " .
217  "OR (source_class='{$targetClass}' AND target_class='{$sourceClass}' AND target_id={$sourceID})");
218  }

◆ setDefaults()

static ConnectableManager::setDefaults ( )
static

Definition at line 36 of file connectable_manager.inc.

37  {
38  //TODO: Set default configuration parameters here
39  }

◆ upgradeComponent()

static ConnectableManager::upgradeComponent (   $version)
static

Definition at line 306 of file connectable_manager.inc.

307  {
309  $mgr->upgrade($version);
310  }

Member Data Documentation

◆ $classLookup

ConnectableManager::$classLookup = array()
static

Look-up table of connectable classes from their primary keys.

Definition at line 28 of file connectable_manager.inc.

◆ $connectables

ConnectableManager::$connectables = array()
static

Connectable type map defining which items can be connected together.

Definition at line 27 of file connectable_manager.inc.

◆ $contextRouter

ConnectableManager::$contextRouter = ""
static

Definition at line 29 of file connectable_manager.inc.


The documentation for this class was generated from the following file: