elysia_cron.ctools.inc 7.6 KB

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