crud.inc 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  1. <?php
  2. /**
  3. * @file
  4. * CRUD functions for backup and migrate types (schedules, profiles etc.).
  5. */
  6. define('BACKUP_MIGRATE_STORAGE_NONE', 0);
  7. define('BACKUP_MIGRATE_STORAGE_DB', 1);
  8. define('BACKUP_MIGRATE_STORAGE_OVERRIDEN', 2);
  9. /**
  10. * Return a list of CRUD types in the module.
  11. */
  12. function backup_migrate_crud_types() {
  13. $out = array(
  14. 'schedule' => array(
  15. 'class' => 'backup_migrate_schedule',
  16. 'include' => 'schedules',
  17. ),
  18. 'source' => array(
  19. 'class' => 'backup_migrate_source',
  20. 'include' => 'sources',
  21. ),
  22. 'destination' => array(
  23. 'class' => 'backup_migrate_destination',
  24. 'include' => 'destinations',
  25. ),
  26. 'profile' => array(
  27. 'class' => 'backup_migrate_profile',
  28. 'include' => 'profiles',
  29. ),
  30. );
  31. return $out;
  32. }
  33. /**
  34. * Get the info for a particular crud type.
  35. */
  36. function backup_migrate_crud_type_info($type) {
  37. $types = backup_migrate_crud_types();
  38. if (isset($types[$type])) {
  39. return $types[$type];
  40. }
  41. return NULL;
  42. }
  43. /**
  44. * Get a list of avaiable classes of each crud type.
  45. */
  46. function backup_migrate_crud_subtypes($type) {
  47. $out = array();
  48. if ($info = backup_migrate_crud_type_info($type)) {
  49. // Include the function that contains the type base.
  50. if (!empty($info['include'])) {
  51. backup_migrate_include($info['include']);
  52. }
  53. // Allow modules (including this one) to declare backup and migrate subtypes.
  54. // We don't use module_invoke_all so we can avoid the side-effects of array_merge_recursive.
  55. $out = array();
  56. foreach (module_implements('backup_migrate_' . $type . '_subtypes') as $module) {
  57. $function = $module . '_backup_migrate_' . $type . '_subtypes';
  58. $result = $function();
  59. if (isset($result) && is_array($result)) {
  60. foreach ($result as $key => $val) {
  61. $out[$key] = $val;
  62. }
  63. }
  64. }
  65. }
  66. return $out;
  67. }
  68. /**
  69. * Get the info for a particular crud subtype.
  70. */
  71. function backup_migrate_crud_subtype_info($type, $subtype) {
  72. $types = backup_migrate_crud_subtypes($type);
  73. if (isset($types[$subtype])) {
  74. return $types[$subtype];
  75. }
  76. return NULL;
  77. }
  78. /**
  79. * Get a generic object of the given type to be used for static-like functions.
  80. *
  81. * I'm not using actual static method calls since they don't work on variables prior to PHP 5.3.0
  82. */
  83. function backup_migrate_crud_type_load($type, $subtype = NULL) {
  84. $out = $info = NULL;
  85. if ($subtype) {
  86. $info = backup_migrate_crud_subtype_info($type, $subtype);
  87. }
  88. else {
  89. $info = backup_migrate_crud_type_info($type);
  90. }
  91. if (!empty($info)) {
  92. if (!empty($info['include'])) {
  93. backup_migrate_include($info['include']);
  94. }
  95. if (!empty($info['file'])) {
  96. include_once './'. (isset($info['path']) ? $info['path'] : '') . $info['file'];
  97. }
  98. if (class_exists($info['class'])) {
  99. $out = new $info['class'];
  100. $out = $out->create(array('subtype' => $subtype));
  101. }
  102. }
  103. return $out;
  104. }
  105. /**
  106. * Page callback to create a new item.
  107. */
  108. function backup_migrate_crud_create($type, $subtype = NULL) {
  109. if ($item = backup_migrate_crud_type_load($type, $subtype)) {
  110. return $item;
  111. }
  112. return NULL;
  113. }
  114. /**
  115. * Get the menu items handled by the CRUD code.
  116. */
  117. function backup_migrate_crud_menu() {
  118. $items = array();
  119. foreach (backup_migrate_crud_types() as $type => $info) {
  120. $item = backup_migrate_crud_type_load($type);
  121. $items += (array)$item->get_menu_items();
  122. foreach (backup_migrate_crud_subtypes($type) as $subtype => $info) {
  123. $subitem = backup_migrate_crud_type_load($type, $subtype);
  124. $items += (array)$subitem->get_menu_items();
  125. }
  126. }
  127. return $items;
  128. }
  129. /**
  130. * Page callback to create a new item.
  131. */
  132. function backup_migrate_crud_ui_create($type, $subtype = NULL) {
  133. if ($item = backup_migrate_crud_create($type, $subtype)) {
  134. return drupal_get_form('backup_migrate_crud_edit_form', $item);
  135. }
  136. return drupal_not_found();
  137. }
  138. /**
  139. * Page callback to list all items.
  140. */
  141. function backup_migrate_crud_ui_list($type) {
  142. $out = '';
  143. if ($type = backup_migrate_crud_type_load($type)) {
  144. $out = $type->get_list();
  145. }
  146. return $out;
  147. }
  148. /**
  149. * Page callback to list all items.
  150. */
  151. function backup_migrate_crud_ui_list_all() {
  152. $out = '';
  153. foreach (backup_migrate_crud_types() as $type => $info) {
  154. $type = backup_migrate_crud_type_load($type);
  155. $out[] = theme('backup_migrate_group', array('title' => t($type->title_plural), 'body' => $type->get_list()));
  156. }
  157. return implode('', $out);
  158. }
  159. /**
  160. * Page callback to edit an item.
  161. */
  162. function backup_migrate_crud_ui_edit($type, $item_id = NULL) {
  163. if ($type = backup_migrate_crud_type_load($type)) {
  164. if ($item_id && $item = $type->item($item_id)) {
  165. return drupal_get_form('backup_migrate_crud_edit_form', $item);
  166. }
  167. drupal_goto($type->get_settings_path());
  168. }
  169. }
  170. /**
  171. * Does a crud item with the given name exist.
  172. *
  173. * Callback for the 'machine_name' form type.
  174. */
  175. function backup_migrate_crud_item_exists($machine_name, $element, $form_state) {
  176. return $form_state['values']['item']->item_exists($machine_name);
  177. }
  178. /**
  179. * A form callback to edit an item.
  180. */
  181. function backup_migrate_crud_edit_form($form, $form_state, $item) {
  182. $form = $item->edit_form();
  183. $form['item'] = array(
  184. '#type' => 'value',
  185. '#value' => $item,
  186. );
  187. $form['#validate'][] = 'backup_migrate_crud_edit_form_validate';
  188. $form['#submit'][] = 'backup_migrate_crud_edit_form_submit';
  189. return $form;
  190. }
  191. /**
  192. * Validate the item edit form.
  193. */
  194. function backup_migrate_crud_edit_form_validate($form, &$form_state) {
  195. $item = $form_state['values']['item'];
  196. $item->edit_form_validate($form, $form_state);
  197. }
  198. /**
  199. * Submit the item edit form.
  200. */
  201. function backup_migrate_crud_edit_form_submit($form, &$form_state) {
  202. $item = $form_state['values']['item'];
  203. $item->edit_form_submit($form, $form_state);
  204. if (empty($form_state['redirect'])) {
  205. $form_state['redirect'] = $item->get_settings_path();
  206. }
  207. }
  208. /**
  209. * Page callback to delete an item.
  210. */
  211. function backup_migrate_crud_ui_delete($type, $item_id = NULL) {
  212. if ($type = backup_migrate_crud_type_load($type)) {
  213. if ($item_id && $item = $type->item($item_id)) {
  214. return drupal_get_form('backup_migrate_crud_delete_confirm_form', $item);
  215. }
  216. drupal_goto($type->get_settings_path());
  217. }
  218. }
  219. /**
  220. * Ask confirmation for deletion of a item.
  221. */
  222. function backup_migrate_crud_delete_confirm_form($form, &$form_state, $item) {
  223. $form['item'] = array(
  224. '#type' => 'value',
  225. '#value' => $item,
  226. );
  227. if ($item->storage == BACKUP_MIGRATE_STORAGE_OVERRIDEN) {
  228. $message = $item->revert_confirm_message();
  229. return confirm_form($form, t('Are you sure?'), $item->get_settings_path(), $message, t('Revert'), t('Cancel'));
  230. } else {
  231. $message = $item->delete_confirm_message();
  232. return confirm_form($form, t('Are you sure?'), $item->get_settings_path(), $message, t('Delete'), t('Cancel'));
  233. }
  234. }
  235. /**
  236. * Delete a item after confirmation.
  237. */
  238. function backup_migrate_crud_delete_confirm_form_submit($form, &$form_state) {
  239. if ($form_state['values']['confirm']) {
  240. $item = $form_state['values']['item'];
  241. $item->delete();
  242. }
  243. $form_state['redirect'] = $item->get_settings_path();
  244. }
  245. /**
  246. * Export an item.
  247. */
  248. function backup_migrate_crud_ui_export($type, $item_id = NULL) {
  249. if ($type = backup_migrate_crud_type_load($type)) {
  250. if ($item_id && $item = $type->item($item_id)) {
  251. return drupal_get_form('backup_migrate_crud_export_form', $item->export());
  252. }
  253. drupal_goto($type->get_settings_path());
  254. }
  255. }
  256. /**
  257. * Ask confirmation for deletion of a destination.
  258. */
  259. function backup_migrate_crud_export_form($form, &$form_state, $export) {
  260. $form['export'] = array(
  261. '#title' => t('Exported content'),
  262. '#type' => 'textarea',
  263. '#rows' => min(30, count(explode("\n", $export))),
  264. '#value' => $export,
  265. );
  266. return $form;
  267. }
  268. /**
  269. * Page callback to import an item.
  270. */
  271. function backup_migrate_crud_ui_import() {
  272. return drupal_get_form('backup_migrate_crud_import_form');
  273. }
  274. /**
  275. * Ask confirmation for deletion of a item.
  276. */
  277. function backup_migrate_crud_import_form($form, &$form_state) {
  278. $form['code'] = array(
  279. '#type' => 'textarea',
  280. '#title' => t('Paste Exported Code Here'),
  281. '#required' => TRUE,
  282. );
  283. $form['submit'] = array(
  284. '#type' => 'submit',
  285. '#value' => t('Import'),
  286. );
  287. return $form;
  288. }
  289. /**
  290. * Validate handler to import a view
  291. */
  292. function backup_migrate_crud_import_form_validate($form, &$form_state) {
  293. $item = backup_migrate_crud_create_from_import($form_state['values']['code']);
  294. if ($item) {
  295. $form_state['values']['item'] = $item;
  296. }
  297. else {
  298. form_set_error('code', t('Unable to import item.'));
  299. }
  300. }
  301. /**
  302. * import a item after confirmation.
  303. */
  304. function backup_migrate_crud_import_form_submit($form, &$form_state) {
  305. $item = $form_state['values']['item'];
  306. $item->save();
  307. _backup_migrate_message('Your !type was imported', array('!type' => t($item->singular)));
  308. $form_state['redirect'] = $item->get_settings_path();
  309. }
  310. /**
  311. * Create an object from the exported object.
  312. */
  313. function backup_migrate_crud_create_from_import($code) {
  314. $item = NULL;
  315. $code = 'return ' . $code . ';';
  316. ob_start();
  317. $values = eval($code);
  318. ob_end_clean();
  319. if ($values) {
  320. if (!empty($values['type_name']) && $type = backup_migrate_crud_type_load($values['type_name'])) {
  321. $item = $type->create($values);
  322. // Make sure the item's ID doesn't already exist.
  323. $item->unique_id();
  324. }
  325. }
  326. return $item;
  327. }
  328. /**
  329. * Get all items of the given type.
  330. */
  331. function backup_migrate_crud_get_items($type) {
  332. if ($type = backup_migrate_crud_type_load($type)) {
  333. return $type->all_items();
  334. }
  335. }
  336. /**
  337. * Get an item of the specified type.
  338. */
  339. function backup_migrate_crud_get_item($type, $id) {
  340. if ($type = backup_migrate_crud_type_load($type)) {
  341. return $type->item($id);
  342. }
  343. }
  344. /**
  345. * Create a new item of the given type.
  346. */
  347. function backup_migrate_crud_create_item($type, $params) {
  348. if ($type = backup_migrate_crud_type_load($type)) {
  349. return $type->create($params);
  350. }
  351. }
  352. /**
  353. * A base class for items which can be stored in the database, listed, edited, deleted etc.
  354. */
  355. class backup_migrate_item {
  356. var $show_in_list = TRUE;
  357. var $settings_path = '/settings/';
  358. var $db_table = '';
  359. var $type_name = '';
  360. var $storage = FALSE;
  361. var $default_values = array();
  362. var $singular = 'item';
  363. var $plural = 'items';
  364. var $title_plural = 'Items';
  365. var $title_singular = 'Item';
  366. /**
  367. * This function is not supposed to be called. It is just here to help the po extractor out.
  368. */
  369. function strings() {
  370. // Help the pot extractor find these strings.
  371. t('item');
  372. t('items');
  373. t('Items');
  374. t('Item');
  375. // Help the pot extractor find these strings.
  376. t('List !type');
  377. t('Create !type');
  378. t('Delete !type');
  379. t('Edit !type');
  380. t('Export !type');
  381. }
  382. /**
  383. * Constructor, set the basic info pulled from the db or generated programatically.
  384. */
  385. function __construct($params = array()) {
  386. $this->from_array($this->_merge_defaults((array)$params, (array)$this->get_default_values()));
  387. }
  388. /**
  389. * Merge parameters with the given defaults.
  390. *
  391. * Works like array_merge_recursive, but it doesn't turn scalar values into arrays.
  392. */
  393. function _merge_defaults($params, $defaults) {
  394. foreach ($defaults as $key => $val) {
  395. if (!isset($params[$key])) {
  396. $params[$key] = $val;
  397. }
  398. else if (is_array($params[$key])) {
  399. $params[$key] = $this->_merge_defaults($params[$key], $val);
  400. }
  401. }
  402. return $params;
  403. }
  404. /**
  405. * Get the default values for standard parameters.
  406. */
  407. function get_default_values() {
  408. return $this->default_values;
  409. }
  410. /**
  411. * Save the item to the database.
  412. */
  413. function save() {
  414. if (!$this->get_id()) {
  415. $this->unique_id();
  416. }
  417. $record = $this->to_array();
  418. drupal_write_record($this->db_table, $record, !empty($this->storage) ? $this->get_primary_key() : array());
  419. }
  420. /**
  421. * Delete the item from the database.
  422. */
  423. function delete() {
  424. $keys = (array)$this->get_machine_name_field();
  425. db_query('DELETE FROM {' . $this->db_table . '} WHERE ' . $keys[0] . ' = :id', array(':id' => $this->get_id()));
  426. }
  427. /**
  428. * Load an existing item from an array.
  429. */
  430. function from_array($params) {
  431. foreach ($params as $key => $value) {
  432. if (method_exists($this, 'set_'. $key)) {
  433. $this->{'set_'. $key}($value);
  434. }
  435. else {
  436. $this->{$key} = $value;
  437. }
  438. }
  439. }
  440. /**
  441. * Return as an array of values.
  442. */
  443. function to_array() {
  444. $out = array();
  445. // Return fields as specified in the schema.
  446. $schema = $this->get_schema();
  447. if (!empty($schema['fields']) && is_array($schema['fields'])) {
  448. foreach ($schema['fields'] as $field => $info) {
  449. $out[$field] = $this->get($field);
  450. }
  451. }
  452. return $out;
  453. }
  454. /**
  455. * Return as an exported array of values.
  456. */
  457. function export() {
  458. $out = $this->to_array();
  459. $out['type_name'] = $this->type_name;
  460. ob_start();
  461. var_export($out);
  462. $out = ob_get_contents();
  463. ob_end_clean();
  464. return $out;
  465. }
  466. /**
  467. * Load an existing item from an database (serialized) array.
  468. */
  469. function load_row($data) {
  470. $params = array();
  471. $schema = $this->get_schema();
  472. // Load fields as specified in the schema.
  473. foreach ($schema['fields'] as $field => $info) {
  474. $params[$field] = empty($info['serialize']) ? $data[$field] : unserialize($data[$field]);
  475. }
  476. $this->from_array($params);
  477. }
  478. /**
  479. * Decode a loaded db row (unserialize necessary fields).
  480. */
  481. function decode_db_row($data) {
  482. $params = array();
  483. $schema = $this->get_schema();
  484. // Load fields as specified in the schema.
  485. foreach ($schema['fields'] as $field => $info) {
  486. $params[$field] = empty($info['serialize']) ? $data[$field] : unserialize($data[$field]);
  487. }
  488. return $params;
  489. }
  490. /**
  491. * Return the fields which must be serialized before saving to the db.
  492. */
  493. function get_serialized_fields() {
  494. $out = array();
  495. $schema = $this->get_schema();
  496. foreach ($schema['fields'] as $field => $info) {
  497. if (!empty($info['serialize'])) {
  498. $out[] = $field;
  499. }
  500. }
  501. return $out;
  502. }
  503. /**
  504. * Get the primary key field title from the schema.
  505. */
  506. function get_primary_key() {
  507. $schema = $this->get_schema();
  508. return @$schema['primary key'];
  509. }
  510. /**
  511. * Get the machine name field name from the schema.
  512. */
  513. function get_machine_name_field() {
  514. $schema = $this->get_schema();
  515. if (isset($schema['export']['key'])) {
  516. return $schema['export']['key'];
  517. }
  518. return @$schema['primary key'];
  519. }
  520. /**
  521. * Get the schema for the item type.
  522. */
  523. function get_schema() {
  524. return drupal_get_schema($this->db_table);
  525. }
  526. /**
  527. * Get the primary id for this item (if any is set).
  528. *
  529. * We only handle single field keys since that's all we need.
  530. */
  531. function get_id() {
  532. $keys = (array)$this->get_machine_name_field();
  533. return !empty($keys[0]) && !empty($this->{$keys[0]}) ? (string)$this->{$keys[0]} : '';
  534. }
  535. /**
  536. * Set the primary id for this item (if any is set).
  537. */
  538. function set_id($id) {
  539. $keys = (array)$this->get_machine_name_field();
  540. if (!empty($keys[0])) {
  541. return $this->{$keys[0]} = $id;
  542. }
  543. return NULL;
  544. }
  545. /**
  546. * Return a random (very very likely unique) string id for a new item.
  547. */
  548. function generate_id() {
  549. $id = md5(uniqid(mt_rand(), true));
  550. // Find the shortest possible unique id from (min 4 chars).
  551. for ($i = 4; $i < 32; $i++) {
  552. $new_id = substr($id, 0, $i);
  553. if (!$this->item($new_id)) {
  554. return $new_id;
  555. }
  556. }
  557. // If we get here, then all 28 increasingly complex ids were already taken so we'll try again.
  558. // this could theoretially lead to an infinite loop, but the odds are incredibly low.
  559. return $this->generate_id();
  560. }
  561. /**
  562. * Make sure this item has a unique id. Should only be called for new items or the item will collide with itself.
  563. */
  564. function unique_id() {
  565. $id = $this->get_id();
  566. // Unset the autoincrement field so it can be regenerated.
  567. foreach ((array)$this->get_primary_key() as $key) {
  568. $this->{$key} = NULL;
  569. }
  570. // If the item doesn't have an ID or if it's id is already taken, generate random one.
  571. if (!$id || $this->item($id)) {
  572. $this->set_id($this->generate_id());
  573. }
  574. }
  575. /**
  576. * Get the name of the item.
  577. */
  578. function get_name() {
  579. return @$this->name;
  580. }
  581. /**
  582. * Get the member with the given key.
  583. */
  584. function get($key) {
  585. if (method_exists($this, 'get_'. $key)) {
  586. return $this->{'get_'. $key}();
  587. }
  588. return @$this->{$key};
  589. }
  590. /* UI Stuff */
  591. /**
  592. * Get the action links for a destination.
  593. */
  594. function get_action_links() {
  595. $out = array();
  596. $item_id = $this->get_id();
  597. $path = $this->get_settings_path();
  598. if (@$this->storage == BACKUP_MIGRATE_STORAGE_DB || @$this->storage == BACKUP_MIGRATE_STORAGE_OVERRIDEN) {
  599. $out['edit'] = l(t("edit"), $path . "/edit/$item_id");
  600. }
  601. else if (@$this->storage == BACKUP_MIGRATE_STORAGE_NONE) {
  602. $out['edit'] = l(t("override"), $path . "/edit/$item_id");
  603. }
  604. if (@$this->storage == BACKUP_MIGRATE_STORAGE_DB) {
  605. $out['delete'] = l(t("delete"), $path . "/delete/$item_id");
  606. }
  607. else if (@$this->storage == BACKUP_MIGRATE_STORAGE_OVERRIDEN) {
  608. $out['delete'] = l(t("revert"), $path . "/delete/$item_id");
  609. }
  610. $out['export'] = l(t("export"), $path . "/export/$item_id");
  611. return $out;
  612. }
  613. /**
  614. * Get a table of all items of this type.
  615. */
  616. function get_list() {
  617. $items = $this->all_items();
  618. $rows = array();
  619. foreach ((array)$items as $item) {
  620. if ($item->show_in_list()) {
  621. if ($row = $item->get_list_row()) {
  622. $rows[] = $row;
  623. }
  624. }
  625. }
  626. if (count($rows)) {
  627. $out = theme('table', array('header' => $this->get_list_header(), 'rows' => $rows));
  628. }
  629. else {
  630. $out = t('There are no !items to display.', array('!items' => $this->plural));
  631. }
  632. if (user_access('administer backup and migrate')) {
  633. $out .= ' '. l(t('Create a new !item', array('!item' => $this->singular)), $this->get_settings_path() .'/add');
  634. }
  635. return $out;
  636. }
  637. /**
  638. * Get the columns needed to list the type.
  639. */
  640. function show_in_list() {
  641. return $this->show_in_list;
  642. }
  643. /**
  644. * Get the columns needed to list the type.
  645. */
  646. function get_settings_path() {
  647. return BACKUP_MIGRATE_MENU_PATH . $this->settings_path . $this->type_name;
  648. }
  649. /**
  650. * Get the columns needed to list the type.
  651. */
  652. function get_list_column_info() {
  653. return array(
  654. 'actions' => array('title' => t('Operations'), 'html' => TRUE),
  655. );
  656. }
  657. /**
  658. * Get header for a lost of this type.
  659. */
  660. function get_list_header() {
  661. $out = array();
  662. foreach ($this->get_list_column_info() as $key => $col) {
  663. $out[] = $col['title'];
  664. }
  665. return $out;
  666. }
  667. /**
  668. * Get a row of data to be used in a list of items of this type.
  669. */
  670. function get_list_row() {
  671. $out = array();
  672. foreach ($this->get_list_column_info() as $key => $col) {
  673. $out[$key] = empty($col['html']) ? check_plain($this->get($key)) : $this->get($key);
  674. if (isset($col['class'])) {
  675. $out[$key] = array('data' => $out[$key], 'class' => $col['class']);
  676. }
  677. }
  678. return $out;
  679. }
  680. /**
  681. * Get the rendered action links for a destination.
  682. */
  683. function get_actions() {
  684. $links = $this->get_action_links();
  685. return implode(" &nbsp; ", $links);
  686. }
  687. /**
  688. * Get the edit form for the item.
  689. */
  690. function edit_form() {
  691. $form = array();
  692. $form['item'] = array(
  693. '#type' => 'value',
  694. '#value' => $this,
  695. );
  696. $name = $this->get('name');
  697. $form['name'] = array(
  698. "#type" => "textfield",
  699. "#title" => t("!type name", array('!type' => $this->title_singular)),
  700. "#default_value" => empty($name) ? t('Untitled !type', array('!type' => $this->title_singular)) : $name,
  701. "#required" => TRUE,
  702. );
  703. $form['id'] = array(
  704. '#type' => 'value',
  705. '#value' => $this->get_id(),
  706. );
  707. $form['machine_name'] = array(
  708. '#type' => 'machine_name',
  709. '#default_value' => $this->get_id(),
  710. '#maxlength' => 255,
  711. '#machine_name' => array(
  712. 'source' => array('name'),
  713. 'exists' => 'backup_migrate_crud_item_exists',
  714. ),
  715. );
  716. $form['actions'] = array('#prefix' => '<div class="container-inline">', '#suffix' => '</div>', '#weight' => 99);
  717. $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save !type', array('!type' => t($this->singular))));
  718. $form['actions']['cancel'] = array('#value' => l(t('Cancel'), $this->get_settings_path()));
  719. return $form;
  720. }
  721. /**
  722. * Validate the edit form for the item.
  723. */
  724. function edit_form_validate($form, &$form_state) {
  725. }
  726. /**
  727. * Submit the edit form for the item.
  728. */
  729. function edit_form_submit($form, &$form_state) {
  730. $this->from_array($form_state['values']);
  731. $this->save();
  732. _backup_migrate_message('Your !type was saved', array('!type' => t($this->singular)));
  733. }
  734. /**
  735. * Get the message to send to the user when confirming the deletion of the item.
  736. */
  737. function delete_confirm_message() {
  738. return t('Are you sure you want to delete the !type %name?', array('!type' => t($this->singular), '%name' => $this->get('name')));
  739. }
  740. /**
  741. * Get the message to send to the user when confirming the deletion of the item.
  742. */
  743. function revert_confirm_message() {
  744. return t('Are you sure you want to revert the !type %name back to the default settings?', array('!type' => t($this->singular), '%name' => $this->get('name')));
  745. }
  746. /* Static Functions */
  747. /**
  748. * Get the menu items for manipulating this type.
  749. */
  750. function get_menu_items() {
  751. $path = $this->get_settings_path();
  752. $type = $this->type_name;
  753. $items[$path] = array(
  754. 'title' => $this->title_plural,
  755. 'page callback' => 'backup_migrate_menu_callback',
  756. 'page arguments' => array('crud', 'backup_migrate_crud_ui_list', TRUE, $this->type_name),
  757. 'access arguments' => array('administer backup and migrate'),
  758. 'weight' => 2,
  759. 'type' => MENU_LOCAL_TASK,
  760. );
  761. $items[$path .'/list'] = array(
  762. 'title' => 'List !type',
  763. 'title arguments' => array('!type' => t($this->title_plural)),
  764. 'weight' => 1,
  765. 'type' => MENU_DEFAULT_LOCAL_TASK,
  766. );
  767. $items[$path .'/add'] = array(
  768. 'title' => 'Add !type',
  769. 'title arguments' => array('!type' => t($this->title_singular)),
  770. 'page callback' => 'backup_migrate_menu_callback',
  771. 'page arguments' => array('crud', 'backup_migrate_crud_ui_create', TRUE, $this->type_name),
  772. 'access arguments' => array('administer backup and migrate'),
  773. 'weight' => 2,
  774. 'type' => MENU_LOCAL_ACTION,
  775. );
  776. $items[$path .'/delete'] = array(
  777. 'title' => 'Delete !type',
  778. 'title arguments' => array('!type' => t($this->title_singular)),
  779. 'page callback' => 'backup_migrate_menu_callback',
  780. 'page arguments' => array('crud', 'backup_migrate_crud_ui_delete', TRUE, $this->type_name),
  781. 'access arguments' => array('administer backup and migrate'),
  782. 'type' => MENU_CALLBACK,
  783. );
  784. $items[$path .'/edit'] = array(
  785. 'title' => 'Edit !type',
  786. 'title arguments' => array('!type' => t($this->title_singular)),
  787. 'page callback' => 'backup_migrate_menu_callback',
  788. 'page arguments' => array('crud', 'backup_migrate_crud_ui_edit', TRUE, $this->type_name),
  789. 'access arguments' => array('administer backup and migrate'),
  790. 'type' => MENU_CALLBACK,
  791. );
  792. $items[$path .'/export'] = array(
  793. 'title' => 'Export !type',
  794. 'title arguments' => array('!type' => t($this->title_singular)),
  795. 'page callback' => 'backup_migrate_menu_callback',
  796. 'page arguments' => array('crud', 'backup_migrate_crud_ui_export', TRUE, $this->type_name),
  797. 'access arguments' => array('administer backup and migrate'),
  798. 'type' => MENU_CALLBACK,
  799. );
  800. return $items;
  801. }
  802. /**
  803. * Create a new items with the given input. Doesn't load the parameters, but could use them to determine what type to create.
  804. */
  805. function create($params = array()) {
  806. $type = get_class($this);
  807. return new $type($params);
  808. }
  809. /**
  810. * Get all of the given items.
  811. */
  812. function all_items() {
  813. static $cache = array();
  814. $items = array();
  815. // Get any items stored as a variable. This allows destinations to be defined in settings.php
  816. $defaults = (array)variable_get($this->db_table . '_defaults', array());
  817. foreach ($defaults as $info) {
  818. if (is_array($info) && $item = $this->create($info)) {
  819. $items[$item->get_id()] = $item;
  820. }
  821. }
  822. // Get the items from the db.
  823. $result = db_query("SELECT * FROM {{$this->db_table}}", array(), array('fetch' => PDO::FETCH_ASSOC));
  824. foreach ($result as $info) {
  825. $info = $this->decode_db_row($info);
  826. if ($item = $this->create($info)) {
  827. $item->storage = empty($items[$item->get_id()]) ? BACKUP_MIGRATE_STORAGE_DB : BACKUP_MIGRATE_STORAGE_OVERRIDEN;
  828. $items[$item->get_id()] = $item;
  829. }
  830. }
  831. // Allow other modules to declare destinations programatically.
  832. $default_items = module_invoke_all($this->db_table);
  833. // Get CTools exported versions.
  834. if (function_exists('ctools_include')) {
  835. ctools_include('export');
  836. $defaults = ctools_export_load_object($this->db_table);
  837. foreach ($defaults as $info) {
  838. $info = (array)$info;
  839. if (!empty($info) && $item = $this->create($info)) {
  840. $default_items[$item->get_id()] = $item;
  841. }
  842. }
  843. }
  844. // Get any items stored as a variable again to correctly mark overrides.
  845. $defaults = (array)variable_get($this->db_table . '_defaults', array());
  846. foreach ($defaults as $info) {
  847. if (is_array($info) && $item = $this->create($info)) {
  848. $default_items[] = $item;
  849. }
  850. }
  851. // Add the default items to the array or set the storage flag if they've already been overridden.
  852. foreach ($default_items as $item) {
  853. if (isset($items[$item->get_id()])) {
  854. $items[$item->get_id()]->storage = BACKUP_MIGRATE_STORAGE_OVERRIDEN;
  855. }
  856. else {
  857. $item->storage = BACKUP_MIGRATE_STORAGE_NONE;
  858. $items[$item->get_id()] = $item;
  859. }
  860. }
  861. // Allow other modules to alter the items. This should maybe be before the db override code above
  862. // but then the filters are not able to set defaults for missing values. Other modules should just
  863. // be careful not to overwrite the user's UI changes in an unexpected way.
  864. drupal_alter($this->db_table, $items);
  865. return $items;
  866. }
  867. /**
  868. * A particular item.
  869. */
  870. function item($item_id) {
  871. $items = $this->all_items();
  872. return !empty($items[$item_id]) ? $items[$item_id] : NULL;
  873. }
  874. /**
  875. * A particular item.
  876. */
  877. function item_exists($item_id) {
  878. $items = $this->all_items();
  879. return !empty($items[$item_id]);
  880. }
  881. }