PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/blog/core/model/modx/processors/resource/reload.class.php

https://bitbucket.org/orchdork10159/dnsman.ly
PHP | 105 lines | 87 code | 6 blank | 12 comment | 19 complexity | 5edcf35f2a7d19c7ffdc3725b73f7007 MD5 | raw file
  1. <?php
  2. /**
  3. * save resource form data for reload
  4. */
  5. class modResourceReloadProcessor extends modProcessor {
  6. /** @var modRegister registry */
  7. private $reg;
  8. /**
  9. * initialization tasks before processing
  10. *
  11. * @return bool|string true to continue with processing, otherwise err msg
  12. */
  13. public function initialize() {
  14. $return = true;
  15. $modx =& $this->modx;
  16. if(!isset($modx->registry)) {
  17. if(!$modx->getService('registry', 'registry.modRegistry')) {
  18. $return = 'Could not instantiate registry service.';
  19. }
  20. }
  21. $modx->registry->addRegister('resource_reload', 'registry.modDbRegister', array('directory' => 'resource_reload'));
  22. $this->reg = $modx->registry->resource_reload;
  23. if(!$this->reg->connect()) {
  24. $return = 'Could not connect to resource_reload queue.';
  25. }
  26. return $return;
  27. }
  28. public function process() {
  29. $return = '';
  30. $scriptProperties = $this->getProperties();
  31. $modx = $this->modx;
  32. foreach ($scriptProperties as $key => &$value) {
  33. $matched = preg_match("/^tv(\d+)$/i", $key, $matches);
  34. if ($matched && !empty($matches[1])) {
  35. $tv = $this->modx->getObject('modTemplateVar', $matches[1]);
  36. if ($tv) {
  37. /* validation for different types */
  38. switch ($tv->get('type')) {
  39. case 'url':
  40. $prefix = $this->getProperty($key.'_prefix','');
  41. if ($prefix != '--') {
  42. $value = str_replace(array('ftp://','http://'),'', $value);
  43. $value = $prefix.$value;
  44. }
  45. break;
  46. case 'date':
  47. $value = empty($value) ? '' : strftime('%Y-%m-%d %H:%M:%S',strtotime($value));
  48. break;
  49. /* ensure tag types trim whitespace from tags */
  50. case 'tag':
  51. case 'autotag':
  52. $tags = explode(',',$value);
  53. $newTags = array();
  54. foreach ($tags as $tag) {
  55. $newTags[] = trim($tag);
  56. }
  57. $value = implode(',',$newTags);
  58. break;
  59. default:
  60. /* handles checkboxes & multiple selects elements */
  61. if (is_array($value)) {
  62. $featureInsert = array();
  63. while (list($featureValue, $featureItem) = each($value)) {
  64. if(empty($featureItem)) { continue; }
  65. $featureInsert[count($featureInsert)] = $featureItem;
  66. }
  67. $value = implode('||',$featureInsert);
  68. }
  69. break;
  70. }
  71. }
  72. }
  73. }
  74. if(array_key_exists('create-resource-token', $scriptProperties) && !empty($scriptProperties['create-resource-token'])) {
  75. $topic = '/resourcereload/';
  76. $this->reg->subscribe($topic);
  77. if(array_key_exists('id', $scriptProperties) && is_numeric($scriptProperties['id']) && intval($scriptProperties['id']) > 0) {
  78. $return = $modx->error->success('', array(
  79. 'id'=> $scriptProperties['id']
  80. ,'reload'=> $scriptProperties['create-resource-token']
  81. ,'action'=> 'resource/update'
  82. ,'class_key' => $scriptProperties['class_key']
  83. ));
  84. } else {
  85. $return = $modx->error->success('', array(
  86. 'reload'=> $scriptProperties['create-resource-token']
  87. ,'action'=> 'resource/create'
  88. ,'class_key' => $scriptProperties['class_key']
  89. ));
  90. }
  91. $this->reg->send($topic, array($scriptProperties['create-resource-token'] => $scriptProperties), array('ttl' => 300,'delay' => -time()));
  92. $this->reg->unsubscribe($topic);
  93. } else {
  94. return $modx->error->failure($modx->lexicon('resource_err_save'));
  95. }
  96. return $return;
  97. }
  98. }
  99. return 'modResourceReloadProcessor';