Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
cron_job_attempt_safe_trash.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  parent::__construct($assetid);
44 
45  }//end constructor
46 
47 
56  protected function _createAdditional(Array &$link)
57  {
58  if (!parent::_createAdditional($link)) return FALSE;
59 
60  if (!empty($this->_tmp['asset_in_link'])) {
61  if (!$GLOBALS['SQ_SYSTEM']->am->acquireLock($this->id, 'links')) {
62  trigger_localised_error('CRON0018', E_USER_WARNING);
63  return FALSE;
64  }
65 
66  $link = $this->_tmp['asset_in_link'];
67  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($link['minorid'], $link['minor_type_code']);
68  if (is_null($asset) || !$this->setAssetToUpdate($asset)) {
69  trigger_localised_error('CRON0017', E_USER_WARNING);
70  return FALSE;
71  }
72 
73  unset($this->_tmp['asset_in_link']);
74  $GLOBALS['SQ_SYSTEM']->am->releaseLock($this->id, 'links');
75  }
76  return TRUE;
77 
78  }//end _createAdditional()
79 
80 
90  protected function _getName($short_name=FALSE)
91  {
92  $asset = $this->getAssetToUpdate();
93  if (is_null($asset)) {
94  return translate('cron_job_assetless_attempt_safe_trash');
95  } else if ($short_name) {
96  return translate('cron_safe_trash_for', translate('asset_format', $asset->short_name, $asset->id));
97  } else {
98  return translate('cron_attempt_safe_trash_for', translate('asset_format', $asset->name, $asset->id));
99  }
100 
101  }//end _getName()
102 
103 
111  public function _getAllowedLinks()
112  {
113  $links = parent::_getAllowedLinks();
114 
115  // make sure that we can link to every asset
116  if (empty($links[SQ_LINK_NOTICE]['asset'])) {
117  $links[SQ_LINK_NOTICE]['asset'] = Array('card' => 1);
118  }
119 
120  return $links;
121 
122  }//end _getAllowedLinks()
123 
124 
134  public function canDelete()
135  {
136  if (parent::canDelete()) return TRUE;
137 
138  // read-only?
139  if ($this->attr('read_only')) return FALSE;
140 
141  // TODO: will this restriction be allowed too?
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 'type' :
162  trigger_localised_error('CRON0038', E_USER_NOTICE);
163  return FALSE;
164  break;
165  }//end if
166 
167  return parent::setAttrValue($name, $value);
168 
169  }//end setAttrValue()
170 
171 
180  public function setAssetToUpdate(Asset $asset)
181  {
182  if (!($asset instanceof Asset)) {
183  trigger_localised_error('CRON0030', E_USER_WARNING);
184  return FALSE;
185  }
186 
187  if ($this->id) {
188  $existing_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($this->id, SQ_LINK_NOTICE, '', TRUE, 'major', $value='updating');
189  $link_exists = FALSE;
190  foreach ($existing_links as $old_link) {
191  if ($old_link['minorid'] == $asset->id) {
192  $link_exists = TRUE;
193  } else {
194  $GLOBALS['SQ_SYSTEM']->am->deleteAssetLink($old_link['linkid']);
195  }
196  }
197  if ($link_exists) {
198  return TRUE;
199  } else {
200  return (bool) $this->createLink($asset, SQ_LINK_NOTICE, 'updating');
201  }
202  } else {
203  if (!isset($this->_tmp['asset_in_link'])) {
204  $this->_tmp['asset_in_link'] = Array();
205  }
206  $this->_tmp['asset_in_link'] = Array('minorid' => $asset->id, 'minor_type_code' => $asset->type());
207  return TRUE;
208  }
209 
210  }//end setAssetToUpdate()
211 
212 
219  public function getAssetToUpdate()
220  {
221  // because we need to return by ref
222  $null = NULL;
223 
224  if ($this->id) {
225  $link = $GLOBALS['SQ_SYSTEM']->am->getLink($this->id, SQ_LINK_NOTICE, 'asset', FALSE, 'updating');
226  } else {
227  $link = (isset($this->_tmp['asset_in_link'])) ? $this->_tmp['asset_in_link'] : Array();
228  }
229  if (empty($link)) return $null;
230 
231  return $GLOBALS['SQ_SYSTEM']->am->getAsset($link['minorid'], $link['minor_type_code']);
232 
233  }//end getAssetToUpdate()
234 
235 
247  protected function _exec(&$msg)
248  {
249  $asset = $this->getAssetToUpdate();
250  if (!is_null($asset)) {
251 
252  // Can we safe trash?
253  $safe_trash_enabled = $GLOBALS['SQ_SYSTEM']->getUserPrefs('user', 'SQ_USER_SAFE_TYPE3_TRASH');
254  $safe_trash_errors = $GLOBALS['SQ_SYSTEM']->am->canSafeTrashAsset($asset->id);
255 
256  if ($safe_trash_enabled && !empty($safe_trash_errors)) {
257  $msg = 'Cannot trash '.$asset->id;
258  // We keep this job hanging around until we can delete it
259  return SQ_CRON_JOB_COMPLETED;
260  } else {
261  $GLOBALS['SQ_SYSTEM']->am->trashAsset($asset->id);
262  $msg = 'Successfully trashed '.$asset->id;
263  // We can remove it now
264  return SQ_CRON_JOB_COMPLETED | SQ_CRON_JOB_REMOVE;
265  }
266 
267  } else {
268  // asset not found
269  trigger_localised_error('CRON0020', E_USER_WARNING);
270  }
271 
272  // we can be removed because the error is beyound our control
273  return SQ_CRON_JOB_ERROR | SQ_CRON_JOB_REMOVE;
274 
275  }//end _exec()
276 
277 
278 }//end class
279 
280 ?>