elysia_cron.ctools.inc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /*******************************************************************************
  3. * EXPORTABLES
  4. ******************************************************************************/
  5. // WARN Features button "Revert components" will reset also statistics
  6. function elysia_cron_get_ctools_defaults() {
  7. if (module_exists('ctools') && function_exists('ctools_include')) {
  8. ctools_include('export');
  9. if (function_exists('ctools_export_get_schema') && function_exists('_ctools_export_get_defaults') && ($schema = ctools_export_get_schema('elysia_cron'))) {
  10. $export = $schema['export'];
  11. return _ctools_export_get_defaults('elysia_cron', $export);
  12. }
  13. }
  14. return array();
  15. }
  16. /**
  17. * Ctools load callback
  18. * Ctools does not support override of PARTIAL record, this is an elysia cron specific replacement to support it
  19. */
  20. function elysia_cron_ctools_export_load($name) {
  21. $schema = ctools_export_get_schema('elysia_cron');
  22. if (!empty($schema)) {
  23. $export = $schema['export'];
  24. if (EC_DRUPAL_VERSION >= 7) {
  25. $object = db_query("select " . implode(", ", $GLOBALS['_ec_columns']) . " from {elysia_cron} where name = :name", array(':name' => $name))->fetch();
  26. }
  27. else {
  28. $object = db_fetch_object(db_query("select " . implode(", ", $GLOBALS['_ec_columns']) . " from {elysia_cron} where name = '%s'", $name));
  29. }
  30. $default_objects = _ctools_export_get_defaults('elysia_cron', $export);
  31. if ($object) {
  32. if (isset($default_objects[$name])) {
  33. return _elysia_cron_ctools_export_load_object_db_and_code($object, $default_objects[$name], $export);
  34. }
  35. else {
  36. return _elysia_cron_ctools_export_load_object_db($object, $export);
  37. }
  38. }
  39. elseif (isset($default_objects[$name])) {
  40. return _elysia_cron_ctools_export_load_object_code($default_objects[$name], $export);
  41. }
  42. }
  43. }
  44. /**
  45. * Ctools load all callback
  46. * Ctools does not support override of PARTIAL record, this is an elysia cron specific replacement to support it
  47. */
  48. function elysia_cron_ctools_export_load_all() {
  49. $schema = ctools_export_get_schema('elysia_cron');
  50. if (empty($schema)) {
  51. return array();
  52. }
  53. $export = $schema['export'];
  54. if (EC_DRUPAL_VERSION >= 7) {
  55. $objects = db_query("select " . implode(", ", $GLOBALS['_ec_columns']) . " from {elysia_cron}")->fetchAll();
  56. }
  57. else {
  58. $objects = array();
  59. $rs = db_query("select " . implode(", ", $GLOBALS['_ec_columns']) . " from {elysia_cron}");
  60. while ($o = db_fetch_object($rs)) {
  61. $objects[] = $o;
  62. }
  63. }
  64. $default_objects = _ctools_export_get_defaults('elysia_cron', $export);
  65. $result = array();
  66. foreach ($objects as $object) {
  67. $key = $object->{$export['key']};
  68. if (isset($default_objects[$key])) {
  69. $result[$key] = _elysia_cron_ctools_export_load_object_db_and_code($object, $default_objects[$key], $export);
  70. unset($default_objects[$key]);
  71. } else {
  72. $result[$key] = _elysia_cron_ctools_export_load_object_db($object, $export);
  73. }
  74. }
  75. foreach ($default_objects as $key => $object) {
  76. $result[$key] = _elysia_cron_ctools_export_load_object_code($object, $export);
  77. }
  78. return $result;
  79. }
  80. function _elysia_cron_ctools_export_load_object_db_and_code($object, $code_object, $export) {
  81. $overridden = false;
  82. foreach ($code_object as $keyd => $value) {
  83. if (!isset($object->$keyd) || is_null($object->$keyd)) {
  84. $object->$keyd = $value;
  85. }
  86. else if ($object->$keyd !== $value) {
  87. $overridden = true;
  88. }
  89. }
  90. $object->table = 'elysia_cron';
  91. $object->export_type = EXPORT_IN_DATABASE | EXPORT_IN_CODE;
  92. if (!empty($export['export type string'])) {
  93. $object->{$export['export type string']} = $overridden ? t('Overridden') : t('Normal');
  94. }
  95. return $object;
  96. }
  97. function _elysia_cron_ctools_export_load_object_db($object, $export) {
  98. $object->table = 'elysia_cron';
  99. $object->export_type = EXPORT_IN_DATABASE;
  100. if (!empty($export['export type string'])) {
  101. $object->{$export['export type string']} = t('Normal');
  102. }
  103. return $object;
  104. }
  105. function _elysia_cron_ctools_export_load_object_code($object, $export) {
  106. $object->table = 'elysia_cron';
  107. $object->export_type = EXPORT_IN_CODE;
  108. if (!empty($export['export type string'])) {
  109. $object->{$export['export type string']} = t('Default');
  110. }
  111. $object->in_code_only = TRUE;
  112. return $object;
  113. }
  114. /**
  115. * Ctools export object factory
  116. * Original ctools export factory (_ctools_export_unpack_object) does not handle NULL values correctly.
  117. * This function does not support $schema['join'].
  118. */
  119. function elysia_cron_ctools_export_object_factory($schema, $data) {
  120. $object = new stdClass;
  121. foreach ($schema['fields'] as $field => $info) {
  122. $object->$field = isset($data->$field) && !is_null($data->$field) ? (empty($info['serialize']) ? $data->$field : unserialize($data->$field)) : NULL;
  123. }
  124. return $object;
  125. }
  126. /**
  127. * Ctools export callback
  128. * Handles NULL value (it's not possible to do this with "field" export callback, because null values are rewritten before its call)
  129. */
  130. function elysia_cron_ctools_export_callback($object, $indent) {
  131. $table = 'elysia_cron';
  132. $schema = ctools_export_get_schema($table);
  133. $identifier = $schema['export']['identifier'];
  134. $output = $indent . '$' . $identifier . ' = new ' . get_class($object) . ";\n";
  135. if ($schema['export']['can disable']) {
  136. $output .= $indent . '$' . $identifier . '->disabled = FALSE; /* Edit this to true to make a default ' . $identifier . ' disabled initially */' . "\n";
  137. }
  138. if (!empty($schema['export']['api']['current_version'])) {
  139. $output .= $indent . '$' . $identifier . '->api_version = ' . $schema['export']['api']['current_version'] . ";\n";
  140. }
  141. $fields = $schema['fields'];
  142. foreach ($fields as $field => $info) {
  143. if (!empty($info['no export'])) {
  144. continue;
  145. }
  146. $value = isset($object->$field) ? $object->$field : (isset($info['default']) ? $info['default'] : NULL);
  147. if (!is_null($value) && $info['type'] == 'int') {
  148. $value = (isset($info['size']) && $info['size'] == 'tiny') ? (bool) $value : (int) $value;
  149. }
  150. $output .= $indent . '$' . $identifier . '->' . $field . ' = ' . ctools_var_export($value, $indent) . ";\n";
  151. }
  152. return $output;
  153. }
  154. /**
  155. * Ctools export to hook code callback
  156. * Original "to hook code" callback doesn't support the replacement of "load/load all" callback (it simply ignores them, even if defined and supported elsewhere)
  157. * This code is equal to the original ctools one, but uses specific load callback
  158. */
  159. function elysia_cron_ctools_to_hook_code($names, $name) {
  160. $table = 'elysia_cron';
  161. $schema = ctools_export_get_schema($table);
  162. $export = $schema['export'];
  163. $output = '';
  164. $objects = elysia_cron_ctools_export_load_all();
  165. $objects = array_intersect_key($objects, array_flip($names));
  166. if ($objects) {
  167. $output = "/**\n";
  168. $output .= " * Implementation of hook_{$export['default hook']}()\n";
  169. $output .= " */\n";
  170. $output .= "function " . $name . "_{$export['default hook']}() {\n";
  171. $output .= " \${$export['identifier']}s = array();\n\n";
  172. foreach ($objects as $object) {
  173. $output .= ctools_export_crud_export($table, $object, ' ');
  174. $output .= " \${$export['identifier']}s['" . check_plain($object->$export['key']) . "'] = \${$export['identifier']};\n\n";
  175. }
  176. $output .= " return \${$export['identifier']}s;\n";
  177. $output .= "}\n";
  178. }
  179. return $output;
  180. }