rules_link.admin.inc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. <?php
  2. /**
  3. * @file
  4. * Generate a admin UI.
  5. */
  6. /**
  7. * UI controller.
  8. */
  9. class RulesLinkUIController extends EntityDefaultUIController {
  10. /**
  11. * Overrides EntityDefaultUIController::hook_menu() defaults.
  12. */
  13. public function hook_menu() {
  14. $items = parent::hook_menu();
  15. $id_count = count(explode('/', $this->path));
  16. $wildcard = isset($this->entityInfo['admin ui']['menu wildcard']) ? $this->entityInfo['admin ui']['menu wildcard'] : '%entity_object';
  17. $items[$this->path]['description'] = 'Manage links that trigger rules.';
  18. $items[$this->path . '/manage/' . $wildcard . '/components'] = array(
  19. 'title' => t('Configure rules'),
  20. 'page callback' => 'drupal_get_form',
  21. 'page arguments' => array('rules_link_component_form', $this->entityType, $id_count + 1),
  22. 'load arguments' => array($this->entityType),
  23. 'access callback' => 'entity_access',
  24. 'access arguments' => array('update', $this->entityType, $id_count + 1),
  25. 'file' => 'rules_link.admin.inc',
  26. 'file path' => drupal_get_path('module', 'rules_link'),
  27. 'type' => MENU_LOCAL_TASK,
  28. 'weight' => 2,
  29. );
  30. return $items;
  31. }
  32. /**
  33. * Overrides EntityDefaultUIController::overviewTableHeaders().
  34. */
  35. protected function overviewTableHeaders($conditions, $rows, $additional_header = array()) {
  36. $additional_header[] = t('Entity');
  37. return parent::overviewTableHeaders($conditions, $rows, $additional_header);
  38. }
  39. /**
  40. * Overrides EntityDefaultUIController::overviewTableRow().
  41. */
  42. protected function overviewTableRow($conditions, $id, $entity, $additional_cols = array()) {
  43. // Add a column showing the entity type.
  44. $entity_info = entity_get_info($entity->entity_type);
  45. $additional_cols[] = $entity_info['label'];
  46. return parent::overviewTableRow($conditions, $id, $entity, $additional_cols);
  47. }
  48. /**
  49. * Overrides EntityDefaultUIController::entityFormSubmitBuildEntity().
  50. */
  51. public function entityFormSubmitBuildEntity($form, &$form_state) {
  52. $rules_link = $form_state[$this->entityType];
  53. $values = $form_state['values'];
  54. $rules_link->name = $values['name'];
  55. $rules_link->label = $values['label'];
  56. $rules_link->entity_type = $values['entity_type'];
  57. $rules_link->path = $values['path'];
  58. $rules_link->settings['text'] = $values['text'];
  59. $rules_link->settings['link_type'] = $values['link_type'];
  60. $rules_link->settings['bundles'] = $values['bundles'];
  61. $rules_link->settings['entity_link'] = isset($values['entity_link']) ? $values['entity_link'] : '';
  62. if ($rules_link->settings['link_type'] == 'confirm') {
  63. $rules_link->settings['confirm_question'] = $values['question'];
  64. $rules_link->settings['confirm_description'] = $values['description'];
  65. }
  66. return $rules_link;
  67. }
  68. }
  69. /**
  70. * Generates the rules link editing form.
  71. */
  72. function rules_link_form($form, &$form_state, $rules_link, $op = 'edit') {
  73. drupal_add_css(drupal_get_path('module', 'rules_link') . '/rules_link.css', 'file');
  74. if ($op == 'clone') {
  75. $rules_link->label .= ' (cloned)';
  76. $rules_link->name .= '_cloned';
  77. }
  78. $form['label'] = array(
  79. '#title' => t('Label'),
  80. '#type' => 'textfield',
  81. '#default_value' => isset($rules_link->label) ? $rules_link->label : '',
  82. '#description' => t('The human-readable name of this rules link'),
  83. '#required' => TRUE,
  84. '#size' => 30,
  85. );
  86. $form['name'] = array(
  87. '#type' => 'machine_name',
  88. '#description' => t('A unique machine-readable name for this rules link. It must only contain lowercase letters, numbers, and underscores and must not begin with a number.'),
  89. '#default_value' => isset($rules_link->name) ? $rules_link->name : '',
  90. '#disabled' => $rules_link->hasStatus(ENTITY_IN_CODE) && $op != 'clone',
  91. '#required' => TRUE,
  92. '#maxlength' => 32,
  93. '#machine_name' => array(
  94. 'exists' => 'rules_link_get_links',
  95. 'source' => array('label'),
  96. ),
  97. );
  98. $entity_options = array();
  99. $entities = entity_get_info();
  100. foreach ($entities as $key => $entity) {
  101. $entity_options[$key] = t($entity['label']);
  102. }
  103. $default_entity = isset($form_state['values']['entity_type']) ? $form_state['values']['entity_type'] : (isset($rules_link->entity_type) ? $rules_link->entity_type : 'node');
  104. $form['entity_type'] = array(
  105. '#title' => t('Entity Type'),
  106. '#description' => t('Enity type to which the link should be attached to.'),
  107. '#default_value' => $default_entity,
  108. '#options' => $entity_options,
  109. '#required' => TRUE,
  110. '#type' => 'select',
  111. '#maxlength' => 32,
  112. '#ajax' => array(
  113. 'callback' => 'rules_link_bundle_js_callback',
  114. 'wrapper' => 'rules-link-bundle',
  115. 'speed' => 'fast',
  116. ),
  117. );
  118. $bundle_options = array();
  119. if (isset($form_state['clicked_button']) && $form_state['clicked_button']['#parents'][0] == 'show_all') {
  120. foreach ($entities as $key => $entity) {
  121. foreach ($entity['bundles'] as $key => $bundle) {
  122. $bundle_options[$key] = $bundle['label'];
  123. }
  124. }
  125. }
  126. else {
  127. foreach ($entities[$default_entity]['bundles'] as $key => $bundle) {
  128. $bundle_options[$key] = isset($bundle['label']) ? $bundle['label'] : $key;
  129. }
  130. }
  131. $form['bundle_wrapper'] = array(
  132. '#prefix' => '<div id="rules-link-bundle">',
  133. '#suffix' => '</div>',
  134. );
  135. $form['bundle_wrapper']['bundles'] = array(
  136. '#title' => t('Bundles'),
  137. '#description' => t('Bundles to which the link should be attached to. If left empty, the link is not restricted for any bundles.'),
  138. '#type' => 'select',
  139. '#options' => $bundle_options,
  140. '#default_value' => isset($rules_link->settings['bundles']) ? $rules_link->settings['bundles'] : array(),
  141. '#multiple' => TRUE,
  142. );
  143. // If the entity type does not make use of bundles, make sure we are not
  144. // restricting by bundle.
  145. $entity_info = entity_get_info($default_entity);
  146. if (empty($entity_info['entity keys']['bundle'])) {
  147. $form['bundle_wrapper']['bundles']['#access'] = FALSE;
  148. $form['bundle_wrapper']['bundles']['#value'] = array();
  149. }
  150. $html_id_show_all= drupal_html_id('rules-link-show-all-wrapper');
  151. // Button for users without js.
  152. $form['show_all'] = array(
  153. '#type' => 'button',
  154. '#value' => t('Show All'),
  155. '#prefix' => "<div id='$html_id_show_all'>",
  156. '#suffix' => '</div>'
  157. );
  158. // Hide the button for users with javascript.
  159. drupal_add_js('(function ($) { $("#' . $html_id_show_all . '").hide() }(jQuery));',
  160. array('type' => 'inline', 'scope' => 'footer')
  161. );
  162. $form['text'] = array(
  163. '#title' => t('Link text'),
  164. '#type' => 'textfield',
  165. '#default_value' => isset($rules_link->settings['text']) ? $rules_link->settings['text'] : '',
  166. '#description' => t('The text of the link.'),
  167. '#required' => TRUE,
  168. '#size' => 30,
  169. );
  170. $form['link_type'] = array(
  171. '#type' => 'radios',
  172. '#title' => t('Link type'),
  173. '#default_value' => isset($rules_link->settings['link_type']) ? $rules_link->settings['link_type'] : 'token',
  174. '#options' => array('javascript' => t('Javascript'), 'token' => t('Normal'), 'confirm' => t('Confirmation')),
  175. );
  176. $form['confirm']['confirm_question'] = array(
  177. '#type' => 'textfield',
  178. '#title' => t('Confirm question'),
  179. '#default_value' => isset($rules_link->settings['confirm_text']) ? $rules_link->settings['confirm_text'] : '',
  180. '#required' => TRUE,
  181. '#weight' => 50,
  182. '#states' => array(
  183. // Show the form only when the link type is confirm.
  184. 'visible' => array(
  185. 'input[name="link_type"]' => array('value' => 'confirm'),
  186. ),
  187. ),
  188. );
  189. $form['confirm'] = array(
  190. '#type' => 'fieldset',
  191. '#title' => 'Confirmation form settings.',
  192. '#states' => array(
  193. // Show the form only when the link type is confirm.
  194. 'visible' => array(
  195. 'input[name="link_type"]' => array('value' => 'confirm'),
  196. ),
  197. ),
  198. );
  199. $form['confirm']['question'] = array(
  200. '#type' => 'textfield',
  201. '#title' => t('Confirm question'),
  202. '#description' => t('The question that should appear, when confirmation form is shown.'),
  203. '#default_value' => isset($rules_link->settings['confirm_question']) ? $rules_link->settings['confirm_question'] : '',
  204. );
  205. $form['confirm']['description'] = array(
  206. '#type' => 'textfield',
  207. '#title' => t('Confirm description'),
  208. '#description' => t('The description that should appear, when confirmation form is shown.'),
  209. '#default_value' => isset($rules_link->settings['confirm_description']) ? $rules_link->settings['confirm_description'] : '',
  210. );
  211. global $base_url;
  212. $form['path'] = array(
  213. '#title' => t('Path'),
  214. '#description' => t('(Optional) Path for the link. It not set a default link will be generated.'),
  215. '#default_value' => isset($rules_link->path) ? $rules_link->path : '',
  216. '#type' => 'textfield',
  217. '#field_prefix' => $base_url . '/',
  218. );
  219. $form['entity_link'] = array(
  220. '#type' => 'checkbox',
  221. '#title' => t('Show link when entity is rendered'),
  222. '#description' => t('If checked, the link is shown when viewing an entity to which the bundle and the visibility conditions apply. Only applies to displayed entity types and if no addition variables were added.'),
  223. '#default_value' => isset($rules_link->settings['entity_link']) ? $rules_link->settings['entity_link'] : FALSE,
  224. );
  225. if ($op == 'add') {
  226. RulesPluginUI::formDefaults($form, $form_state);
  227. $rules_link_temp_component = rules_plugin_factory('and');
  228. $rules_link_temp_component->form($form['variables'], $form_state, array('show settings' => TRUE, 'button' => TRUE, 'init' => TRUE));
  229. unset($form['variables']['settings']['label']);
  230. unset($form['variables']['settings']['name']);
  231. unset($form['variables']['settings']['tags']);
  232. $form['variables']['settings']['vars']['#title'] = t('Additional Variables');
  233. $form['variables']['settings']['vars']['#description'] = t('Here you can add additional variable, which should be used apart from the entity type by the access conditions and the rule sets.') . ' ' . $form['variables']['settings']['vars']['#description'];
  234. $form['variables']['settings']['#collapsible'] = FALSE;
  235. $form['variables']['settings']['#type'] = 'container';
  236. $form['variables']['settings']['label']['#default_value'] = '';
  237. $form['variables']['settings']['#weight'] = -1;
  238. // Add JS file that sets the correct type, label and machine name for the
  239. // selected entity type.
  240. drupal_add_js(drupal_get_path('module', 'rules_link') . '/rules_link.admin.js', 'file');
  241. $form['variables']['settings']['vars']['items'][0]['type']['#default_value'] = 'node';
  242. $form['variables']['settings']['vars']['items'][0]['type']['#disabled'] = TRUE;
  243. //$form['variables']['settings']['vars']['items'][0]['type']
  244. $form['variables']['settings']['vars']['items'][0]['label']['#default_value'] = 'node';
  245. $form['variables']['settings']['vars']['items'][0]['label']['#disabled'] = TRUE;
  246. $form['variables']['settings']['vars']['items'][0]['name']['#default_value'] = 'node';
  247. $form['variables']['settings']['vars']['items'][0]['name']['#disabled'] = TRUE;
  248. // Hide the rule elements stuff for now.
  249. foreach (array('elements', 'negate') as $key) {
  250. $form[$key]['#access'] = FALSE;
  251. }
  252. foreach (array('active', 'weight') as $key) {
  253. $form['settings'][$key]['#access'] = FALSE;
  254. }
  255. unset($form['variables']['negate']);
  256. $form_state['rules_link_temp_component'] = &$rules_link_temp_component;
  257. $form['entity_link']['#states'] = array(
  258. // Disable this form if additional variables were added.
  259. 'disabled' => array(
  260. 'input[name="settings[vars][items][1][name]"]' => array('filled' => TRUE),
  261. ),
  262. 'unchecked' => array(
  263. 'input[name="settings[vars][items][1][name]"]' => array('filled' => TRUE),
  264. ),
  265. );
  266. }
  267. else {
  268. $condition_set = rules_link_load_condition_set($rules_link);
  269. $condition_vars = $condition_set->componentVariables();
  270. $form['entity_link']['#disabled'] = count($condition_vars) > 1 ? TRUE : FALSE;
  271. $form['actions']['submit'] = array(
  272. '#type' => 'submit',
  273. '#value' => t('Save Rules Link'),
  274. '#weight' => 40,
  275. );
  276. }
  277. return $form;
  278. }
  279. /**
  280. * Callback for the select box for bundles.
  281. */
  282. function rules_link_bundle_js_callback($form, $form_state) {
  283. return $form['bundle_wrapper'];
  284. }
  285. /**
  286. * Validation form for editing form.
  287. */
  288. function rules_link_form_validate(&$form, &$form_state) {
  289. $rules_link = entity_ui_form_submit_build_entity($form, $form_state);
  290. if (!entity_property_verify_data_type($rules_link->name, 'token')) {
  291. form_error($form['name'], t('The name "%name" is not a valid name. Please select a different name.', array('%name' => $rules_link->name)));
  292. }
  293. $path = empty($rules_link->path) ? $rules_link->name : $rules_link->path;
  294. $menu_item = menu_get_item($path);
  295. $lookup_path = drupal_lookup_path('source', $path);
  296. if (!empty($menu_item) || !empty($lookup_path)) {
  297. form_error($form['path'], t('The path "%path" is already in use. This system cannot override existing paths. Please select a different name or enter a different path.', array('%path' => $path)));
  298. }
  299. foreach (rules_link_get_links() as $link) {
  300. if ($link->path == $path && $link->name != $rules_link->name) {
  301. form_error($form['path'], t('The path "%path" is taken by the rules link "%link". This system cannot override existing paths.',
  302. array(
  303. '%path' => $path,
  304. '%link' => $link->name,
  305. )
  306. ));
  307. }
  308. }
  309. }
  310. /**
  311. * Form API submit callback for the type form.
  312. */
  313. function rules_link_form_submit(&$form, &$form_state) {
  314. $rules_link = entity_ui_form_submit_build_entity($form, $form_state);
  315. $form_state['redirect'] = 'admin/config/workflow/rules_links';
  316. if (empty($rules_link->path)) {
  317. $rules_link->path = $rules_link->name;
  318. }
  319. if ($form_state['op'] == 'add') {
  320. $form_state['redirect'] = 'admin/config/workflow/rules_links/manage/' . $rules_link->name;
  321. $rule_set = rules_link_load_rules_set($rules_link);
  322. $condition_set = rules_link_load_condition_set($rules_link);
  323. $rules_set_vars = &$rule_set->componentVariables();
  324. $condition_vars = &$condition_set->componentVariables();
  325. // Remove the first element, since it is decoration only and it might be
  326. // wrong in case Javascript is disabled.
  327. array_shift($form_state['values']['settings']['vars']['items']);
  328. foreach ($form_state['values']['settings']['vars']['items'] as $var) {
  329. if ($var['type'] !== '0') {
  330. $val = array(
  331. 'label' => $var['label'],
  332. 'type' => $var['type'],
  333. );
  334. $rules_set_vars[$var['name']] = $val;
  335. $condition_vars[$var['name']] = $val;
  336. }
  337. }
  338. $condition_set->label = 'Rules link: ' . $rules_link->label . ' condition';
  339. $condition_set->save(rules_link_get_condition_set_name($rules_link), 'rules_link');
  340. $rule_set->label = 'Rules link: ' . $rules_link->label . ' rules set';
  341. $rule_set->save(rules_link_get_rules_set_name($rules_link), 'rules_link');
  342. drupal_set_message(t('Your Rules Link has been created along with the required components. Please remember to set <a href="!url">%link</a> for your link and to add actions to your reactions rule.', array('%link' => t('the permissions'), '!url' => url('admin/people/permissions', array('fragment' => 'module-rules_link')))));
  343. }
  344. else {
  345. drupal_set_message(t('Your Rules Link has been saved.'));
  346. }
  347. // Save and go back.
  348. $rules_link->save();
  349. }
  350. /**
  351. * Form function for the rules link components.
  352. */
  353. function rules_link_component_form($form, &$form_state, $entity_type, $rules_link) {
  354. $form = array();
  355. $form_state['rules_link'] = $rules_link;
  356. $condition = rules_link_load_condition_set($rules_link);
  357. $condition->form($form['condition_set'], $form_state);
  358. $form['condition_set']['elements']['#caption'] = '';
  359. $form['condition_set']['help']['#title'] = t('Access conditions');
  360. $form['condition_set']['help']['#type'] = 'item';
  361. $form['condition_set']['help']['#markup'] = t('The access conditions can additionally to <a href="!url">%link</a> specify, when to show the rules links.', array('%link' => t('the permissions'), '!url' => url('admin/people/permissions', array('fragment' => 'module-rules_link'))));
  362. // Since we never want to negate the result, we can hide the form.
  363. unset($form['condition_set']['negate']);
  364. $form['condition_set']['#tree'] = TRUE;
  365. $rule_set = rules_link_load_rules_set($rules_link);
  366. $rule_set->form($form['rule_set'], $form_state, array('show settings' => TRUE));
  367. // Correct the state setup - default ruleset form doesn't expect this structure
  368. $form['rule_set']['settings']['access']['permissions']['#states'] = array(
  369. 'visible' => array(
  370. ':input[name="rule_set[settings][access][access_exposed]"]' => array('checked' => TRUE),
  371. ),
  372. );
  373. $form['rule_set']['elements']['#caption'] = '';
  374. $form['rule_set']['#tree'] = TRUE;
  375. $form['rule_set']['help']['#title'] = t('Reactions');
  376. $form['rule_set']['help']['#type'] = 'item';
  377. $form['rule_set']['help']['#markup'] = t('To output a system message, simply use the "Show a message on the site" action in one of your rules. Javascript links will replace the link with the text from the system message.');
  378. $form['rule_set']['settings']['vars']['#description'] .= '<br/>' . t('Please also note that Rules Link only checks if there are enough variables available, but not if the entities could be loaded. You will have to check that your self in your components.');
  379. $form['rule_set']['settings']['label']['#access'] = FALSE;
  380. $form['rule_set']['settings']['name']['#type'] = 'value';
  381. $form['rule_set']['settings']['tags']['#access'] = FALSE;
  382. $form['rule_set']['settings']['vars']['items'][$rules_link->entity_type]['#disabled'] = TRUE;
  383. $form_state['rules_link_condition_set'] = $condition;
  384. $form_state['rules_link_rules_set'] = $rule_set;
  385. $form['actions'] = array('#type' => 'actions');
  386. $form['actions']['submit'] = array(
  387. '#type' => 'submit',
  388. '#value' => t('Save Rules Link'),
  389. '#weight' => 40,
  390. );
  391. return $form;
  392. }
  393. function rules_link_component_form_validate(&$form, &$form_state) {
  394. $form_state['rules_link_condition_set']->form_validate($form['condition_set'], $form_state);
  395. $form_state['rules_link_rules_set']->form_validate($form['rule_set'], $form_state);
  396. }
  397. /**
  398. * Form API submit callback for the type form.
  399. */
  400. function rules_link_component_form_submit(&$form, &$form_state) {
  401. $rules_set_vars = $form_state['rules_link_rules_set']->componentVariables();
  402. $form_state['rules_link_rules_set']->form_submit($form['rule_set'], $form_state);
  403. $condition_vars = &$form_state['rules_link_condition_set']->componentVariables();
  404. foreach ($condition_vars as $key => $value) {
  405. unset($condition_vars[$key]);
  406. }
  407. foreach ($rules_set_vars as $key => $value) {
  408. $condition_vars[$key] = $value;
  409. }
  410. $form_state['rules_link_condition_set']->form_submit($form['condition_set'], $form_state);
  411. // If the rule is renamed, rename also the rules set.
  412. if ($form_state['rules_link_condition_set']->name != rules_link_get_condition_set_name($form_state['rules_link'])) {
  413. $form_state['rules_link_condition_set']->label = 'Rules link: ' . $form_state['rules_link']->label . ' condition';
  414. $form_state['rules_link_condition_set']->save(rules_link_get_condition_set_name($form_state['rules_link']), 'rules_link');
  415. }
  416. // If the rule is renamed, rename also the rules set.
  417. if ($form_state['rules_link_rules_set']->name != rules_link_get_rules_set_name($form_state['rules_link'])) {
  418. $form_state['rules_link_rules_set']->label = 'Rules link: ' . $form_state['rules_link']->label . ' rules set';
  419. $form_state['rules_link_rules_set']->save(rules_link_get_rules_set_name($form_state['rules_link']), 'rules_link');
  420. }
  421. // Save the permission matrix if access is exposed
  422. if (isset($form_state['values']['rule_set']['settings']['access']) && !empty($form_state['values']['rule_set']['settings']['access']['access_exposed'])) {
  423. // Save the permission matrix.
  424. foreach ($form_state['values']['rule_set']['settings']['access']['permissions']['matrix']['checkboxes'] as $rid => $value) {
  425. user_role_change_permissions($rid, $value);
  426. }
  427. }
  428. // If an additional variable was added, then disable 'show link in entity'.
  429. if (count($rules_set_vars) > 1) {
  430. $form_state['rules_link']->settings['entity_link'] = FALSE;
  431. $form_state['rules_link']->save();
  432. }
  433. drupal_set_message(t('Your Rules Link components has been saved.'));
  434. }