profiles.inc 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. $form['file']['filename']['#description'] = t('You can use tokens in the file name.');
  79. $form['file']['token_help'] = array(
  80. '#title' => t('Replacement patterns'),
  81. '#type' => 'fieldset',
  82. '#collapsible' => TRUE,
  83. '#collapsed' => TRUE,
  84. );
  85. $form['file']['token_help']['help'] = array(
  86. '#theme' => 'token_tree',
  87. '#token_types' => array('current-date', 'site'),
  88. '#global_types' => FALSE,
  89. );
  90. $form['file']['append_timestamp'] = array(
  91. "#type" => "checkbox",
  92. "#title" => t("Append a timestamp."),
  93. "#default_value" => $profile->append_timestamp,
  94. );
  95. $form['file']['timestamp_format'] = array(
  96. "#type" => "textfield",
  97. "#title" => t("Timestamp format"),
  98. "#default_value" => $profile->timestamp_format,
  99. "#description" => t('Should be a PHP <a href="!url">date()</a> format string.', array('!url' => 'http://www.php.net/date')),
  100. );
  101. $form['advanced'] = array('#weight' => 10);
  102. $form = array_merge_recursive($form, backup_migrate_filters_settings_form($profile->filters, 'backup'));
  103. // Add the advanced fieldset if there are any fields in it.
  104. if ($form['advanced']) {
  105. $form['advanced']['#type'] = 'fieldset';
  106. $form['advanced']['#title'] = t('Advanced Options');
  107. $form['advanced']['#collapsed'] = true;
  108. $form['advanced']['#collapsible'] = true;
  109. }
  110. $form['#validate'][] = '_backup_migrate_ui_backup_settings_form_validate';
  111. $form['#submit'][] = '_backup_migrate_ui_backup_settings_form_submit';
  112. return $form;
  113. }
  114. /**
  115. * Validate the profile form.
  116. */
  117. function _backup_migrate_ui_backup_settings_form_validate($form, &$form_state) {
  118. backup_migrate_filters_settings_form_validate('backup', $form, $form_state);
  119. }
  120. /**
  121. * Submit the profile form.
  122. */
  123. function _backup_migrate_ui_backup_settings_form_submit($form, &$form_state) {
  124. backup_migrate_filters_settings_form_submit('backup', $form, $form_state);
  125. }
  126. /**
  127. * Get the default profile.
  128. */
  129. function _backup_migrate_profile_default_profile() {
  130. backup_migrate_include('files', 'filters');
  131. return array(
  132. 'source_id' => 'db',
  133. 'filename' => _backup_migrate_default_filename(),
  134. 'append_timestamp' => 1,
  135. 'timestamp_format' => 'Y-m-d\TH-i-s',
  136. 'filters' => backup_migrate_filters_settings_default('backup'),
  137. );
  138. }
  139. /**
  140. * Get the default profile saved by the user (or the module default if none exists).
  141. */
  142. function _backup_migrate_profile_saved_default_profile($profile_id = NULL) {
  143. $profile_id = $profile_id ? $profile_id : variable_get("backup_migrate_profile_id", 'default');
  144. $profile = NULL;
  145. if ($profile_id) {
  146. $profile = backup_migrate_get_profile($profile_id);
  147. }
  148. if (!$profile) {
  149. $profile = backup_migrate_get_profile('default');
  150. }
  151. return $profile;
  152. }
  153. /**
  154. * A profile class for crud operations.
  155. */
  156. class backup_migrate_profile extends backup_migrate_item {
  157. var $db_table = "backup_migrate_profiles";
  158. var $type_name = "profile";
  159. var $singular = 'profile';
  160. var $plural = 'profiles';
  161. /**
  162. * This function is not supposed to be called. It is just here to help the po extractor out.
  163. */
  164. function strings() {
  165. // Help the pot extractor find these strings.
  166. t('Profile');
  167. t('Profiles');
  168. t('profile');
  169. t('profiles');
  170. }
  171. /**
  172. * Get the default values for standard parameters.
  173. */
  174. function get_default_values() {
  175. return _backup_migrate_profile_default_profile() + array('name' => t("Untitled Profile"));
  176. }
  177. /**
  178. * Get a table of all items of this type.
  179. */
  180. function get_list() {
  181. drupal_add_css(drupal_get_path('module', 'backup_migrate') .'/backup_migrate.css');
  182. return parent::get_list();
  183. }
  184. /**
  185. * Get the columns needed to list the type.
  186. */
  187. function get_list_column_info() {
  188. $out = parent::get_list_column_info();
  189. $out = array(
  190. 'name' => array('title' => t('Name')),
  191. 'source_name' => array('title' => t('Source')),
  192. 'filename' => array('title' => t('Filename')),
  193. ) + $out;
  194. return $out;
  195. }
  196. /**
  197. * Get a row of data to be used in a list of items of this type.
  198. */
  199. function get_list_row() {
  200. $row = parent::get_list_row();
  201. if (empty($this->enabled)) {
  202. foreach ($row as $key => $field) {
  203. $row[$key] = array('data' => $field, 'class' => 'profile-list-disabled');
  204. }
  205. }
  206. return $row;
  207. }
  208. /**
  209. * Set the source of this setings profile. Takes either a source object or source id.
  210. */
  211. function set_source($source) {
  212. if (is_object($source)) {
  213. $this->source = $source;
  214. $this->source_id = $source->get_id();
  215. }
  216. else {
  217. $this->source_id = $source;
  218. unset($this->source);
  219. }
  220. }
  221. /**
  222. * Get the source of the profile.
  223. */
  224. function get_source() {
  225. backup_migrate_include('destinations');
  226. if (!empty($this->source_id) && (empty($this->source) || $this->source->destination_id !== $this->source_id)) {
  227. $this->source = backup_migrate_get_destination($this->source_id);
  228. }
  229. return empty($this->source) ? NULL : $this->source;
  230. }
  231. /**
  232. * Get the name of the source.
  233. */
  234. function get_source_name() {
  235. if ($source = $this->get_source()) {
  236. return $source->get_name();
  237. }
  238. return t("Missing");
  239. }
  240. /**
  241. * Get the destination of the profile.
  242. */
  243. function get_destination() {
  244. backup_migrate_include('destinations');
  245. if (!empty($this->destination_id) && (empty($this->destination) || $this->destination->destination_id !== $this->destination_id)) {
  246. $this->destination = backup_migrate_get_destination($this->destination_id);
  247. }
  248. return empty($this->destination) ? NULL : $this->destination;
  249. }
  250. /**
  251. * Get the name of the destination.
  252. */
  253. function get_destination_name() {
  254. if ($destination = $this->get_destination()) {
  255. return $destination->get_name();
  256. }
  257. return t("Missing");
  258. }
  259. /**
  260. * Get the edit form.
  261. */
  262. function edit_form() {
  263. $form = parent::edit_form();
  264. $form['name'] = array(
  265. "#type" => "textfield",
  266. "#title" => t("Profile Name"),
  267. '#required' => TRUE,
  268. "#default_value" => $this->get('name'),
  269. );
  270. $form += _backup_migrate_ui_backup_settings_form($this);
  271. return $form;
  272. }
  273. /**
  274. * Get the message to send to the user when confirming the deletion of the item.
  275. */
  276. function delete_confirm_message() {
  277. 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')));
  278. }
  279. }