profiles.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. <?php
  2. /**
  3. * @file
  4. * All of the settings profiles handling code for Backup and Migrate.
  5. */
  6. require_once dirname(__FILE__) . '/crud.inc';
  7. /**
  8. * Implements hook_backup_migrate_profile_subtypes().
  9. *
  10. * Get the built in Backup and Migrate profile types.
  11. */
  12. function backup_migrate_backup_migrate_profile_subtypes() {
  13. $out = array(
  14. 'backup' => array(
  15. 'include' => 'profiles',
  16. 'type_name' => t('Backup Settings Profile'),
  17. 'class' => 'backup_migrate_profile',
  18. ),
  19. );
  20. return $out;
  21. }
  22. /**
  23. * Get all the available backup profiles.
  24. */
  25. function backup_migrate_get_profiles() {
  26. require_once dirname(__FILE__) . '/filters.inc';
  27. $profiles = &drupal_static('backup_migrate_get_profiles', NULL);
  28. // Get the list of profiles and cache them locally.
  29. if ($profiles === NULL) {
  30. $profiles = backup_migrate_crud_get_items('profile');
  31. }
  32. return $profiles;
  33. }
  34. /**
  35. * Implements hook_backup_migrate_profiles_alter().
  36. *
  37. * Add default settings for any plugins which didn't exist when the profile was saved.
  38. */
  39. function backup_migrate_backup_migrate_profiles_alter(&$profiles) {
  40. foreach ($profiles as $id => $profile) {
  41. // Set the default values for filter setting which don't exist in the profile.
  42. $profiles[$id]->filters = (array) @$profile->filters + (array) backup_migrate_filters_settings_default('backup');
  43. }
  44. }
  45. /**
  46. * Get the profile info for the profile with the given ID, or NULL if none exists.
  47. */
  48. function backup_migrate_get_profile($profile_id) {
  49. $profiles = backup_migrate_get_profiles();
  50. return @$profiles[$profile_id];
  51. }
  52. /**
  53. * Implements hook_backup_migrate_profiles().
  54. */
  55. function backup_migrate_backup_migrate_profiles() {
  56. $out = array();
  57. // Get the module default profile.
  58. $out['default'] = backup_migrate_crud_create_item('profile', array('name' => t("Default Settings"), 'machine_name' => 'default'));
  59. return $out;
  60. }
  61. /* Utilities */
  62. /**
  63. * Get the available profiles as an options array for a form item.
  64. */
  65. function _backup_migrate_get_profile_form_item_options() {
  66. $out = array();
  67. foreach ((array) backup_migrate_get_profiles() as $key => $profile) {
  68. $out[$key] = $profile->get('name');
  69. }
  70. return $out;
  71. }
  72. /**
  73. * Get a form to configure the profile.
  74. */
  75. function _backup_migrate_ui_backup_settings_form($profile) {
  76. require_once dirname(__FILE__) . '/destinations.inc';
  77. require_once dirname(__FILE__) . '/files.inc';
  78. require_once dirname(__FILE__) . '/filters.inc';
  79. drupal_add_js(array('backup_migrate' => array('checkboxLinkText' => t('View as checkboxes'))), array('type' => 'setting'));
  80. drupal_add_js(drupal_get_path('module', 'backup_migrate') . '/backup_migrate.js', array('type' => 'file', 'scope' => 'footer'));
  81. drupal_add_css(drupal_get_path('module', 'backup_migrate') . '/backup_migrate.css');
  82. $form = array();
  83. $form['file'] = array(
  84. "#type" => "fieldset",
  85. "#title" => t("Backup File"),
  86. "#collapsible" => TRUE,
  87. "#collapsed" => FALSE,
  88. "#tree" => FALSE,
  89. );
  90. $form['file']['filename'] = array(
  91. "#type" => "textfield",
  92. "#title" => t("Backup file name"),
  93. "#default_value" => $profile->filename,
  94. );
  95. if (module_exists('token')) {
  96. $form['file']['token_help'] = array(
  97. '#title' => t('Replacement patterns'),
  98. '#type' => 'fieldset',
  99. '#collapsible' => TRUE,
  100. '#collapsed' => TRUE,
  101. );
  102. $form['file']['token_help']['help'] = array(
  103. '#theme' => 'token_tree',
  104. '#token_types' => array('current-date', 'site'),
  105. '#global_types' => FALSE,
  106. );
  107. $form['file']['filename']['#description'] = t('You can use tokens in the file name.');
  108. }
  109. $form['file']['append_timestamp'] = array(
  110. "#type" => "radios",
  111. '#options' => array(
  112. 0 => t('Create separate backups if `Backup file name` already exists'),
  113. 2 => t('Overwrite the existing backup file'),
  114. 1 => t('Append the timestamp'),
  115. ),
  116. "#title" => t("Save mode"),
  117. "#default_value" => $profile->append_timestamp,
  118. );
  119. $form['file']['timestamp_format_wrapper'] = array(
  120. '#type' => 'backup_migrate_dependent',
  121. '#dependencies' => array(
  122. 'append_timestamp' => 1,
  123. ),
  124. );
  125. $form['file']['timestamp_format_wrapper']['timestamp_format'] = array(
  126. "#type" => "textfield",
  127. "#title" => t("Timestamp format"),
  128. "#default_value" => $profile->timestamp_format,
  129. "#description" => t('Should be a PHP <a href="!url">date()</a> format string.', array('!url' => 'http://www.php.net/date')),
  130. );
  131. $form['advanced'] = array('#weight' => 10);
  132. $form = array_merge_recursive($form, backup_migrate_filters_settings_form($profile->filters, 'backup'));
  133. // Add the advanced fieldset if there are any fields in it.
  134. if ($form['advanced']) {
  135. $form['advanced']['#type'] = 'fieldset';
  136. $form['advanced']['#title'] = t('Advanced Options');
  137. $form['advanced']['#collapsed'] = TRUE;
  138. $form['advanced']['#collapsible'] = TRUE;
  139. }
  140. $form['#validate'][] = '_backup_migrate_ui_backup_settings_form_validate';
  141. $form['#submit'][] = '_backup_migrate_ui_backup_settings_form_submit';
  142. return $form;
  143. }
  144. /**
  145. * Validate the profile form.
  146. */
  147. function _backup_migrate_ui_backup_settings_form_validate($form, &$form_state) {
  148. backup_migrate_filters_settings_form_validate('backup', $form, $form_state);
  149. }
  150. /**
  151. * Submit the profile form.
  152. */
  153. function _backup_migrate_ui_backup_settings_form_submit($form, &$form_state) {
  154. backup_migrate_filters_settings_form_submit('backup', $form, $form_state);
  155. }
  156. /**
  157. * Get the default profile.
  158. */
  159. function _backup_migrate_profile_default_profile() {
  160. require_once dirname(__FILE__) . '/files.inc';
  161. require_once dirname(__FILE__) . '/filters.inc';
  162. return array(
  163. 'source_id' => 'db',
  164. 'filename' => _backup_migrate_default_filename(),
  165. 'append_timestamp' => 1,
  166. 'timestamp_format' => 'Y-m-d\TH-i-s',
  167. 'filters' => backup_migrate_filters_settings_default('backup'),
  168. );
  169. }
  170. /**
  171. * Get the default profile saved by the user (or the module default if none exists).
  172. */
  173. function _backup_migrate_profile_saved_default_profile($profile_id = NULL) {
  174. $profile_id = $profile_id ? $profile_id : variable_get("backup_migrate_profile_id", 'default');
  175. $profile = NULL;
  176. if ($profile_id) {
  177. $profile = backup_migrate_get_profile($profile_id);
  178. }
  179. if (!$profile) {
  180. $profile = backup_migrate_get_profile('default');
  181. }
  182. return $profile;
  183. }
  184. /**
  185. * A profile class for crud operations.
  186. */
  187. class backup_migrate_profile extends backup_migrate_item {
  188. public $db_table = "backup_migrate_profiles";
  189. public $type_name = "profile";
  190. public $singular = 'settings profile';
  191. public $plural = 'settings profiles';
  192. public $title_plural = 'Settings Profiles';
  193. public $title_singular = 'Settings Profile';
  194. /**
  195. * Perform a shallow merge of the defaults and the parameters.
  196. *
  197. * This is needed because otherwise it will *combine* the nested arrays and
  198. * make it impossible to deselect database tables from the 'nodata' setting.
  199. *
  200. * @param array $params
  201. */
  202. public function __construct(array $params = array()) {
  203. $params = (array) $params;
  204. $defaults = (array) $this->get_default_values();
  205. foreach ($defaults as $key => $val) {
  206. if (!isset($params[$key])) {
  207. $params[$key] = $val;
  208. }
  209. }
  210. $this->from_array($params);
  211. }
  212. /**
  213. * This function is not supposed to be called. It is just here to help the po extractor out.
  214. */
  215. public function strings() {
  216. // Help the pot extractor find these strings.
  217. t('Settings Profile');
  218. t('Settings Profiles');
  219. t('settings profile');
  220. t('settings profiles');
  221. }
  222. /**
  223. * Get the default values for standard parameters.
  224. */
  225. public function get_default_values() {
  226. return _backup_migrate_profile_default_profile() + array('name' => t("Untitled Profile"));
  227. }
  228. /**
  229. * Get a table of all items of this type.
  230. */
  231. public function get_list() {
  232. drupal_add_css(drupal_get_path('module', 'backup_migrate') . '/backup_migrate.css');
  233. return parent::get_list();
  234. }
  235. /**
  236. * Get the columns needed to list the type.
  237. */
  238. public function get_list_column_info() {
  239. $out = parent::get_list_column_info();
  240. $out = array(
  241. 'name' => array('title' => t('Name')),
  242. 'source_name' => array('title' => t('Source')),
  243. 'filename' => array('title' => t('Filename')),
  244. ) + $out;
  245. return $out;
  246. }
  247. /**
  248. * Set the source of this setings profile. Takes either a source object or source id.
  249. */
  250. public function set_source($source) {
  251. if (is_object($source)) {
  252. $this->source = $source;
  253. $this->source_id = $source->get_id();
  254. }
  255. else {
  256. $this->source_id = $source;
  257. unset($this->source);
  258. }
  259. }
  260. /**
  261. * Get the source of the profile.
  262. */
  263. public function get_source() {
  264. require_once dirname(__FILE__) . '/locations.inc';
  265. if (!empty($this->source_id) && (empty($this->source) || $this->source->get_id() !== $this->source_id)) {
  266. $this->source = backup_migrate_get_source($this->source_id);
  267. }
  268. return empty($this->source) ? NULL : $this->source;
  269. }
  270. /**
  271. * Get the name of the source.
  272. */
  273. public function get_source_name() {
  274. if ($source = $this->get_source()) {
  275. return $source->get_name();
  276. }
  277. return t("Missing");
  278. }
  279. /**
  280. * Get the destination of the profile.
  281. */
  282. public function get_destination() {
  283. $destinations = (array) $this->get_destinations();
  284. return reset($destinations);
  285. }
  286. /**
  287. * Get the destination of the profile.
  288. */
  289. public function get_destinations() {
  290. require_once dirname(__FILE__) . '/destinations.inc';
  291. if (empty($this->destinations)) {
  292. $this->destinations = array();
  293. $ids = $weights = array();
  294. if (!empty($this->destination_id)) {
  295. foreach ((array) $this->destination_id as $destination_id) {
  296. if (!in_array($destination_id, $ids) && $destination = backup_migrate_get_destination($destination_id)) {
  297. $this->destinations[] = $destination;
  298. $weights[] = $destination->get('weight');
  299. $ids[] = $destination_id;
  300. }
  301. }
  302. }
  303. // Sort the destinations by weight.
  304. array_multisort($weights, SORT_NUMERIC, $this->destinations);
  305. }
  306. return $this->destinations;
  307. }
  308. /**
  309. * Get the name of the destination.
  310. */
  311. public function get_destination_name() {
  312. $out = array();
  313. foreach ($this->get_destinations() as $destination) {
  314. $out[] = $destination->get_name();
  315. }
  316. if ($out) {
  317. return implode(', ', $out);
  318. }
  319. return t("Missing");
  320. }
  321. /**
  322. * Get the source and destinations specified in the given settings profile.
  323. */
  324. public function get_all_locations() {
  325. $out = array();
  326. $out += $this->get('destinations');
  327. $out[] = $this->get('source');
  328. return $out;
  329. }
  330. /**
  331. * Get the edit form.
  332. */
  333. public function edit_form() {
  334. $form = parent::edit_form();
  335. $form['name'] = array(
  336. "#type" => "textfield",
  337. "#title" => t("Profile Name"),
  338. '#required' => TRUE,
  339. "#default_value" => $this->get('name'),
  340. );
  341. $form += _backup_migrate_ui_backup_settings_form($this);
  342. return $form;
  343. }
  344. /**
  345. * Get the message to send to the user when confirming the deletion of the item.
  346. */
  347. public function delete_confirm_message() {
  348. return t('Are you sure you want to delete the profile %name? Any schedules using this profile will be disabled.', array('%name' => $this->get('name')));
  349. }
  350. }