ACL Extension  0.3
 All Data Structures Namespaces Files Functions Variables
AclObject.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * To change this template, choose Tools | Templates
5  * and open the template in the editor.
6  */
7 
16 abstract class AclObject extends CActiveRecord{
17 
23  abstract public function join($obj);
24 
31  protected function beforeJoin(&$obj){
32  $this->assureSafety($obj);
33  }
34 
41  protected function beforeLeave(&$obj){
42  $this->assureSafety($obj);
43  }
44 
51  protected function beforeIs(&$obj){
52  $this->assureSafety($obj);
53  }
54 
60  abstract public function leave($obj);
61 
67  abstract public function is($obj);
68 
76  abstract public function getFreeNodes();
77 
84  abstract public function fetchComprisedPositions();
85 
94  abstract public function addPositionCheck($positions, $type, $table = 't');
95 
102  abstract protected function createNode($parent = NULL);
103 
116  abstract public function getDirectChildNodes(AclObject $child = NULL);
117 
130  abstract public function getDirectParentNodes(AclObject $parent = NULL);
131 
136  public function getChildObjects(){
137  $childNodes = $this->getDirectChildNodes(NULL);
138  $objects = array();
139 
140  $type = Util::getDataBaseType($this);
141  foreach($childNodes as $node){
142  $obj = $node->{$type};
143  //Why this way? Several nodes may be associated to the same object
144  $objects[$obj->id] = $obj;
145  }
146 
147  return $objects;
148  }
149 
154  public function getParentObjects(){
155  $parentNodes = $this->getDirectParentNodes(NULL);
156  $objects = array();
157 
158  $type = Util::getDataBaseType($this);
159  foreach($parentNodes as $node){
160  $obj = $node->{$type};
161  //Why this way? Several nodes may be associated to the same object
162  $objects[$obj->id] = $obj;
163  }
164 
165  return $objects;
166  }
167 
173  public static function loadObjectStatic($identifier, $model){
174  return self::loadObjectsStatic($identifier, $model, true);
175  }
176 
182  public function loadObject($identifier, $model = NULL){
183  if(!$model)
184  $model = get_class($this);
185  return self::loadObjectsStatic($identifier, $model, true);
186  }
187 
193  public function loadObjects($identifier, $model = NULL, $onlyFirst = true){
194  if(!$model)
195  $model = get_class($this);
196  return self::loadObjects($identifier, $model, $onlyFirst);
197  }
198 
199 
221  public static function loadObjectsStatic($identifier, $model = NULL, $onlyFirst = true){
222 
223  //There are several ways to define the object in question
224  if(is_string($model)){
225  $class = Strategy::getClass($model);
226  $model = new $class();
227  }
228 
229  if($onlyFirst)
230  $method = 'find';
231  else
232  $method = 'findAll';
233 
234  //An alias is being used
235  if(is_string($identifier)){
236 
237  $objects = $model->$method('alias=:alias', array('alias' => $identifier));
238 
239  if(!$objects){
240  if(Strategy::get('strictMode')){
241  throw new Exception('Unknown alias for ACL-ObjectCollection');
242  }
243  else{
244  $obj = new $model;
245  $obj->alias = $identifier;
246  $obj->save();
247 
248  $objects = $onlyFirst ? $obj : array($obj);
249  }
250  }
251 
252  }
253 
254  //The object is searched by its model
255  elseif(
256  is_array($identifier) &&
257  (isset($identifier['foreign_key']) && isset($identifier['model']))
258  ){
259 
260  $objects = $model->$method('foreign_key = :foreign_key AND model = :model',
261  array(':foreign_key' => $identifier['foreign_key'], ':model' => $identifier['model']));
262  if(!$objects){
263  if(Strategy::get('strictMode')){
264  throw new Exception('Unknown foreign key and/or model for ACL-ObjectCollection');
265  }
266  else{
267  $obj = new $model;
268  $obj->foreign_key = $identifier['foreign_key'];
269  $obj->model = $identifier['model'];
270  $obj->save();
271 
272  $objects = $onlyFirst ? $obj : array($obj);
273  }
274 
275  }
276  }
277 
278  //The object is passed directly - do not do anything
279  elseif(is_a($identifier, "AclObject") || (
280  is_a($identifier, "HiddenClass")
281  && is_subclass_of($identifier->pretends(), "Aclobject")
282  )){
283  $objects = $identifier;
284  }
285  elseif(is_a($identifier, "CActiveRecord")){
286  return self::loadObjectsStatic( array('model' => get_class($identifier), 'foreign_key' => $identifier->id), $model, $onlyFirst);
287  }
288  else{
289  throw new Exception('Unknown ACL-Object specification');
290  }
291 
292  return $objects;
293  }
294 
302  public function getNodes(){
303  $class = Util::getNodeNameOfObject($this);
304 
305  return $class::model()->findAll('collection_id = :id', array(':id' => $this->id));
306  }
307 
311  public function afterSave(){
312  parent::afterSave();
313 
314  //If we're new here, we also need a new node for the permissions :)
315  if($this->isNewRecord){
316  $this->createNode();
317  }
318  }
319 
324  public function getAssociatedObject(){
325  if(is_subclass_of($this->model, "CActiveRecord")){
326  $model = $this->model;
327  return $model::model()->findByPk($this->foreign_key);
328  }
329  return NULL;
330  }
331 
335  protected function assureSaved(){
336  $args = func_get_args();
337  foreach($args as $arg){
338  if(is_object($arg) && is_a($arg, "CActiveRecord") && $arg->getIsNewRecord()){
339  if(!$arg->save())
340  throw new RuntimeException('Unable to save object');
341  }
342  }
343  }
344 
351  protected function assureSafety(&$obj){
352  $obj = $this->loadObject($obj);
353  //Assure that objects have been saved
354  $this->assureSaved($this, $obj);
355  }
356 
357 
358 
359 }
360 
361 ?>