Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
cache_manager_edit_fns.inc
1 <?php
17 require_once SQ_INCLUDE_PATH.'/asset_edit/asset_edit_fns.inc';
18 require_once SQ_INCLUDE_PATH.'/asset_edit_interface.inc';
19 
34 {
35 
36 
42  function __construct()
43  {
44  parent::__construct();
45 
46  unset($this->static_screens['settings']);
47  unset($this->static_screens['preview']);
48  unset($this->static_screens['lookupValues']);
49  unset($this->static_screens['web_paths']);
50  unset($this->static_screens['metadata']);
51  unset($this->static_screens['metadataSchemas']);
52  unset($this->static_screens['workflow']);
53  unset($this->static_screens['tagging']);
54  unset($this->static_screens['dependants']);
55  unset($this->static_screens['linking']);
56  unset($this->static_screens['roles']);
57  unset($this->static_screens['permissions']);
58 
59  }//end constructor
60 
61 
62 //-- SPECIFIC CACHE EXPIRY --//
63 
64 
75  function paintChooseTypeCode(&$asset, &$o, $prefix)
76  {
77  $type_codes = $asset->attr('type_codes');
78  $asset_types = $GLOBALS['SQ_SYSTEM']->am->getTypeList();
79 
80  foreach ($asset_types as $type_code) {
81  $info = $GLOBALS['SQ_SYSTEM']->am->getTypeInfo($type_code);
82  $options[$type_code] = $info['name'];
83  }
84  asort($options);
85 
86  // we dont use the combo_box function here as we are going
87  // to colourise the options that have been customised
88 
89  if (isset($_REQUEST[$prefix.'_select_type'])) {
90  $selected = $_REQUEST[$prefix.'_select_type'];
91  } else {
92  $selected = (isset($_REQUEST[$prefix.'_type'])) ? $_REQUEST[$prefix.'_type'] : '';
93  }
94 
95  ?>
96  <select name="<?php echo $prefix; ?>_select_type" onchange="this.form.submit();">
97  <option value="">-- <?php echo translate('select_asset_type'); ?> --</option>
98  <?php
99  foreach ($options as $type_code => $name) {
100  $style = (isset($type_codes[$type_code])) ? ' style="background-color: #C8BDCB";' : '';
101  ?>
102  <option<?php echo $style; ?> value="<?php echo $type_code; ?>"<?php echo ($selected == $type_code) ? ' selected' : ''; ?>>
103  <?php echo $name; ?>
104  </option>
105  <?php
106  }
107  ?></select><?php
108  $o->addFormActionGetVar($prefix.'_type', $selected, TRUE);
109  submit_button($prefix.'_go', translate('go'));
110  echo '<br />'.translate('type_code_precedence_over_root_node');
111 
112  if (isset($_REQUEST[$prefix.'_select_type']) && !empty($_REQUEST[$prefix.'_select_type'])) {
113  echo '<br/><br/><b>'.translate('currently_customising', $options[$_REQUEST[$prefix.'_select_type']]).'</b>';
114  }
115 
116  return FALSE;
117 
118  }//end paintChooseTypeCode()
119 
120 
131  function processChooseTypeCode(&$asset, &$o, $prefix)
132  {
133  return FALSE;
134 
135  }//end processChooseTypeCode()
136 
137 
148  function paintCustomiseTypeCodeCaching(&$asset, &$o, $prefix)
149  {
150 
151  $write_access = $asset->writeAccess('attributes');
152 
153  if (isset($_REQUEST[$prefix.'_select_type'])) {
154  $selected = $_REQUEST[$prefix.'_select_type'];
155  } else {
156  $selected = (isset($_REQUEST[$prefix.'_type'])) ? $_REQUEST[$prefix.'_type'] : '';
157  }
158 
159  if (!empty($selected)) {
160 
161  $this->_paintSpecificCacheManagement($asset, $o, $prefix, 'type_codes', $selected, FALSE);
162 
163  } else {
164  echo translate('customise_asset_selection');
165  return FALSE;
166 
167  }//end if !empty()
168 
169  return $write_access;
170 
171  }//end paintCustomiseTypeCodeCaching()
172 
173 
187  function _paintSpecificCacheManagement(&$asset, &$o, $prefix, $attr_name, $selected, $check_exists=FALSE)
188  {
189 
190  $wa = $asset->writeAccess('attributes');
191  $current_values = $asset->attr($attr_name);
192  // check if the value to be configured actually exists if specified
193  if ($check_exists) {
194  $paint = in_array($selected, array_keys($current_values)) ? TRUE : FALSE;
195  } else {
196  $paint = TRUE;
197  }
198  if ($paint) {
199  // field opened by asset_edit_interface
200  $using_default = !isset($current_values[$selected]['enabled']);
201  if ($using_default) {
202  echo translate('using_default_value_of',(($asset->attr('enabled')) ? translate('on') : translate('off'))).'<br/>';
203  } else {
204  $caching = ($current_values[$selected]['enabled']) ? 1 : 0;
205  if ($wa) {
206  combo_box($prefix.'_enabled', Array('1' => translate('on'), '0' => translate('off')), FALSE, $caching);
207  } else {
208  echo ($current_values[$selected]['enabled']) ? translate('on') : translate('off');
209  }
210  }
211  if ($wa) {
212  check_box($prefix.'_default_enabled', '1', $using_default);
213  echo ' '.translate('use_default');
214  }
215  $o->closeField();
216 
217  $o->openField(translate('default_expiry'), '', translate('valid_cache_length'));
218  $using_default = !isset($current_values[$selected]['expiry']);
219  if ($using_default) {
220  echo translate('using_default_value_of', $asset->attr('expiry').' seconds').'<br/>';
221  } else {
222  if ($wa) {
223  text_box($prefix.'_expiry', $current_values[$selected]['expiry'], 5);
224  } else {
225  echo $current_values[$selected]['expiry'];
226  }
227  }
228  if ($wa) {
229  check_box($prefix.'_default_expiry', '1', $using_default);
230  echo ' '.translate('use_default');
231  }
232  $o->closeField();
233 
234  $o->openField(translate('cache_manager_browser_cache_expiry'), '', translate('cache_manager_browser_valid_cache_length'));
235  $using_default = !isset($current_values[$selected]['browser_cache_expiry']);
236  if ($using_default) {
237  $browser_cache_expiry = $asset->attr('browser_cache_expiry');
238  if (empty($browser_cache_expiry)) {
239  echo translate('cache_manager_using_blank_default_value_equal_to', 'the system Default Expiry', $asset->attr('expiry').' seconds').'<br>';
240  } else {
241  echo translate('using_default_value_of', $browser_cache_expiry.' seconds').'<br/>';
242  }
243  } else {
244  if ($wa) {
245  text_box($prefix.'_browser_cache_expiry', $current_values[$selected]['browser_cache_expiry'], 5);
246  } else {
247  echo $current_values[$selected]['browser_cache_expiry'];
248  }
249  }
250 
251  if ($wa) {
252  check_box($prefix.'_default_browser_cache_expiry', '1', $using_default);
253  echo ' '.translate('use_default');
254  }
255  $o->closeField();
256 
257  $o->openField(translate('http_header_cacheable_field'));
258  $using_default = !isset($current_values[$selected]['send_cacheable_header']);
259  if ($using_default) {
260  echo translate('using_default_value_of', SQ_CONF_SEND_CACHEABLE_HEADER ? translate('yes') : translate('no')).'<br/>';
261  } else {
262  if ($wa) {
263  combo_box($prefix.'_send_cacheable_header', Array('1' => translate('on'), '0' => translate('off')), FALSE, $current_values[$selected]['send_cacheable_header']);
264  } else {
265  echo $current_values[$selected]['send_cacheable_header'];
266  }
267  }
268  if ($wa) {
269  check_box($prefix.'_default_send_cacheable_header', '1', $using_default);
270  echo ' '.translate('use_default');
271  }
272 
273  // field closed by asset_edit_interface
274  }
275 
276  }//end _paintSpecificCacheManagement();
277 
278 
292  function _processSpecificCacheManagement(&$asset, &$o, $prefix, $attr_name, $value_get_var, $indicate_customised=FALSE)
293  {
294  $current_values = $asset->attr($attr_name);
295 
296  if ($asset->writeAccess('attributes') && !isset($_POST[$value_get_var]) && isset($_GET[$value_get_var])) {
297  $type = $_REQUEST[$value_get_var];
298  $defaults = Array('enabled', 'expiry', 'browser_cache_expiry', 'send_cacheable_header');
299  $save = FALSE;
300 
301  foreach ($defaults as $default) {
302  $using_default = FALSE;
303  if (!isset($_POST[$prefix.'_default_'.$default])) {
304  if (!isset($current_values[$type][$default])) {
305  $save = TRUE;
306  if($default === 'send_cacheable_header') {
307  $current_values[$type][$default] = SQ_CONF_SEND_CACHEABLE_HEADER;
308  }
309  else {
310  $current_values[$type][$default] = $asset->attr($default);
311  }
312  }
313  } else {
314  $save = TRUE;
315  $using_default = TRUE;
316  if (isset($current_values[$type][$default])) {
317  unset($current_values[$type][$default]);
318  }
319  }
320  if (isset($_POST[$prefix.'_'.$default]) && !$using_default) {
321  if (!isset($current_values[$type][$default]) || $current_values[$type][$default] != $_POST[$prefix.'_'.$default]) {
322  $current_values[$type][$default] = $_POST[$prefix.'_'.$default];
323  $save = TRUE;
324  }
325  }
326  }
327  if (isset($current_values[$type]) && empty($current_values[$type])) {
328  unset($current_values[$type]);
329  }
330 
331  if ($indicate_customised) {
332  // indicate that this value is customised
333  if (count((array_diff($defaults, array_keys($current_values[$type])))) < count($defaults)) {
334  $current_values[$type]['customised'] = 1;
335  } else {
336  $current_values[$type]['customised'] = 0;
337  }
338  }
339 
340  return ($save) ? $asset->setAttrValue($attr_name, $current_values) : FALSE;
341  }
342  return FALSE;
343 
344  }//end _processSpecificCacheManagement()
345 
346 
357  function processCustomiseTypeCodeCaching(&$asset, &$o, $prefix)
358  {
359  return $this->_processSpecificCacheManagement($asset, $o, $prefix, 'type_codes', $prefix.'_type', FALSE);
360 
361  }//end processCustomiseTypeCodeCaching()
362 
363 
374  function paintChooseRootNode(&$asset, &$o, $prefix)
375  {
376  $root_nodes = $asset->attr('root_nodes');
377  $wa = $asset->writeAccess('attributes');
378  if ($wa && empty($root_nodes)) {
379  $root_nodes = Array(0);
380  }
381 
382  $key = 0;
383  foreach ($root_nodes as $assetid => $data) {
384  ?>
385  <table>
386  <tbody>
387  <tr>
388  <td colspan="2">
389  <?php
390  if ($wa) {
391  // holds the previous value
392  hidden_field($prefix.'_root_node_previous_values['.$key.']', $assetid);
393  asset_finder($prefix.'_root_node_X_'.$key.'_X_', $assetid);
394  } else {
395  echo get_asset_tag_line($assetid);
396  }
397  ?>
398  </td>
399  <td>
400  <?php
401  if (isset($data['customised'])) {
402  $customised = $data['customised'];
403  } else {
404  $customised = FALSE;
405  }
406  if ($wa && !empty($assetid)) {
407  $radio_name = $prefix.'_customise_root_node';
408  $radio_id = $prefix.'_'.$assetid.'_customise_root_node';
409  check_box($radio_name, $assetid, FALSE, 'this.form.submit();', 'id="'.$radio_id.'"');
410  if ($customised) {
411  label(translate('view_customisations'), $radio_id);
412  } else {
413  label(translate('customise'), $radio_id);
414  }
415  } else {
416  if ($customised) {
417  $radio_name = $prefix.'_customise_root_node';
418  $radio_id = $prefix.'_'.$assetid.'_customise_root_node';
419  check_box($radio_name, $assetid, FALSE, 'this.form.submit();', 'id="'.$radio_id.'"');
420  label('<i>'.translate('view_customisations').'</i>', $radio_id);
421  } else {
422  if (!empty($assetid)) {
423  echo '<i>'.translate('not_yet_customised').'</i>';
424  }
425  }
426  }
427  ?>
428  </td>
429  </tr>
430  <tr>
431  <td width="360px">
432  <?php
433  if ($wa) {
434  $options = Array(
435  0 => translate('no'),
436  1 => translate('yes'),
437  );
438  $combo_name = $prefix.'_all_underneath_X_'.$key.'_X_';
439  combo_box($combo_name, $options, FALSE, $data['all_underneath']);
440  label(' '.translate('include_all_assets'), $combo_name);
441  } else {
442  if ($data['all_underneath']) {
443  echo '<b>'.translate('assets_underneath_root_node_affected').'</b>';
444  } else {
445  echo '<b>'.translate('assets_underneath_root_node_not_affected').'</b>';
446  }
447  }
448  ?>
449  </td>
450  <td colspan="2">
451  <?php
452  if ($wa && $key+1 === count($root_nodes)) {
453  $more_button_name = $prefix.'_root_node_more_button_X_'.$key.'_X_';
454  normal_button($more_button_name, translate('more').'...', 'var table = prependLastTable(this); resetLastTableFields(table, false)');
455  }
456  ?>
457  </td>
458  </tr>
459  </tbody>
460  </table>
461  <?php
462  $key++;
463  if ($key !== count($root_nodes)) echo '<br />';
464  }//end foreach
465 
466  if (isset($_REQUEST[$prefix.'_customise_root_node']) && in_array($_REQUEST[$prefix.'_customise_root_node'], array_keys($root_nodes))) {
467  if ($wa) {
468  echo '<br/><br/><b>'.translate('currently_customising', get_asset_tag_line($_REQUEST[$prefix.'_customise_root_node'])).'</b>';
469  } else {
470  echo '<br/><br/><b>'.translate('currently_viewing', get_asset_tag_line($_REQUEST[$prefix.'_customise_root_node'])).'</b>';
471  }
472  } else {
473  unset($_REQUEST[$prefix.'_customise_root_node']);
474  }
475 
476  ?>
477  <script type="text/javascript">
478 
479  function prependLastTable(moreButton)
480  {
481  var lastTable = moreButton.parentNode;
482  while (lastTable.tagName != 'TABLE') {
483  lastTable = lastTable.parentNode;
484  }
485 
486  // to be used when adding the button back
487  var lastSibling = lastTable.previousSibling;
488  var lastTableButtonCell = moreButton.parentNode;
489 
490  // look for sq-backend-data to prepend this table
491  var validLocation = moreButton.parentNode;
492  while (validLocation.className != 'sq-backend-data') {
493  validLocation = validLocation.parentNode;
494  }
495 
496  var clone = lastTable.cloneNode(true);
497 
498  // inserting clone after original
499  validLocation.insertBefore(clone, lastTable.nextSibling);
500 
501  // append the 'More' button to the cloned table
502  lastTableButtonCell.appendChild(moreButton);
503 
504  // remove the 'More' button from the original after cloning
505  moreButton.parentNode.removeChild(moreButton);
506 
507  // remove the customise button elements from the clone
508  var cloneCustomiseCell = clone.rows[0].cells[1];
509  removeElementChildNodes(cloneCustomiseCell);
510 
511  // add text in place of the customise button elements in the clone
512  var cloneCustomiseText = document.createElement('i');
513  cloneCustomiseCell.appendChild(cloneCustomiseText);
514 
515  // use innerHTML for IE6
516  cloneCustomiseText.innerHTML = js_translate('customise_after_commit');
517 
518  // remove the asset finder from the clone
519  var cloneAssetFinderCell = clone.rows[0].cells[0];
520  removeElementChildNodes(cloneAssetFinderCell);
521 
522  // traverse DOM and increment each field name
523  incrementTableFieldNames(clone);
524 
525  // add an asset finder for the cloned table
526  cloneInnerHTML(lastTable.rows[0].cells[0], cloneAssetFinderCell);
527 
528  lastTable.appendChild(document.createElement('br'));
529 
530  return clone;
531  }
532 
533  // adds a new asset finder as a child of the specified element
534  function cloneInnerHTML(cellToCopy, cellToPopulate)
535  {
536  var htmlToClone = cellToCopy.innerHTML;
537  // discover what the new key should be
538  var currentKey = htmlToClone.match(/_X_(\d+)_X_/);
539  var newKey = parseInt(currentKey[1]) + 1;
540  cellToPopulate.innerHTML = htmlToClone.replace(/_X_(\d+)_X_/g, '_X_' + newKey + '_X_');
541  }
542 
543  // removes all childNodes from the specified element
544  function removeElementChildNodes(element)
545  {
546  if (element.hasChildNodes()) {
547  while (element.childNodes.length > 0 ) {
548  element.removeChild(element.firstChild);
549  }
550  }
551  }
552 
553  function incrementTableFieldNames(element)
554  {
555  var i=0;
556  var elementChildren = element.childNodes[i];
557  var tagName = element.tagName;
558 
559  if ((tagName == 'INPUT') || (tagName == 'SELECT')) {
560  // increment field name
561  // have to use the _X_ delimiters because asset_finder cleans form names
562  var currentKey = element.name.match(/_X_(\d+)_X_/);
563  var newKey = parseInt(currentKey[1]) + 1;
564  element.name = element.name.replace(/_X_(\d+)_X_/, '_X_' + newKey + '_X_');
565  element.id = element.id.replace(/_X_(\d+)_X_/, '_X_' + newKey + '_X_');
566  }
567 
568  while (elementChildren) {
569  incrementTableFieldNames(elementChildren);
570  i++;
571  elementChildren = element.childNodes[i];
572  }
573  }
574 
575  function resetLastTableFields(element, findTable)
576  {
577  // clear all elements inside the table
578  if (findTable) {
579  var element = element.parentNode;
580  while (element.tagName != 'TABLE') {
581  element = element.parentNode;
582  }
583  }
584 
585  var i=0;
586  var elementChildren = element.childNodes[i];
587  var tagName = element.tagName;
588 
589  if (tagName == 'INPUT') {
590  // don't clear buttons
591  if (element.type != 'button') {
592  element.value = '';
593  }
594  return;
595  } else if (tagName == 'SELECT') {
596  element.selectedIndex = 0;
597  return;
598  }
599 
600  while (elementChildren) {
601  resetLastTableFields(elementChildren, false);
602  i++;
603  elementChildren = element.childNodes[i];
604  }
605  }
606  </script>
607  <?php
608 
609  return TRUE;
610 
611  }//end paintChooseRootNode()
612 
613 
624  function processChooseRootNode(&$asset, &$o, $prefix)
625  {
626  $wa = $asset->writeAccess('attributes');
627  if ($wa) {
628  $root_node_key = $prefix.'_root_node';
629  $submitted_root_nodes = Array();
630  $deleteme = Array();
631  foreach ($_POST as $key => $value) {
632  // look for form names which have _X_ delimiters
633  if (strpos($key, '_X_')) {
634  // retrieve the current index value
635  if (preg_match("/^$root_node_key".'_X_(\d+)_X_/', $key, $matches)) {
636  $current_row = $matches[1];
637  if (isset($_POST[$prefix.'_root_node_previous_values'][$current_row])) {
638  $previous_value = $_POST[$prefix.'_root_node_previous_values'][$current_row];
639  } else {
640  // populate the previous value with the actual value when it is retrieved
641  // this is because when new root nodes are created, they do not have a previous value
642  $previous_value = '';
643  }
644  }
645 
646  // retrieve the actual values submitted
647  if (preg_match("/^$root_node_key/", $key)) {
648  $root_node = htmlspecialchars($_POST[$key]['assetid']);
649  // if no root node was submitted, skip it and mark for deletion
650  if (!$root_node) {
651  // record the previous assetid (i.e. the one to delete)
652  $deleteme[] = $previous_value;
653  continue;
654  }
655 
656  if (empty($previous_value)) {
657  $submitted_root_nodes[$root_node]['previous'] = $root_node;
658  } else {
659  $submitted_root_nodes[$root_node]['previous'] = $previous_value;
660  }
661  } else if (isset($submitted_root_nodes[$root_node]) && strpos($key, 'all_underneath')) {
662  $all_underneath = $_POST[$key];
663  $submitted_root_nodes[$root_node]['all_underneath'] = $all_underneath;
664  }
665  }
666  }
667 
668  // compare submitted values with stored ones and update/delete accordingly
669  $current_root_nodes = $asset->attr('root_nodes');
670  if (!empty($submitted_root_nodes)) {
671  foreach ($submitted_root_nodes as $assetid => $data) {
672  if (isset($current_root_nodes[$assetid])) {
673  // updating this existing node's options
674  foreach ($current_root_nodes[$assetid] as $key => $value) {
675  if (isset($data[$key])) {
676  $current_root_nodes[$assetid][$key] = $data[$key];
677  } else {
678  // submitted data does not have this key so use the existing value
679  $current_root_nodes[$assetid][$key] = $value;
680  }
681  }
682  } else if ($assetid !== $data['previous'] && isset($current_root_nodes[$data['previous']])) {
683  // updating this existing's nodes assetid
684  $old_values = $current_root_nodes[$data['previous']];
685  $current_root_nodes[$assetid] = $old_values;
686  unset($current_root_nodes[$data['previous']]);
687  } else if (!empty($submitted_root_nodes[$assetid])) {
688  // this value does not exist so create it
689  $current_root_nodes[$assetid] = $data;
690  // do not show root node cache management section
691  unset($_REQUEST[$prefix.'_customise_root_node']);
692  }
693  }
694 
695  // delete entries marked for deletion
696  if (!empty($deleteme)) {
697  foreach($deleteme as $row => $assetid) {
698  unset($current_root_nodes[$assetid]);
699  }
700  }
701 
702  // do not need 'previous' anymore
703  if (isset($current_root_nodes[$assetid]['previous'])) {
704  unset($current_root_nodes[$assetid]['previous']);
705  }
706  } else {
707  // delete them all
708  $current_root_nodes = Array();
709  }
710  $asset->setAttrValue('root_nodes', $current_root_nodes);
711  }
712  return $wa;
713 
714  }//end processChooseRootNode()
715 
716 
727  function paintCustomiseRootNodeCaching(&$asset, &$o, $prefix)
728  {
729  $wa = $asset->writeAccess('attributes');
730 
731  // get the assetid to customise
732  if (isset($_REQUEST[$prefix.'_customise_root_node'])) {
733  $this->_paintSpecificCacheManagement($asset, $o, $prefix, 'root_nodes', $_REQUEST[$prefix.'_customise_root_node'], TRUE);
734  } else {
735  if ($wa) {
736  echo translate('customise_root_node_selection');
737  } else {
738  echo translate('view_root_node_selection');
739  }
740  }
741 
742  return $wa;
743 
744  }//end paintCustomiseRootNodeCaching()
745 
746 
757  function processCustomiseRootNodeCaching(&$asset, &$o, $prefix)
758  {
759  if (isset($_REQUEST[$prefix.'_customise_root_node'])) {
760  $o->addFormActionGetVar($prefix.'_customise_root_node', $_REQUEST[$prefix.'_customise_root_node'], TRUE);
761 
762  // first check if the value still exists
763  if (in_array($_REQUEST[$prefix.'_customise_root_node'], array_keys($asset->attr('root_nodes')))) {
764  return $this->_processSpecificCacheManagement($asset, $o, $prefix, 'root_nodes', $prefix.'_customise_root_node', TRUE);
765  } else {
766  return FALSE;
767  }
768  }
769 
770  }//end processCustomiseRootNodeCaching()
771 
772 
773 //-- CLEAR CACHE --//
774 
775 
786  function paintClearCache(&$asset, &$o, $prefix)
787  {
788  if (!$asset->writeAccess('attributes')) {
789  echo translate('cannot_clear_cache');
790  return FALSE;
791  }
792 
793  if (!$asset->canClearByAssetid()) {
794  echo translate('cannot_clear_cache_by_assetid');
795  return FALSE;
796  }
797 
798  multiple_asset_finder($prefix.'_clear_cache', Array());
799 
800  $o->openField(translate('level'));
801  $options = Array(
802  'single' => translate('this_asset'),
803  'dependants' => translate('this_asset_and_dependants'),
804  'children' => translate('this_asset_and_children'),
805  );
806  combo_box($prefix.'_level', $options, '', FALSE);
807  $o->closeField();
808 
809  $o->openField(translate('asset_types'));
810  $type_options = Array();
811  $asset_types = $GLOBALS['SQ_SYSTEM']->am->getTypeList();
812  foreach ($asset_types as $type_code) {
813  $info = $GLOBALS['SQ_SYSTEM']->am->getTypeInfo($type_code);
814  if ($info['allowed_access'] != 'system' && $info['instantiable']) {
815  $type_options[$type_code] = $info['name'];
816  }
817  }
818 
819  asort($type_options);
820  $type_options = array_reverse($type_options, TRUE);
821  $type_options[''] = '-- '.translate('all_asset_types').' --';
822  $type_options = array_reverse($type_options, TRUE);
823  combo_box($prefix.'_type_codes', $type_options, TRUE, '', 8);
824  $o->closeField();
825 
826  return TRUE;
827 
828  }//end paintClearCache()
829 
830 
841  function processClearCache(&$asset, &$o, $prefix)
842  {
843  if (isset($_POST[$prefix.'_clear_cache']) && $_POST[$prefix.'_delete_all'] != 'yes') {
844  $level = (isset($_POST[$prefix.'_level'])) ? $_POST[$prefix.'_level'] : 'single';
845 
846  if (isset($_POST[$prefix.'_type_codes']) && !empty($_POST[$prefix.'_type_codes'][0])) {
847  $type_codes = $_POST[$prefix.'_type_codes'];
848  } else {
849  $type_codes = Array();
850  }
851 
852  $assets_info = array_get_index($_POST, $prefix.'_clear_cache', Array());
853  $assetids = Array();
854  foreach ($assets_info as $asset_info) {
855  if (!empty($asset_info['assetid'])) {
856  $assetids[] = $asset_info['assetid'];
857  }//end if
858  }//end foreach
859 
860  $delete_all = $_POST[$prefix.'_delete_all'];
861 
862  $hh = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
863  $vars = Array(
864  'assetid' => $assetids,
865  'level' => $level,
866  'type_codes' => $type_codes,
867  'delete_all' => $delete_all,
868  );
869  $hh->queueHipo('hipo_job_clear_cache', $vars);
870 
871  }
872 
873  return TRUE;
874 
875  }//end processClearCache()
876 
877 
888  function paintSystemWideClearCache(&$asset, &$o, $prefix)
889  {
890  if (!$asset->writeAccess('attributes')) {
891  echo translate('cannot_clear_cache');
892  return FALSE;
893  }
894 
895  $o->openField(translate('delete_all_cache'));
896  $delete_all_ops = Array(
897  'yes' => translate('yes'),
898  'no' => translate('no'),
899  );
900  combo_box($prefix.'_delete_all', $delete_all_ops, FALSE, 'no', 0, "onChange=\"Javascript: if (this.value == 'yes') { if(!confirm(js_translate('confirm_purge_cache'))) { this.value = 'no'; } } \"");
901  $o->note(translate('trash_cache_warning'));
902  $o->closeField();
903 
904  return TRUE;
905 
906  }//end paintSystemWideClearCache()
907 
908 
919  function processSystemWideClearCache(&$asset, &$o, $prefix)
920  {
921  if ($_POST[$prefix.'_delete_all'] == 'yes') {
922  $hh = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
923  $vars = Array(
924  'delete_all' => 'yes',
925  );
926  $hh->queueHipo('hipo_job_clear_cache', $vars);
927  }
928 
929  return TRUE;
930 
931  }//end processSystemWideClearCache()
932 
933 
934 //-- REFRESH CACHE --//
935 
936 
947  function paintAddJob(&$asset, &$o, $prefix)
948  {
949  $wa = $asset->writeAccess('attributes');
950  $caching_on = $asset->attr('enabled');
951  if ($wa) {
952  if ($this->cronConfigured()) {
953  $cron_mgr = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('cron_manager');
954 
955  // Include Refresh Cache Cron Job
956  $GLOBALS['SQ_SYSTEM']->am->includeAsset('cron_job_refresh_cache');
957 
958  // Caching is off. Show the warning only
959  if (!$caching_on) {
960  ?>
961  <span class="sq-backend-warning"><b>NOTE:</b> The caching is turned off at the moment. Although you can add a job here, it will not have any effect</span><br /><br />
962  <?php
963  }
964 
965  require_once SQ_FUDGE_PATH.'/general/datetime.inc';
966  $cron_job = new Cron_Job_Refresh_Cache();
967  $edit_fns = $cron_job->getEditFns();
968  $edit_fns->paintInLineBackend($cron_job, $o, $prefix.'_new_rc');
969  $o->note(translate('cron_manager_next_run', $cron_mgr->readableRefreshTime(), readable_datetime($cron_mgr->timeOfNextRun())));
970 
971  } else {
972  // Cron Manager is not configured yet.
973  echo translate('cron_manager_not_configured_refresh');
974  }
975  }
976 
977  return TRUE;
978 
979  }//end paintAddJob()
980 
981 
992  function processAddJob(&$asset, &$o, $prefix)
993  {
994  $wa = $asset->writeAccess('attributes');
995  if ($wa && $this->cronConfigured()) {
996  $cron_mgr = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('cron_manager');
997 
998  // Include Refresh Cache Cron Job
999  $GLOBALS['SQ_SYSTEM']->am->includeAsset('cron_job_refresh_cache');
1000 
1001  if ($asset->writeAccess('')) {
1002  $rc = new Cron_Job_Refresh_Cache();
1003  $edit_fns = $rc->getEditFns();
1004  if ($edit_fns->processInLineBackend($rc, $o, $prefix.'_new_rc')) {
1005  $rc->saveAttributes();
1006  } else {
1007  return FALSE;
1008  }
1009 
1010  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
1011  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
1012  if ($cron_mgr->addJob($rc, $GLOBALS['SQ_SYSTEM']->user)) {
1013  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
1014  } else {
1015  trigger_localised_error('CRON0054', E_USER_WARNING);
1016  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
1017  return FALSE;
1018  }
1019  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
1020  }
1021 
1022  } else {
1023  // Cron Manager not configured
1024  return FALSE;
1025  }
1026 
1027  return TRUE;
1028 
1029  }//end processAddJob()
1030 
1031 
1042  function paintCurrentJobs(&$asset, &$o, $prefix)
1043  {
1044  $wa = $asset->writeAccess('attributes');
1045  $cron_mgr = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('cron_manager');
1046  $GLOBALS['SQ_SYSTEM']->am->includeAsset('cron_job_refresh_cache');
1047  $rc_jobs = $cron_mgr->getJobs('cron_job_refresh_cache');
1048 
1049  if (empty($rc_jobs)) {
1050  echo translate('cron_manager_refresh_no_job');
1051  } else {
1052 ?>
1053  <table class="sq-backend-table">
1054  <tr>
1055  <th class="sq-backend-table-header">When</th>
1056  <th class="sq-backend-table-header">Status</th>
1057  <th class="sq-backend-table-header">Run As</th>
1058  <th class="sq-backend-table-header">Delete</th>
1059  </tr>
1060  <?php
1061  foreach ($rc_jobs as $rc_job) {
1062  ?>
1063  <tr>
1064  <td class="sq-backend-table-cell">
1065  <?php
1066  echo get_asset_tag_line($rc_job->attr('asset'));
1067  echo '<br /><b>Scheduled At:&nbsp;</b>';
1068  echo $rc_job->readableWhen();
1069  ?>
1070  </td>
1071  <td class="sq-backend-table-cell">
1072  <?php
1073  if ($rc_job->attr('long') && $rc_job->attr('running')) {
1074  echo translate('cron_manager_refresh_cache_running');
1075  } else {
1076  echo translate('cron_manager_refresh_cache_not_running');
1077  }
1078  ?>
1079  </td>
1080  <td class="sq-backend-table-cell">
1081  <?php
1082  $user = $rc_job->getRunningUser();
1083  echo get_asset_tag_line($user->id);
1084  unset($user);
1085  ?>
1086  </td>
1087  <td class="sq-backend-table-cell">
1088  <?php
1089  if ($wa) {
1090  check_box($prefix.'_remove_job_'.$rc_job->id);
1091  }
1092  ?>
1093  </td>
1094  </tr>
1095  <?php
1096  }//end foreach
1097  ?>
1098  </table>
1099  <?php
1100  }//end else
1101  return TRUE;
1102 
1103  }//end paintCurrentJobs()
1104 
1105 
1116  function processCurrentJobs(&$asset, &$o, $prefix)
1117  {
1118  $wa = $asset->writeAccess('attributes');
1119  if ($wa && $this->cronConfigured()) {
1120  $cron_mgr = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('cron_manager');
1121 
1122  // Include Refresh Cache Cron Job
1123  $GLOBALS['SQ_SYSTEM']->am->includeAsset('cron_job_refresh_cache');
1124 
1125  // Any job delete?
1126  $jobs = $cron_mgr->getJobs('cron_job_refresh_cache');
1127  for ($i = 0; $i < count($jobs); $i++) {
1128  if (!empty($_POST[$prefix.'_remove_job_'.($jobs[$i]->id)])) {
1129 
1130  if ($GLOBALS['SQ_SYSTEM']->am->acquireLock($cron_mgr->id, 'links')) {
1131  if (!$cron_mgr->removeJob($jobs[$i])) {
1132  trigger_localised_error('CRON0055', E_USER_WARNING);
1133  }
1134  $GLOBALS['SQ_SYSTEM']->am->releaseLock($cron_mgr->id, 'links');
1135  }
1136  }
1137  }//end for delete job
1138  }
1139 
1140  return TRUE;
1141 
1142  }//end processCurrentJobs()
1143 
1144 
1151  function cronConfigured()
1152  {
1153  $cron_mgr = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('cron_manager');
1154  $cron_epoc = (int)$cron_mgr->attr('epoch');
1155  $cron_refresh = (int)$cron_mgr->attr('refresh_time');
1156 
1157  return ($cron_epoc > 0) && ($cron_refresh > 0);
1158 
1159  }//end cronConfigured()
1160 
1161 
1162 //-- DETAILS --//
1163 
1164 
1175  function paintCachableRootURLs(&$asset, &$o, $prefix)
1176  {
1177  $root_urls = explode("\n", SQ_CONF_SYSTEM_ROOT_URLS);
1178  $non_cachable_urls = $asset->attr('non_cachable_urls');
1179  $wa = $asset->writeAccess('attributes');
1180  ?>
1181  <table class="sq-backend-table" style="width: auto">
1182  <thead>
1183  <tr>
1184  <th><?php echo translate('url'); ?></th>
1185  <th><?php echo translate('use_cache'); ?></th>
1186  </tr>
1187  </thead>
1188  <tbody>
1189  <?php
1190  foreach ($root_urls as $i => $url) {
1191  ?>
1192  <tr>
1193  <td><?php echo $url; ?></td>
1194  <td>
1195  <?php
1196  if ($wa) {
1197  check_box($prefix.'_url_'.$i, 1, !in_array($url, $non_cachable_urls));
1198  } else {
1199  $filename = in_array($url, $non_cachable_urls) ? 'cross' : 'tick';
1200  echo '<img src="'.sq_web_path('lib').'/web/images/'.$filename.'.gif" />';
1201  }
1202  ?>
1203  </td>
1204  </tr>
1205  <?php
1206  }
1207  ?>
1208  </tbody>
1209  </table>
1210  <?php
1211 
1212  }//end paintCachableRootURLs()
1213 
1214 
1225  function processCachableRootURLs(&$asset, &$o, $prefix)
1226  {
1227  $root_urls = explode("\n", SQ_CONF_SYSTEM_ROOT_URLS);
1228  $cachable = Array();
1229  foreach ($root_urls as $i => $url) {
1230  if (!empty($_POST[$prefix.'_url_'.$i])) {
1231  $cachable[] = $url;
1232  }
1233  }
1234  // Note that we save the *non-cachable* root URLs, so that any new URLs are
1235  // cachable by default.
1236  $non_cachable = array_diff($root_urls, $cachable);
1237  return $asset->setAttrValue('non_cachable_urls', $non_cachable);
1238 
1239  }//end processCachableRootURLs()
1240 
1241 
1252  function paintCacheStorageType(&$asset, &$o, $prefix)
1253  {
1254  $wa = $asset->writeAccess('attributes');
1255  $current_type = $asset->attr('cache_storage_type');
1256 
1257  $storage_types = $GLOBALS['SQ_SYSTEM']->am->getTypeDescendants('cache_storage');
1258  $names = $GLOBALS['SQ_SYSTEM']->am->getTypeInfo($storage_types, 'name');
1259 
1260  // Eliminate those cache storage types that are not available
1261  // based on the current settings
1262  foreach ($storage_types as $storage_type) {
1263  $GLOBALS['SQ_SYSTEM']->am->includeAsset($storage_type);
1264  if (eval('return '.$storage_type.'::isAvailable();') === FALSE) {
1265  unset($names[$storage_type]);
1266  }
1267  }
1268 
1269  // If the current type has been eliminated, revert to the default
1270  if (isset($names['cache_storage_'.$current_type]) === FALSE) {
1271  $current_type = 'default';
1272  }
1273 
1274  if ($wa) {
1275  combo_box($prefix.'_cache_storage_type', $names, FALSE, 'cache_storage_'.$current_type);
1276  } else {
1277  echo $names['cache_storage_'.$current_type];
1278  }
1279 
1280  }//end paintCacheStorageType()
1281 
1282 
1293  function processCacheStorageType(&$asset, &$o, $prefix)
1294  {
1295  if ($asset->writeAccess('attributes') === FALSE) return FALSE;
1296 
1297  $type = $_POST[$prefix.'_cache_storage_type'];
1298  $type = substr($type, strlen('cache_storage_'));
1299  return $asset->setAttrValue('cache_storage_type', $type);
1300 
1301  }//end processCacheStorageType()
1302 
1303 
1304 //-- ROOT URL SPECEFIC --//
1305 
1306 
1318  function paintCacheableHeadersRootURLs(&$asset, &$o, $prefix)
1319  {
1320  $root_urls = explode("\n", SQ_CONF_SYSTEM_ROOT_URLS);
1321  $cacheable_header_urls = $asset->attr('non_cacheable_header_urls');
1322  $cacheable_user_header_urls = $asset->attr('user_cacheable_header_urls');
1323  $wa = $asset->writeAccess('attributes');
1324  ?>
1325  <table class="sq-backend-table" style="width: auto">
1326  <thead>
1327  <tr>
1328  <th><?php echo translate('url'); ?></th>
1329  <th><?php echo "Send Headers"; ?></th>
1330  <th><?php echo "Public User Only"; ?></th>
1331  </tr>
1332  </thead>
1333  <tbody>
1334  <?php
1335  foreach ($root_urls as $i => $url) {
1336  ?>
1337  <tr>
1338  <td><?php echo $url; ?></td>
1339  <td>
1340  <?php
1341  if ($wa) {
1342  check_box($prefix.'_url_'.$i, 1, !in_array($url, $cacheable_header_urls));
1343  } else {
1344  $filename = in_array($url, $cacheable_header_urls) ? 'cross' : 'tick';
1345  echo '<img src="'.sq_web_path('lib').'/web/images/'.$filename.'.gif" />';
1346  }
1347  ?>
1348  </td>
1349  <td>
1350  <?php
1351  if ($wa) {
1352  check_box($prefix.'_user_url_'.$i, 1, !in_array($url, $cacheable_user_header_urls));
1353  } else {
1354  $filename = in_array($url, $cacheable_user_header_urls) ? 'cross' : 'tick';
1355  echo '<img src="'.sq_web_path('lib').'/web/images/'.$filename.'.gif" />';
1356  }
1357  ?>
1358  </td>
1359  </tr>
1360  <?php
1361  }
1362  ?>
1363  </tbody>
1364  </table>
1365  <?php
1366 
1367  return $wa;
1368 
1369  }//end processCacheableHeadersRootURLs()
1370 
1371 
1383  function processCacheableHeadersRootURLs(&$asset, &$o, $prefix)
1384  {
1385  $root_urls = explode("\n", SQ_CONF_SYSTEM_ROOT_URLS);
1386  $cachable = Array();
1387  foreach ($root_urls as $i => $url) {
1388  if (!empty($_POST[$prefix.'_url_'.$i])) {
1389  $cachable[] = $url;
1390  }
1391  }
1392  // NOTE: Though it might look little confusing, we save the root URLs that shouldn't send cacheable headers,
1393  // so that any new URLs send cacheable headers by default.
1394  $non_cachable = array_diff($root_urls, $cachable);
1395 
1396  $user_cachable = Array();
1397  foreach ($root_urls as $i => $url) {
1398  if (isset($_POST[$prefix.'_user_url_'.$i]) && !empty($_POST[$prefix.'_user_url_'.$i])) {
1399  $user_cachable[] = $url;
1400  }
1401  }
1402  // save urls that we will send headers for ALL users
1403  $user_cacheable = array_diff($root_urls, $user_cachable);
1404  $asset->setAttrValue('user_cacheable_header_urls', $user_cacheable);
1405 
1406  return $asset->setAttrValue('non_cacheable_header_urls', $non_cachable);
1407 
1408  }//end processCacheableHeadersRootURLs()
1409 
1410 
1421  function paintCacheableHeadersSettings(&$asset, &$o, $prefix)
1422  {
1423  $o->openField(translate('http_header_cacheable_field'));
1424  echo (SQ_CONF_SEND_CACHEABLE_HEADER) ? translate('yes') : translate('no');
1425  $o->note(translate('cache_manager_send_cacheable_headers_settings_node'));
1426  $o->closeField();
1427  }
1428 
1429 
1430 }//end class
1431 
1432 ?>