Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
tool_clear_matrix_cache.inc
1 <?php
18 require_once SQ_SYSTEM_ROOT.'/core/assets/system/tool/tool.inc';
19 
34 {
35 
36 
43  function Tool_Clear_Matrix_Cache($assetid=0)
44  {
45  $this->Tool($assetid);
46 
47  }//end constructor
48 
49 
59  public static function paintTool(&$o, $type_code)
60  {
61  $tool_info = $GLOBALS['SQ_SYSTEM']->am->getTypeInfo($type_code);
62 
63  // caching is not enabled
64  $cache_manager = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('cache_manager');
65  if (!$cache_manager->attr('enabled')) {
66  $o->openSection($tool_info['name']);
67  $o->openField('Note');
68  $o->note('<span class="sq-backend-warning">Matrix caching status is currently "Off".</span><br />');
69  $o->closeField();
70  $o->closeSection();
71  return;
72  }
73 
74  $o->openSection($tool_info['name']);
75 
76  // interface to get to asset using asset finder
77  $o->openField(translate('choose_asset'));
78  asset_finder($type_code.'_assetid');
79  $o->closeField();
80 
81  // level option
82  $o->openField(translate('level'));
83  $options = Array(
84  'single' => translate('this_asset'),
85  'dependants' => translate('this_asset_and_dependants'),
86  'children' => translate('this_asset_and_children'),
87  );
88  combo_box($type_code.'_level', $options, '', FALSE);
89  $o->closeField();
90 
91  // asset types dropdown
92  $o->openField(translate('asset_types'));
93  $type_options = Array();
94  $asset_types = $GLOBALS['SQ_SYSTEM']->am->getTypeList();
95  foreach ($asset_types as $code) {
96  $info = $GLOBALS['SQ_SYSTEM']->am->getTypeInfo($code);
97  if ($info['allowed_access'] != 'system' && $info['instantiable']) {
98  $type_options[$code] = $info['name'];
99  }
100  }
101  asort($type_options);
102  $type_options = array_reverse($type_options, TRUE);
103  $type_options[''] = '-- '.translate('all_asset_types').' --';
104  $type_options = array_reverse($type_options, TRUE);
105  combo_box($type_code.'_type_codes', $type_options, TRUE, '', 8);
106  $o->closeField();
107 
108  // if repopulate box is checked, will request page using CURL
109  $o->openField(translate('tool_matrix_cache_populate_question'));
110  $disabled = (function_exists('curl_init')) ? '' : 'disabled';
111  check_box($type_code.'_repopulate', TRUE, FALSE, '', $disabled);
112  echo translate('tool_matrix_cache_populate_note');
113  if ($disabled) {
114  // we cannot repopulate if libcurl is not installed
115  $o->note('<span class="sq-backend-warning">Note: PHP libcurl package is not installed</span>');
116  }
117  echo '<br /><br />';
118  $o->note(translate('tool_matrix_cache_note'));
119  $o->closeField();
120 
121  // if clear now box is checked, cache is cleared at commit
122  $o->openField(translate('tool_matrix_cache_clear_now_field'));
123  check_box($type_code.'_clear_now', TRUE);
124  $o->closeField();
125 
126  $o->closeSection();
127 
128 
129  // scheduled jobs section
130  $o->openSection(translate('matrix_cache_schedule_section'));
131 
132  $cron_mgr = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('cron_manager');
133  $GLOBALS['SQ_SYSTEM']->am->includeAsset('Cron_Job_Clear_Matrix_Cache');
135 
136  // print scheduled jobs, one_off or repeating
137  if (!empty($purge_jobs)) {
138  $o->openField(translate('matrix_cache_scheduled_job_field'), 'new_line');
139  ?>
140  <table class="sq-backend-table">
141  <tr>
142  <td class="sq-backend-table-header"><?php echo translate('asset'); ?></td>
143  <td class="sq-backend-table-header"><?php echo translate('when'); ?></td>
144  <td align="center" class="sq-backend-table-header" style="text-align: center;"><?php echo translate('delete_question'); ?></td>
145  </tr>
146  <?php
147  for ($i = 0; $i < count($purge_jobs); $i++) {
148  ?>
149  <tr>
150  <td class="sq-backend-table-cell">
151  <?php echo get_asset_tag_line($purge_jobs[$i]->attr('ownerid')); ?>
152 
153  </td>
154  <td class="sq-backend-table-cell">
155  <?php echo $purge_jobs[$i]->readableWhen(); ?>
156  </td>
157  <td align="center" class="sq-backend-table-cell">
158  <?php
159  check_box($type_code.'_remove_fs[]', $purge_jobs[$i]->id);
160  ?>
161  </td>
162  </tr>
163  <?php
164  }//end for
165  ?>
166  </table><br />
167  <?php
168  $o->closeField();
169  }//end if
170 
171  $cron_epoc = (int)$cron_mgr->attr('epoch');
172  $cron_refresh = (int)$cron_mgr->attr('refresh_time');
173  if ($cron_epoc > 0 && $cron_refresh > 0) {
174 
175  // prints the interface for adding a new one-off job
176  $o->openField(translate('matrix_cache_one_off_field'), 'new_line');
177  require_once SQ_FUDGE_PATH.'/general/datetime.inc';
178  $new_job = new Cron_Job_Clear_Matrix_Cache();
179  $edit_fns = $new_job->getEditFns();
180  $edit_fns->paintInLineBackend($new_job, $o, $type_code.'_one_off');
181  echo '&nbsp;<br /><br />';
182  $o->closeField();
183 
184  // prints the interface for adding a new repeating job
185  $o->openField(translate('matrix_cache_repeating_field'), 'new_line');
186  $new_job = new Cron_Job_Clear_Matrix_Cache();
187  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
188  $new_job->setAttrValue('type', 'repeating');
189  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
190  $edit_fns = $new_job->getEditFns();
191  $edit_fns->paintInLineBackend($new_job, $o, $type_code.'_repeating');
192  $o->note(translate('cron_manager_next_run', $cron_mgr->readableRefreshTime(), readable_datetime($cron_mgr->timeOfNextRun())));
193  $o->closeField();
194 
195  } else {
196  $o->openField('Note');
197  $o->note(translate('cron_manager_not_configured'));
198  $o->closeField();
199  }
200 
201  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($cron_mgr);
202  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($cache_manager);
203 
204  $o->closeSection();
205 
206  }//end paintTool()
207 
208 
218  public static function processTool(&$o, $type_code)
219  {
220  $cache_manager = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('cache_manager');
221  if (!$cache_manager->attr('enabled')) return;
222 
223  $cron_mgr = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('cron_manager');
224  $GLOBALS['SQ_SYSTEM']->am->includeAsset('Cron_Job_Clear_Matrix_Cache');
225 
226  // remove cron jobs
227  if (!empty($_POST[$type_code.'_remove_fs'])) {
228  $GLOBALS['SQ_SYSTEM']->am->includeAsset('Cron_Job_Clear_Matrix_Cache');
230  if (!empty($purge_jobs)) {
231  if ($GLOBALS['SQ_SYSTEM']->am->acquireLock($cron_mgr->id, 'links')) {
232  for ($i = 0; $i < count($purge_jobs); $i++) {
233  if (in_array($purge_jobs[$i]->id, $_POST[$type_code.'_remove_fs'])) {
234  $cron_mgr->removeJob($purge_jobs[$i]);
235  }
236  }
237 
238  $GLOBALS['SQ_SYSTEM']->am->releaseLock($cron_mgr->id, 'links');
239  } else {
240  trigger_localised_error('SYS0232', E_USER_NOTICE);
241  }
242  }
243  // remove job only, do not print error msg
244  if (!$_REQUEST[$type_code.'_assetid']['assetid']) {
245  return FALSE;
246  }
247  }
248 
249  if (isset($_POST[$type_code.'_assetid']) && !empty($_POST[$type_code.'_assetid']['assetid'])) {
250  // running vars
251  $assetid = $_POST[$type_code.'_assetid']['assetid'];
252  $level = (isset($_POST[$type_code.'_level'])) ? $_POST[$type_code.'_level'] : 'single';
253  $type_codes = Array();
254  if (isset($_POST[$type_code.'_type_codes']) && !empty($_POST[$type_code.'_type_codes'][0])) {
255  $type_codes = $_POST[$type_code.'_type_codes'];
256  }
257  $repopulate = FALSE;
258  if (isset($_POST[$type_code.'_repopulate']) && $_POST[$type_code.'_repopulate']) {
259  $repopulate = TRUE;
260  }
261  $run_now = isset($_POST[$type_code.'_clear_now']) && $_POST[$type_code.'_clear_now'];
262  if ($repopulate) $run_now = TRUE;
263 
264  // one off
265  $new_job = new Cron_Job_Clear_Matrix_Cache();
266  $edit_fns = $new_job->getEditFns();
267  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
268  $new_job->setAttrValue('ownerid', $assetid);
269  $new_job->setAttrValue('level', $level);
270  $new_job->setAttrValue('type_codes', $type_codes);
271  $new_job->setAttrValue('repopulate', $repopulate);
272  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
273  if ($edit_fns->processInLineBackend($new_job, $o, $type_code.'_one_off')) {
274  $cron_mgr->addJob($new_job, $GLOBALS['SQ_SYSTEM']->user);
275  }
276 
277  // repeating
278  $new_job = new Cron_Job_Clear_Matrix_Cache();
279  $edit_fns = $new_job->getEditFns();
280  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
281  $new_job->setAttrValue('type', 'repeating');
282  $new_job->setAttrValue('ownerid',$assetid);
283  $new_job->setAttrValue('level', $level);
284  $new_job->setAttrValue('type_codes', $type_codes);
285  $new_job->setAttrValue('repopulate', $repopulate);
286  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
287  if ($edit_fns->processInLineBackend($new_job, $o, $type_code.'_repeating')) {
288  $cron_mgr->addJob($new_job, $GLOBALS['SQ_SYSTEM']->user);
289  }
290 
291  // run HIPO job if the 'clear now' box is checked
292  if ($run_now) {
293  $hh = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
294  $vars = Array(
295  'assetid' => Array ($assetid),
296  'level' => $level,
297  'type_codes' => $type_codes,
298  'delete_all' => 'no',
299  'repopulate' => $repopulate,
300  );
301  $hh->queueHipo('hipo_job_clear_cache', $vars, $_SERVER['REQUEST_URI'].'&tool_type_code=tool_clear_matrix_cache');
302  $url = $hh->runQueuedJobs();
303  if (!empty($url)) $o->setRedirect($url);
304  }
305  }//end if asset id is set
306 
307  return FALSE;
308 
309  }//end processTool()
310 
311 
312 }//end class
313 ?>