ACL Extension  0.3
 All Data Structures Namespaces Files Functions Variables
Action.php
Go to the documentation of this file.
1 <?php
2 
22 class Action extends CActiveRecord
23 {
24 
30  public static function translateActions($obj, $actions){
31  if(is_string($actions))
32  $actions = static::translateStringActions($actions);
33 
34  //Now, check if the object restricts the actions
35  $class = NULL;
36 
37  if($obj instanceof CActiveRecord)
38  $class = get_class($class);
39  elseif($obj->model !== NULL){
40  $class = $obj->model;
41  }
42 
43  if($class === NULL)
44  return array();
45 
46 
47  if(isset($class::$possibleActions)){
48  $newActions = array();
49  foreach($actions as $action){
50  if(in_array($action, $class::$possibleActions))
51  $newActions[] = $action;
52  }
53  $actions = $newActions;
54  }
55 
56 
57 
58  return $actions;
59  }
60 
65  protected static function translateStringActions($actions){
66  //Nothing more to do
67  if(is_array($actions))
68  return $actions;
69 
70  //Now, it is a string
71  //Search the first occurence of a modificator (+ or -)
72  $posMinus = strpos($actions, '-');
73  $posPlus = strpos($actions, '+');
74 
75  //If none is found, we can split it up into the actions
76  if($posMinus === false && $posPlus === false){
77  $actions = str_replace(",", " ",$actions);
78  $actions = explode(" ", $actions);
79 
80  $completedActions = array();
81  foreach($actions as $action){
82  $action = trim($action);
83  if(strlen($action) > 0){
84 
85  if($action == '*')
86  $completedActions = array_merge($completedActions, static::getAllStringActions());
87  else
88  $completedActions[] = $action;
89  }
90  }
91 
92  return $completedActions;
93  }
94 
95  else{
96  return static::processActionOperation($posMinus, $posPlus, $actions);
97  }
98  }
99 
107  protected static function processActionOperation($posMinus, $posPlus, $actions){
108  $firstPos = NULL;
109  if($posMinus !== false && $posPlus !== false)
110  $firstPos = min($posMinus, $posPlus);
111  elseif($posMinus === false)
112  $firstPos = $posPlus;
113  else
114  $firstPos = $posMinus;
115 
116  $operation = $firstPos == $posMinus ? '-' : '+';
117 
118  $startStr = substr($actions, 0, $firstPos);
119  $endStr = substr($actions, $firstPos + 1);
120 
121  $startActions = static::translateStringActions($startStr);
122  $endActions = static::translateStringActions($endStr);
123 
124  if($operation == '+'){
125  return array_merge($startActions, $endActions);
126  }
127  else{
128  return array_diff($startActions, $endActions);
129  }
130  }
131 
136  protected static function getAllStringActions(){
137  $actions = Action::model()->findAll();
138  $sActions = array();
139 
140  foreach($actions as $action){
141  $sActions[] = $action->name;
142  }
143  return $sActions;
144  }
145 
151  public static function model($className=__CLASS__)
152  {
153  return parent::model($className);
154  }
155 
159  public function tableName()
160  {
161  return '{{action}}';
162  }
163 
167  public function rules()
168  {
169  // NOTE: you should only define rules for those attributes that
170  // will receive user inputs.
171  return array(
172  array('name, created', 'required'),
173  array('created', 'numerical', 'integerOnly'=>true),
174  array('name', 'length', 'max'=>15),
175  // The following rule is used by search().
176  // Please remove those attributes that should not be searched.
177  array('id, name, created', 'safe', 'on'=>'search'),
178  );
179  }
180 
184  public function relations()
185  {
186  // NOTE: you may need to adjust the relation name and the related
187  // class name for the relations automatically generated below.
188  return array(
189  );
190  }
191 
195  public function attributeLabels()
196  {
197  return array(
198  'id' => 'ID',
199  'name' => 'Name',
200  'created' => 'Created',
201  );
202  }
203 
208  public function search()
209  {
210  // Warning: Please modify the following code to remove attributes that
211  // should not be searched.
212 
213  $criteria=new CDbCriteria;
214 
215  $criteria->compare('id',$this->id);
216  $criteria->compare('name',$this->name,true);
217  $criteria->compare('created',$this->created);
218 
219  return new CActiveDataProvider($this, array(
220  'criteria'=>$criteria,
221  ));
222  }
223 }
224 ?>