custom.inc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. <?php
  2. /**
  3. * @file
  4. * Custom content type.
  5. *
  6. * This content type is nothing more than a title and a body that is entered
  7. * by the user and run through standard filters. The information is stored
  8. * right in the config, so each custom content is unique.
  9. */
  10. /**
  11. * Plugins are described by creating a $plugin array which will be used
  12. * by the system that includes this file.
  13. */
  14. $plugin = array(
  15. 'title' => t('Custom content'),
  16. 'no title override' => TRUE,
  17. 'defaults' => array('admin_title' => '', 'title' => '', 'title_heading' => 'h2', 'body' => '', 'format' => filter_default_format(), 'substitute' => TRUE),
  18. 'js' => array('misc/autocomplete.js', 'misc/textarea.js', 'misc/collapse.js'),
  19. // Make sure the edit form is only used for some subtypes.
  20. 'edit form' => '',
  21. 'add form' => '',
  22. 'edit text' => t('Edit'),
  23. 'all contexts' => TRUE,
  24. );
  25. /**
  26. * Return the custom content types with the specified $subtype_id.
  27. */
  28. function ctools_custom_content_type_content_type($subtype_id) {
  29. if ($subtype_id == 'custom') {
  30. return _ctools_default_content_type_content_type();
  31. }
  32. elseif (module_exists('ctools_custom_content')) {
  33. ctools_include('export');
  34. $content = ctools_export_crud_load('ctools_custom_content', $subtype_id);
  35. if ($content) {
  36. return _ctools_custom_content_type_content_type($content);
  37. }
  38. }
  39. }
  40. /**
  41. * Return all custom content types available.
  42. */
  43. function ctools_custom_content_type_content_types() {
  44. $types = &drupal_static(__FUNCTION__);
  45. if (isset($types)) {
  46. return $types;
  47. }
  48. ctools_include('export');
  49. $types = array();
  50. $types['custom'] = _ctools_default_content_type_content_type();
  51. if (module_exists('ctools_custom_content')) {
  52. foreach (ctools_export_crud_load_all('ctools_custom_content') as $name => $content) {
  53. $types[$name] = _ctools_custom_content_type_content_type($content);
  54. }
  55. }
  56. return $types;
  57. }
  58. /**
  59. * Settings for the default custom content type.
  60. *
  61. * The default is the one that allows the user to actually create a type.
  62. */
  63. function _ctools_default_content_type_content_type() {
  64. $info = array(
  65. 'name' => 'custom',
  66. 'title' => t('New custom content'),
  67. 'top level' => TRUE,
  68. 'category' => t('Custom'),
  69. 'description' => t('Create a completely custom piece of HTML content.'),
  70. 'edit form' => 'ctools_custom_content_type_edit_form',
  71. 'all contexts' => TRUE,
  72. 'check editable' => 'ctools_custom_content_type_editable',
  73. );
  74. return $info;
  75. }
  76. /**
  77. * Return an info array for a specific custom content type.
  78. */
  79. function _ctools_custom_content_type_content_type($content) {
  80. $info = array(
  81. 'name' => $content->name,
  82. 'title' => check_plain($content->admin_title),
  83. 'description' => check_plain($content->admin_description),
  84. 'category' => $content->category ? check_plain($content->category) : t('Miscellaneous'),
  85. 'all contexts' => TRUE,
  86. 'icon' => 'icon_block_custom.png',
  87. // Store this here to make it easy to access.
  88. 'content' => $content,
  89. );
  90. return $info;
  91. }
  92. /**
  93. * Given a subtype and a $conf, return the actual settings to use.
  94. *
  95. * The actual settings may be stored directly in the pane or this may
  96. * be a pointer to re-usable content that may be in the database or in
  97. * an export. We have to determine from the subtype whether or not it
  98. * is local or shared custom content.
  99. */
  100. function ctools_custom_content_type_get_conf($subtype, $conf) {
  101. if ($subtype['name'] != 'custom') {
  102. $settings = $subtype['content']->settings;
  103. $settings['custom_type'] = 'fixed';
  104. $settings['content'] = $subtype['content'];
  105. }
  106. else {
  107. // This means they created it as custom content and then set it as
  108. // reusable. Since we're not allowed to change the subtype, we're
  109. // still stored as though we are local, but are pointing off to
  110. // non-local.
  111. if (!empty($conf['name']) && module_exists('ctools_custom_content')) {
  112. ctools_include('export');
  113. $content = ctools_export_crud_load('ctools_custom_content', $conf['name']);
  114. if ($content) {
  115. $settings = $content->settings;
  116. $settings['custom_type'] = 'fixed';
  117. $settings['content'] = $content;
  118. $settings['admin_title'] = $content->admin_title;
  119. }
  120. else {
  121. $content = ctools_export_crud_new('ctools_custom_content');
  122. $content->name = $conf['name'];
  123. $settings = array(
  124. 'admin_title' => t('Missing/deleted content'),
  125. 'title' => '',
  126. 'title_heading' => '',
  127. 'body' => '',
  128. 'format' => filter_default_format(),
  129. 'substitute' => TRUE,
  130. 'custom_type' => 'fixed',
  131. 'content' => $content,
  132. );
  133. }
  134. }
  135. // This means that it is created as custom and has not been set to
  136. // reusable.
  137. else {
  138. $settings = $conf;
  139. $settings['custom_type'] = 'local';
  140. }
  141. }
  142. // Correct for an error that came in because filter format changed.
  143. if (is_array($settings['body'])) {
  144. $settings['format'] = $settings['body']['format'];
  145. $settings['body'] = $settings['body']['value'];
  146. }
  147. return $settings;
  148. }
  149. function ctools_custom_content_type_editable($content_type, $subtype, $conf) {
  150. if ($subtype['name'] == 'custom' && !empty($conf['name'])) {
  151. return FALSE;
  152. }
  153. return TRUE;
  154. }
  155. /**
  156. * Output function for the 'custom' content type. Outputs a custom
  157. * based on the module and delta supplied in the configuration.
  158. */
  159. function ctools_custom_content_type_render($subtype, $conf, $args, $contexts) {
  160. $settings = ctools_custom_content_type_get_conf(ctools_custom_content_type_content_type($subtype), $conf);
  161. static $delta = 0;
  162. $block = new stdClass();
  163. $block->subtype = ++$delta;
  164. $block->title = filter_xss_admin($settings['title']);
  165. $block->title_heading = isset($settings['title_heading']) ? $settings['title_heading'] : 'h2';
  166. // Add keyword substitutions if we were configured to do so.
  167. $content = $settings['body'];
  168. if (!empty($contexts) && !empty($settings['substitute'])) {
  169. $content = ctools_context_keyword_substitute($content, array(), $contexts);
  170. }
  171. $block->content = check_markup($content, $settings['format']);
  172. if ($settings['custom_type'] == 'fixed' && user_access('administer custom content')) {
  173. $block->admin_links = array(
  174. array(
  175. 'title' => t('Configure content pane'),
  176. 'alt' => t("Configure this pane in administer >> structure >> custom content panes"),
  177. 'href' => 'admin/structure/ctools-content/list/' . $settings['content']->name . '/edit',
  178. 'query' => drupal_get_destination(),
  179. ),
  180. );
  181. }
  182. return $block;
  183. }
  184. /**
  185. * Callback to provide the administrative title of the custom content.
  186. */
  187. function ctools_custom_content_type_admin_title($subtype, $conf) {
  188. $settings = ctools_custom_content_type_get_conf(ctools_custom_content_type_content_type($subtype), $conf);
  189. $output = t('Custom');
  190. $title = !empty($settings['admin_title']) ? $settings['admin_title'] : $settings['title'];
  191. if ($title) {
  192. if ($settings['custom_type'] != 'fixed') {
  193. $output = t('Custom: @title', array('@title' => $title));
  194. }
  195. else {
  196. $output = $title;
  197. }
  198. }
  199. return $output;
  200. }
  201. /**
  202. * Callback to provide administrative info. In this case we'll render the
  203. * content as long as it's not PHP, which is too risky to render here.
  204. */
  205. function ctools_custom_content_type_admin_info($subtype, $conf) {
  206. $settings = ctools_custom_content_type_get_conf(ctools_custom_content_type_content_type($subtype), $conf);
  207. $block = new stdClass();
  208. $block->title = filter_xss_admin($settings['title']);
  209. // We don't want to render php output on preview here, because if something is
  210. // wrong the whole display will be borked. So we check to see if the php
  211. // evaluator filter is being used, and make a temporary change to the filter
  212. // so that we get the printed php, not the eval'ed php.
  213. $php_filter = FALSE;
  214. foreach (filter_list_format($settings['format']) as $filter) {
  215. if ($filter->module == 'php') {
  216. $php_filter = TRUE;
  217. break;
  218. }
  219. }
  220. // If a php filter is active, just print the source, but only if the current
  221. // user has access to the actual filter.
  222. if ($php_filter) {
  223. $filter = filter_format_load($settings['format']);
  224. if (!filter_access($filter)) {
  225. return NULL;
  226. }
  227. $block->content = '<pre>' . check_plain($settings['body']) . '</pre>';
  228. }
  229. else {
  230. // We also need to filter through XSS admin because <script> tags can
  231. // cause javascript which will interfere with our ajax.
  232. $block->content = filter_xss_admin(check_markup($settings['body'], $settings['format']));
  233. }
  234. return $block;
  235. }
  236. /**
  237. * Returns an edit form for the custom type.
  238. */
  239. function ctools_custom_content_type_edit_form($form, &$form_state) {
  240. $settings = ctools_custom_content_type_get_conf($form_state['subtype'], $form_state['conf']);
  241. $form_state['settings'] = $settings;
  242. if ($settings['custom_type'] == 'fixed') {
  243. return $form; // no form for this case.
  244. }
  245. $form['admin_title'] = array(
  246. '#type' => 'textfield',
  247. '#default_value' => isset($settings['admin_title']) ? $settings['admin_title'] : '',
  248. '#title' => t('Administrative title'),
  249. '#description' => t('This title will be used administratively to identify this pane. If blank, the regular title will be used.'),
  250. );
  251. // Copy over the title override settings for a title heading.
  252. $form['aligner_start'] = array(
  253. '#markup' => '<div class="option-text-aligner clearfix">',
  254. );
  255. $form['title'] = array(
  256. '#type' => 'textfield',
  257. '#default_value' => $settings['title'],
  258. '#title' => t('Title'),
  259. '#id' => 'override-title-textfield',
  260. );
  261. $form['title_heading'] = array(
  262. '#type' => 'select',
  263. '#default_value' => isset($settings['title_heading']) ? $settings['title_heading'] : 'h2',
  264. '#options' => array(
  265. 'h1' => t('h1'),
  266. 'h2' => t('h2'),
  267. 'h3' => t('h3'),
  268. 'h4' => t('h4'),
  269. 'h5' => t('h5'),
  270. 'h6' => t('h6'),
  271. 'div' => t('div'),
  272. 'span' => t('span'),
  273. ),
  274. '#id' => 'override-title-heading',
  275. );
  276. $form['aligner_stop'] = array(
  277. '#markup' => '</div>',
  278. );
  279. $form['body'] = array(
  280. '#type' => 'text_format',
  281. '#title' => t('Body'),
  282. '#default_value' => $settings['body'],
  283. '#format' => $settings['format'],
  284. );
  285. if (!empty($form_state['contexts'])) {
  286. // Set extended description if both CCK and Token modules are enabled, notifying of unlisted keywords
  287. if (module_exists('content') && module_exists('token')) {
  288. $description = t('If checked, context keywords will be substituted in this content. Note that CCK fields may be used as keywords using patterns like <em>%node:field_name-formatted</em>.');
  289. }
  290. elseif (!module_exists('token')) {
  291. $description = t('If checked, context keywords will be substituted in this content. More keywords will be available if you install the Token module, see http://drupal.org/project/token.');
  292. }
  293. else {
  294. $description = t('If checked, context keywords will be substituted in this content.');
  295. }
  296. $form['substitute'] = array(
  297. '#type' => 'checkbox',
  298. '#title' => t('Use context keywords'),
  299. '#description' => $description,
  300. '#default_value' => !empty($settings['substitute']),
  301. );
  302. $form['contexts'] = array(
  303. '#title' => t('Substitutions'),
  304. '#type' => 'fieldset',
  305. '#collapsible' => TRUE,
  306. '#collapsed' => TRUE,
  307. );
  308. $rows = array();
  309. foreach ($form_state['contexts'] as $context) {
  310. foreach (ctools_context_get_converters('%' . check_plain($context->keyword) . ':', $context) as $keyword => $title) {
  311. $rows[] = array(
  312. check_plain($keyword),
  313. t('@identifier: @title', array('@title' => $title, '@identifier' => $context->identifier)),
  314. );
  315. }
  316. }
  317. $header = array(t('Keyword'), t('Value'));
  318. $form['contexts']['context'] = array('#markup' => theme('table', array('header' => $header, 'rows' => $rows)));
  319. }
  320. if (!user_access('administer custom content') || !module_exists('ctools_custom_content')) {
  321. return $form;
  322. }
  323. // Make the other form items dependent upon it.
  324. ctools_include('dependent');
  325. ctools_add_js('dependent');
  326. $form['reusable'] = array(
  327. '#type' => 'checkbox',
  328. '#title' => t('Make this content reusable'),
  329. '#default_value' => FALSE,
  330. );
  331. $form['name'] = array(
  332. '#type' => 'textfield',
  333. '#title' => t('Machine name'),
  334. '#description' => t('The machine readable name of this content. It must be unique, and it must contain only alphanumeric characters and underscores. Once created, you will not be able to change this value!'),
  335. '#dependency' => array('edit-reusable' => array(1)),
  336. );
  337. $form['category'] = array(
  338. '#type' => 'textfield',
  339. '#title' => t('Category'),
  340. '#description' => t('What category this content should appear in. If left blank the category will be "Miscellaneous".'),
  341. '#dependency' => array('edit-reusable' => array(1)),
  342. );
  343. $form['admin_description'] = array(
  344. '#type' => 'textarea',
  345. '#title' => t('Administrative description'),
  346. '#description' => t('A description of what this content is, does or is for, for administrative use.'),
  347. '#dependency' => array('edit-reusable' => array(1)),
  348. );
  349. return $form;
  350. }
  351. function _ctools_custom_content_type_edit_save(&$content, $form_state) {
  352. // Apply updates to the content object.
  353. $content->category = $form_state['values']['category'];
  354. $content->admin_title = $form_state['values']['admin_title'];
  355. $content->admin_description = $form_state['values']['admin_description'];
  356. foreach (array_keys($form_state['plugin']['defaults']) as $key) {
  357. if (isset($form_state['values'][$key])) {
  358. $content->settings[$key] = $form_state['values'][$key];
  359. }
  360. }
  361. ctools_export_crud_save('ctools_custom_content', $content);
  362. }
  363. /**
  364. * The validate form to ensure the custom content data is okay.
  365. */
  366. function ctools_custom_content_type_edit_form_validate(&$form, &$form_state) {
  367. if ($form_state['settings']['custom_type'] != 'fixed' && !empty($form_state['values']['reusable'])) {
  368. if (empty($form_state['values']['name'])) {
  369. form_error($form['name'], t('Name is required.'));
  370. }
  371. // Check for string identifier sanity
  372. if (!preg_match('!^[a-z0-9_]+$!', $form_state['values']['name'])) {
  373. form_error($form['name'], t('The name can only consist of lowercase letters, underscores, and numbers.'));
  374. return;
  375. }
  376. if (!module_exists('ctools_custom_content')) {
  377. return;
  378. }
  379. // Check for name collision
  380. if ($form_state['values']['name'] == 'custom' || (ctools_export_crud_load('ctools_custom_content', $form_state['values']['name']))) {
  381. form_error($form['name'], t('Content with this name already exists. Please choose another name or delete the existing item before creating a new one.'));
  382. }
  383. }
  384. }
  385. /**
  386. * The submit form stores the data in $conf.
  387. */
  388. function ctools_custom_content_type_edit_form_submit($form, &$form_state) {
  389. // Because of changes in filter form, these two keys are out of position:
  390. $form_state['values']['format'] = $form_state['values']['body']['format'];
  391. $form_state['values']['body'] = $form_state['values']['body']['value'];
  392. if ($form_state['settings']['custom_type'] == 'fixed') {
  393. _ctools_custom_content_type_edit_save($form_state['settings']['content'], $form_state);
  394. }
  395. // If the 'reusable' checkbox was checked, we will create a new
  396. // custom content and give it the proper values.
  397. else if (!empty($form_state['values']['reusable'])) {
  398. $content = ctools_export_crud_new('ctools_custom_content');
  399. $content->name = $form_state['values']['name'];
  400. _ctools_custom_content_type_edit_save($content, $form_state);
  401. $form_state['conf']['name'] = $content->name;
  402. }
  403. else {
  404. // Otherwise, just save values into $conf normally.
  405. foreach (array_keys($form_state['plugin']['defaults']) as $key) {
  406. $form_state['conf'][$key] = isset($form_state['values'][$key]) ? $form_state['values'][$key] : $form_state['plugin']['defaults'][$key];
  407. }
  408. }
  409. }