skinr_ui.rules.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <?php
  2. /**
  3. * @file
  4. * Admin page callbacks for Skinr module's rules.
  5. */
  6. /**
  7. * Menu callback; displays the skinr rules listing.
  8. */
  9. function skinr_rules() {
  10. $output = '';
  11. $headers = array(
  12. array('data' => t('Title'), 'field' => 'title'),
  13. array('data' => t('Type'), 'field' => 'type'),
  14. array('data' => t('Operations'), 'colspan' => 2)
  15. );
  16. $rules = skinr_rule_load_multiple();
  17. $rows = array();
  18. foreach ($rules as $rule) {
  19. $row = array(
  20. check_plain($rule->title),
  21. check_plain($rule->rule_type),
  22. l(t('edit'), 'admin/structure/skinr/rules/'. $rule->rid . '/edit'),
  23. l(t('delete'), 'admin/structure/skinr/rules/'. $rule->rid . '/delete'),
  24. );
  25. $rows[] = $row;
  26. }
  27. $link = l(t('Create a new rule'), 'admin/structure/skinr/rules/add');
  28. $row = array();
  29. if (empty($rows)) {
  30. $row[] = array(
  31. 'data' => t('No rules have been set up yet. !url.', array('!url' => $link)),
  32. 'colspan' => 4,
  33. );
  34. }
  35. else {
  36. $row[] = array(
  37. 'data' => t('!url.', array('!url' => $link)),
  38. 'colspan' => 4,
  39. );
  40. }
  41. $rows[] = $row;
  42. $output .= theme('table', array('header' => $headers, 'rows' => $rows));
  43. $output .= drupal_render($form);
  44. return $output;
  45. }
  46. /**
  47. * Menu callback; displays the edit form for a skinr rule.
  48. *
  49. * @ingroup forms
  50. */
  51. function skinr_rule_add($form, &$form_state) {
  52. $form = array();
  53. $form['#tree'] = TRUE;
  54. $form['rule']['title'] = array(
  55. '#type' => 'textfield',
  56. '#title' => t('Title'),
  57. '#default_value' => !empty($form_state['values']['rule']['title']) ? $form_state['values']['rule']['title'] : '',
  58. '#description' => t('Descriptive title for this rule; used by administrators.'),
  59. '#required' => TRUE,
  60. );
  61. $options = array('page' => t('Page'));
  62. foreach (list_themes() as $theme_name => $theme) {
  63. if (empty($theme->status)) {
  64. continue;
  65. }
  66. // Create a list options containing visible regions of this theme.
  67. $regions = array();
  68. foreach (system_region_list($theme_name, REGIONS_VISIBLE) as $region_name => $region) {
  69. $regions['region__' . $region_name] = $region;
  70. }
  71. // Group the list of options by theme.
  72. $key = t('@name Regions', array('@name' => $theme->info['name']));
  73. $options[$key] = $regions;
  74. }
  75. $form['rule']['rule_type'] = array(
  76. '#type' => 'select',
  77. '#title' => t('Type'),
  78. '#options' => $options,
  79. '#default_value' => !empty($form_state['values']['rule']['rule_type']) ? $form_state['values']['rule']['rule_type'] : '',
  80. '#description' => t('Type of element the rule is applied to.'),
  81. '#required' => TRUE,
  82. );
  83. $form['buttons']['save'] = array(
  84. '#type' => 'submit',
  85. '#value' => t('Add'),
  86. );
  87. return $form;
  88. }
  89. /**
  90. * Process skinr_rule_add() submissions.
  91. */
  92. function skinr_rule_add_submit($form, &$form_state) {
  93. $rule = new stdClass();
  94. $rule->rid = NULL;
  95. $rule->title = $form_state['values']['rule']['title'];
  96. $rule->rule_type = $form_state['values']['rule']['rule_type'];
  97. $rule->node_types = array();
  98. $rule->roles = array();
  99. $rule->visibility = 0;
  100. $rule->pages = '';
  101. skinr_rule_save($rule);
  102. // Set rule id, if we inserted a new rule to allow others to know what rule they're working with.
  103. $form_state['values']['rule']['rid'] = $rule->rid;
  104. $form_state['redirect'] = 'admin/structure/skinr/rules/'. $rule->rid . '/edit';
  105. }
  106. /**
  107. * Form builder for the rule configuration form.
  108. *
  109. * @param $rid
  110. * The rule ID.
  111. *
  112. * @see skinr_rule_edit_submit()
  113. * @ingroup forms
  114. */
  115. function skinr_rule_edit($form, &$form_state, $rule) {
  116. $form['skinr']['module'] = array(
  117. '#type' => 'hidden',
  118. '#value' => 'rules',
  119. );
  120. $form['skinr']['element'] = array(
  121. '#type' => 'hidden',
  122. '#value' => $rule->rid,
  123. );
  124. $form['rule'] = array(
  125. '#weight' => -1,
  126. );
  127. $form['rule']['rid'] = array(
  128. '#type' => 'value',
  129. '#value' => $rule->rid,
  130. );
  131. $form['rule']['title'] = array(
  132. '#type' => 'textfield',
  133. '#title' => t('Rule title'),
  134. '#default_value' => $rule->title,
  135. '#description' => t('Descriptive title for this rule; used by administrators.'),
  136. '#required' => TRUE,
  137. );
  138. $form['rule']['rule_type'] = array(
  139. '#type' => 'hidden',
  140. '#value' => $rule->rule_type,
  141. );
  142. $form['rule']['rule_type_displayed'] = array(
  143. '#type' => 'item',
  144. '#title' => t('Rule type'),
  145. '#markup' => $rule->rule_type,
  146. '#description' => t('Type of element the rule is applied to.'),
  147. );
  148. // Visibility settings.
  149. $form['visibility_title'] = array(
  150. '#type' => 'item',
  151. '#title' => t('Visibility settings'),
  152. );
  153. $form['visibility'] = array(
  154. '#type' => 'vertical_tabs',
  155. '#attached' => array(
  156. 'js' => array(drupal_get_path('module', 'skinr_ui') . '/js/skinr_ui.rules.js'),
  157. ),
  158. );
  159. // Per-path visibility.
  160. $form['visibility']['path'] = array(
  161. '#type' => 'fieldset',
  162. '#title' => t('Pages'),
  163. '#collapsible' => TRUE,
  164. '#collapsed' => TRUE,
  165. '#group' => 'visibility',
  166. '#weight' => 0,
  167. );
  168. $access = user_access('use PHP for settings');
  169. if ($rule->visibility == SKINR_RULE_VISIBILITY_PHP && !$access) {
  170. $form['visibility']['path']['visibility'] = array(
  171. '#type' => 'value',
  172. '#value' => SKINR_RULE_VISIBILITY_PHP,
  173. );
  174. $form['visibility']['path']['pages'] = array(
  175. '#type' => 'value',
  176. '#value' => $rule->pages,
  177. );
  178. }
  179. else {
  180. $options = array(
  181. SKINR_RULE_VISIBILITY_NOTLISTED => t('All pages except those listed'),
  182. SKINR_RULE_VISIBILITY_LISTED => t('Only the listed pages'),
  183. );
  184. $description = t("Specify pages by using their paths. Enter one path per line. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array('%blog' => 'blog', '%blog-wildcard' => 'blog/*', '%front' => '<front>'));
  185. if (module_exists('php') && $access) {
  186. $options += array(SKINR_RULE_VISIBILITY_PHP => t('Pages on which this PHP code returns <code>TRUE</code> (experts only)'));
  187. $title = t('Pages or PHP code');
  188. $description .= ' ' . t('If the PHP option is chosen, enter PHP code between %php. Note that executing incorrect PHP code can break your Drupal site.', array('%php' => '<?php ?>'));
  189. }
  190. else {
  191. $title = t('Pages');
  192. }
  193. $form['visibility']['path']['visibility'] = array(
  194. '#type' => 'radios',
  195. '#title' => t('Show block on specific pages'),
  196. '#options' => $options,
  197. '#default_value' => $rule->visibility,
  198. );
  199. $form['visibility']['path']['pages'] = array(
  200. '#type' => 'textarea',
  201. '#title' => '<span class="element-invisible">' . $title . '</span>',
  202. '#default_value' => $rule->pages,
  203. '#description' => $description,
  204. );
  205. }
  206. // Per-node visbility.
  207. $form['visibility']['node_type'] = array(
  208. '#type' => 'fieldset',
  209. '#title' => t('Content types'),
  210. '#collapsible' => TRUE,
  211. '#collapsed' => TRUE,
  212. '#group' => 'visibility',
  213. '#weight' => 5,
  214. );
  215. $form['visibility']['node_type']['types'] = array(
  216. '#type' => 'checkboxes',
  217. '#title' => t('Show block for specific content types'),
  218. '#default_value' => $rule->node_types,
  219. '#options' => node_type_get_names(),
  220. '#description' => t('Show this block only on pages that display content of the given type(s). If you select no types, there will be no type-specific limitation.'),
  221. );
  222. // Per-role visibility.
  223. $role_options = array_map('check_plain', user_roles());
  224. $form['visibility']['role'] = array(
  225. '#type' => 'fieldset',
  226. '#title' => t('Roles'),
  227. '#collapsible' => TRUE,
  228. '#collapsed' => TRUE,
  229. '#group' => 'visibility',
  230. '#weight' => 10,
  231. );
  232. $form['visibility']['role']['roles'] = array(
  233. '#type' => 'checkboxes',
  234. '#title' => t('Show block for specific roles'),
  235. '#default_value' => $rule->roles,
  236. '#options' => $role_options,
  237. '#description' => t('Show this rule only for the selected role(s). If you select no roles, the rule will be visible to all users.'),
  238. );
  239. $form['actions'] = array('#type' => 'actions');
  240. $form['actions']['submit'] = array(
  241. '#type' => 'submit',
  242. '#value' => t('Save rule'),
  243. );
  244. $form['actions']['delete'] = array(
  245. '#type' => 'submit',
  246. '#value' => t('Delete'),
  247. '#submit' => array('skinr_rule_delete_submit'),
  248. );
  249. return $form;
  250. }
  251. /**
  252. * Form submission handler for the rule configuration form.
  253. *
  254. * @see skinr_rule_edit()
  255. */
  256. function skinr_rule_edit_submit($form, &$form_state) {
  257. $rule = new stdClass();
  258. $rule->rid = !empty($form_state['values']['rid']) ? $form_state['values']['rid'] : NULL;
  259. $rule->rule_type = $form_state['values']['rule_type'];
  260. $rule->title = $form_state['values']['title'];
  261. $rule->node_types = array_filter($form_state['values']['types']);
  262. $rule->roles = $form_state['values']['roles'];
  263. $rule->visibility = (int) $form_state['values']['visibility'];
  264. $rule->pages = trim($form_state['values']['pages']);
  265. skinr_rule_save($rule);
  266. // Set rule id, if we inserted a new rule to allow others to know what rule they're working with.
  267. $form_state['values']['rid'] = $rule->rid;
  268. $form_state['redirect'] = 'admin/structure/skinr/rules';
  269. }
  270. /**
  271. * Called from within the rule edit form; redirects to skinr_rule_delete_confirm().
  272. *
  273. * @ingroup forms
  274. */
  275. function skinr_rule_delete_submit($form, &$form_state) {
  276. $destination = array();
  277. if (isset($_REQUEST['destination'])) {
  278. $destination = drupal_get_destination();
  279. unset($_REQUEST['destination']);
  280. }
  281. $form_state['redirect'] = array('admin/structure/skinr/rules/' . $form_state['values']['rid'] . 'delete', $destination);
  282. }
  283. /**
  284. * Menu callback; displays the delete confirmation for a skinr rule.
  285. *
  286. * @param $rid
  287. * The rule ID.
  288. *
  289. * @ingroup forms
  290. */
  291. function skinr_rule_delete_confirm($form, &$form_state, $rule) {
  292. $form['rid'] = array(
  293. '#type' => 'value',
  294. '#value' => $rule->rid,
  295. );
  296. return confirm_form($form,
  297. t('Are you sure you want to delete %title?', array('%title' => $rule->title)),
  298. isset($_GET['destination']) ? $_GET['destination'] : 'admin/structure/skinr/rules',
  299. t('This action cannot be undone.'),
  300. t('Delete'),
  301. t('Cancel')
  302. );
  303. }
  304. /**
  305. * Process skinr_rule_delete_confirm() submissions.
  306. */
  307. function skinr_rule_delete_confirm_submit($form, &$form_state) {
  308. if ($form_state['values']['confirm']) {
  309. skinr_rule_delete($form_state['values']['rid']);
  310. }
  311. $form_state['redirect'] = 'admin/structure/skinr/rules';
  312. }