export-ui.inc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. <?php
  2. /**
  3. * @file
  4. * Provide a tool for creating UIs for exportable objects.
  5. *
  6. * See Advanced Help for documentation.
  7. */
  8. /**
  9. * Process an export-ui plugin to provide it with defaults.
  10. */
  11. function ctools_export_ui_process(&$plugin, $info) {
  12. ctools_include('export');
  13. $plugin += array(
  14. 'has menu' => TRUE,
  15. 'title' => $plugin['name'],
  16. 'export' => array(),
  17. 'allowed operations' => array(),
  18. 'menu' => array(),
  19. 'redirect' => array(),
  20. 'form' => array(),
  21. 'strings' => array(),
  22. 'list' => NULL,
  23. 'access' => 'administer site configuration',
  24. );
  25. // Provide CRUD access defaults based on the base 'access' setting:
  26. $plugin += array(
  27. 'create access' => $plugin['access'],
  28. 'delete access' => $plugin['access'],
  29. );
  30. if (empty($plugin['has menu'])) {
  31. return;
  32. }
  33. // The following keys are required and the plugin cannot be processed
  34. // without them.
  35. $keys = array(
  36. 'title singular',
  37. 'title plural',
  38. 'title singular proper',
  39. 'title plural proper',
  40. 'schema',
  41. );
  42. foreach ($keys as $key) {
  43. if (empty($plugin[$key])) {
  44. drupal_set_message(t('The plugin definition of @plugin is missing the %key key.', array('%key' => $key, '@plugin' => $plugin['name'])), 'error');
  45. }
  46. }
  47. // If we're on the modules page and building a menu, there is a design flaw
  48. // in Drupal core that causes modules to be installed but the schema does
  49. // not become available until AFTER menu rebuild. This helps smooth that
  50. // out. This is a HACK but it should work:
  51. $schema = ctools_export_get_schema($plugin['schema']);
  52. if (empty($schema)) {
  53. // If we're updating the schema may not have been read yet, so don't report this error in that case.
  54. if (!defined('MAINTENANCE_MODE')) {
  55. drupal_set_message(t('The plugin definition of @plugin cannot locate schema %schema.', array('%schema' => $plugin['schema'], '@plugin' => $plugin['name'])), 'error');
  56. }
  57. return;
  58. }
  59. if (empty($schema['export'])) {
  60. drupal_set_message(t('The plugin definition of @plugin uses %schema, but it has no export section.', array('%schema' => $plugin['schema'], '@plugin' => $plugin['name'])), 'error');
  61. return;
  62. }
  63. $plugin['export'] += $schema['export'];
  64. $plugin['export'] += array(
  65. // Add the identifier key from the schema so we don't have to call
  66. // ctools_export_get_schema() just for that.
  67. 'key' => $schema['export']['key'],
  68. );
  69. // Add some default fields that appear often in exports
  70. // If these use different keys they can easily be specified in the
  71. // $plugin.
  72. if (empty($plugin['export']['admin_title']) && !empty($schema['fields']['admin_title'])) {
  73. $plugin['export']['admin_title'] = 'admin_title';
  74. }
  75. if (empty($plugin['export']['admin_description']) && !empty($schema['fields']['admin_description'])) {
  76. $plugin['export']['admin_description'] = 'admin_description';
  77. }
  78. // Define allowed operations, and the name of the operations.
  79. $plugin['allowed operations'] += array(
  80. 'edit' => array('title' => t('Edit')),
  81. 'enable' => array('title' => t('Enable'), 'ajax' => TRUE, 'token' => TRUE),
  82. 'disable' => array('title' => t('Disable'), 'ajax' => TRUE, 'token' => TRUE),
  83. 'revert' => array('title' => t('Revert')),
  84. 'delete' => array('title' => t('Delete')),
  85. 'clone' => array('title' => t('Clone')),
  86. 'import' => array('title' => t('Import')),
  87. 'export' => array('title' => t('Export')),
  88. );
  89. $plugin['menu'] += array(
  90. 'menu item' => str_replace(' ', '-', $plugin['name']),
  91. 'menu prefix' => 'admin/structure',
  92. 'menu title' => $plugin['title'],
  93. 'menu description' => '',
  94. );
  95. $base_path = ctools_export_ui_plugin_base_path($plugin);
  96. $prefix_count = count(explode('/', $plugin['menu']['menu prefix']));
  97. $plugin['menu'] += array(
  98. // Default menu items that should be declared.
  99. 'items' => array(),
  100. );
  101. $plugin['menu']['items'] += array(
  102. 'list callback' => array(),
  103. 'list' => array(),
  104. 'add' => array(),
  105. 'edit callback' => array(),
  106. 'edit' => array(),
  107. );
  108. $plugin['menu']['items']['list callback'] += array(
  109. 'path' => '',
  110. // Menu items are translated by the menu system.
  111. // TODO: We need more flexibility in title. The title of the admin page
  112. // is not necessarily the title of the object, plus we need
  113. // plural, singular, proper, not proper, etc.
  114. 'title' => $plugin['menu']['menu title'],
  115. 'description' => $plugin['menu']['menu description'],
  116. 'page callback' => 'ctools_export_ui_switcher_page',
  117. 'page arguments' => array($plugin['name'], 'list'),
  118. 'access callback' => 'ctools_export_ui_task_access',
  119. 'access arguments' => array($plugin['name'], 'list'),
  120. 'type' => MENU_NORMAL_ITEM,
  121. );
  122. $plugin['menu']['items']['list'] += array(
  123. 'path' => 'list',
  124. 'title' => 'List',
  125. 'page callback' => 'ctools_export_ui_switcher_page',
  126. 'page arguments' => array($plugin['name'], 'list'),
  127. 'access callback' => 'ctools_export_ui_task_access',
  128. 'access arguments' => array($plugin['name'], 'list'),
  129. 'type' => MENU_DEFAULT_LOCAL_TASK,
  130. 'weight' => -10,
  131. );
  132. $plugin['menu']['items']['add'] += array(
  133. 'path' => 'add',
  134. 'title' => 'Add',
  135. 'page callback' => 'ctools_export_ui_switcher_page',
  136. 'page arguments' => array($plugin['name'], 'add'),
  137. 'access callback' => 'ctools_export_ui_task_access',
  138. 'access arguments' => array($plugin['name'], 'add'),
  139. 'type' => MENU_LOCAL_ACTION,
  140. );
  141. $plugin['menu']['items']['edit callback'] += array(
  142. 'path' => 'list/%ctools_export_ui',
  143. 'page callback' => 'ctools_export_ui_switcher_page',
  144. 'page arguments' => array($plugin['name'], 'edit', $prefix_count + 2),
  145. 'load arguments' => array($plugin['name']),
  146. 'access callback' => 'ctools_export_ui_task_access',
  147. 'access arguments' => array($plugin['name'], 'edit', $prefix_count + 2),
  148. 'type' => MENU_CALLBACK,
  149. );
  150. $plugin['menu']['items']['edit'] += array(
  151. 'path' => 'list/%ctools_export_ui/edit',
  152. 'title' => 'Edit',
  153. 'page callback' => 'ctools_export_ui_switcher_page',
  154. 'page arguments' => array($plugin['name'], 'edit', $prefix_count + 2),
  155. 'load arguments' => array($plugin['name']),
  156. 'access callback' => 'ctools_export_ui_task_access',
  157. 'access arguments' => array($plugin['name'], 'edit', $prefix_count + 2),
  158. 'type' => MENU_DEFAULT_LOCAL_TASK,
  159. 'weight' => -10,
  160. );
  161. if ($plugin['allowed operations']['import']) {
  162. $plugin['menu']['items'] += array('import' => array());
  163. $plugin['menu']['items']['import'] += array(
  164. 'path' => 'import',
  165. 'title' => 'Import',
  166. 'page callback' => 'ctools_export_ui_switcher_page',
  167. 'page arguments' => array($plugin['name'], 'import'),
  168. 'access callback' => 'ctools_export_ui_task_access',
  169. 'access arguments' => array($plugin['name'], 'import'),
  170. 'type' => MENU_LOCAL_ACTION,
  171. );
  172. }
  173. if ($plugin['allowed operations']['export']) {
  174. $plugin['menu']['items'] += array('export' => array());
  175. $plugin['menu']['items']['export'] += array(
  176. 'path' => 'list/%ctools_export_ui/export',
  177. 'title' => 'Export',
  178. 'page callback' => 'ctools_export_ui_switcher_page',
  179. 'page arguments' => array($plugin['name'], 'export', $prefix_count + 2),
  180. 'load arguments' => array($plugin['name']),
  181. 'access callback' => 'ctools_export_ui_task_access',
  182. 'access arguments' => array($plugin['name'], 'export', $prefix_count + 2),
  183. 'type' => MENU_LOCAL_TASK,
  184. );
  185. }
  186. if ($plugin['allowed operations']['revert']) {
  187. $plugin['menu']['items'] += array('revert' => array());
  188. $plugin['menu']['items']['revert'] += array(
  189. 'path' => 'list/%ctools_export_ui/revert',
  190. 'title' => 'Revert',
  191. 'page callback' => 'ctools_export_ui_switcher_page',
  192. // Note: Yes, 'delete' op is correct.
  193. 'page arguments' => array($plugin['name'], 'delete', $prefix_count + 2),
  194. 'load arguments' => array($plugin['name']),
  195. 'access callback' => 'ctools_export_ui_task_access',
  196. 'access arguments' => array($plugin['name'], 'revert', $prefix_count + 2),
  197. 'type' => MENU_CALLBACK,
  198. );
  199. }
  200. if ($plugin['allowed operations']['delete']) {
  201. $plugin['menu']['items'] += array('delete' => array());
  202. $plugin['menu']['items']['delete'] += array(
  203. 'path' => 'list/%ctools_export_ui/delete',
  204. 'title' => 'Delete',
  205. 'page callback' => 'ctools_export_ui_switcher_page',
  206. 'page arguments' => array($plugin['name'], 'delete', $prefix_count + 2),
  207. 'load arguments' => array($plugin['name']),
  208. 'access callback' => 'ctools_export_ui_task_access',
  209. 'access arguments' => array($plugin['name'], 'delete', $prefix_count + 2),
  210. 'type' => MENU_CALLBACK,
  211. );
  212. }
  213. if ($plugin['allowed operations']['clone']) {
  214. $plugin['menu']['items'] += array('clone' => array());
  215. $plugin['menu']['items']['clone'] += array(
  216. 'path' => 'list/%ctools_export_ui/clone',
  217. 'title' => 'Clone',
  218. 'page callback' => 'ctools_export_ui_switcher_page',
  219. 'page arguments' => array($plugin['name'], 'clone', $prefix_count + 2),
  220. 'load arguments' => array($plugin['name']),
  221. 'access callback' => 'ctools_export_ui_task_access',
  222. 'access arguments' => array($plugin['name'], 'clone', $prefix_count + 2),
  223. 'type' => MENU_CALLBACK,
  224. );
  225. }
  226. if ($plugin['allowed operations']['enable']) {
  227. $plugin['menu']['items'] += array('enable' => array());
  228. $plugin['menu']['items']['enable'] += array(
  229. 'path' => 'list/%ctools_export_ui/enable',
  230. 'title' => 'Enable',
  231. 'page callback' => 'ctools_export_ui_switcher_page',
  232. 'page arguments' => array($plugin['name'], 'enable', $prefix_count + 2),
  233. 'load arguments' => array($plugin['name']),
  234. 'access callback' => 'ctools_export_ui_task_access',
  235. 'access arguments' => array($plugin['name'], 'enable', $prefix_count + 2),
  236. 'type' => MENU_CALLBACK,
  237. );
  238. }
  239. if ($plugin['allowed operations']['disable']) {
  240. $plugin['menu']['items'] += array('disable' => array());
  241. $plugin['menu']['items']['disable'] += array(
  242. 'path' => 'list/%ctools_export_ui/disable',
  243. 'title' => 'Disable',
  244. 'page callback' => 'ctools_export_ui_switcher_page',
  245. 'page arguments' => array($plugin['name'], 'disable', $prefix_count + 2),
  246. 'load arguments' => array($plugin['name']),
  247. 'access callback' => 'ctools_export_ui_task_access',
  248. 'access arguments' => array($plugin['name'], 'disable', $prefix_count + 2),
  249. 'type' => MENU_CALLBACK,
  250. );
  251. }
  252. // Define some redirects that should happen after edit/add/clone/delete operations.
  253. $plugin['redirect'] += array(
  254. 'add' => $base_path,
  255. 'clone' => $base_path,
  256. 'edit' => $base_path,
  257. 'delete' => $base_path,
  258. 'revert' => $base_path,
  259. 'import' => $base_path,
  260. );
  261. // Define form elements.
  262. $plugin['form'] += array(
  263. 'settings' => function_exists($plugin['name'] . '_form') ? $plugin['name'] . '_form' : '',
  264. 'validate' => function_exists($plugin['name'] . '_form_validate') ? $plugin['name'] . '_form_validate' : '',
  265. 'submit' => function_exists($plugin['name'] . '_form_submit') ? $plugin['name'] . '_form_submit' : '',
  266. );
  267. // Define strings.
  268. // For all strings, %title may be filled in at a later time via str_replace
  269. // since we do not know the title now.
  270. $plugin['strings'] += array(
  271. 'title' => array(),
  272. 'confirmation' => array(),
  273. 'help' => array(),
  274. 'message' => array(),
  275. );
  276. // Strings used in drupal_set_title().
  277. $plugin['strings']['title'] += array(
  278. 'add' => t('Add a new @plugin', array('@plugin' => $plugin['title singular'])),
  279. // The "%title" will be replaced in ctools_export_ui_form(), as in this
  280. // stage we dont have the specific exportable object.
  281. 'edit' => t('Edit @plugin %title', array('@plugin' => $plugin['title singular'])),
  282. 'clone' => t('Clone @plugin %title', array('@plugin' => $plugin['title singular'])),
  283. 'import' => t('Import @plugin', array('@plugin' => $plugin['title singular'])),
  284. 'export' => t('Export @plugin %title', array('@plugin' => $plugin['title singular'])),
  285. );
  286. // Strings used in confirmation pages.
  287. $plugin['strings']['confirmation'] += array(
  288. 'revert' => array(),
  289. 'delete' => array(),
  290. 'add' => array(),
  291. 'edit' => array(),
  292. );
  293. $plugin['strings']['confirmation']['revert'] += array(
  294. 'question' => t('Are you sure you want to revert %title?'),
  295. 'information' => t('This action will permanently remove any customizations made to this item.'),
  296. 'success' => t('The item has been reverted.'),
  297. );
  298. $plugin['strings']['confirmation']['delete'] += array(
  299. 'question' => t('Are you sure you want to delete %title?'),
  300. 'information' => t('This action will permanently remove this item from your database..'),
  301. 'success' => t('The item has been deleted.'),
  302. );
  303. $plugin['strings']['confirmation']['add'] += array(
  304. 'success' => t('%title has been created.'),
  305. 'fail' => t('%title could not be created.'),
  306. );
  307. $plugin['strings']['confirmation']['edit'] += array(
  308. 'success' => t('%title has been updated.'),
  309. 'fail' => t('%title could not be updated.'),
  310. );
  311. // Strings used in $forms.
  312. $plugin['strings']['help'] += array(
  313. 'import' => t('You can import an exported definition by pasting the exported object code into the field below.'),
  314. );
  315. // Strings used in drupal_set_message().
  316. $plugin['strings']['message'] += array(
  317. 'enable' => t('@plugin %title was enabled.', array('@plugin' => $plugin['title singular proper'])),
  318. 'disable' => t('@plugin %title was disabled.', array('@plugin' => $plugin['title singular proper'])),
  319. 'no items' => t('There are no @titles to display.', array('@titles' => $plugin['title plural'])),
  320. );
  321. }
  322. /**
  323. * Get the class to handle creating a list of exportable items.
  324. *
  325. * If a plugin does not define a lister class at all, then the default
  326. * lister class will be used.
  327. *
  328. * @return
  329. * Either the lister class or FALSE if one could not be had.
  330. */
  331. function ctools_export_ui_get_handler($plugin) {
  332. $cache = &drupal_static(__FUNCTION__, array());
  333. if (empty($cache[$plugin['name']])) {
  334. // If a list class is not specified by the plugin, fall back to the
  335. // default ctools_export_ui plugin instead.
  336. if (empty($plugin['handler'])) {
  337. $default = ctools_get_export_ui('ctools_export_ui');
  338. $class = ctools_plugin_get_class($default, 'handler');
  339. }
  340. else {
  341. $class = ctools_plugin_get_class($plugin, 'handler');
  342. }
  343. if ($class) {
  344. $cache[$plugin['name']] = new $class();
  345. $cache[$plugin['name']]->init($plugin);
  346. }
  347. }
  348. return !empty($cache[$plugin['name']]) ? $cache[$plugin['name']] : FALSE;
  349. }
  350. /**
  351. * Get the base path from a plugin.
  352. *
  353. * @param $plugin
  354. * The plugin.
  355. *
  356. * @return
  357. * The menu path to the plugin's list.
  358. */
  359. function ctools_export_ui_plugin_base_path($plugin) {
  360. return $plugin['menu']['menu prefix'] . '/' . $plugin['menu']['menu item'];
  361. }
  362. /**
  363. * Get the path to a specific menu item from a plugin.
  364. *
  365. * @param $plugin
  366. * The plugin name.
  367. * @param $item_id
  368. * The id in the menu items from the plugin.
  369. * @param $export_key
  370. * The export key of the item being edited, if it exists.
  371. * @return
  372. * The menu path to the plugin's list.
  373. */
  374. function ctools_export_ui_plugin_menu_path($plugin, $item_id, $export_key = NULL) {
  375. $path = $plugin['menu']['items'][$item_id]['path'];
  376. if ($export_key) {
  377. $path = str_replace('%ctools_export_ui', $export_key, $path);
  378. }
  379. return ctools_export_ui_plugin_base_path($plugin) . '/' . $path;
  380. }
  381. /**
  382. * Helper function to include CTools plugins and get an export-ui exportable.
  383. *
  384. * @param $plugin_name
  385. * The plugin that should be laoded.
  386. */
  387. function ctools_get_export_ui($plugin_name) {
  388. ctools_include('plugins');
  389. return ctools_get_plugins('ctools', 'export_ui', $plugin_name);
  390. }
  391. /**
  392. * Helper function to include CTools plugins and get all export-ui exportables.
  393. */
  394. function ctools_get_export_uis() {
  395. ctools_include('plugins');
  396. return ctools_get_plugins('ctools', 'export_ui');
  397. }
  398. /**
  399. * Main page callback to manipulate exportables.
  400. *
  401. * This simply loads the object defined in the plugin and hands it off to
  402. * a method based upon the name of the operation in use. This can easily
  403. * be used to add more ops.
  404. */
  405. function ctools_export_ui_switcher_page($plugin_name, $op) {
  406. $args = func_get_args();
  407. $js = !empty($_REQUEST['js']);
  408. // Load the $plugin information
  409. $plugin = ctools_get_export_ui($plugin_name);
  410. $handler = ctools_export_ui_get_handler($plugin);
  411. if ($handler) {
  412. $method = $op . '_page';
  413. if (method_exists($handler, $method)) {
  414. // replace the first two arguments:
  415. $args[0] = $js;
  416. $args[1] = $_POST;
  417. return call_user_func_array(array($handler, $method), $args);
  418. }
  419. }
  420. else {
  421. return t('Configuration error. No handler found.');
  422. }
  423. }