Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
cron_job_future_permission.inc
1 <?php
18 require_once SQ_CORE_PACKAGE_PATH.'/system/cron/cron_job/cron_job.inc';
19 
32 {
33 
34 
41  function __construct($assetid=0)
42  {
43  $this->_ser_attrs = TRUE;
44  parent::__construct($assetid);
45 
46  }//end constructor
47 
48 
57  protected function _createAdditional(Array &$link)
58  {
59  if (!parent::_createAdditional($link)) return FALSE;
60 
61  if (!empty($this->_tmp['asset_in_link'])) {
62  if (!$GLOBALS['SQ_SYSTEM']->am->acquireLock($this->id, 'links')) {
63  trigger_localised_error('CRON0059', E_USER_WARNING);
64  return FALSE;
65  }
66 
67  $link = $this->_tmp['asset_in_link'];
68  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($link['minorid'], $link['minor_type_code']);
69  if (is_null($asset) || !$this->setAssetToUpdate($asset)) {
70  trigger_localised_error('CRON0060', E_USER_WARNING);
71  return FALSE;
72  }
73 
74  unset($this->_tmp['asset_in_link']);
75  $GLOBALS['SQ_SYSTEM']->am->releaseLock($this->id, 'links');
76  }
77  return TRUE;
78 
79  }//end _createAdditional()
80 
81 
91  protected function _getName($short_name=FALSE)
92  {
93  $asset = $this->getAssetToUpdate();
94  if (is_null($asset)) {
95  return translate('cron_job_asset_less_future_permission');
96  } else if ($short_name) {
97  return translate('cron_fp_for', translate('asset_format', $asset->short_name, $asset->id));
98  } else {
99  return translate('cron_future_permission_for', translate('asset_format', $asset->name, $asset->id));
100  }
101 
102  }//end _getName()
103 
104 
112  public function _getAllowedLinks()
113  {
114  $links = parent::_getAllowedLinks();
115 
116  // make sure that we can link to every asset
117  if (empty($links[SQ_LINK_NOTICE]['asset'])) {
118  $links[SQ_LINK_NOTICE]['asset'] = Array('card' => 1);
119  }
120 
121  return $links;
122 
123  }//end _getAllowedLinks()
124 
125 
135  public function canDelete()
136  {
137  if (parent::canDelete()) return TRUE;
138 
139  // read-only?
140  if ($this->attr('read_only')) return FALSE;
141 
142  $asset = $this->getAssetToUpdate();
143  if (!is_null($asset)) return $asset->adminAccess('');
144  return FALSE;
145 
146  }//end canDelete()
147 
148 
158  public function setAttrValue($name, $value)
159  {
160  switch ($name) {
161  case 'permission' :
162  $value = (int) $value;
163  if (!in_array($value, $this->_getAllPermissions())) {
164  trigger_localised_error('CRON0061', E_USER_NOTICE);
165  return FALSE;
166  }
167  break;
168 
169  case 'type' :
170  trigger_localised_error('CRON0062', E_USER_NOTICE);
171  return FALSE;
172  break;
173  }//end if
174 
175  return parent::setAttrValue($name, $value);
176 
177  }//end setAttrValue()
178 
179 
188  protected static function _getAllPermissions()
189  {
190  return Array('read' => SQ_PERMISSION_READ, 'write' => SQ_PERMISSION_WRITE, 'admin' => SQ_PERMISSION_ADMIN);
191 
192  }//end _getAllPermissions()
193 
194 
204  public static function getAllPermissionDescs()
205  {
206  static $descs = Array();
207 
208  if (empty($descs)) {
209  $permissions = self::_getAllPermissions();
210  foreach ($permissions as $name => $value) {
211  $descs[$value] = ucfirst($name);
212  }
213  }
214 
215  return $descs;
216 
217  }//end getAllPermissionDescs()
218 
219 
226  public function permissionName()
227  {
228  $descs = self::getAllPermissionDescs();
229  return $descs[$this->attr('permission')];
230 
231  }//end permissionName()
232 
233 
242  public function getGrantedUsers($grant = TRUE)
243  {
244  $grant_int = $grant? 1 : 0;
245  $userid_grant_list = $this->attr('userids');
246  $userids = Array();
247  foreach ($userid_grant_list as $userid => $user_grant) {
248  if ($user_grant == $grant_int) {
249  $userids[] = $userid;
250  }
251  }
252 
253  return $userids;
254 
255  }//end getGrantedUsers()
256 
263  public function actionName()
264  {
265  return ($this->attr('add')? translate('add') : translate('remove'));
266 
267  }//end actionName()
268 
277  public function setAssetToUpdate(Asset $asset)
278  {
279  if ($this->id) {
280  $existing_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($this->id, SQ_LINK_NOTICE, '', TRUE, 'major', $value='updating');
281  $link_exists = FALSE;
282  foreach ($existing_links as $old_link) {
283  if ($old_link['minorid'] == $asset->id) {
284  $link_exists = TRUE;
285  } else {
286  $GLOBALS['SQ_SYSTEM']->am->deleteAssetLink($old_link['linkid']);
287  }
288  }
289  if ($link_exists) {
290  return TRUE;
291  } else {
292  return (bool) $this->createLink($asset, SQ_LINK_NOTICE, 'updating');
293  }
294  } else {
295  if (!isset($this->_tmp['asset_in_link'])) {
296  $this->_tmp['asset_in_link'] = Array();
297  }
298  $this->_tmp['asset_in_link'] = Array('minorid' => $asset->id, 'minor_type_code' => $asset->type());
299  return TRUE;
300  }
301 
302  }//end setAssetToUpdate()
303 
304 
311  public function getAssetToUpdate()
312  {
313  // because we need to return by ref
314  $null = NULL;
315 
316  if ($this->id) {
317  $link = $GLOBALS['SQ_SYSTEM']->am->getLink($this->id, SQ_LINK_NOTICE, 'asset', FALSE, 'updating');
318  } else {
319  $link = (isset($this->_tmp['asset_in_link'])) ? $this->_tmp['asset_in_link'] : Array();
320  }
321  if (empty($link)) return $null;
322 
323  return $GLOBALS['SQ_SYSTEM']->am->getAsset($link['minorid'], $link['minor_type_code']);
324 
325  }//end getAssetToUpdate()
326 
327 
339  protected function _exec(&$msg)
340  {
341  $asset = $this->getAssetToUpdate();
342  if (!is_null($asset)) {
343  $add_permissions = $this->attr('add');
344  $permission = $this->attr('permission');
345  $userid_grant_list = $this->attr('userids');
346  $cascade_to_new = $this->attr('cascade_to_new');
347  $previous_access = NULL;
348  $dependants_only = $this->attr('dependants_only');
349  $permission_vars = Array();
350  foreach ($userid_grant_list as $userid => $user_grant) {
351  $granted = $add_permissions? $user_grant : -1;
352  if (!$add_permissions) {
353  $previous_access = $user_grant;
354  }
355  $permission_vars[] = Array(
356  'permission' => $permission,
357  'assetids' => Array($asset->id),
358  'granted' => $granted,
359  'userid' => $userid,
360  'cascades' => $cascade_to_new,
361  'previous_access' => $previous_access,
362  'dependants_only' => !(!$dependants_only && $cascade_to_new), //consistency with the permission screen logic
363  );
364  }
365 
366  $hh = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
367  $vars = Array(
368  'permission_changes' => $permission_vars,
369  );
370  $errors = $hh->freestyleHipo('hipo_job_edit_permissions', $vars);
371  if (empty($errors)) {
372  //success
373  return SQ_CRON_JOB_COMPLETED | SQ_CRON_JOB_REMOVE;
374  } else {
375  // process failed
376  trigger_localised_error('CRON0063', E_USER_WARNING, $asset->name, $asset->id);
377  }
378 
379  } else {
380  // asset not found
381  trigger_localised_error('CRON0064', E_USER_WARNING);
382  }
383 
384  // we can be removed because the error is beyound our control
385  return SQ_CRON_JOB_ERROR | SQ_CRON_JOB_REMOVE;
386 
387  }//end _exec()
388 
389 
400  public static function &getActiveJobs(Asset $asset)
401  {
402  $links = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_LINK_NOTICE, 'cron_job_future_permission', TRUE, 'minor', 'updating');
403  $assetids = Array();
404  foreach ($links as $link) {
405  $assetids[] = $link['majorid'];
406  }
407 
408  sort($assetids);
409 
410  $fp_jobs = Array();
411  foreach ($assetids as $assetid) {
412  $fp_jobs[] = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid, 'cron_job_future_permission');
413  }
414 
415  return $fp_jobs;
416 
417  }//end getActiveJobs()
418 
419 
420 }//end class
421 
422 ?>