flag.export.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. <?php
  2. /**
  3. * @file
  4. * Import/Export functionality provided by Flag module.
  5. */
  6. /**
  7. * Export a flag to code.
  8. *
  9. * @param $flags
  10. * An array of flag objects, or flag name.
  11. * @param $module
  12. * Optional. The name of the module that will be created if exporting to use
  13. * in hook_flag_default_flags().
  14. */
  15. function flag_export_flags($flags = array(), $module = '', $indent = '') {
  16. module_load_include('inc', 'features', 'features.export'); // For features_var_export() (optional).
  17. $output = $indent . '$flags = array();' . "\n";
  18. foreach ($flags as $item) {
  19. if (is_object($item)) {
  20. $flag = $item;
  21. }
  22. else {
  23. // We got just the flag name, for example from the features
  24. // implementation.
  25. if (!($flag = flag_load($item, TRUE))) {
  26. continue;
  27. }
  28. }
  29. if (!$flag->is_compatible()) {
  30. drupal_set_message(t('Could not export flag %flag-name: Your flag was created by a different version of the Flag module than is now being used.', array('%flag-name' => $flag->name)), 'error');
  31. continue;
  32. }
  33. $flag->api_version = FLAG_API_VERSION;
  34. $new_flag = (array) $flag;
  35. if (!empty($module)) {
  36. // Even though Flag adds the module name itself later, we add the module
  37. // name here for reference by other modules (such as Features).
  38. $new_flag['module'] = $module;
  39. // Lock the flag name, as is normally desired by modules using
  40. // hook_flag_default_flags(), and needed by Features.
  41. $new_flag['locked'] = array('name');
  42. }
  43. // Allow other modules to change the exported flag.
  44. drupal_alter('flag_export', $new_flag);
  45. // Remove properties we don't export.
  46. $unset_properties = array(
  47. // Remove the flag ID.
  48. 'fid',
  49. // The name is emitted as the key for the array.
  50. 'name',
  51. // The entity info is just used as helper data.
  52. 'entity_info',
  53. // Remove roles.
  54. 'roles',
  55. // Remove errors.
  56. 'errors',
  57. );
  58. foreach ($unset_properties as $property) {
  59. unset($new_flag[$property]);
  60. }
  61. $output .= $indent . '// Exported flag: "' . check_plain($flag->get_title()) . '"' . ".\n";
  62. $output .= $indent . '$flags[\'' . $flag->name . '\'] = ' . (function_exists('features_var_export') ? features_var_export($new_flag, $indent) : var_export($new_flag, TRUE)) . ";\n";
  63. }
  64. $output .= $indent . 'return $flags;' . "\n";
  65. return $output;
  66. }
  67. /**
  68. * Form to import a flag.
  69. */
  70. function flag_import_form() {
  71. $form = array();
  72. $form['import'] = array(
  73. '#title' => t('Flag import code'),
  74. '#type' => 'textarea',
  75. '#default_value' => '',
  76. '#rows' => 15,
  77. '#required' => TRUE,
  78. '#description' => t('Paste the code from a <a href="@export-url">flag export</a> here to import it into you site. Flags imported with the same name will update existing flags. Flags with a new name will be created.', array('@export-url' => url(FLAG_ADMIN_PATH . '/export'))),
  79. );
  80. $form['submit'] = array(
  81. '#value' => t('Import'),
  82. '#type' => 'submit',
  83. );
  84. return $form;
  85. }
  86. /**
  87. * Validate handler; Import a flag.
  88. */
  89. function flag_import_form_validate($form, &$form_state) {
  90. $flags = array();
  91. ob_start();
  92. eval($form_state['values']['import']);
  93. ob_end_clean();
  94. if (!isset($flags) || !is_array($flags)) {
  95. form_set_error('import', t('A valid list of flags could not be found in the import code.'));
  96. return;
  97. }
  98. // Create the flag object.
  99. foreach ($flags as $flag_name => $flag_info) {
  100. // Backward compatibility: old exported flags have their names in $flag_info
  101. // instead, so we use the += operator to not overwrite it.
  102. $flag_info += array(
  103. 'name' => $flag_name,
  104. );
  105. $new_flag = flag_flag::factory_by_array($flag_info);
  106. // Give new flags with the same name a matching FID, which tells Flag to
  107. // update the existing flag, rather than creating a new one.
  108. if ($existing_flag = flag_get_flag($new_flag->name)) {
  109. $new_flag->fid = $existing_flag->fid;
  110. }
  111. if ($errors = $new_flag->validate()) {
  112. $message = t('The import of the %flag flag failed because the following errors were encountered during the import:', array('%flag' => $new_flag->name));
  113. $message_errors = array();
  114. foreach ($errors as $field => $field_errors) {
  115. foreach ($field_errors as $error) {
  116. $message_errors[] = $error['message'];
  117. }
  118. }
  119. form_set_error('import', $message . theme('item_list', array('items' => $message_errors)));
  120. }
  121. else {
  122. // Save the new flag for the submit handler.
  123. $form_state['flags'][] = $new_flag;
  124. }
  125. }
  126. }
  127. /**
  128. * Submit handler; Import a flag.
  129. */
  130. function flag_import_form_submit($form, &$form_state) {
  131. module_load_include('inc', 'flag', 'includes/flag.admin');
  132. // Build up values for the cache clear.
  133. $entity_types = array();
  134. $new = FALSE;
  135. foreach ($form_state['flags'] as $flag) {
  136. $flag->save();
  137. if (!empty($flag->status)) {
  138. $flag->enable();
  139. }
  140. if ($flag->is_new) {
  141. drupal_set_message(t('Flag @name has been imported.', array('@name' => $flag->name)));
  142. $new = TRUE;
  143. }
  144. else {
  145. drupal_set_message(t('Flag @name has been updated.', array('@name' => $flag->name)));
  146. }
  147. $entity_types[] = $flag->entity_type;
  148. }
  149. _flag_clear_cache($entity_types, $new);
  150. $form_state['redirect'] = FLAG_ADMIN_PATH;
  151. }
  152. /**
  153. * Export a flag and display it in a form.
  154. */
  155. function flag_export_form($form, &$form_state, $flag = NULL) {
  156. // If we were passed a flag, use it as the list of flags to export.
  157. if ($flag) {
  158. $flags = array($flag);
  159. }
  160. // Display a list of flags to export.
  161. if (!isset($flags)) {
  162. if (isset($form_state['values']['flags'])) {
  163. $flags = array();
  164. foreach ($form_state['values']['flags'] as $flag_name) {
  165. if ($flag_name && $flag = flag_get_flag($flag_name)) {
  166. $flags[] = $flag;
  167. }
  168. }
  169. }
  170. else {
  171. $form['flags'] = array(
  172. '#type' => 'checkboxes',
  173. '#title' => t('Flags to export'),
  174. '#options' => drupal_map_assoc(array_keys(flag_get_flags())),
  175. '#description' => t('Exporting your flags is useful for moving flags from one site to another, or when including your flag definitions in a module.'),
  176. );
  177. $form['submit'] = array(
  178. '#type' => 'submit',
  179. '#value' => t('Export'),
  180. );
  181. }
  182. }
  183. if (isset($flags)) {
  184. $code = flag_export_flags($flags);
  185. // Link to the Features page if module is present, otherwise link to the
  186. // Drupal project page.
  187. $features_link = module_exists('features') ? url('admin/build/features') : url('http://drupal.org/project/features');
  188. $form['export'] = array(
  189. '#type' => 'textarea',
  190. '#title' => t('Flag exports'),
  191. '#description' => t('Use the exported code to later <a href="@import-flag">import</a> it. Exports can be included in modules using <a href="http://drupal.org/node/305086#default-flags">hook_flag_default_flags()</a> or using the <a href="@features-url">Features</a> module.', array('@import-flag' => url(FLAG_ADMIN_PATH . '/import'), '@features-url' => $features_link)),
  192. '#value' => $code,
  193. '#rows' => 15,
  194. );
  195. }
  196. return $form;
  197. }
  198. /**
  199. * Submit handler; Rebuild the export form after the list of flags has been set.
  200. */
  201. function flag_export_form_submit($form, &$form_state) {
  202. $form_state['rebuild'] = TRUE;
  203. }
  204. /**
  205. * Page for displaying an upgrade message and export form for Flag 1.x flags.
  206. */
  207. function flag_update_page($flag) {
  208. if ($flag->is_compatible()) {
  209. drupal_set_message(t('The flag %name is already up-to-date with the latest Flag API and does not need upgrading.', array('%name' => $flag->name)));
  210. drupal_goto(FLAG_ADMIN_PATH);
  211. }
  212. drupal_set_message(t('The flag %name is currently using the Flag API version @version, which is not compatible with the current version of Flag. You can upgrade this flag by pasting the below code into <em>@module_flag_default_flags()</em> function in the @module.module file.', array('%name' => $flag->name, '@version' => $flag->api_version, '@module' => $flag->module)), 'warning');
  213. flag_update_export($flag);
  214. return drupal_get_form('flag_export_form', $flag);
  215. }
  216. /**
  217. * Update a flag before export.
  218. *
  219. * @param $flag
  220. * The flag object passed by reference.
  221. */
  222. function flag_update_export(&$flag) {
  223. // Set the API version to 1 by default: version 1 did not explicitly define
  224. // the API version.
  225. if (empty($flag->api_version)) {
  226. $flag->api_version = 1;
  227. }
  228. // Get all our update classes.
  229. // This is not terribly graceful, but the alternative is declaring our classes
  230. // explicitly, or registering them with the Drupal autoloader and then running
  231. // a database query, which seems a waste of space given we only ever need
  232. // these here.
  233. $classes = get_declared_classes();
  234. $update_handlers = array();
  235. foreach ($classes as $class) {
  236. // Any class whose name is of the form 'FlagUpdate_foo' is one of ours, we
  237. // assume. Should this prove problematic, we can add use of reflection here.
  238. if (substr($class, 0, 11) == 'FlagUpdate_') {
  239. // @todo: change this to work with the static class when we drop support
  240. // for PHP 5.2: see commit d5b517.
  241. $update_handler = new $class;
  242. // Cast to string, as decimals as array keys seem to be rounded down to
  243. // ints, WTF PHP?
  244. $version = (string) $update_handler->old_api_version;
  245. $update_handlers[$version] = $update_handler;
  246. }
  247. }
  248. // Sort the classes by old version number.
  249. uksort($update_handlers, 'version_compare');
  250. // Work through each update handler.
  251. foreach ($update_handlers as $old_api_version => $update_handler) {
  252. // Skip update classes that are older than our current flag.
  253. if (version_compare($old_api_version, $flag->api_version, '<')) {
  254. continue;
  255. }
  256. // Run the update and change the API version on the flag.
  257. $update_handler->update($flag);
  258. $flag->api_version = $update_handler->new_api_version;
  259. }
  260. }
  261. /**
  262. * Flag update class for API 1 flags -> API 2.
  263. *
  264. * The class name after the prefix is immaterial, though we follow the Drupal
  265. * system update convention whereby the number here is what we update to.
  266. */
  267. class FlagUpdate_2 {
  268. /**
  269. * The API version this class updates a flag from.
  270. *
  271. * @todo: Change this to a class constant when we drop support for PHP 5.2.
  272. */
  273. public $old_api_version = 1;
  274. /**
  275. * The API version this class updates a flag to.
  276. */
  277. public $new_api_version = 2;
  278. /**
  279. * The update function for the flag.
  280. */
  281. static function update(&$flag) {
  282. if (isset($flag->roles) && !isset($flag->roles['flag'])) {
  283. $flag->roles = array(
  284. 'flag' => $flag->roles,
  285. 'unflag' => $flag->roles,
  286. );
  287. }
  288. }
  289. }
  290. /**
  291. * Flag update class for API 2 flags -> API 3.
  292. */
  293. class FlagUpdate_3 {
  294. public $old_api_version = 2;
  295. public $new_api_version = 3;
  296. static function update(&$flag) {
  297. // Change the content_type property to entity_type.
  298. if (isset($flag->content_type)) {
  299. $flag->entity_type = $flag->content_type;
  300. unset($flag->content_type);
  301. }
  302. // We can't convert the flag roles data to user permissions at this point
  303. // because the flag is disabled and hence hook_permission() doesn't see it
  304. // to define its permissions.
  305. // Instead, we copy it to import_roles, which the flag add form will handle
  306. // on new flags (which this flag will behave as when it is re-enabled).
  307. // @see flag_form()
  308. if (isset($flag->roles)) {
  309. $flag->import_roles = $flag->roles;
  310. }
  311. // Update show_on_teaser property to use new view mode settings.
  312. if (!empty($flag->show_on_teaser)) {
  313. $flag->show_in_links['teaser'] = TRUE;
  314. unset($flag->show_on_teaser);
  315. }
  316. // Update show_on_page property to use new view mode settings.
  317. if (!empty($flag->show_on_page)) {
  318. $flag->show_in_links['full'] = TRUE;
  319. unset($flag->show_on_page);
  320. }
  321. // Update show_on_comment and show_on_entity properties to use new view
  322. // mode settings. Since the old logic was to show on all view modes, do that.
  323. if (!empty($flag->show_on_entity) || !empty($flag->show_on_comment)) {
  324. if ($entity_info = entity_get_info($flag->entity_type)) {
  325. foreach ($entity_info['view modes'] as $view_mode => $value) {
  326. $flag->show_in_links[$view_mode] = TRUE;
  327. }
  328. }
  329. unset($flag->show_on_entity, $flag->show_on_comment);
  330. }
  331. }
  332. }