profiles.inc 9.0 KB

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