crud.inc 27 KB

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