flag.admin.inc 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. <?php
  2. /**
  3. * @file
  4. * Contains administrative pages for creating, editing, and deleting flags.
  5. */
  6. /**
  7. * Flag administration page. Display a list of existing flags.
  8. */
  9. function flag_admin_page() {
  10. $flags = flag_get_flags();
  11. $default_flags = flag_get_default_flags(TRUE);
  12. $flag_admin_listing = drupal_get_form('flag_admin_listing', $flags);
  13. return theme('flag_admin_page', array(
  14. 'flags' => $flags,
  15. 'default_flags' => $default_flags,
  16. 'flag_admin_listing' => $flag_admin_listing,
  17. ));
  18. }
  19. /**
  20. * A form for ordering the weights of all the active flags in the system.
  21. */
  22. function flag_admin_listing($form, &$form_state, $flags) {
  23. $form['#flags'] = $flags;
  24. $form['#tree'] = TRUE;
  25. foreach ($flags as $flag) {
  26. $form['flags'][$flag->name]['weight'] = array(
  27. '#type' => 'weight',
  28. '#delta' => count($flags) + 5,
  29. '#default_value' => $flag->weight,
  30. '#attributes' => array('class' => array('flag-weight')),
  31. );
  32. }
  33. $form['actions'] = array(
  34. '#type' => 'actions',
  35. );
  36. if (count($flags) == 1) {
  37. // Don't show weights with only one flag.
  38. unset($form['flags'][$flag->name]['weight']);
  39. }
  40. elseif (count($flags) > 1) {
  41. // Only show the form button if there are several flags.
  42. $form['actions']['submit'] = array(
  43. '#type' => 'submit',
  44. '#value' => t('Save flag order'),
  45. );
  46. }
  47. return $form;
  48. }
  49. /**
  50. * Submit handler for the flag_admin_listing form. Save flag weight ordering.
  51. */
  52. function flag_admin_listing_submit($form, &$form_state) {
  53. foreach ($form['#flags'] as $flag) {
  54. if ($flag->weight != $form_state['values']['flags'][$flag->name]['weight']) {
  55. $flag->weight = $form_state['values']['flags'][$flag->name]['weight'];
  56. $flag->save();
  57. }
  58. }
  59. }
  60. /**
  61. * Theme the output of the normal, database flags into a table.
  62. */
  63. function theme_flag_admin_listing($variables) {
  64. $form = $variables['form'];
  65. $flags = $form['#flags'];
  66. $output = '';
  67. foreach ($flags as $flag) {
  68. $ops = array(
  69. 'flags_edit' => array('title' => t('edit'), 'href' => $flag->admin_path('edit')),
  70. 'flags_fields' => array('title' => t('manage fields'), 'href' => $flag->admin_path('fields')),
  71. 'flags_delete' => array('title' => t('delete'), 'href' => $flag->admin_path('delete')),
  72. 'flags_export' => array('title' => t('export'), 'href' => $flag->admin_path('export')),
  73. );
  74. if (!module_exists('field_ui')) {
  75. unset($ops['flags_fields']);
  76. }
  77. $permission = "flag $flag->name";
  78. $roles = user_roles(FALSE, $permission);
  79. $row = array();
  80. $row[] = check_plain($flag->title) . ' <small>(' . t('Machine name: @name', array('@name' => $flag->name)) . ')</small>';
  81. if (count($flags) > 1) {
  82. $row[] = drupal_render($form['flags'][$flag->name]['weight']);
  83. }
  84. $row[] = $flag->entity_type;
  85. $row[] = empty($roles) ? '<em>' . t('No roles') . '</em>' : implode(', ', $roles);
  86. $row[] = $flag->types ? implode(', ', $flag->types) : '-';
  87. $row[] = $flag->global ? t('Yes') : t('No');
  88. $row[] = theme('links', array('links' => $ops));
  89. $rows[] = array(
  90. 'data' => $row,
  91. 'class' => array('draggable'),
  92. );
  93. }
  94. if (!$flags) {
  95. $rows[] = array(
  96. array('data' => t('No flags are currently defined.'), 'colspan' => 7),
  97. );
  98. }
  99. elseif (count($flags) > 1) {
  100. drupal_add_tabledrag('flag-admin-listing-table', 'order', 'sibling', 'flag-weight');
  101. }
  102. $header = array(t('Flag'));
  103. if (count($flags) > 1) {
  104. $header[] = t('Weight');
  105. }
  106. $header = array_merge($header, array(
  107. t('Flag type'),
  108. t('Roles'),
  109. t('Entity bundles'),
  110. t('Global?'),
  111. t('Operations'),
  112. ));
  113. $output .= theme('table', array(
  114. 'header' => $header,
  115. 'rows' => $rows,
  116. 'attributes' => array('id' => 'flag-admin-listing-table'),
  117. ));
  118. $output .= drupal_render_children($form);
  119. return $output;
  120. }
  121. /**
  122. * Theme the list of disabled flags into a table.
  123. */
  124. function theme_flag_admin_listing_disabled($variables) {
  125. $flags = $variables['flags'];
  126. $default_flags = $variables['default_flags'];
  127. $output = '';
  128. // Build a list of disabled, module-based flags.
  129. $rows = array();
  130. foreach ($default_flags as $name => $flag) {
  131. if (!isset($flags[$name])) {
  132. $ops = array();
  133. if (!$flag->is_compatible()) {
  134. $flag_updates_needed = TRUE;
  135. $ops['flags_update'] = array(
  136. 'title' => '<strong>' . t('update code') . '</strong>',
  137. 'href' => $flag->admin_path('update'),
  138. 'html' => TRUE,
  139. );
  140. }
  141. else {
  142. $ops['flags_enable'] = array('title' => t('enable'), 'href' => $flag->admin_path('edit'));
  143. }
  144. // $flag->roles['flag'] not exist on older flags.
  145. $roles = array_flip(array_intersect(array_flip(user_roles()), !empty($flag->roles['flag']) ? $flag->roles['flag'] : array()));
  146. $rows[] = array(
  147. $flag->name,
  148. $flag->module,
  149. $flag->entity_type ? $flag->entity_type : t('Unknown'),
  150. theme('links', array('links' => $ops)),
  151. );
  152. }
  153. }
  154. if (isset($flag_updates_needed)) {
  155. drupal_set_message(t('Some flags provided by modules need to be updated to a new format before they can be used with this version of Flag. See the disabled flags for a list of flags that need updating.'), 'warning');
  156. }
  157. if (!empty($rows)) {
  158. $header = array(
  159. t('Disabled Flags'),
  160. t('Module'),
  161. t('Flag type'),
  162. t('Operations'),
  163. );
  164. $output .= theme('table', array('header' => $header, 'rows' => $rows));
  165. }
  166. return $output;
  167. }
  168. /**
  169. * Theme the output for the main flag administration page.
  170. */
  171. function theme_flag_admin_page($variables) {
  172. $flags = $variables['flags'];
  173. $default_flags = $variables['default_flags'];
  174. $output = '';
  175. $output .= drupal_render($variables['flag_admin_listing']);
  176. $output .= theme('flag_admin_listing_disabled', array('flags' => $flags, 'default_flags' => $default_flags));
  177. if (!module_exists('views')) {
  178. $output .= '<p>' . t('The <a href="@views-url">Views</a> module is not installed, or not enabled. It is recommended that you install the Views module to be able to easily produce lists of flagged content.', array('@views-url' => url('http://drupal.org/project/views'))) . '</p>';
  179. }
  180. else {
  181. $output .= '<p>';
  182. $output .= t('Lists of flagged content can be displayed using views. You can configure these in the <a href="@views-url">Views administration section</a>.', array('@views-url' => url('admin/structure/views')));
  183. if (flag_get_flag('bookmarks')) {
  184. $output .= ' ' . t('Flag module automatically provides a few <a href="@views-url">default views for the <em>bookmarks</em> flag</a>. You can use these as templates by cloning these views and then customizing as desired.', array('@views-url' => url('admin/structure/views', array('query' => array('tag' => 'flag')))));
  185. }
  186. $output .= ' ' . t('The <a href="@flag-handbook-url">Flag module handbook</a> contains extensive <a href="@customize-url">documentation on creating customized views</a> using flags.', array('@flag-handbook-url' => 'http://drupal.org/handbook/modules/flag', '@customize-url' => 'http://drupal.org/node/296954'));
  187. $output .= '</p>';
  188. }
  189. if (!module_exists('flag_actions')) {
  190. $output .= '<p>' . t('Flagging an item may trigger <em>actions</em>. However, you don\'t have the <em>Flag actions</em> module <a href="@modules-url">enabled</a>, so you won\'t be able to enjoy this feature.', array('@actions-url' => url(FLAG_ADMIN_PATH . '/actions'), '@modules-url' => url('admin/modules'))) . '</p>';
  191. }
  192. else {
  193. $output .= '<p>' . t('Flagging an item may trigger <a href="@actions-url">actions</a>.', array('@actions-url' => url(FLAG_ADMIN_PATH . '/actions'))) . '</p>';
  194. }
  195. if (!module_exists('rules')) {
  196. $output .= '<p>' . t('Flagging an item may trigger <em>rules</em>. However, you don\'t have the <a href="@rules-url">Rules</a> module enabled, so you won\'t be able to enjoy this feature. The Rules module is a more extensive solution than Flag actions.', array('@rules-url' => url('http://drupal.org/node/407070'))) . '</p>';
  197. }
  198. else {
  199. $output .= '<p>' . t('Flagging an item may trigger <a href="@rules-url">rules</a>.', array('@rules-url' => url('admin/config/workflow/rules'))) . '</p>';
  200. }
  201. $output .= '<p>' . t('To learn about the various ways to use flags, please check out the <a href="@handbook-url">Flag module handbook</a>.', array('@handbook-url' => 'http://drupal.org/handbook/modules/flag')) . '</p>';
  202. return $output;
  203. }
  204. /**
  205. * Menu callback for adding a new flag.
  206. *
  207. * @param string|NULL $entity_type
  208. * The entity type for the new flag, taken from the path argument. If not
  209. * present (i.e., '/add'), a form showing all possible flag types is shown.
  210. * Otherwise, this shows a form for adding af flag the given type.
  211. *
  212. * @see flag_add_form()
  213. * @see flag_form()
  214. */
  215. function flag_add_page($entity_type = NULL) {
  216. if (isset($entity_type)) {
  217. $flag = flag_flag::factory_by_entity_type($entity_type);
  218. // Mark the flag as new.
  219. $flag->is_new = TRUE;
  220. $type_info = flag_fetch_definition($entity_type);
  221. drupal_set_title(t('Add new @type flag', array('@type' => $type_info['title'])));
  222. return drupal_get_form('flag_form', $flag);
  223. }
  224. drupal_set_title(t('Select flag type'));
  225. return drupal_get_form('flag_add_form');
  226. }
  227. /**
  228. * Present a form for creating a new flag, setting the type of flag.
  229. */
  230. function flag_add_form($form, &$form_state) {
  231. $types = array();
  232. foreach (flag_fetch_definition() as $type => $info) {
  233. $types[$type] = $info['title'] . '<div class="description">' . $info['description'] . '</div>';
  234. }
  235. $form['type'] = array(
  236. '#type' => 'radios',
  237. '#title' => t('Flag type'),
  238. '#default_value' => 'node',
  239. '#description' => t('The type of object this flag will affect. This cannot be changed once the flag is created.'),
  240. '#required' => TRUE,
  241. '#options' => $types,
  242. );
  243. $form['actions'] = array(
  244. '#type' => 'actions',
  245. );
  246. $form['actions']['submit'] = array(
  247. '#type' => 'submit',
  248. '#value' => t('Continue'),
  249. );
  250. return $form;
  251. }
  252. function flag_add_form_validate($form, &$form_state) {
  253. $flag = flag_flag::factory_by_entity_type($form_state['values']['type']);
  254. if (get_class($flag) == 'flag_broken') {
  255. form_set_error('type', t("This flag type, %type, isn't valid.", array('%type' => $form_state['values']['type'])));
  256. }
  257. }
  258. function flag_add_form_submit($form, &$form_state) {
  259. $form_state['redirect'] = FLAG_ADMIN_PATH . '/add/' . $form_state['values']['type'];
  260. }
  261. /**
  262. * Add/Edit flag page.
  263. */
  264. function flag_form($form, &$form_state, $flag) {
  265. $form['#flag'] = $flag;
  266. $form['#flag_name'] = $flag->name;
  267. $form['title'] = array(
  268. '#type' => 'textfield',
  269. '#title' => t('Title'),
  270. '#default_value' => $flag->title,
  271. '#description' => t('A short, descriptive title for this flag. It will be used in administrative interfaces to refer to this flag, and in page titles and menu items of some <a href="@insite-views-url">views</a> this module provides (theses are customizable, though). Some examples could be <em>Bookmarks</em>, <em>Favorites</em>, or <em>Offensive</em>.', array('@insite-views-url' => url('admin/structure/views'))),
  272. '#maxlength' => 255,
  273. '#required' => TRUE,
  274. '#weight' => -3,
  275. );
  276. $form['name'] = array(
  277. '#type' => 'machine_name',
  278. '#title' => t('Machine name'),
  279. '#default_value' => $flag->name,
  280. '#description' => t('The machine-name for this flag. It may be up to 32 characters long and may only contain lowercase letters, underscores, and numbers. It will be used in URLs and in all API calls.'),
  281. '#maxlength' => 32,
  282. '#weight' => -2,
  283. '#machine_name' => array(
  284. 'exists' => 'flag_get_flag',
  285. 'source' => array('title'),
  286. ),
  287. );
  288. $form['global'] = array(
  289. '#type' => 'checkbox',
  290. '#title' => t('Global flag'),
  291. '#default_value' => $flag->global,
  292. '#description' => t('If checked, flag is considered "global" and each entity is either flagged or not. If unchecked, each user has individual flags on entities.'),
  293. '#weight' => -1,
  294. );
  295. // Don't allow the 'global' checkbox to be changed when flaggings exist:
  296. // there are too many unpleasant consequences in either direction.
  297. // @todo: Allow this, but with a confirmation form, assuming anyone actually
  298. // needs this feature.
  299. if (!empty($flag->fid) && flag_get_flag_counts($flag->name)) {
  300. $form['global']['#disabled'] = TRUE;
  301. $form['global']['#description'] .= '<br />' . t('This setting cannot be changed when flaggings exist for this flag.');
  302. }
  303. $form['messages'] = array(
  304. '#type' => 'fieldset',
  305. '#title' => t('Messages'),
  306. );
  307. $form['messages']['flag_short'] = array(
  308. '#type' => 'textfield',
  309. '#title' => t('Flag link text'),
  310. '#default_value' => !empty($flag->flag_short) ? $flag->flag_short : t('Flag this item'),
  311. '#description' => t('The text for the "flag this" link for this flag.'),
  312. '#required' => TRUE,
  313. );
  314. $form['messages']['flag_long'] = array(
  315. '#type' => 'textfield',
  316. '#title' => t('Flag link description'),
  317. '#default_value' => $flag->flag_long,
  318. '#description' => t('The description of the "flag this" link. Usually displayed on mouseover.'),
  319. );
  320. $form['messages']['flag_message'] = array(
  321. '#type' => 'textfield',
  322. '#title' => t('Flagged message'),
  323. '#default_value' => $flag->flag_message,
  324. '#description' => t('Message displayed after flagging content. If JavaScript is enabled, it will be displayed below the link. If not, it will be displayed in the message area.'),
  325. );
  326. $form['messages']['unflag_short'] = array(
  327. '#type' => 'textfield',
  328. '#title' => t('Unflag link text'),
  329. '#default_value' => !empty($flag->unflag_short) ? $flag->unflag_short : t('Unflag this item'),
  330. '#description' => t('The text for the "unflag this" link for this flag.'),
  331. '#required' => TRUE,
  332. );
  333. $form['messages']['unflag_long'] = array(
  334. '#type' => 'textfield',
  335. '#title' => t('Unflag link description'),
  336. '#default_value' => $flag->unflag_long,
  337. '#description' => t('The description of the "unflag this" link. Usually displayed on mouseover.'),
  338. );
  339. $form['messages']['unflag_message'] = array(
  340. '#type' => 'textfield',
  341. '#title' => t('Unflagged message'),
  342. '#default_value' => $flag->unflag_message,
  343. '#description' => t('Message displayed after content has been unflagged. If JavaScript is enabled, it will be displayed below the link. If not, it will be displayed in the message area.'),
  344. );
  345. $form['messages']['tokens_help'] = array(
  346. '#title' => t('Token replacement'),
  347. '#type' => 'fieldset',
  348. '#description' =>
  349. '<p>' . t('The above six texts may contain any of the tokens listed below. For example, <em>"Flag link text"</em> could be entered as:') . '</p>' .
  350. theme('item_list', array(
  351. 'items' => array(
  352. t('Add &lt;em&gt;[node:title]&lt;/em&gt; to your favorites'),
  353. t('Add this [node:type] to your favorites'),
  354. t('Vote for this proposal ([node:flag-vote-count] people have already done so)'),
  355. ),
  356. 'attributes' => array('class' => array('token-examples')),
  357. )) .
  358. '<p>' . t('These tokens will be replaced with the appropriate fields from the node (or user, or comment).') . '</p>' .
  359. theme('flag_tokens_browser', array('types' => $flag->get_labels_token_types())),
  360. '#collapsible' => TRUE,
  361. '#collapsed' => TRUE,
  362. );
  363. $form['access'] = array(
  364. '#type' => 'fieldset',
  365. '#title' => t('Flag access'),
  366. '#tree' => FALSE,
  367. '#weight' => 10,
  368. );
  369. // Flag classes will want to override this form element.
  370. $form['access']['types'] = array(
  371. '#type' => 'checkboxes',
  372. '#title' => t('Flaggable types'),
  373. '#options' => array(),
  374. '#default_value' => $flag->types,
  375. '#description' => t('Check any sub-types that this flag may be used on.'),
  376. '#required' => TRUE,
  377. '#weight' => 10,
  378. );
  379. // Disabled access breaks checkboxes unless #value is hard coded.
  380. if (!empty($flag->locked['types'])) {
  381. $form['access']['types']['#value'] = $flag->types;
  382. }
  383. // Load the user permissions into the flag.
  384. if (isset($flag->fid)) {
  385. $flag->fetch_roles();
  386. }
  387. elseif (isset($flag->import_roles)) {
  388. // Convert the roles data from old API 2 flags that have been run through
  389. // the update system.
  390. // @see FlagUpdate_2::update()
  391. $flag->roles = $flag->import_roles;
  392. }
  393. else {
  394. // For new flags, provide a reasonable default value.
  395. $flag->roles = array(
  396. 'flag' => array(DRUPAL_AUTHENTICATED_RID),
  397. 'unflag' => array(DRUPAL_AUTHENTICATED_RID),
  398. );
  399. }
  400. $form['access']['roles'] = array(
  401. '#title' => t('Roles that may use this flag'),
  402. '#description' => t('Users may only unflag content if they have access to flag the content initially. Checking <em>authenticated user</em> will allow access for all logged-in users.'),
  403. '#theme' => 'flag_form_roles',
  404. '#theme_wrappers' => array('form_element'),
  405. '#weight' => -2,
  406. '#attached' => array(
  407. 'js' => array(drupal_get_path('module', 'flag') . '/theme/flag-admin.js'),
  408. 'css' => array(drupal_get_path('module', 'flag') . '/theme/flag-admin.css'),
  409. ),
  410. );
  411. if (module_exists('session_api')) {
  412. $form['access']['roles']['#description'] .= ' ' . t('Support for anonymous users is being provided by <a href="http://drupal.org/project/session_api">Session API</a>.');
  413. }
  414. else {
  415. $form['access']['roles']['#description'] .= ' ' . t('Anonymous users may flag content if the <a href="http://drupal.org/project/session_api">Session API</a> module is installed.');
  416. }
  417. $form['access']['roles']['flag'] = array(
  418. '#type' => 'checkboxes',
  419. '#options' => user_roles(!module_exists('session_api')),
  420. '#default_value' => $flag->roles['flag'],
  421. '#parents' => array('roles', 'flag'),
  422. );
  423. $form['access']['roles']['unflag'] = array(
  424. '#type' => 'checkboxes',
  425. '#options' => user_roles(!module_exists('session_api')),
  426. '#default_value' => $flag->roles['unflag'],
  427. '#parents' => array('roles', 'unflag'),
  428. );
  429. $form['access']['unflag_denied_text'] = array(
  430. '#type' => 'textfield',
  431. '#title' => t('Unflag not allowed text'),
  432. '#default_value' => $flag->unflag_denied_text,
  433. '#description' => t('If a user is allowed to flag but not unflag, this text will be displayed after flagging. Often this is the past-tense of the link text, such as "flagged".'),
  434. '#weight' => -1,
  435. );
  436. $form['display'] = array(
  437. '#type' => 'fieldset',
  438. '#title' => t('Display options'),
  439. '#description' => t('Flags are usually controlled through links that allow users to toggle their behavior. You can choose how users interact with flags by changing options here. It is legitimate to have none of the following checkboxes ticked, if, for some reason, you wish <a href="@placement-url">to place the the links on the page yourself</a>.', array('@placement-url' => 'http://drupal.org/node/295383')),
  440. '#tree' => FALSE,
  441. '#weight' => 20,
  442. '#after_build' => array('flag_link_type_options_states'),
  443. );
  444. $form['display']['link_type'] = array(
  445. '#type' => 'radios',
  446. '#title' => t('Link type'),
  447. '#options' => _flag_link_type_options(),
  448. '#after_build' => array('flag_check_link_types'),
  449. '#default_value' => $flag->link_type,
  450. // Give this a high weight so additions by the flag classes for entity-
  451. // specific options go above.
  452. '#weight' => 18,
  453. '#attached' => array(
  454. 'js' => array(drupal_get_path('module', 'flag') . '/theme/flag-admin.js'),
  455. ),
  456. '#attributes' => array(
  457. 'class' => array('flag-link-options'),
  458. ),
  459. );
  460. // Add the descriptions to each ratio button element. These attach to the
  461. // elements when FormAPI expands them.
  462. foreach (_flag_link_type_descriptions() as $key => $description) {
  463. $form['display']['link_type'][$key]['#description'] = $description;
  464. }
  465. $form['display']['link_options_intro'] = array(
  466. // This is a hack to allow a markup element to use FormAPI states.
  467. // @see http://www.bywombats.com/blog/06-25-2011/using-containers-states-enabled-markup-form-elements
  468. '#type' => 'container',
  469. '#children' => '<p id="link-options-intro">' . t('The selected link type may require these additional settings:') . '</p>',
  470. '#weight' => 20,
  471. );
  472. $form['display']['link_options_confirm'] = array(
  473. '#type' => 'fieldset',
  474. '#title' => t('Options for the "Confirmation form" link type'),
  475. // Any "link type" provider module must put its settings fields inside
  476. // a fieldset whose HTML ID is link-options-LINKTYPE, where LINKTYPE is
  477. // the machine-name of the link type. This is necessary for the
  478. // radiobutton's JavaScript dependency feature to work.
  479. '#id' => 'link-options-confirm',
  480. '#weight' => 21,
  481. );
  482. $form['display']['link_options_confirm']['flag_confirmation'] = array(
  483. '#type' => 'textfield',
  484. '#title' => t('Flag confirmation message'),
  485. '#default_value' => isset($flag->flag_confirmation) ? $flag->flag_confirmation : '',
  486. '#description' => t('Message displayed if the user has clicked the "flag this" link and confirmation is required. Usually presented in the form of a question such as, "Are you sure you want to flag this content?"'),
  487. // This will get changed to a state by flag_link_type_options_states().
  488. '#required' => TRUE,
  489. );
  490. $form['display']['link_options_confirm']['unflag_confirmation'] = array(
  491. '#type' => 'textfield',
  492. '#title' => t('Unflag confirmation message'),
  493. '#default_value' => isset($flag->unflag_confirmation) ? $flag->unflag_confirmation : '',
  494. '#description' => t('Message displayed if the user has clicked the "unflag this" link and confirmation is required. Usually presented in the form of a question such as, "Are you sure you want to unflag this content?"'),
  495. // This will get changed to a state by flag_link_type_options_states().
  496. '#required' => TRUE,
  497. );
  498. $form['actions'] = array(
  499. '#type' => 'actions',
  500. );
  501. $form['actions']['submit'] = array(
  502. '#type' => 'submit',
  503. '#value' => t('Save flag'),
  504. // We put this button on the form before calling $flag->options_form()
  505. // to give the flag handler a chance to remove it (e.g. flag_broken).
  506. '#weight' => 999,
  507. );
  508. // Add our process handler to disable access to locked properties.
  509. $form['#process'][] = 'flag_form_locked_process';
  510. // Allow the flag handler to make additions and changes to the form.
  511. // Note that the flag_broken handler will completely empty the form array!
  512. $flag->options_form($form);
  513. return $form;
  514. }
  515. /**
  516. * FormAPI after_build function to set states on link type options fieldsets.
  517. *
  518. * We do this in an after build so we handle further link types fieldsets from
  519. * other modules that provide link types.
  520. *
  521. * This expects a link type's fieldset to be $form['display'][link_options_TYPE]
  522. * so that can be matched up with the radio button value.
  523. */
  524. function flag_link_type_options_states($element) {
  525. $intro_element_values_array = array();
  526. foreach (element_children($element) as $key) {
  527. if (isset($element[$key]['#type']) && $element[$key]['#type'] == 'fieldset' && substr($key, 0, 12) == 'link_options') {
  528. // Trim the radio value from the fieldset key. This assumed the fieldset
  529. // key is 'link_options_TYPE'.
  530. $radio_value = substr($key, 13);
  531. $element[$key]['#states'] = array(
  532. 'visible' => array(
  533. ':input[name="link_type"]' => array('value' => $radio_value),
  534. ),
  535. );
  536. // If an element in a link type options fieldset is required, then we
  537. // remove this, as this would break the form, by demanding the user
  538. // enter a value for a form element they possibly can't see!
  539. // Instead, we set the required property as a state.
  540. foreach (element_children($element[$key]) as $child_key) {
  541. if (!empty($element[$key][$child_key]['#required'])) {
  542. $element[$key][$child_key]['#required'] = FALSE;
  543. $element[$key][$child_key]['#states']['required'] = array(
  544. ':input[name="link_type"]' => array('value' => $radio_value),
  545. );
  546. }
  547. }
  548. // Gather up the radio values for the format we need for a multiple
  549. // value state.
  550. $intro_element_values_array[] = array('value' => $radio_value);
  551. }
  552. }
  553. $element['link_options_intro']['#states'] = array(
  554. 'visible' => array(
  555. ':input[name="link_type"]' => $intro_element_values_array,
  556. ),
  557. );
  558. return $element;
  559. }
  560. /**
  561. * Form process handler for locking flag properties.
  562. *
  563. * Flags defined in code may define an array of properties in $flag->locked that
  564. * are to be locked and may not be edited by the user.
  565. */
  566. function flag_form_locked_process($element, &$form_state, $form) {
  567. $flag = $form['#flag'];
  568. // Disable access to a form element whose name matches a locked flag property.
  569. if (isset($element['#name']) && !empty($flag->locked[$element['#name']])) {
  570. $element['#access'] = FALSE;
  571. }
  572. // Recurse into the form array.
  573. foreach (element_children($element) as $key) {
  574. // Workaround for Core inconvenience: setting #process here prevents an
  575. // element's essential #process handlers from its hook_element_info()
  576. // definition from being set in form_builder().
  577. // @see http://drupal.org/node/1779496
  578. if (isset($element[$key]['#type']) && ($info = element_info($element[$key]['#type']))) {
  579. if (isset($info['#process'])) {
  580. $element[$key]['#process'] = $info['#process'];
  581. }
  582. }
  583. $element[$key]['#process'][] = 'flag_form_locked_process';
  584. }
  585. return $element;
  586. }
  587. /**
  588. * Add/Edit flag form validate.
  589. */
  590. function flag_form_validate($form, &$form_state) {
  591. $form_state['values']['title'] = trim($form_state['values']['title']);
  592. $form_values = $form_state['values'];
  593. $flag = $form['#flag'];
  594. $flag->form_input($form_values);
  595. $errors = $flag->validate();
  596. foreach ($errors as $field => $field_errors) {
  597. foreach ($field_errors as $error) {
  598. form_set_error($field, $error['message']);
  599. }
  600. }
  601. }
  602. /**
  603. * Add/Edit flag form submit.
  604. */
  605. function flag_form_submit($form, &$form_state) {
  606. $flag = $form['#flag'];
  607. $form_state['values']['title'] = trim($form_state['values']['title']);
  608. $flag->form_input($form_state['values']);
  609. $flag->save();
  610. $flag->enable();
  611. drupal_set_message(t('Flag @title has been saved.', array('@title' => $flag->get_title())));
  612. // We clear caches more vigorously if the flag was new.
  613. _flag_clear_cache($flag->entity_type, !empty($flag->is_new));
  614. if (!empty($flag->is_new)) {
  615. field_attach_create_bundle('flagging', $flag->name);
  616. }
  617. // Save permissions.
  618. // This needs to be done after the flag cache has been cleared, so that
  619. // the new permissions are picked up by hook_permission().
  620. // This may need to move to the flag class when we implement extra permissions
  621. // for different flag types: http://drupal.org/node/879988
  622. // If the flag machine name as changed, clean up all the obsolete permissions
  623. // and notify FieldAPI.
  624. if ($flag->name != $form['#flag_name']) {
  625. $old_name = $form['#flag_name'];
  626. $permissions = array("flag $old_name", "unflag $old_name");
  627. foreach (array_keys(user_roles()) as $rid) {
  628. user_role_revoke_permissions($rid, $permissions);
  629. }
  630. field_attach_rename_bundle('flagging', $old_name, $flag->name);
  631. }
  632. foreach (array_keys(user_roles(!module_exists('session_api'))) as $rid) {
  633. // Create an array of permissions, based on the checkboxes element name.
  634. $permissions = array(
  635. "flag $flag->name" => $flag->roles['flag'][$rid],
  636. "unflag $flag->name" => $flag->roles['unflag'][$rid],
  637. );
  638. user_role_change_permissions($rid, $permissions);
  639. }
  640. // @todo: when we add database caching for flags we'll have to clear the
  641. // cache again here.
  642. $form_state['redirect'] = FLAG_ADMIN_PATH;
  643. }
  644. /**
  645. * Output the access options for roles in a table.
  646. */
  647. function theme_flag_form_roles($variables) {
  648. $element = $variables['element'];
  649. $header = array(
  650. array('class' => array('checkbox'), 'data' => t('Flag')),
  651. array('class' => array('checkbox'), 'data' => t('Unflag')),
  652. t('Role'),
  653. );
  654. $rows = array();
  655. foreach (element_children($element['flag']) as $role) {
  656. $row = array();
  657. $role_name = $element['flag'][$role]['#title'];
  658. unset($element['flag'][$role]['#title']);
  659. unset($element['unflag'][$role]['#title']);
  660. $element['flag'][$role]['#attributes']['class'] = array('flag-access');
  661. $element['unflag'][$role]['#attributes']['class'] = array('unflag-access');
  662. $row[] = array('class' => array('checkbox'), 'data' => drupal_render($element['flag'][$role]));
  663. $row[] = array('class' => array('checkbox'), 'data' => drupal_render($element['unflag'][$role]));
  664. $row[] = $role_name;
  665. $rows[] = $row;
  666. }
  667. return theme('table', array(
  668. 'header' => $header,
  669. 'rows' => $rows,
  670. 'attributes' => array(
  671. 'class' => array('flag-admin-table'),
  672. 'id' => 'flag-roles',
  673. ),
  674. ));
  675. }
  676. /**
  677. * Delete flag page.
  678. */
  679. function flag_delete_confirm($form, &$form_state, $flag) {
  680. $form['#flag'] = $flag;
  681. return confirm_form($form,
  682. t('Are you sure you want to delete %title?', array('%title' => $flag->get_title())),
  683. !empty($_GET['destination']) ? $_GET['destination'] : FLAG_ADMIN_PATH,
  684. isset($flag->module) ? t('This flag is provided by the %module module. It will lose any customizations and be disabled.', array('%module' => $flag->module)) : t('This action cannot be undone.'),
  685. t('Delete'), t('Cancel')
  686. );
  687. }
  688. function flag_delete_confirm_submit($form, &$form_state) {
  689. $flag = $form['#flag'];
  690. if ($form_state['values']['confirm']) {
  691. $flag->delete();
  692. $flag->disable();
  693. _flag_clear_cache($flag->entity_type, TRUE);
  694. }
  695. drupal_set_message(t('Flag @name has been deleted.', array('@name' => $flag->get_title())));
  696. $form_state['redirect'] = FLAG_ADMIN_PATH;
  697. }
  698. /**
  699. * FormAPI after_build function to check that the link type exists.
  700. */
  701. function flag_check_link_types($element) {
  702. $link_types = flag_get_link_types();
  703. if (!isset($link_types[$element['#value']])) {
  704. drupal_set_message(t('This flag uses a link type of %type, which does not exist.', array('%type' => $element['#value'])), 'error');
  705. }
  706. return $element;
  707. }
  708. /**
  709. * Clears various caches when one or more flags are modified.
  710. *
  711. * @param string|array $entity_types
  712. * The entity types for the flags. May be a single value or an array.
  713. * @param bool $is_insert_or_delete
  714. * Whether the modified flag is being inserted (saved for the first time) or
  715. * deleted. This results in a more vigorous clearing of caches. In
  716. * particular, when no flags exist yet, no Field admin UI paths exist and these
  717. * need to be created.
  718. */
  719. function _flag_clear_cache($entity_types, $is_insert_or_delete = FALSE) {
  720. if (!is_array($entity_types)) {
  721. $entity_types = array($entity_types);
  722. }
  723. // Reset our flags cache, thereby making the following code aware of the
  724. // modifications.
  725. drupal_static_reset('flag_get_flags');
  726. if ($is_insert_or_delete) {
  727. // A new or deleted flag means we are changing bundles on the Flagging
  728. // entity, and thus need to clear the entity info cache.
  729. entity_info_cache_clear();
  730. }
  731. // Clear FieldAPI's field_extra cache, so our changes to pseudofields are
  732. // noticed. It's rather too much effort to both a) check whether the
  733. // pseudofield setting has changed either way, and b) specifically clear just
  734. // the bundles that are (or were!!) affected, so we just clear for all bundles
  735. // on our entity type regardlesss.
  736. foreach ($entity_types as $entity_type) {
  737. cache_clear_all("field_info:bundle_extra:$entity_type:", 'cache_field', TRUE);
  738. }
  739. if (module_exists('views')) {
  740. views_invalidate_cache();
  741. }
  742. // The title of a flag may appear in the menu (indirectly, via our "default
  743. // views"), so we need to clear the menu cache. This call also clears the
  744. // page cache, which is desirable too because the flag labels may have
  745. // changed.
  746. menu_rebuild();
  747. }