profiles.inc 11 KB

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