Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
RAuthItemBehavior.php
1 <?php
2 /**
3 * Rights authorization item behavior class file.
4 *
5 * @author Christoffer Niska <cniska@live.com>
6 * @copyright Copyright &copy; 2010 Christoffer Niska
7 * @since 0.9.11
8 */
9 class RAuthItemBehavior extends CBehavior
10 {
11  /**
12  * @property integer the id of the user to whom this item is assigned.
13  */
14  public $userId;
15  /**
16  * @property CAuthItem the parent item.
17  */
18  public $parent;
19  /**
20  * @property integer the amount of children this item has.
21  */
22  public $childCount;
23 
24  /**
25  * Constructs the behavior.
26  *
27  * @param integer $userId the id of the user to whom this item is assigned
28  * @param \CAuthItem $parent the parent item.
29  *
30  * @return \RAuthItemBehavior
31  */
32  public function __construct($userId=null, CAuthItem $parent=null)
33  {
34  $this->userId = $userId;
35  $this->parent = $parent;
36  }
37 
38  /**
39  * Returns the item name.
40  *
41  * @return string the markup.
42  */
43  public function getNameText()
44  {
45  return (Rights::module()->displayDescription===true && $this->owner->description!==null) ? $this->owner->description : $this->owner->name;
46  }
47 
48  /**
49  * Returns the link to update the item.
50  *
51  * @return string the markup.
52  */
53  public function getNameLink()
54  {
55  return CHtml::link(
56  $this->getNameText(),
57  array(
58  'authItem/update',
59  'name'=>urlencode($this->owner->name),
60  )
61  );
62  }
63 
64  /**
65  * Returns the markup for the name link to displayed in the grid.
66  *
67  * @return string the markup.
68  */
69  public function getGridNameLink()
70  {
71  $markup = CHtml::link($this->owner->name, array('authItem/update', 'name'=>urlencode($this->owner->name)));
72 
73  $markup .= $this->childCount();
74  $markup .= $this->sortableId();
75 
76  return $markup;
77  }
78 
79  /**
80  * Returns the markup for the child count.
81  *
82  * @return string the markup.
83  */
84  public function childCount()
85  {
86  if ($this->childCount === null) {
87  $this->childCount = count($this->owner->getChildren());
88  }
89 
90  return $this->childCount>0 ? ' [ <span class="child-count">'.$this->childCount.'</span> ]' : '';
91  }
92 
93  /**
94  * Returns the markup for the id required by jui sortable.
95  *
96  * @return string the markup.
97  */
98  public function sortableId()
99  {
100  return ' <span class="auth-item-name" style="display:none;">'.$this->owner->name.'</span>';
101  }
102 
103  /**
104  * Returns the markup for the item type.
105  *
106  * @return string the markup.
107  */
108  public function getTypeText()
109  {
110  return Rights::getAuthItemTypeName($this->owner->type);
111  }
112 
113  /**
114  * Returns the markup for the delete operation link.
115  *
116  * @return string the markup.
117  */
118  public function getDeleteOperationLink()
119  {
120  return CHtml::linkButton(
121  Rights::t('core', 'Delete'),
122  array(
123  'submit'=>array('authItem/delete', 'name'=>urlencode($this->owner->name)),
124  'confirm'=>Rights::t('core', 'Are you sure you want to delete this operation?'),
125  'class'=>'delete-link',
126  'csrf'=>Yii::app()->request->enableCsrfValidation,
127  )
128  );
129  }
130 
131  /**
132  * Returns the markup for the delete task link.
133  * @return string the markup.
134  */
135  public function getDeleteTaskLink()
136  {
137  return CHtml::linkButton(Rights::t('core', 'Delete'), array(
138  'submit'=>array('authItem/delete', 'name'=>urlencode($this->owner->name)),
139  'confirm'=>Rights::t('core', 'Are you sure you want to delete this task?'),
140  'class'=>'delete-link',
141  'csrf'=>Yii::app()->request->enableCsrfValidation,
142  ));
143  }
144 
145  /**
146  * Returns the markup for the delete role link.
147  *
148  * @return string the markup.
149  */
150  public function getDeleteRoleLink()
151  {
152  // We do not want to show the delete link for the superuser role.
153  if ( $this->owner->name!==Rights::module()->superuserName ) {
154  return CHtml::linkButton(
155  Rights::t('core', 'Delete'),
156  array(
157  'submit'=>array('authItem/delete', 'name'=>urlencode($this->owner->name)),
158  'confirm'=>Rights::t('core', 'Are you sure you want to delete this role?'),
159  'class'=>'delete-link',
160  'csrf'=>Yii::app()->request->enableCsrfValidation,
161  )
162  );
163  }
164  }
165 
166  /**
167  * Returns the markup for the remove parent link.
168  *
169  * @return string the markup.
170  */
171  public function getRemoveParentLink()
172  {
173  return CHtml::linkButton(
174  Rights::t('core', 'Remove'),
175  array(
176  'submit'=>array('authItem/removeChild', 'name'=>urlencode($this->owner->name), 'child'=>urlencode($this->parent->name)),
177  'class'=>'remove-link',
178  'csrf'=>Yii::app()->request->enableCsrfValidation,
179  )
180  );
181  }
182 
183  /**
184  * Returns the markup for the remove child link.
185  *
186  * @return string the markup.
187  */
188  public function getRemoveChildLink()
189  {
190  return CHtml::linkButton(
191  Rights::t('core', 'Remove'),
192  array(
193  'submit'=>array('authItem/removeChild', 'name'=>urlencode($this->parent->name), 'child'=>urlencode($this->owner->name)),
194  'class'=>'remove-link',
195  'csrf'=>Yii::app()->request->enableCsrfValidation,
196  )
197  );
198  }
199 
200  /**
201  * Returns the markup for the revoke assignment link.
202  *
203  * @return string the markup.
204  */
205  public function getRevokeAssignmentLink()
206  {
207  return CHtml::linkButton(
208  Rights::t('core', 'Revoke'),
209  array(
210  'submit'=>array('assignment/revoke', 'id'=>$this->userId, 'name'=>urlencode($this->owner->name)),
211  'class'=>'revoke-link',
212  'csrf'=>Yii::app()->request->enableCsrfValidation,
213  )
214  );
215  }
216 
217  /**
218  * Returns the markup for the revoke permission link.
219  *
220  * @param CAuthItem $role the role the permission is for.
221  *
222  * @return string the markup.
223  */
224  public function getRevokePermissionLink(CAuthItem $role)
225  {
226  $csrf = Rights::getDataCsrf();
227 
228  return CHtml::link(
229  Rights::t('core', 'Revoke'), '#',
230  array(
231  'onclick'=>"
232  jQuery.ajax({
233  type:'POST',
234  url:'".Yii::app()->controller->createUrl('authItem/revoke', array('name'=>urlencode($role->name), 'child'=>urlencode($this->owner->name)))."',
235  data:{ ajax:1 $csrf },
236  success:function() {
237  $('#permissions').load('".Yii::app()->controller->createUrl('authItem/permissions')."', { ajax:1 $csrf });
238  }
239  });
240 
241  return false;
242  ",
243  'class'=>'revoke-link',
244  )
245  );
246  }
247 
248  /**
249  * Returns the markup for the assign permission link.
250  *
251  * @param CAuthItem $role the role the permission is for.
252  *
253  * @return string the markup.
254  */
255  public function getAssignPermissionLink(CAuthItem $role)
256  {
257  $csrf = Rights::getDataCsrf();
258 
259  return CHtml::link(
260  Rights::t('core', 'Assign'), '#',
261  array(
262  'onclick'=>"
263  jQuery.ajax({
264  type:'POST',
265  url:'".Yii::app()->controller->createUrl('authItem/assign', array('name' => urlencode($role->name), 'child' => urlencode($this->owner->name)))."',
266  data:{ ajax:1 $csrf },
267  success:function() {
268  $('#permissions').load('".Yii::app()->controller->createUrl('authItem/permissions')."', { ajax:1 $csrf });
269  }
270  });
271 
272  return false;
273  ",
274  'class'=>'assign-link',
275  )
276  );
277  }
278 
279  /**
280  * Returns the markup for a inherited permission.
281  *
282  * @param array $parents the parents for this item.
283  * @param boolean $displayType whether to display the parent item type.
284  *
285  * @return string the markup.
286  */
287  public function getInheritedPermissionText($parents, $displayType=false)
288  {
289  $items = array();
290  foreach ( $parents as $itemName=>$item ) {
291  $itemMarkup = $item->getNameText();
292 
293  if ($displayType===true) {
294  $itemMarkup .= ' ('.Rights::getAuthItemTypeName($item->type).')';
295  }
296 
297  $items[] = $itemMarkup;
298  }
299 
300  return '<span class="inherited-item" title="'.implode('<br />', $items).'">'.Rights::t('core', 'Inherited').' *</span>';
301  }
302 }