cer.install 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. <?php
  2. /**
  3. * @file
  4. * Install file providing corresponding entity reference schema.
  5. */
  6. /**
  7. * Implements hook_schema().
  8. */
  9. function cer_schema() {
  10. $schema = array();
  11. // @see entity_exportable_schema_fields()
  12. $schema['cer_preset'] = array(
  13. 'fields' => array(
  14. 'pid' => array(
  15. 'type' => 'serial',
  16. 'not null' => TRUE,
  17. ),
  18. 'identifier' => array(
  19. 'type' => 'text',
  20. 'size' => 'tiny',
  21. 'not null' => TRUE,
  22. 'description' => "The preset's machine-readable export identifier.",
  23. ),
  24. 'status' => array(
  25. 'type' => 'int',
  26. 'size' => 'tiny',
  27. 'not null' => TRUE,
  28. 'default' => 0x01, // ENTITY_CUSTOM
  29. 'description' => "The preset's exportable status.",
  30. ),
  31. 'module' => array(
  32. 'type' => 'varchar',
  33. 'length' => 255,
  34. 'not null' => FALSE,
  35. 'default' => 'cer',
  36. 'description' => 'The module, if any, which exports this preset.',
  37. ),
  38. ),
  39. 'primary key' => array('pid'),
  40. );
  41. return $schema;
  42. }
  43. /**
  44. * Implements hook_install().
  45. */
  46. function cer_install() {
  47. field_info_cache_clear();
  48. $fields = _cer_field_definitions();
  49. array_walk($fields, 'field_create_field');
  50. $instances = _cer_field_instance_definitions();
  51. array_walk($instances, 'field_create_instance');
  52. if (module_exists('corresponding_node_references')) {
  53. _cer_hijack_cnr();
  54. }
  55. }
  56. /**
  57. * Contains the default field definitions for presets.
  58. */
  59. function _cer_field_definitions() {
  60. $field_bases = array();
  61. $field_bases[] = array(
  62. 'active' => 1,
  63. 'cardinality' => 1,
  64. 'deleted' => 0,
  65. 'entity_types' => array(),
  66. 'field_name' => 'cer_bidirectional',
  67. 'foreign keys' => array(),
  68. 'indexes' => array(
  69. 'value' => array(
  70. 0 => 'value',
  71. ),
  72. ),
  73. 'locked' => 0,
  74. 'module' => 'list',
  75. 'settings' => array(
  76. 'allowed_values' => array(
  77. 0 => 0,
  78. 1 => 1,
  79. ),
  80. 'allowed_values_function' => '',
  81. ),
  82. 'translatable' => 0,
  83. 'type' => 'list_boolean',
  84. );
  85. $field_bases[] = array(
  86. 'active' => 1,
  87. 'cardinality' => 1,
  88. 'deleted' => 0,
  89. 'entity_types' => array(),
  90. 'field_name' => 'cer_enabled',
  91. 'foreign keys' => array(),
  92. 'indexes' => array(
  93. 'value' => array(
  94. 0 => 'value',
  95. ),
  96. ),
  97. 'locked' => 1,
  98. 'module' => 'list',
  99. 'settings' => array(
  100. 'allowed_values' => array(
  101. 0 => 0,
  102. 1 => 1,
  103. ),
  104. 'allowed_values_function' => '',
  105. ),
  106. 'translatable' => 0,
  107. 'type' => 'list_boolean',
  108. );
  109. $field_bases[] = array(
  110. 'active' => 1,
  111. 'cardinality' => 1,
  112. 'deleted' => 0,
  113. 'entity_types' => array(),
  114. 'field_name' => 'cer_weight',
  115. 'foreign keys' => array(),
  116. 'indexes' => array(
  117. 'value' => array(
  118. 0 => 'value',
  119. ),
  120. ),
  121. 'locked' => 0,
  122. 'module' => 'list',
  123. 'settings' => array(
  124. 'allowed_values' => array(),
  125. 'allowed_values_function' => 'cer_weight_options',
  126. ),
  127. 'translatable' => 0,
  128. 'type' => 'list_integer',
  129. );
  130. $field_bases[] = array(
  131. 'active' => 1,
  132. 'cardinality' => 1,
  133. 'deleted' => 0,
  134. 'entity_types' => array(),
  135. 'field_name' => 'cer_left',
  136. 'foreign keys' => array(
  137. 'field_instance' => array(
  138. 'columns' => array(
  139. 'fiid' => 'id',
  140. ),
  141. 'table' => 'field_config_instance',
  142. ),
  143. ),
  144. 'indexes' => array(),
  145. 'locked' => 1,
  146. 'module' => 'field_object',
  147. 'settings' => array(),
  148. 'translatable' => 0,
  149. 'type' => 'field_object',
  150. );
  151. $field_bases[] = array(
  152. 'active' => 1,
  153. 'cardinality' => 1,
  154. 'deleted' => 0,
  155. 'entity_types' => array(),
  156. 'field_name' => 'cer_right',
  157. 'foreign keys' => array(
  158. 'field_instance' => array(
  159. 'columns' => array(
  160. 'fiid' => 'id',
  161. ),
  162. 'table' => 'field_config_instance',
  163. ),
  164. ),
  165. 'indexes' => array(),
  166. 'locked' => 1,
  167. 'module' => 'field_object',
  168. 'settings' => array(),
  169. 'translatable' => 0,
  170. 'type' => 'field_object',
  171. );
  172. return $field_bases;
  173. }
  174. /**
  175. * Contains the default field instance definitions for presets.
  176. */
  177. function _cer_field_instance_definitions() {
  178. $field_instances = array();
  179. $field_instances[] = array(
  180. 'bundle' => 'cer',
  181. 'default_value' => array(
  182. 0 => array(
  183. 'value' => 1,
  184. ),
  185. ),
  186. 'deleted' => 0,
  187. 'description' => '',
  188. 'display' => array(
  189. 'default' => array(
  190. 'label' => 'above',
  191. 'module' => 'list',
  192. 'settings' => array(),
  193. 'type' => 'list_default',
  194. 'weight' => 1,
  195. ),
  196. ),
  197. 'entity_type' => 'cer',
  198. 'field_name' => 'cer_bidirectional',
  199. 'label' => 'Bidirectional',
  200. 'required' => 0,
  201. 'settings' => array(),
  202. 'widget' => array(
  203. 'active' => 1,
  204. 'module' => 'options',
  205. 'settings' => array(
  206. 'display_label' => 1,
  207. ),
  208. 'type' => 'options_onoff',
  209. 'weight' => 3,
  210. ),
  211. );
  212. $field_instances[] = array(
  213. 'bundle' => 'cer',
  214. 'default_value' => array(
  215. 0 => array(
  216. 'value' => 1,
  217. ),
  218. ),
  219. 'deleted' => 0,
  220. 'description' => '',
  221. 'display' => array(
  222. 'default' => array(
  223. 'label' => 'above',
  224. 'module' => 'list',
  225. 'settings' => array(),
  226. 'type' => 'list_default',
  227. 'weight' => 0,
  228. ),
  229. ),
  230. 'entity_type' => 'cer',
  231. 'field_name' => 'cer_enabled',
  232. 'label' => 'Enabled',
  233. 'required' => 0,
  234. 'settings' => array(),
  235. 'widget' => array(
  236. 'active' => 1,
  237. 'module' => 'options',
  238. 'settings' => array(
  239. 'display_label' => 1,
  240. ),
  241. 'type' => 'options_onoff',
  242. 'weight' => 2,
  243. ),
  244. );
  245. $field_instances[] = array(
  246. 'bundle' => 'cer',
  247. 'default_value' => array(
  248. 0 => array(
  249. 'value' => 0,
  250. ),
  251. ),
  252. 'deleted' => 0,
  253. 'description' => '',
  254. 'display' => array(
  255. 'default' => array(
  256. 'label' => 'above',
  257. 'module' => 'list',
  258. 'settings' => array(),
  259. 'type' => 'list_default',
  260. 'weight' => 4,
  261. ),
  262. ),
  263. 'entity_type' => 'cer',
  264. 'field_name' => 'cer_weight',
  265. 'label' => 'Weight',
  266. 'required' => 1,
  267. 'settings' => array(),
  268. 'widget' => array(
  269. 'active' => 1,
  270. 'module' => 'options',
  271. 'settings' => array(),
  272. 'type' => 'options_select',
  273. 'weight' => 4,
  274. ),
  275. );
  276. $field_instances[] = array(
  277. 'bundle' => 'cer',
  278. 'default_value' => NULL,
  279. 'deleted' => 0,
  280. 'description' => '',
  281. 'display' => array(
  282. 'default' => array(
  283. 'label' => 'hidden',
  284. 'module' => NULL,
  285. 'settings' => array(),
  286. 'type' => 'field_object_default',
  287. 'weight' => 5,
  288. ),
  289. ),
  290. 'entity_type' => 'cer',
  291. 'field_name' => 'cer_left',
  292. 'label' => 'Left Field',
  293. 'required' => 1,
  294. 'settings' => array(
  295. 'function' => 'CerFieldChain::collectAll',
  296. ),
  297. 'widget' => array(
  298. 'active' => 1,
  299. 'module' => 'options',
  300. 'settings' => array(),
  301. 'type' => 'options_select',
  302. 'weight' => 0,
  303. ),
  304. );
  305. $field_instances[] = array(
  306. 'bundle' => 'cer',
  307. 'default_value' => NULL,
  308. 'deleted' => 0,
  309. 'description' => '',
  310. 'display' => array(
  311. 'default' => array(
  312. 'label' => 'hidden',
  313. 'module' => NULL,
  314. 'settings' => array(),
  315. 'type' => 'field_object_default',
  316. 'weight' => 6,
  317. ),
  318. ),
  319. 'entity_type' => 'cer',
  320. 'field_name' => 'cer_right',
  321. 'label' => 'Right Field',
  322. 'required' => 1,
  323. 'settings' => array(
  324. 'function' => 'CerFieldChain::collectAll',
  325. ),
  326. 'widget' => array(
  327. 'active' => 1,
  328. 'module' => 'options',
  329. 'settings' => array(),
  330. 'type' => 'options_select',
  331. 'weight' => 1,
  332. ),
  333. );
  334. return $field_instances;
  335. }
  336. /**
  337. * Implements hook_uninstall().
  338. */
  339. function cer_uninstall() {
  340. variable_del('cer_debug');
  341. // Delete fields created by cer_install()
  342. field_delete_field('cer_left');
  343. field_delete_field('cer_right');
  344. field_delete_field('cer_bidirectional');
  345. field_delete_field('cer_enabled');
  346. field_delete_field('cer_weight');
  347. }
  348. /**
  349. * Converts all CNR presets to CER presets, then disables CNR.
  350. */
  351. function _cer_hijack_cnr() {
  352. $cnr = db_query('SELECT * FROM {corresponding_node_references} WHERE 1')->fetchAll();
  353. foreach ($cnr as $index => $preset) {
  354. // Prepend 'node*' to each side of the preset. CNR, sensibly enough, assumes everything
  355. // is on a node, but CER needs this specified explicitly.
  356. $key = explode('*', $preset->node_types_content_fields);
  357. $cnr[$index]->entity_types_content_fields = "node*{$key[0]}*{$key[1]}*node*{$key[2]}*{$key[3]}";
  358. }
  359. array_walk($cnr, '_cer_rebuild_preset');
  360. module_disable(array('corresponding_node_references'));
  361. drupal_set_message(t('Rebuilt CNR presets.'));
  362. }
  363. /**
  364. * Rename table to shorten module name.
  365. */
  366. function cer_update_7001() {
  367. if (db_table_exists('corresponding_entity_references')) {
  368. db_rename_table('corresponding_entity_references', 'cer');
  369. }
  370. }
  371. /**
  372. * Creates the {cer_preset} table to store CerPreset entities, and rebuilds
  373. * legacy presets.
  374. */
  375. function cer_update_7005() {
  376. // Register new classes with the autoloader.
  377. registry_rebuild();
  378. // Create the table for preset entities.
  379. $tables = cer_schema();
  380. db_create_table('cer_preset', $tables['cer_preset']);
  381. // Install Field Reference, which powers the cer_left and cer_right fields.
  382. if (! module_exists('field_object')) {
  383. module_enable(array('field_object'));
  384. }
  385. // Create preset entities' fields.
  386. cer_install();
  387. // Convert legacy presets to entities, including 1.x, 2.x and the older
  388. // 3.x schemas.
  389. $old = db_select('cer')->fields('cer')->execute()->fetchAll();
  390. $old = array_merge($old, module_invoke_all('cer_default_presets'));
  391. array_walk($old, '_cer_rebuild_preset');
  392. // So long, legacy table...
  393. db_drop_table('cer');
  394. // Delete defunct status variable maintained by CTools.
  395. variable_del('default_cer');
  396. if (module_exists('corresponding_node_references')) {
  397. _cer_hijack_cnr();
  398. }
  399. }
  400. /**
  401. * Rebuilds a legacy CER or CNR preset as a CerPreset entity.
  402. */
  403. function _cer_rebuild_preset($old) {
  404. if (isset($old->entity_types_content_fields)) {
  405. $keys = explode('*', $old->entity_types_content_fields);
  406. $old->a = implode(':', array_slice($keys, 0, 3));
  407. $old->b = implode(':', array_slice($keys, 3));
  408. }
  409. if (! isset($old->bidirectional)) {
  410. $old->bidirectional = TRUE;
  411. }
  412. if (! isset($old->weight)) {
  413. $old->weight = 0;
  414. }
  415. // If the 'enabled' flag doesn't exist, we can presume that this preset
  416. // was exported in code generated by CTools, which means that it should
  417. // have a 'disabled' flag, which we can invert to get the status.
  418. if (! isset($old->enabled)) {
  419. $old->enabled = (! $old->disabled);
  420. }
  421. // Validate both field chains by trying to unpack them. If any field in
  422. // either chain isn't exposed by hook_cer_fields(), an exception will be
  423. // thrown and we can bail out with an error message.
  424. try {
  425. CerFieldChain::unpack($old->a);
  426. CerFieldChain::unpack($old->b);
  427. }
  428. catch (Exception $e) {
  429. $variables = array(
  430. '%key' => "{$old->a}*{$old->b}",
  431. );
  432. return drupal_set_message(t('Could not rebuild preset %key because it refers to invalid fields.', $variables), 'error');
  433. }
  434. $preset = new CerPreset();
  435. $preset->wrapper->cer_left->set($old->a);
  436. $preset->wrapper->cer_right->set($old->b);
  437. $preset->wrapper->cer_enabled->set($old->enabled);
  438. $preset->wrapper->cer_bidirectional->set($old->bidirectional);
  439. $preset->wrapper->cer_weight->set($old->weight);
  440. $preset->save();
  441. }