metatag_context.admin.inc 9.4 KB

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