metatag_context.admin.inc 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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('Edit', 'admin/config/search/metatags/context/' . $context->name, array('query' => array('destination' => 'admin/config/search/metatags/context'))),
  20. l('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. // Load the METATAG form.
  93. metatag_metatags_form($form, $instance, $context->reactions['metatag_context_reaction']['metatags'], $options);
  94. $form['paths'] = array(
  95. '#title' => 'Path',
  96. '#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").'),
  97. '#type' => 'textarea',
  98. '#default_value' => isset($context->conditions['path']['values']) ? html_entity_decode(implode('&#13;&#10;', $context->conditions['path']['values'])) : '',
  99. '#required' => 1,
  100. '#weight' => -100,
  101. );
  102. // If other conditions are assigned, mention it.
  103. $conditions = array_keys($context->conditions);
  104. foreach ($conditions as $key => $condition) {
  105. if ($condition == 'path') {
  106. unset($conditions[$key]);
  107. }
  108. }
  109. if (!empty($conditions)) {
  110. $form['other_conditions'] = array(
  111. '#prefix' => '<p><em>',
  112. '#markup' => t('Other conditions have been assigned that must be controlled through the main Context settings page.'),
  113. '#suffix' => '</em></p>',
  114. '#weight' => -99.9,
  115. );
  116. }
  117. $form['help'] = array(
  118. '#prefix' => '<hr /><p><em>',
  119. '#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'))),
  120. '#suffix' => '</em></p>',
  121. '#weight' => -99,
  122. );
  123. // Show all tokens.
  124. $form['metatags']['tokens']['#token_types'] = 'all';
  125. $form['metatags']['#type'] = 'container';
  126. unset($form['metatags']['#collapsed']);
  127. unset($form['metatags']['#collapsible']);
  128. $form['actions']['#type'] = 'actions';
  129. $form['actions']['save'] = array(
  130. '#type' => 'submit',
  131. '#value' => t('Save'),
  132. );
  133. $form['actions']['cancel'] = array(
  134. '#type' => 'link',
  135. '#title' => t('Cancel'),
  136. '#href' => isset($_GET['destination']) ? $_GET['destination'] : 'admin/config/search/metatags/context',
  137. );
  138. $form['#submit'][] = 'metatag_context_config_edit_form_submit';
  139. return $form;
  140. }
  141. /**
  142. * FormAPI callback for the final submission.
  143. */
  144. function metatag_context_config_edit_form_submit($form, &$form_state) {
  145. $context = $form_state['metatag_context']['context'];
  146. $context->reactions['metatag_context_reaction']['metatags'] = $form_state['values']['metatags'];
  147. $paths = explode("\n", str_replace("\r", "", $form_state['values']['paths']));
  148. $paths = array_combine($paths, $paths);
  149. $context->conditions['path']['values'] = $paths;
  150. context_save($context);
  151. $form_state['redirect'] = 'admin/config/search/metatags/context';
  152. }
  153. /**
  154. * FormAPI callback to build the 'delete' form.
  155. */
  156. function metatag_context_delete_form($form, &$form_state, $context) {
  157. $form_state['metatag_context']['context'] = $context;
  158. $form['delete'] = array(
  159. '#value' => 'This action will permanently remove this item from your database.'
  160. );
  161. $form['actions']['#type'] = 'actions';
  162. $form['actions']['save'] = array(
  163. '#type' => 'submit',
  164. '#value' => t('Delete'),
  165. );
  166. $form['actions']['cancel'] = array(
  167. '#type' => 'link',
  168. '#title' => t('Cancel'),
  169. '#href' => isset($_GET['destination']) ? $_GET['destination'] : 'admin/config/search/metatags/context',
  170. );
  171. $form['#submit'][] = 'metatag_context_delete_form_submit';
  172. return $form;
  173. }
  174. /**
  175. * FormAPI submission callback for the 'delete' form.
  176. */
  177. function metatag_context_delete_form_submit($form, &$form_state) {
  178. context_delete($form_state['metatag_context']['context']);
  179. $form_state['redirect'] = 'admin/config/search/metatags/context';
  180. }
  181. /**
  182. * Create a default Metatag-focused context.
  183. *
  184. * @return object
  185. * A context structure in the form of a StdClass object.
  186. */
  187. function metatag_context_load_default_context() {
  188. $context = new stdClass();
  189. $context->disabled = FALSE; /* Edit this to true to make a default context disabled initially */
  190. $context->api_version = 3;
  191. $context->name = 'default_metatag_context';
  192. $context->description = '';
  193. $context->tag = 'Metatag';
  194. $context->metatag = TRUE;
  195. $context->conditions = array(
  196. 'path' => array(
  197. 'values' => array(
  198. ),
  199. ),
  200. );
  201. $context->reactions = array(
  202. 'metatag_context_reaction' => array(
  203. 'metatags' => array(),
  204. 'metatag_admin' => 1,
  205. ),
  206. );
  207. $context->condition_mode = 0;
  208. $context->weight = 0;
  209. // Translatables
  210. // Included for use with string extractors like potx.
  211. t('Metatag');
  212. return $context;
  213. }