metatag_context.admin.inc 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. /**
  3. * @file
  4. * Admin settings page for Metatag Context.
  5. */
  6. /**
  7. * Provides administration overview page for metatags by path settings.
  8. */
  9. function metatag_context_context_overview() {
  10. $contexts = context_enabled_contexts(TRUE);
  11. $header = array(t('Name'), t('Paths'), t('Operations'));
  12. $rows = array();
  13. $caption = t('Values assigned here inherit from the <a href="@url" title="Edit the global default meta tags.">global defaults</a> and will override any other meta tags assigned elsewhere.', array('@url' => url('admin/config/search/metatags/config/global')));
  14. foreach ($contexts as $name => $context) {
  15. // Only show context items that are specifically selected to be "Shown on
  16. // metatag admin page".
  17. if (isset($context->reactions['metatag_context_reaction']['metatag_admin']) && $context->reactions['metatag_context_reaction']['metatag_admin']) {
  18. $ops = array(
  19. l(t('Edit'), 'admin/config/search/metatags/context/' . $context->name, array('query' => array('destination' => 'admin/config/search/metatags/context'))),
  20. l(t('Delete'), 'admin/config/search/metatags/context/' . $context->name . '/delete', array('query' => array('destination' => 'admin/config/search/metatags/context'))),
  21. );
  22. $rows[] = array(
  23. $context->name,
  24. isset($context->conditions['path']) ? htmlspecialchars(implode(', ', $context->conditions['path']['values'])) : t('No path condition.'),
  25. implode(' | ', $ops),
  26. );
  27. }
  28. }
  29. return theme('table', array('header' => $header, 'rows' => $rows, 'caption' => $caption));
  30. }
  31. /**
  32. * FormAPI callback to build the 'config_add' form.
  33. */
  34. function metatag_context_config_add_form($form, &$form_state) {
  35. $form['name'] = array(
  36. '#title' => 'Name',
  37. '#type' => 'textfield',
  38. '#default_value' => '',
  39. '#description' => 'The unique ID for this metatag path context rule. This must contain only lower case letters, numbers and underscores.',
  40. '#required' => 1,
  41. '#maxlength' => 255,
  42. '#element_validate' => array('metatag_context_edit_name_validate'),
  43. );
  44. $form['actions']['#type'] = 'actions';
  45. $form['actions']['save'] = array(
  46. '#type' => 'submit',
  47. '#value' => t('Add and configure'),
  48. );
  49. $form['actions']['cancel'] = array(
  50. '#type' => 'link',
  51. '#title' => t('Cancel'),
  52. '#href' => isset($_GET['destination']) ? $_GET['destination'] : 'admin/config/search/metatags/context',
  53. );
  54. return $form;
  55. }
  56. /**
  57. * FormAPI callback to validate the 'edit_name' field.
  58. */
  59. function metatag_context_edit_name_validate($element, &$form_state) {
  60. // Check for string identifier sanity.
  61. if (!preg_match('!^[a-z0-9_-]+$!', $element['#value'])) {
  62. form_error($element, t('The name can only consist of lowercase letters, underscores, dashes, and numbers.'));
  63. return;
  64. }
  65. // Ensure the CTools exportables system is loaded.
  66. ctools_include('export');
  67. // Check for name collision.
  68. if ($exists = ctools_export_crud_load('context', $element['#value'])) {
  69. form_error($element, t('A context with this name already exists. Please choose another name or delete the existing item before creating a new one.'));
  70. }
  71. }
  72. /**
  73. * FormAPI callback to save the 'edit_name' form.
  74. */
  75. function metatag_context_config_add_form_submit($form, &$form_state) {
  76. $context = metatag_context_load_default_context();
  77. $context->name = $form_state['values']['name'];
  78. context_save($context);
  79. $form_state['redirect'] = 'admin/config/search/metatags/context/' . $context->name;
  80. }
  81. /**
  82. * FormAPI callback to build the edit form.
  83. */
  84. function metatag_context_config_edit_form($form, &$form_state, $context) {
  85. $form_state['metatag_context']['context'] = $context;
  86. // Empty form to start with.
  87. $form = array();
  88. // Don't care about the instance name, the data is being managed by Context
  89. // and not Metatag.
  90. $instance = "";
  91. $options = array();
  92. $metatags = $context->reactions['metatag_context_reaction']['metatags'];
  93. if (!isset($metatags[LANGUAGE_NONE])) {
  94. $metatags = array(
  95. LANGUAGE_NONE => $metatags,
  96. );
  97. }
  98. // Load the METATAG form.
  99. metatag_metatags_form($form, $instance, $metatags[LANGUAGE_NONE], $options);
  100. $form['paths'] = array(
  101. '#title' => 'Path',
  102. '#description' => t('Set this metatag context when any of the paths above match the page path. Put each path on a separate line. You can use the <code>*</code> character (asterisk) as a wildcard and the <code>~</code> character (tilde) to exclude one or more paths. Use <code>&lt;front&gt;</code> for the site front page. Only local paths (e.g. "example/page") will work, do not use relative URLs ("/example/page") or absolute URLs ("http://example.com/example/page").'),
  103. '#type' => 'textarea',
  104. '#default_value' => isset($context->conditions['path']['values']) ? html_entity_decode(implode('&#13;&#10;', $context->conditions['path']['values'])) : '',
  105. '#required' => 1,
  106. '#weight' => -100,
  107. );
  108. // If other conditions are assigned, mention it.
  109. $conditions = array_keys($context->conditions);
  110. foreach ($conditions as $key => $condition) {
  111. if ($condition == 'path') {
  112. unset($conditions[$key]);
  113. }
  114. }
  115. if (!empty($conditions)) {
  116. $form['other_conditions'] = array(
  117. '#prefix' => '<p><em>',
  118. '#markup' => t('Other conditions have been assigned that must be controlled through the main Context settings page.'),
  119. '#suffix' => '</em></p>',
  120. '#weight' => -99.9,
  121. );
  122. }
  123. $form['help'] = array(
  124. '#prefix' => '<hr /><p><em>',
  125. '#markup' => t('Values assigned here inherit from the <a href="@url" title="Edit the global default meta tags.">global defaults</a> and will override any other meta tags assigned elsewhere.', array('@url' => url('admin/config/search/metatags/config/global'))),
  126. '#suffix' => '</em></p>',
  127. '#weight' => -99,
  128. );
  129. // Show all tokens.
  130. $form['metatags']['tokens']['#token_types'] = 'all';
  131. $form['metatags']['#type'] = 'container';
  132. unset($form['metatags']['#collapsed']);
  133. unset($form['metatags']['#collapsible']);
  134. $form['actions']['#type'] = 'actions';
  135. $form['actions']['save'] = array(
  136. '#type' => 'submit',
  137. '#value' => t('Save'),
  138. );
  139. $form['actions']['cancel'] = array(
  140. '#type' => 'link',
  141. '#title' => t('Cancel'),
  142. '#href' => isset($_GET['destination']) ? $_GET['destination'] : 'admin/config/search/metatags/context',
  143. );
  144. $form['#submit'][] = 'metatag_context_config_edit_form_submit';
  145. return $form;
  146. }
  147. /**
  148. * FormAPI callback for the final submission.
  149. */
  150. function metatag_context_config_edit_form_submit($form, &$form_state) {
  151. $context = $form_state['metatag_context']['context'];
  152. $context->reactions['metatag_context_reaction']['metatags'] = $form_state['values']['metatags'];
  153. $paths = explode("\n", str_replace("\r", "", $form_state['values']['paths']));
  154. $paths = array_combine($paths, $paths);
  155. $context->conditions['path']['values'] = $paths;
  156. context_save($context);
  157. $form_state['redirect'] = 'admin/config/search/metatags/context';
  158. }
  159. /**
  160. * FormAPI callback to build the 'delete' form.
  161. */
  162. function metatag_context_delete_form($form, &$form_state, $context) {
  163. $form_state['metatag_context']['context'] = $context;
  164. $form['delete'] = array(
  165. '#value' => 'This action will permanently remove this item from your database.'
  166. );
  167. $form['actions']['#type'] = 'actions';
  168. $form['actions']['save'] = array(
  169. '#type' => 'submit',
  170. '#value' => t('Delete'),
  171. );
  172. $form['actions']['cancel'] = array(
  173. '#type' => 'link',
  174. '#title' => t('Cancel'),
  175. '#href' => isset($_GET['destination']) ? $_GET['destination'] : 'admin/config/search/metatags/context',
  176. );
  177. $form['#submit'][] = 'metatag_context_delete_form_submit';
  178. return $form;
  179. }
  180. /**
  181. * FormAPI submission callback for the 'delete' form.
  182. */
  183. function metatag_context_delete_form_submit($form, &$form_state) {
  184. context_delete($form_state['metatag_context']['context']);
  185. $form_state['redirect'] = 'admin/config/search/metatags/context';
  186. }
  187. /**
  188. * Create a default Metatag-focused context.
  189. *
  190. * @return object
  191. * A context structure in the form of a StdClass object.
  192. */
  193. function metatag_context_load_default_context() {
  194. $context = new stdClass();
  195. $context->disabled = FALSE; /* Edit this to true to make a default context disabled initially */
  196. $context->api_version = 3;
  197. $context->name = 'default_metatag_context';
  198. $context->description = '';
  199. $context->tag = 'Metatag';
  200. $context->metatag = TRUE;
  201. $context->conditions = array(
  202. 'path' => array(
  203. 'values' => array(
  204. ),
  205. ),
  206. );
  207. $context->reactions = array(
  208. 'metatag_context_reaction' => array(
  209. 'metatags' => array(),
  210. 'metatag_admin' => 1,
  211. ),
  212. );
  213. $context->condition_mode = 0;
  214. $context->weight = 0;
  215. // Translatables
  216. // Included for use with string extractors like potx.
  217. t('Metatag');
  218. return $context;
  219. }