flag.rules.inc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. <?php
  2. /**
  3. * @file
  4. * Rules integration for the Flag module.
  5. */
  6. /**
  7. * Implements hook_rules_data_info().
  8. * @ingroup rules
  9. */
  10. function flag_rules_data_info() {
  11. return array(
  12. 'flag' => array(
  13. 'label' => t('flag'),
  14. 'ui class' => 'FlagRulesUIClass',
  15. 'wrapper class' => 'FlagRulesDataWrapper',
  16. 'wrap' => TRUE,
  17. ),
  18. 'flagging' => array(
  19. 'label' => t('flagging'),
  20. 'parent' => 'entity',
  21. 'group' => t('flag'),
  22. ),
  23. );
  24. }
  25. /**
  26. * A custom wrapper class for flags to be used with Rules.
  27. * @ingroup rules
  28. */
  29. class FlagRulesDataWrapper extends RulesIdentifiableDataWrapper implements RulesDataWrapperSavableInterface {
  30. protected function extractIdentifier($flag) {
  31. return $flag->name;
  32. }
  33. protected function load($name) {
  34. return flag_get_flag($name);
  35. }
  36. public function save() {
  37. $flag = $this->value();
  38. $flag->save();
  39. }
  40. public function validate($value) {
  41. if (isset($value) && is_string($value)) {
  42. return TRUE;
  43. }
  44. elseif (isset($value) && is_object($value) && $value instanceof flag_flag) {
  45. return TRUE;
  46. }
  47. return parent::validate($value);
  48. }
  49. }
  50. /**
  51. * UI for inputing flags.
  52. * @ingroup rules
  53. */
  54. class FlagRulesUIClass extends RulesDataUI implements RulesDataDirectInputFormInterface {
  55. public static function getDefaultMode() {
  56. return 'input';
  57. }
  58. public static function inputForm($name, $info, $settings, RulesPlugin $element) {
  59. $options = _flag_rules_flags_options(isset($info['flag_type']) ? $info['flag_type'] : NULL);
  60. $header = array(
  61. 'title' => t('Flag:'),
  62. 'type' => t('The flag type'),
  63. 'global' => t('Is the flag global?'),
  64. );
  65. $settings += array($name => isset($info['default value']) ? $info['default value'] : '');
  66. $form[$name] = array(
  67. '#type' => 'tableselect',
  68. '#header' => $header,
  69. '#options' => $options,
  70. '#required' => empty($info['optional']),
  71. '#multiple' => FALSE,
  72. '#default_value' => $settings[$name],
  73. '#empty' => t('There is no suiting flag available.'),
  74. );
  75. return $form;
  76. }
  77. public static function render($value) {
  78. $flag = flag_get_flag($value);
  79. if ($flag === FALSE) {
  80. return array();
  81. }
  82. return array(
  83. 'content' => array('#markup' => check_plain($flag->get_title())),
  84. '#attributes' => array('class' => array('rules-parameter-flag')),
  85. );
  86. }
  87. }
  88. function _flag_rules_flags_options($flag_type = NULL) {
  89. $flags = flag_get_flags();
  90. $options = array();
  91. foreach ($flags as $flag) {
  92. if (!isset($flag_type) || $flag->entity_type == $flag_type) {
  93. $options[$flag->name] = array(
  94. 'title' => $flag->get_title(),
  95. 'type' => $flag->entity_type,
  96. 'global' => $flag->global ? t('Yes') : t('No'),
  97. );
  98. }
  99. }
  100. return $options;
  101. }
  102. /**
  103. * Implements hook_rules_event_info().
  104. */
  105. function flag_rules_event_info() {
  106. $items = array();
  107. $flags = flag_get_flags();
  108. foreach ($flags as $flag) {
  109. // We only support flags on entities.
  110. if ($info = entity_get_info($flag->entity_type)) {
  111. $variables = array(
  112. 'flag' => array(
  113. 'type' => 'flag',
  114. 'label' => t('flag'),
  115. 'flag_type' => $flag->entity_type,
  116. ),
  117. 'flagged_' . $flag->entity_type => array(
  118. 'type' => $flag->entity_type,
  119. 'label' => $info['label'],
  120. ),
  121. 'flagging_user' => array(
  122. 'type' => 'user',
  123. 'label' => t('flagging user'),
  124. ),
  125. 'flagging' => array(
  126. 'type' => 'flagging',
  127. 'label' => t('flagging'),
  128. ),
  129. );
  130. // For each flag we define two events.
  131. $items['flag_flagged_' . $flag->name] = array(
  132. 'group' => t('Flag'),
  133. 'label' => t('A @flag-type has been flagged, under "@flag-title"', array('@flag-title' => $flag->get_title(), '@flag-type' => t($flag->entity_type))),
  134. 'variables' => $variables,
  135. 'access callback' => 'flag_rules_integration_access',
  136. );
  137. $items['flag_unflagged_' . $flag->name] = array(
  138. 'group' => t('Flag'),
  139. 'label' => t('A @flag-type has been unflagged, under "@flag-title"', array('@flag-title' => $flag->get_title(), '@flag-type' => t($flag->entity_type))),
  140. 'variables' => $variables,
  141. 'access callback' => 'flag_rules_integration_access',
  142. );
  143. }
  144. }
  145. return $items;
  146. }
  147. /**
  148. * Implements hook_rules_action_info().
  149. */
  150. function flag_rules_action_info() {
  151. $param_defaults = array(
  152. 'flagging_user' => array(
  153. 'type' => 'user',
  154. 'label' => t('User on whose behalf to flag'),
  155. 'description' => t('For non-global flags, this is the user on whose behalf to flag the object. In addition, if checked below, the access permissions to the flag are checked against this user.'),
  156. ),
  157. 'permission_check' => array(
  158. 'type' => 'boolean',
  159. 'label' => t('Skip permission check'),
  160. 'description' => t('Whether to ignore permissions of the user on whose behalf to flag.'),
  161. 'restriction' => 'input',
  162. ),
  163. );
  164. $items = array(
  165. 'flag_trim' => array(
  166. 'label' => t('Trim a flag'),
  167. 'base' => 'flag_rules_action_trim',
  168. 'parameter' => array(
  169. 'flag' => array(
  170. 'type' => 'flag',
  171. 'label' => t('Flag'),
  172. ),
  173. 'flagging_user' => array(
  174. 'type' => 'user',
  175. 'label' => t('User whose flag to trim'),
  176. 'description' => t('For non-global flags, this is the user whose flag to trim. (For global flags, this argument is ignored.)'),
  177. ),
  178. 'cutoff_size' => array(
  179. 'type' => 'integer',
  180. 'label' => t('Flag queue size'),
  181. 'description' => t('The maximum number of objects to keep in the queue. Newly flagged objects will be kept; older ones will be removed. Tip: by typing "1" here you implement a singleton.'),
  182. ),
  183. 'trim_newest' => array(
  184. 'type' => 'boolean',
  185. 'label' => t('Trim newest flags'),
  186. 'description' => t('Checking this will trim the newest flags. This will prevent new flags once a limit is reached.'),
  187. ),
  188. 'permission_check' => $param_defaults['permission_check'],
  189. ),
  190. 'group' => t('Flag'),
  191. 'access callback' => 'flag_rules_integration_access',
  192. ),
  193. 'fetch_overall_flag_count' => array(
  194. 'label' => t('Fetch overall flag count'),
  195. 'base' => 'flag_rules_action_fetch_overall_flag_count',
  196. 'parameter' => array(
  197. 'flag' => array(
  198. 'type' => 'flag',
  199. 'label' => t('Flag'),
  200. ),
  201. ),
  202. 'provides' => array(
  203. 'overall_flag_count' => array(
  204. 'label' => t('Overall flag count'),
  205. 'description' => t('During a flagging/unflagging event the count
  206. will take into account the current flagging/unflagging procedure.'),
  207. 'type' => 'integer',
  208. ),
  209. ),
  210. 'group' => t('Flag'),
  211. 'access callback' => 'flag_rules_integration_access',
  212. ),
  213. 'fetch_entity_flag_count' => array(
  214. 'label' => t('Fetch entity flag count'),
  215. 'base' => 'flag_rules_action_fetch_entity_flag_count',
  216. 'parameter' => array(
  217. 'flag' => array(
  218. 'type' => 'flag',
  219. 'label' => t('Flag'),
  220. ),
  221. 'entity_type' => array(
  222. 'type' => 'text',
  223. 'label' => t('Entity type'),
  224. 'options list' => 'flag_rules_get_flag_types',
  225. 'restriction' => 'input',
  226. ),
  227. ),
  228. 'provides' => array(
  229. 'entity_flag_count' => array(
  230. 'label' => t('Entity flag count'),
  231. 'description' => t('During a flagging event, the count
  232. will take into account the current flagging procedure. For
  233. an unflagging event, the count will NOT yet be decreased for the
  234. current unflagging procedure.'),
  235. 'type' => 'integer',
  236. ),
  237. ),
  238. 'group' => t('Flag'),
  239. 'access callback' => 'flag_rules_integration_access',
  240. ),
  241. 'fetch_user_flag_count' => array(
  242. 'label' => t('Fetch user flag count'),
  243. 'base' => 'flag_rules_action_fetch_user_flag_count',
  244. 'parameter' => array(
  245. 'flag' => array(
  246. 'type' => 'flag',
  247. 'label' => t('Flag'),
  248. ),
  249. 'user' => array(
  250. 'type' => 'user',
  251. 'label' => t('User'),
  252. ),
  253. ),
  254. 'provides' => array(
  255. 'user_flag_count' => array(
  256. 'label' => t('User flag count'),
  257. 'description' => t('During a flagging event, the count
  258. will take into account the current flagging procedure. For
  259. an unflagging event, the count will NOT yet be decreased for the
  260. current unflagging procedure.'),
  261. 'type' => 'integer',
  262. ),
  263. ),
  264. 'group' => t('Flag'),
  265. 'access callback' => 'flag_rules_integration_access',
  266. ),
  267. );
  268. foreach (flag_get_types() as $type) {
  269. $entity_info = entity_get_info($type);
  270. $label = $entity_info['label'];
  271. $items += array(
  272. 'flag_fetch_' . $type . '_by_user' => array(
  273. 'label' => t('Fetch @label flagged by user', array('@label' => $label)),
  274. 'base' => 'flag_rules_action_fetch_entity_by_user',
  275. 'parameter' => array(
  276. 'flag' => array(
  277. 'type' => 'flag',
  278. 'label' => t('Flag'),
  279. 'flag_type' => $type,
  280. 'description' => t('The flag to check for.'),
  281. ),
  282. 'flagging_user' => array(
  283. 'type' => 'user',
  284. 'label' => t('User who flagged the @label', array('@label' => $label)),
  285. 'description' => t('For non-global flags, this is the user who flagged the @label. (For global flags, this argument is ignored.)', array('@label' => $label)),
  286. ),
  287. ),
  288. 'provides' => array(
  289. 'content_flagged_by_user' => array(
  290. 'label' => t('Content flagged by user'),
  291. 'type' => 'list<' . $type . '>',
  292. ),
  293. ),
  294. 'group' => t('Flag'),
  295. 'access callback' => 'flag_rules_integration_access',
  296. ),
  297. 'flag_flag' . $type => array(
  298. 'label' => t('Flag a @label', array('@label' => $label)),
  299. 'base' => 'flag_rules_action_flag',
  300. 'parameter' => array(
  301. 'flag' => array(
  302. 'type' => 'flag',
  303. 'label' => t('Flag'),
  304. 'flag_type' => $type,
  305. 'description' => t('The flag to check for.'),
  306. ),
  307. $type => array(
  308. 'type' => $type,
  309. 'label' => $label,
  310. ),
  311. ) + $param_defaults,
  312. 'group' => t('Flag'),
  313. 'access callback' => 'flag_rules_integration_access',
  314. ),
  315. 'flag_unflag' . $type => array(
  316. 'label' => t('Unflag a @label', array('@label' => $label)),
  317. 'base' => 'flag_rules_action_unflag',
  318. 'parameter' => array(
  319. 'flag' => array(
  320. 'type' => 'flag',
  321. 'label' => t('Flag'),
  322. 'flag_type' => $type,
  323. 'description' => t('The flag to check for.'),
  324. ),
  325. $type => array(
  326. 'type' => $type,
  327. 'label' => $label,
  328. ),
  329. ) + $param_defaults,
  330. 'group' => t('Flag'),
  331. 'access callback' => 'flag_rules_integration_access',
  332. ),
  333. );
  334. $items['flag_fetch_users_' . $type] = array(
  335. 'label' => t('Fetch users who have flagged a @label', array('@label' => $label)),
  336. 'base' => 'flag_rules_action_fetch_users',
  337. 'parameter' => array(
  338. 'flag' => array(
  339. 'type' => 'flag',
  340. 'label' => t('Flag'),
  341. 'flag_type' => $type,
  342. 'description' => t('Choose the flag for which to fetch the users.'),
  343. ),
  344. $type => array(
  345. 'type' => $type,
  346. 'label' => $label,
  347. ),
  348. ),
  349. 'provides' => array(
  350. 'users' => array(
  351. 'label' => t('Users who flagged'),
  352. 'type' => 'list<user>',
  353. ),
  354. ),
  355. 'group' => t('Flag'),
  356. 'access callback' => 'flag_rules_integration_access',
  357. );
  358. }
  359. // For backward compatibility sake. This was the original name of the
  360. // 'fetch node by user'.
  361. $items['flag_fetch_entity_by_user'] = $items['flag_fetch_node_by_user'];
  362. $items['flag_fetch_entity_by_user']['label'] .= ' ' . t('(Legacy)');
  363. return $items;
  364. }
  365. /**
  366. * Base action implementation: Flag.
  367. */
  368. function flag_rules_action_flag($flag, $entity, $flagging_user, $permissions_check) {
  369. $flag->flag('flag', $flag->get_entity_id($entity), $flagging_user, $permissions_check);
  370. }
  371. /**
  372. * Base action implementation: Unflag.
  373. */
  374. function flag_rules_action_unflag($flag, $entity, $flagging_user, $permissions_check) {
  375. $flag->flag('unflag', $flag->get_entity_id($entity), $flagging_user, $permissions_check);
  376. }
  377. /**
  378. * Base action implementation: Trim flag.
  379. */
  380. function flag_rules_action_trim($flag, $flagging_user, $cutoff_size, $trim_newest, $permissions_check) {
  381. // For some reason, when this action fires in response to a flagging event,
  382. // as an anonymous user, then the $flagging_user is sent through as FALSE.
  383. // Not sure why. This workaround fixes the problem in this specific case.
  384. if ($flagging_user === FALSE) {
  385. $flagging_user = $GLOBALS['user'];
  386. }
  387. flag_trim_flag($flag, $flagging_user, $cutoff_size, $trim_newest, $permissions_check);
  388. }
  389. /**
  390. * Base action implementation: Fetch users who flagged an entity.
  391. */
  392. function flag_rules_action_fetch_users($flag, $entity) {
  393. $result = db_select('flagging', 'fc')
  394. ->fields('fc', array('uid'))
  395. ->condition('entity_type', $flag->entity_type)
  396. ->condition('entity_id', $flag->get_entity_id($entity))
  397. ->condition('fid', $flag->fid)
  398. ->execute();
  399. $uids = $result->fetchCol();
  400. // Filter out anonymous users.
  401. return array('users' => array_filter($uids));
  402. }
  403. /**
  404. * Base action implementation: Fetch entities flagged by a user.
  405. */
  406. function flag_rules_action_fetch_entity_by_user($flag, $entity) {
  407. $query = db_select('flagging', 'fc')
  408. ->fields('fc', array('entity_id'))
  409. ->condition('entity_type', $flag->entity_type)
  410. ->condition('fid', $flag->fid);
  411. // For global flags the user parameter is ignored, so we add the
  412. // extra 'uid' condition when the flag is NOT global.
  413. if (!$flag->global) {
  414. $user = entity_metadata_wrapper('user', $entity);
  415. $sid = $user->flag_sid->value();
  416. $query = $query->condition('uid', $user->uid->value());
  417. // Filter out any bad session ids and any users that aren't anonymous.
  418. if (!empty($sid) && $sid != -1) {
  419. $query->condition('sid', $sid);
  420. }
  421. }
  422. $result = $query->execute();
  423. $flagged = $result->fetchCol();
  424. return array('content_flagged_by_user' => $flagged);
  425. }
  426. /**
  427. * Base action implementation: Fetch overall count for a particular flag.
  428. *
  429. * The count that is returned during a flagging or an unflagging will take into
  430. * account the current flag/unflag process.
  431. */
  432. function flag_rules_action_fetch_overall_flag_count($flag) {
  433. $count = flag_get_flag_counts($flag->name);
  434. return array('overall_flag_count' => $count);
  435. }
  436. /**
  437. * Helper function which will return all the available flag types.
  438. *
  439. * @return
  440. * An array of flag type names keyed by the type name.
  441. */
  442. function flag_rules_get_flag_types() {
  443. $types = array();
  444. foreach (flag_get_types() as $type) {
  445. $types[$type] = $type;
  446. }
  447. return $types;
  448. }
  449. /**
  450. * Base action implementation: Fetch count of flags for a particular entity
  451. * type.
  452. *
  453. * During a flagging, the current flagging will be included in the count.
  454. * During an unflagging, the current flagging being removed will not yet have
  455. * been removed from the count.
  456. */
  457. function flag_rules_action_fetch_entity_flag_count($flag, $entity_type) {
  458. $count = flag_get_entity_flag_counts($flag, $entity_type);
  459. return array('entity_flag_count' => $count);
  460. }
  461. /**
  462. * Base action implementation: Fetch user's flag count.
  463. *
  464. * During a flagging, the current flagging will be included in the count.
  465. * During an unflagging, the current flagging will not yet have been removed
  466. * from the count.
  467. */
  468. function flag_rules_action_fetch_user_flag_count($flag, $user) {
  469. $count = flag_get_user_flag_counts($flag, $user);
  470. return array('user_flag_count' => $count);
  471. }
  472. /**
  473. * Implements hook_rules_condition_info().
  474. */
  475. function flag_rules_condition_info() {
  476. $items = array();
  477. foreach (flag_get_types() as $type) {
  478. $entity_info = entity_get_info($type);
  479. $label = isset($entity_info[$type]['label']) ? $entity_info[$type]['label'] : $type;
  480. $items += array(
  481. 'flag_threshold_' . $type => array(
  482. 'label' => drupal_ucfirst(t('@type has flagging count', array('@type' => $label))),
  483. 'base' => 'flag_rules_condition_threshold',
  484. 'parameter' => array(
  485. 'flag' => array(
  486. 'type' => 'flag',
  487. 'label' => t('Flag'),
  488. 'flag_type' => $type,
  489. 'description' => t('The flag to check for.'),
  490. ),
  491. $type => array(
  492. 'type' => $type,
  493. 'label' => $label,
  494. ),
  495. 'number' => array(
  496. 'type' => 'integer',
  497. 'label' => t('Number'),
  498. 'description' => t('The number against which to test the number of
  499. times the object is flagged. For example, if you type "3" here,
  500. and choose "Greater than" for the operator, then this condition
  501. will return TRUE if the object is flagged more than three times.
  502. During a flagging or an unflagging event the count will take into
  503. account the current flag/unflag process.'),
  504. ),
  505. 'operator' => array(
  506. 'type' => 'text',
  507. 'label' => t('Comparison operator'),
  508. 'options list' => 'flag_rules_condition_threshold_operator_options',
  509. 'restriction' => 'input',
  510. 'default value' => '=',
  511. 'optional' => TRUE,
  512. ),
  513. ),
  514. 'group' => t('Flag'),
  515. 'access callback' => 'flag_rules_integration_access',
  516. ),
  517. 'flag_flagged_' . $type => array(
  518. 'label' => drupal_ucfirst(t('@type is flagged', array('@type' => $label))),
  519. 'base' => 'flag_rules_condition_flagged',
  520. 'parameter' => array(
  521. 'flag' => array(
  522. 'type' => 'flag',
  523. 'label' => t('Flag'),
  524. 'flag_type' => $type,
  525. 'description' => t('The flag to check for.'),
  526. ),
  527. $type => array(
  528. 'type' => $type,
  529. 'label' => $label,
  530. ),
  531. 'flagging_user' => array(
  532. 'type' => 'user',
  533. 'label' => t('User on whose behalf to check'),
  534. 'description' => t('For non-global flags, this is the user on whose behalf the flag is checked.'),
  535. ),
  536. ),
  537. 'group' => t('Flag'),
  538. 'access callback' => 'flag_rules_integration_access',
  539. ),
  540. );
  541. }
  542. return $items;
  543. }
  544. /**
  545. * Options list callback for the operator parameter of the flagging threshold
  546. * condition.
  547. */
  548. function flag_rules_condition_threshold_operator_options() {
  549. return array(
  550. '>' => t('Greater than'),
  551. '>=' => t('Greater than or equal'),
  552. '=' => t('Equal to'),
  553. '<=' => t('Less than or equal'),
  554. '<' => t('Less than'),
  555. );
  556. }
  557. /**
  558. * Condition: Check flagging count.
  559. *
  560. * The count that is returned during a flagging or an unflagging will take into
  561. * acount the current flag/unflag process.
  562. */
  563. function flag_rules_condition_threshold($flag, $entity, $number, $operator = '=') {
  564. $count = $flag->get_count($flag->get_entity_id($entity));
  565. switch ($operator) {
  566. case '>': return $count > $number;
  567. case '>=': return $count >= $number;
  568. case '=': return $count == $number;
  569. case '<': return $count < $number;
  570. case '<=': return $count <= $number;
  571. }
  572. }
  573. /**
  574. * Condition: Flag is flagged.
  575. */
  576. function flag_rules_condition_flagged($flag, $entity, $account) {
  577. return $flag->is_flagged($flag->get_entity_id($entity), $account->uid);
  578. }
  579. /**
  580. * Rules integration access callback.
  581. */
  582. function flag_rules_integration_access($type, $name) {
  583. return user_access('administer flags');
  584. }