profiles.inc 10 KB

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