elysia_cron.ctools.inc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. /**
  3. * @file
  4. * Ctools integration.
  5. */
  6. define('ELYSIA_CRON_CTOOLS_LINEBREAK', "\n");
  7. /**
  8. * Get default cron jobs.
  9. *
  10. * @return array
  11. * Cron jobs.
  12. */
  13. function elysia_cron_get_ctools_defaults() {
  14. if (module_exists('ctools') && function_exists('ctools_include')) {
  15. ctools_include('export');
  16. if (function_exists('ctools_export_get_schema') && function_exists('_ctools_export_get_defaults') && ($schema = ctools_export_get_schema('elysia_cron'))) {
  17. $export = $schema['export'];
  18. return _ctools_export_get_defaults('elysia_cron', $export);
  19. }
  20. }
  21. return array();
  22. }
  23. /**
  24. * Ctools load callback.
  25. *
  26. * Ctools does not support override of PARTIAL record,
  27. * this is an elysia cron specific replacement to support it.
  28. *
  29. * @param string $name
  30. * Cron job name.
  31. *
  32. * @return object|null
  33. * Object to export or NULL if nothing found.
  34. */
  35. function elysia_cron_ctools_export_load($name) {
  36. $schema = ctools_export_get_schema('elysia_cron');
  37. if (!empty($schema)) {
  38. $export = $schema['export'];
  39. $object = db_select('elysia_cron', 'ec')
  40. ->fields('ec', _elysia_cron_columns())
  41. ->condition('name', $name)
  42. ->execute()
  43. ->fetch();
  44. $default_objects = _ctools_export_get_defaults('elysia_cron', $export);
  45. if ($object) {
  46. if (isset($default_objects[$name])) {
  47. return _elysia_cron_ctools_export_load_object_db_and_code($object, $default_objects[$name], $export);
  48. }
  49. else {
  50. return _elysia_cron_ctools_export_load_object_db($object, $export);
  51. }
  52. }
  53. elseif (isset($default_objects[$name])) {
  54. return _elysia_cron_ctools_export_load_object_code($default_objects[$name], $export);
  55. }
  56. }
  57. }
  58. /**
  59. * Ctools load all callback.
  60. *
  61. * Ctools does not support override of PARTIAL record,
  62. * this is an elysia cron specific replacement to support it.
  63. *
  64. * @return array
  65. * Array of object to export.
  66. */
  67. function elysia_cron_ctools_export_load_all() {
  68. $result = array();
  69. $schema = ctools_export_get_schema('elysia_cron');
  70. if (empty($schema)) {
  71. return $result;
  72. }
  73. $export = $schema['export'];
  74. $objects = db_select('elysia_cron', 'ec')
  75. ->fields('ec', _elysia_cron_columns())
  76. ->execute()
  77. ->fetchAll();
  78. $default_objects = _ctools_export_get_defaults('elysia_cron', $export);
  79. foreach ($objects as $object) {
  80. $key = $object->{$export['key']};
  81. if (isset($default_objects[$key])) {
  82. $result[$key] = _elysia_cron_ctools_export_load_object_db_and_code($object, $default_objects[$key], $export);
  83. unset($default_objects[$key]);
  84. }
  85. else {
  86. $result[$key] = _elysia_cron_ctools_export_load_object_db($object, $export);
  87. }
  88. }
  89. foreach ($default_objects as $key => $object) {
  90. $result[$key] = _elysia_cron_ctools_export_load_object_code($object, $export);
  91. }
  92. return $result;
  93. }
  94. /**
  95. * @param object $object
  96. * @param object $code_object
  97. * @param array $export
  98. *
  99. * @return object
  100. */
  101. function _elysia_cron_ctools_export_load_object_db_and_code($object, $code_object, array $export) {
  102. $overridden = FALSE;
  103. foreach ($code_object as $keyd => $value) {
  104. if (!isset($object->$keyd) || is_null($object->$keyd)) {
  105. $object->$keyd = $value;
  106. }
  107. else {
  108. if ($object->$keyd !== $value) {
  109. $overridden = TRUE;
  110. }
  111. }
  112. }
  113. $object->table = 'elysia_cron';
  114. $object->export_type = EXPORT_IN_DATABASE | EXPORT_IN_CODE;
  115. if (!empty($export['export type string'])) {
  116. $object->{$export['export type string']} = $overridden ? t('Overridden') : t('Normal');
  117. }
  118. return $object;
  119. }
  120. /**
  121. * @param object $object
  122. * @param array $export
  123. *
  124. * @return object
  125. */
  126. function _elysia_cron_ctools_export_load_object_db($object, $export) {
  127. $object->table = 'elysia_cron';
  128. $object->export_type = EXPORT_IN_DATABASE;
  129. if (!empty($export['export type string'])) {
  130. $object->{$export['export type string']} = t('Normal');
  131. }
  132. return $object;
  133. }
  134. /**
  135. * @param object $object
  136. * @param array $export
  137. *
  138. * @return object
  139. */
  140. function _elysia_cron_ctools_export_load_object_code($object, $export) {
  141. $object->table = 'elysia_cron';
  142. $object->export_type = EXPORT_IN_CODE;
  143. if (!empty($export['export type string'])) {
  144. $object->{$export['export type string']} = t('Default');
  145. }
  146. $object->in_code_only = TRUE;
  147. return $object;
  148. }
  149. /**
  150. * Ctools export object factory.
  151. *
  152. * Original ctools export factory (_ctools_export_unpack_object)
  153. * does not handle NULL values correctly.
  154. * This function does not support $schema['join'].
  155. *
  156. * @param array $schema
  157. * @param object $data
  158. *
  159. * @return object
  160. */
  161. function elysia_cron_ctools_export_object_factory(array $schema, $data) {
  162. $object = new stdClass();
  163. foreach ($schema['fields'] as $field => $info) {
  164. $object->$field = isset($data->$field) && !is_null($data->$field) ? (empty($info['serialize']) ? $data->$field : unserialize($data->$field)) : NULL;
  165. }
  166. return $object;
  167. }
  168. /**
  169. * Ctools export callback.
  170. *
  171. * Handles NULL value (it's not possible to do this with "field"
  172. * export callback, because null values are rewritten before its call).
  173. *
  174. * @param object $object
  175. * @param string $indent
  176. *
  177. * @return string
  178. */
  179. function elysia_cron_ctools_export_callback($object, $indent) {
  180. $table = 'elysia_cron';
  181. $schema = ctools_export_get_schema($table);
  182. $identifier = $schema['export']['identifier'];
  183. $output = $indent . '$' . $identifier . ' = new ' . get_class($object) . '();' . ELYSIA_CRON_CTOOLS_LINEBREAK;
  184. if ($schema['export']['can disable']) {
  185. $output .= $indent . '$' . $identifier . '->disabled = FALSE; /* Edit this to true to make a default ' . $identifier . ' disabled initially */' . ELYSIA_CRON_CTOOLS_LINEBREAK;
  186. }
  187. if (!empty($schema['export']['api']['current_version'])) {
  188. $output .= $indent . '$' . $identifier . '->api_version = ' . $schema['export']['api']['current_version'] . ';' . ELYSIA_CRON_CTOOLS_LINEBREAK;
  189. }
  190. $fields = $schema['fields'];
  191. foreach ($fields as $field => $info) {
  192. if (!empty($info['no export'])) {
  193. continue;
  194. }
  195. $value = isset($object->$field) ? $object->$field : (isset($info['default']) ? $info['default'] : NULL);
  196. if (!is_null($value) && $info['type'] == 'int') {
  197. $value = (isset($info['size']) && $info['size'] == 'tiny') ? (bool) $value : (int) $value;
  198. }
  199. $output .= $indent . '$' . $identifier . '->' . $field . ' = ' . ctools_var_export($value, $indent) . ';' . ELYSIA_CRON_CTOOLS_LINEBREAK;
  200. }
  201. return $output;
  202. }
  203. /**
  204. * Ctools export to hook code callback.
  205. *
  206. * Original "to hook code" callback does not support the replacement
  207. * of "load/load all" callback (it simply ignores them,
  208. * even if defined and supported elsewhere)
  209. * This code is equal to the original ctools one,
  210. * but uses specific load callback.
  211. *
  212. * @param array $names
  213. * @param string $name
  214. *
  215. * @return string
  216. */
  217. function elysia_cron_ctools_to_hook_code(array $names, $name) {
  218. $output = '';
  219. $table = 'elysia_cron';
  220. $schema = ctools_export_get_schema($table);
  221. $export = $schema['export'];
  222. $objects = elysia_cron_ctools_export_load_all();
  223. $objects = array_intersect_key($objects, array_flip($names));
  224. if ($objects) {
  225. $output = '/**' . ELYSIA_CRON_CTOOLS_LINEBREAK;
  226. $output .= " * Implementation of hook_{$export['default hook']}()" . ELYSIA_CRON_CTOOLS_LINEBREAK;
  227. $output .= ' */' . ELYSIA_CRON_CTOOLS_LINEBREAK;
  228. $output .= "function " . $name . "_{$export['default hook']}() {" . ELYSIA_CRON_CTOOLS_LINEBREAK;
  229. $output .= " \${$export['identifier']}s = array();" . ELYSIA_CRON_CTOOLS_LINEBREAK . ELYSIA_CRON_CTOOLS_LINEBREAK;
  230. foreach ($objects as $object) {
  231. $output .= ctools_export_crud_export($table, $object, ' ');
  232. $output .= " \${$export['identifier']}s['" . check_plain($object->{$export['key']}) . "'] = \${$export['identifier']};" . ELYSIA_CRON_CTOOLS_LINEBREAK . ELYSIA_CRON_CTOOLS_LINEBREAK;
  233. }
  234. $output .= " return \${$export['identifier']}s;" . ELYSIA_CRON_CTOOLS_LINEBREAK;
  235. $output .= '}' . ELYSIA_CRON_CTOOLS_LINEBREAK;
  236. }
  237. return $output;
  238. }