flag_lists.module 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737
  1. <?php
  2. module_load_include('inc', 'flag', 'includes/flag.admin');
  3. module_load_include('inc', 'flag', 'flag');
  4. /**
  5. * @file
  6. * The Flag Lists module.
  7. *
  8. * Extends flag to allow individual users to create personal flags.
  9. */
  10. /**
  11. * Implementation of hook_menu().
  12. */
  13. function flag_lists_menu() {
  14. $items = array();
  15. $items[FLAG_ADMIN_PATH . '/lists'] = array(
  16. 'title' => 'Lists',
  17. 'page callback' => 'drupal_get_form',
  18. 'page arguments' => array('flag_lists_settings_form'),
  19. 'access callback' => 'user_access',
  20. 'access arguments' => array('administer flags'),
  21. 'description' => 'Configure default settings allowing users to mark content with personal flags.',
  22. 'file' => 'flag_lists.admin.inc',
  23. 'type' => MENU_LOCAL_TASK,
  24. 'weight' => 100,
  25. );
  26. $items[FLAG_ADMIN_PATH . '/lists/settings'] = array(
  27. 'title' => 'Settings',
  28. 'page callback' => 'drupal_get_form',
  29. 'page arguments' => array('flag_lists_settings_form'),
  30. 'access callback' => 'user_access',
  31. 'access arguments' => array('administer flags'),
  32. 'description' => 'Configure default settings allowing users to mark content with personal flags.',
  33. 'file' => 'flag_lists.admin.inc',
  34. 'type' => MENU_DEFAULT_LOCAL_TASK,
  35. 'weight' => 1,
  36. );
  37. $items[FLAG_ADMIN_PATH . '/lists/list'] = array(
  38. 'title' => 'List',
  39. 'page callback' => 'flag_lists_admin_page',
  40. 'access callback' => 'user_access',
  41. 'access arguments' => array('administer flags'),
  42. 'file' => 'flag_lists.admin.inc',
  43. 'type' => MENU_LOCAL_TASK,
  44. 'weight' => 2,
  45. );
  46. $items[FLAG_ADMIN_PATH . '/lists/template'] = array(
  47. 'title' => 'New template',
  48. 'page callback' => 'drupal_get_form',
  49. 'page arguments' => array('flag_lists_create_template_form'),
  50. 'access callback' => 'user_access',
  51. 'access arguments' => array('administer flags'),
  52. 'file' => 'flag_lists.admin.inc',
  53. 'type' => MENU_LOCAL_TASK,
  54. 'weight' => 3,
  55. );
  56. if (module_exists('devel_generate')) {
  57. $items['admin/config/development/generate/flag-lists'] = array(
  58. 'title' => 'Generate lists',
  59. 'description' => 'Generate a given number of lists and listings on site content. Optionally delete existing lists and listings.',
  60. 'page callback' => 'drupal_get_form',
  61. 'page arguments' => array('flag_lists_generate_lists_form'),
  62. 'access callback' => 'user_access',
  63. 'access arguments' => array('administer flags'),
  64. 'file' => 'flag_lists.admin.inc',
  65. );
  66. }
  67. $items['flag-lists/add/%'] = array(
  68. 'title' => 'Add a list',
  69. 'page callback' => 'flag_lists_add',
  70. 'page arguments' => array(2),
  71. 'access callback' => 'user_access',
  72. 'access arguments' => array('create flag lists'),
  73. 'file' => 'flag_lists.admin.inc',
  74. 'type' => MENU_NORMAL_ITEM,
  75. );
  76. // Callback for adding a new list through JS
  77. $items['flag-lists/add/%/js'] = array(
  78. 'title' => 'Add a list',
  79. 'page callback' => 'flag_lists_add_js',
  80. 'page arguments' => array(2),
  81. 'access callback' => 'user_access',
  82. 'access arguments' => array('create flag lists'),
  83. 'file' => 'flag_lists.admin.inc',
  84. 'type' => MENU_CALLBACK,
  85. );
  86. $items['flags/lists/edit/%'] = array(
  87. 'title' => 'Edit a list',
  88. 'page callback' => 'drupal_get_form',
  89. 'page arguments' => array('flag_lists_form', 3),
  90. 'access callback' => 'flag_lists_is_owner',
  91. 'access arguments' => array(2, 3),
  92. 'file' => 'flag_lists.admin.inc',
  93. 'type' => MENU_CALLBACK,
  94. );
  95. $items['flags/lists/delete/%'] = array(
  96. 'title' => 'Delete a list',
  97. 'page callback' => 'drupal_get_form',
  98. 'page arguments' => array('flag_lists_delete_confirm', 3),
  99. 'access callback' => 'flag_lists_is_owner',
  100. 'access arguments' => array(2, 3),
  101. 'file' => 'flag_lists.admin.inc',
  102. 'type' => MENU_CALLBACK,
  103. );
  104. $items[FLAG_ADMIN_PATH . '/flag-lists/rebuild'] = array(
  105. 'title' => 'Rebuild all flag lists',
  106. 'page callback' => 'drupal_get_form',
  107. 'page arguments' => array('flag_lists_rebuild_confirm'),
  108. 'access callback' => 'user_access',
  109. 'access arguments' => array('administer flag lists'),
  110. 'file' => 'flag_lists.admin.inc',
  111. 'type' => MENU_CALLBACK,
  112. );
  113. $items['flag-lists'] = array(
  114. 'title' => 'Flag',
  115. 'page callback' => 'flag_lists_page',
  116. 'access callback' => 'user_access',
  117. 'access arguments' => array('access content'),
  118. 'type' => MENU_CALLBACK,
  119. );
  120. $items['user/%user/flags/lists'] = array(
  121. 'title' => ucfirst(variable_get('flag_lists_name', 'list')),
  122. 'page callback' => 'flag_lists_user_page',
  123. 'page arguments' => array(1),
  124. 'access callback' => 'user_access',
  125. 'access arguments' => array('view flag lists'),
  126. 'type' => MENU_LOCAL_TASK,
  127. );
  128. $items['user/%user/flags/lists/%'] = array(
  129. 'title' => ucfirst(variable_get('flag_lists_name', 'list')) . ' content',
  130. 'page callback' => 'flag_lists_user_list',
  131. 'page arguments' => array(1, 4),
  132. 'access callback' => 'user_access',
  133. 'access arguments' => array('view flag lists'),
  134. 'type' => MENU_CALLBACK,
  135. );
  136. return $items;
  137. }
  138. /**
  139. * User flag page. Display a list of user-created flag lists.
  140. */
  141. function flag_lists_user_page($user) {
  142. // Can we use our default view?
  143. if (module_exists('views')) {
  144. $view = views_get_view('flag_lists_user_lists', FALSE);
  145. if (!empty($view)) {
  146. $view->set_display('default');
  147. $view->set_arguments(array($user->uid));
  148. $output = $view->render();
  149. drupal_set_title(str_replace(array_keys($view->build_info['substitutions']), $view->build_info['substitutions'], $view->build_info['title']));
  150. }
  151. return $output;
  152. }
  153. else {
  154. return theme('flag_lists_user_page', array('uid' => $user->uid));
  155. }
  156. }
  157. /**
  158. * Theme the output for a user flag administration page.
  159. */
  160. function theme_flag_lists_user_page($variables) {
  161. $uid = $variables['uid'];
  162. $account = user_load($uid);
  163. drupal_set_title(t('Lists'));
  164. if ($flags = flag_lists_get_user_flags(NULL, $account)) {
  165. // Build the list of flag lists for this node.
  166. foreach ($flags as $flag) {
  167. $ops = theme('flag_lists_ops', array('flag' => $flag));
  168. $items[] = l($flag->title, "user/$uid/flags/lists/" . $flag->fid) . $ops;
  169. }
  170. }
  171. drupal_add_css(drupal_get_path('module', 'flag_lists') . '/theme/flag_lists.css');
  172. return theme('item_list', array('items' => $items));
  173. }
  174. /**
  175. * List the contents of a user-defined list
  176. */
  177. function flag_lists_user_list($user, $fid) {
  178. $uid = $user->uid;
  179. // Can we use our default view?
  180. if (module_exists('views')) {
  181. $view = views_get_view('flag_lists_user_list', FALSE);
  182. if (!empty($view)) {
  183. $view->set_display('default');
  184. $view->set_arguments(array($fid));
  185. $output = $view->render();
  186. drupal_set_title(str_replace(array_keys($view->build_info['substitutions']), $view->build_info['substitutions'], $view->build_info['title']));
  187. }
  188. return $output;
  189. }
  190. else {
  191. return theme('flag_lists_user_list', array('uid' => $uid, 'fid' => $fid));
  192. }
  193. }
  194. /**
  195. * Theme the output of user-defined list page
  196. */
  197. function theme_flag_lists_user_list($variables) {
  198. $uid = $variables['uid'];
  199. $fid = $variables['fid'];
  200. $flag = flag_lists_get_flag($fid);
  201. drupal_set_title($flag->title);
  202. $content = flag_lists_get_flagged_content($fid, $uid);
  203. foreach ($content as $item) {
  204. if ($item->content_type == 'node') {
  205. $node = node_load($item->entity_id);
  206. $items[] = l($node->title, 'node/' . $node->nid);
  207. }
  208. }
  209. $breadcrumb = menu_get_active_breadcrumb();
  210. $breadcrumb[] = l(t('@name lists', array('@name' => drupal_ucfirst(variable_get('flag_lists_name', t('lists'))))), 'user/' . arg(1) . '/flags/lists');
  211. drupal_set_breadcrumb($breadcrumb);
  212. return theme('item_list', array('items' => $items));
  213. }
  214. /**
  215. * Implementation of hook_theme().
  216. */
  217. function flag_lists_theme() {
  218. $path = drupal_get_path('module', 'flag') . '/theme';
  219. return array(
  220. 'flag_lists_list' => array(
  221. 'variables' => array('node' => NULL, 'create' => NULL, 'ops' => NULL, 'use_flags' => NULL),
  222. ),
  223. 'flag_lists_admin_page' => array(
  224. 'variables' => array('flags' => NULL),
  225. ),
  226. 'flag_lists_user_page' => array(
  227. 'variables' => array('uid' => NULL),
  228. ),
  229. 'flag_lists_user_list' => array(
  230. 'variables' => array('flag_name' => NULL),
  231. ),
  232. 'flag_lists_ops' => array(
  233. 'variables' => array('flag' => NULL),
  234. )
  235. );
  236. }
  237. /**
  238. * Implementation of hook_permission().
  239. */
  240. function flag_lists_permission() {
  241. return array(
  242. 'create flag lists' => array(
  243. 'title' => t('Create flag lists'),
  244. 'description' => t(''),
  245. ),
  246. 'edit own flag lists' => array(
  247. 'title' => t('Edit own flag lists'),
  248. 'description' => t(''),
  249. ),
  250. 'delete own flag lists' => array(
  251. 'title' => t('Delete own flag lists'),
  252. 'description' => t(''),
  253. ),
  254. 'view flag lists' => array(
  255. 'title' => t('View flag lists'),
  256. 'description' => t(''),
  257. ));
  258. }
  259. /**
  260. * Implementation of hook_form_alter().
  261. */
  262. function flag_lists_form_alter(&$form, &$form_state, $form_id) {
  263. switch ($form_id) {
  264. case 'flag_form':
  265. // A template flag should always have a record in the flag_lists_types table.
  266. $result = db_select('flag_lists_types', 'f')->fields('f')->execute();
  267. foreach ($result as $type) {
  268. $types[$type->name] = $type->type;
  269. }
  270. if (isset($types[$form['name']['#default_value']])) {
  271. $form['name']['#type'] = 'value';
  272. $form['global']['#type'] = 'value';
  273. $form['title']['#description'] = t('A short, descriptive title for this template. It will be used in administrative interfaces to refer to this template.');
  274. // Warn about types that already have a template.
  275. foreach ($form['access']['types']['#options'] as $option => $value) {
  276. if (in_array($option, $types) && $form['access']['types']['#default_value'] != $option) {
  277. $form['access']['types']['#options'][$option] .= '<span class="description">' . t('(Already has a template.)') . '</span>';
  278. }
  279. }
  280. $form['access']['types']['#description'] .= t('A type may only be selected in one list template.');
  281. // Unset anon permissions for now. @todo allow anon listing.
  282. unset($form['access']['roles']['flag']['#options'][1]);
  283. unset($form['access']['roles']['unflag']['#options'][1]);
  284. foreach (element_children($form['display']) as $display) {
  285. $form['display'][$display]['#type'] = 'value';
  286. }
  287. $form['display']['link_type']['#default_value'] = 'fl_template';
  288. $form['display']['#description'] = t('Unlike normal flags, lists are only displayed in a block provided by this module or in views blocks. See <a href="/admin/build/block/list">the block admin page</a> to place the block.');
  289. $form['#validate'][] = 'flag_lists_template_validate';
  290. $form['#submit'][] = 'flag_lists_template_submit';
  291. }
  292. break;
  293. case 'views_exposed_form':
  294. // Force the exposed filters to perform actions on the page itself because
  295. // the views default viwe didn't come with a page display
  296. if ($form['#id'] == 'views-exposed-form-flag-lists-default') {
  297. $form['#action'] = '/' . implode('/', arg());
  298. }
  299. break;
  300. }
  301. // Flag lists operations related changes
  302. if (strpos($form_id, 'views_form_') === 0) {
  303. $flo = _flag_lists_ops_get_field($form_state['build_info']['args'][0]);
  304. // Not a FLO-enabled views form.
  305. if (empty($flo)) {
  306. return;
  307. }
  308. // Add FLO's custom callbacks.
  309. $form['#validate'][] = 'flag_lists_ops_form_validate';
  310. $form['#submit'][] = 'flag_lists_ops_form_submit';
  311. // Allow FLO to work when embedded using views_embed_view(), or in a block.
  312. if (empty($flo->view->override_path)) {
  313. if (!empty($flo->view->preview) || $flo->view->display_handler instanceof views_plugin_display_block) {
  314. $flo->view->override_path = $_GET['q'];
  315. }
  316. }
  317. // Quickfix for FLO & exposed filters using ajax. See http://drupal.org/node/1191928.
  318. $query = drupal_get_query_parameters($_GET, array('q'));
  319. $form['#action'] = url($flo->view->get_url(), array('query' => $query));
  320. // Add basic FLO functionality.
  321. if ($form_state['step'] == 'views_form_views_form') {
  322. $form = flag_lists_ops_form($form, $form_state, $flo);
  323. }
  324. }
  325. }
  326. /**
  327. * Add the Flag list select menu widget.
  328. */
  329. function flag_lists_ops_form($form, &$form_state, $flo) {
  330. $form['#attached']['js'][] = drupal_get_path('module', 'flag_lists') . '/js/flag_lists_ops.js';
  331. $form['#attached']['css'][] = drupal_get_path('module', 'flag_lists') . '/css/flag_lists_ops.css';
  332. $form['#prefix'] = '<div class="flo-views-form">';
  333. $form['#suffix'] = '</div>';
  334. $form_state['flo_operation'] = $flo->options['flo']['operation'];
  335. // Force browser to reload the page if Back is hit.
  336. if (preg_match('/msie/i', $_SERVER['HTTP_USER_AGENT'])) {
  337. drupal_add_http_header('Cache-Control', 'no-cache'); // works for IE6+
  338. }
  339. else {
  340. drupal_add_http_header('Cache-Control', 'no-store'); // works for Firefox and other browsers
  341. }
  342. global $user;
  343. global $base_url;
  344. $account = user_load($user->uid);
  345. $items = array();
  346. if ($flags = flag_lists_get_user_flags(NULL, $account)) {
  347. // Build the list of flag lists for this node.
  348. foreach ($flags as $flag) {
  349. $items[((string)$flag->fid)] = $flag->title;
  350. }
  351. }
  352. // Group items into a fieldset for easier theming.
  353. $form['flag_lists_' . $form_state['flo_operation']] = array(
  354. '#type' => 'fieldset',
  355. '#title' => t('List operations'),
  356. '#tree' => TRUE,
  357. '#attributes' => array('class' => array('flag-lists-ops-fieldset')),
  358. );
  359. switch ($flo->options['flo']['operation']) {
  360. case 'unflag':
  361. $null_text = 'Remove from a list';
  362. $operation = 'unflag';
  363. $form['flag_lists_' . $form_state['flo_operation']]['list'] = array(
  364. '#type' => 'button',
  365. '#value' => t('Remove from list'),
  366. '#ajax' => array(
  367. 'callback' => 'flag_lists_ops_form_ajax_callback',
  368. 'wrapper' => 'flag-list-ops-container-' . $flo->options['flo']['operation'],
  369. ),
  370. );
  371. break;
  372. default:
  373. $null_text = 'Add to a list';
  374. $operation = 'flag';
  375. drupal_add_library('system', 'ui.dialog');
  376. $form['flag_lists_' . $form_state['flo_operation']]['list'] = array(
  377. '#type' => 'select',
  378. '#options' => array('0' => t('- ' . $null_text . ' -')) + $items,
  379. '#default_value' => '0',
  380. '#ajax' => array(
  381. 'callback' => 'flag_lists_ops_form_ajax_callback',
  382. 'wrapper' => 'flag-list-ops-container-' . $flo->options['flo']['operation'],
  383. ),
  384. '#attributes' => array(
  385. 'class' => array(
  386. 'flag-lists-ops-dropdown',
  387. ),
  388. ),
  389. );
  390. // Add the necessary JS for creating a new list via AJAX
  391. $result = db_select('flag_lists_types')
  392. ->fields('flag_lists_types')
  393. ->execute();
  394. $list_types = array();
  395. foreach ($result as $row) {
  396. if (!empty($row->type)) {
  397. $list_types[] = $row->type;
  398. }
  399. }
  400. drupal_add_js(array('flag_lists' => array('types' => $list_types, 'json_path' => $base_url . '/flag-lists/add/%/js')), 'setting');
  401. break;
  402. }
  403. // Get the $ops from the originating form.
  404. if (!empty($form_state['values']['flag_lists_' . $form_state['flo_operation']]['list'])) {
  405. $list = $form_state['values']['flag_lists_' . $form_state['flo_operation']]['list'];
  406. }
  407. if (!empty($_REQUEST['flag_lists_ops'])) {
  408. $ops = $_REQUEST['flag_lists_ops'];
  409. }
  410. if (!empty($ops) && !empty($list) && $form_state['values']['flag_lists_' . $form_state['flo_operation']]['operation'] == 'unflag') {
  411. $hidden_deleted_values = '';
  412. foreach ($ops as $nid) {
  413. $hidden_deleted_values .= '<input type="hidden" class="flo-deleted-value" value="' . $nid . '" />';
  414. }
  415. }
  416. $form['flag_lists_' . $form_state['flo_operation']]['operation'] = array(
  417. '#type' => 'hidden',
  418. '#value' => $operation,
  419. );
  420. $form['flag_lists_' . $form_state['flo_operation']]['go'] = array(
  421. '#type' => 'submit',
  422. '#value' => t('Go'),
  423. '#attributes' => array(
  424. 'class' => array('flag-lists-ops-go'),
  425. ),
  426. );
  427. unset($form['actions']['submit']);
  428. // Generate a status message for AJAX submission.
  429. $form['flag_lists_' . $form_state['flo_operation']]['status_message'] = array('#markup' => '');
  430. $form['flag_lists_' . $form_state['flo_operation']]['#prefix'] = '<div id="flag-list-ops-container-' . $flo->options['flo']['operation'] . '">';
  431. $form['flag_lists_' . $form_state['flo_operation']]['#suffix'] = (!empty($hidden_deleted_values)) ? $hidden_deleted_values : '' . '</div>';
  432. return $form;
  433. }
  434. function flag_lists_ops_form_ajax_callback($form, $form_state) {
  435. // The form has already been submitted and updated. We can return the replaced
  436. // item as it is.
  437. if (!flag_lists_ops_form_validate($form, $form_state)) {
  438. $message = flag_lists_ops_form_submit($form, $form_state);
  439. }
  440. $form['flag_lists_' . $form_state['flo_operation']]['status_message']['#markup'] = '<div class="alert alert-success">' . $message . '</div>';
  441. if ($form_state['flo_operation'] == 'flag') $form['flag_lists_' . $form_state['flo_operation']]['list']['#value'] = '0';
  442. $form['flag_lists_' . $form_state['flo_operation']]['status_message']['#attributes']['class'][] = 'alert-success';
  443. return $form['flag_lists_' . $form_state['flo_operation']];
  444. }
  445. function flag_lists_ops_form_validate(&$form, &$form_state) {
  446. global $user;
  447. $error_count = 0;
  448. // Check to see if an items are selected, if not fail right away.
  449. if (!isset($_REQUEST['flag_lists_ops']) || empty($_REQUEST['flag_lists_ops'])) {
  450. form_set_error('', t('No content selected.'));
  451. $error_count++;
  452. return $error_count;
  453. }
  454. switch ($form_state['values']['flag_lists_' . $form_state['flo_operation']]['operation']) {
  455. case 'unflag':
  456. $ops = $_REQUEST['flag_lists_ops'];
  457. foreach ($ops as $key => $op) {
  458. $ops[$key] = explode('-', $op);
  459. if (empty($ops[$key][1]) || !($flag = flag_lists_get_flag($ops[$key][1]))) {
  460. form_set_error('flag_lists][remove', t('Invalid options list selected to remove from.'));
  461. $error_count++;
  462. }
  463. else if ($flag->uid != $user->uid) {
  464. form_set_error('flag_lists][remove', t('You are only allowed to remove content from your own lists.'));
  465. $error_count++;
  466. }
  467. }
  468. break;
  469. default:
  470. if (empty($form_state['values']['flag_lists_' . $form_state['flo_operation']]['list'])) {
  471. form_set_error('flag_lists][list', t('No list selected. Please select a list to add to.'));
  472. $error_count++;
  473. }
  474. else if (!($flag = flag_lists_get_flag($form_state['values']['flag_lists_' . $form_state['flo_operation']]['list']))) {
  475. form_set_error('flag_lists][list', t('Invalid list selected. Please select a list to add to.'));
  476. $error_count++;
  477. }
  478. else if ($flag->uid != $user->uid) {
  479. form_set_error('flag_lists][list', t('Invalid list selected. Please select a list to add to.'));
  480. $error_count++;
  481. }
  482. break;
  483. }
  484. return $error_count;
  485. }
  486. function flag_lists_ops_form_submit(&$form, &$form_state) {
  487. // Get the $ops from the originating form.
  488. $ops = $_REQUEST['flag_lists_ops'];
  489. $success_count = 0;
  490. // Get the operation, or set it
  491. switch ($form_state['values']['flag_lists_' . $form_state['flo_operation']]['operation']) {
  492. case 'unflag':
  493. $operation = 'unflag';
  494. $message = 'removed from';
  495. foreach ($ops as $op) {
  496. // Process the ID into 2 parts
  497. list($nid, $fid) = explode('-', $op);
  498. if (empty($nid) || empty($fid)) return;
  499. if (($flag = flag_lists_get_flag($fid)) && ($node = node_load($nid))) {
  500. if (flag_lists_do_flag($flag, $operation, $nid)) {
  501. $success_count++;
  502. }
  503. }
  504. }
  505. break;
  506. default:
  507. $operation = 'flag';
  508. $message = 'added to';
  509. if ($flag = flag_lists_get_flag($form_state['values']['flag_lists_' . $form_state['flo_operation']]['list'])) {
  510. foreach ($ops as $nid) {
  511. if (flag_lists_do_flag($flag, $operation, $nid)) {
  512. $success_count++;
  513. }
  514. }
  515. }
  516. break;
  517. }
  518. $message = t('@count item(s) ' . $message . ' !title', array('@count' => $success_count, '!title' => $flag->title));
  519. if ($_GET['q'] != 'system/ajax') {
  520. drupal_set_message($message);
  521. }
  522. else {
  523. return $message;
  524. }
  525. }
  526. /**
  527. * Gets the FLO field if it exists on the passed-in view.
  528. *
  529. * @return
  530. * The field object if found. Otherwise, FALSE.
  531. */
  532. function _flag_lists_ops_get_field($view) {
  533. foreach ($view->field as $field_name => $field) {
  534. if ($field instanceof flag_lists_handler_field_ops) {
  535. // Add in the view object for convenience.
  536. $field->view = $view;
  537. return $field;
  538. }
  539. }
  540. return FALSE;
  541. }
  542. function flag_lists_template_validate($form, &$form_state) {
  543. $types = array_filter($form_state['values']['types']);
  544. $errors = array();
  545. foreach ($types as $type) {
  546. $result = db_select('flag_lists_types', 'f')
  547. ->fields('f')
  548. ->condition('type', $type)
  549. ->condition('name', $form_state['values']['name'], '<>')
  550. ->execute();
  551. foreach ($result as $errors) {
  552. $content_types[] = $errors->type;
  553. $templates[] = $errors->name;
  554. }
  555. }
  556. if (isset($content_types) && count($content_types)) {
  557. $content_types = implode(', ', $content_types);
  558. $templates = implode(', ', array_unique($templates));
  559. form_set_error('types', t('The flaggable content type(s) "@type" is(are) already assigned to the template(s) "@template." A content type may be assigned to only one template. To reassign a content type you must first remove its other assignment.', array('@type' => $content_types, '@template' => $templates)));
  560. }
  561. }
  562. function flag_lists_template_submit($form, &$form_state) {
  563. $types = array_filter($form_state['values']['types']);
  564. // Clean out the old types, then add the new.
  565. $num_deleted = db_delete('flag_lists_types')
  566. ->condition('name', $form_state['values']['name'])
  567. ->execute();
  568. foreach ($types as $type) {
  569. db_insert('flag_lists_types')
  570. ->fields(array(
  571. 'name' => $form_state['values']['name'],
  572. 'type' => $type,
  573. ))
  574. ->execute();
  575. }
  576. }
  577. /**
  578. * Helper function to build an array of all lists available to or owned by the
  579. * current user and that are available on the current content type.
  580. */
  581. function flag_lists_get_content_fids() {
  582. global $user;
  583. // This is a node view. We only care about nodes for now.
  584. if (arg(0) == 'node' && is_numeric(arg(1)) && is_null(arg(2))) {
  585. $type = db_select('node', 'n')
  586. ->fields('n', array('type'))
  587. ->condition('nid', arg(1))
  588. ->execute()
  589. ->fetchField();
  590. // Get current user's flags for this node.
  591. $query = db_select('flag_lists', 'fc')
  592. ->fields('f', 'fid')
  593. ->condition('fc.uid', $user->uid)
  594. ->condition('fn.type', $type);
  595. $query->leftJoin('flag_types', 'fn', 'fn.fid = fc.fid');
  596. $query->leftJoin('flags', 'f', 'fc.fid = f.fid');
  597. $fc_result = $query->execute();
  598. foreach ($fc_result as $row) {
  599. $fids[] = $row->fid;
  600. }
  601. }
  602. // This is the flag / unflag callback
  603. elseif (arg(0) == 'flag' && (arg(1) == 'flag' || arg(1) == 'unflag')) {
  604. // Get the flag for this request.
  605. $fids[] = db_select('flags', 'f')
  606. ->fields('f', array('fid'))
  607. ->condition('name', arg(2))
  608. ->execute()
  609. ->fetchField();
  610. }
  611. // Get the regular flags for this node. The flag module will narrow by role,
  612. // etc. when flag_get_flags() is called. These flag ids are always returned.
  613. $query = db_select('flags', 'f')
  614. ->fields('f', array('fid'))
  615. ->condition('fc.fid', NULL);
  616. $query->leftJoin('flag_lists', 'fc', 'fc.fid = f.fid');
  617. $f_result = $query->execute();
  618. foreach ($f_result as $obj) {
  619. $fids[] = $obj->fid;
  620. }
  621. if (is_array($fids)) {
  622. return array_unique($fids);
  623. }
  624. else {
  625. return array();
  626. }
  627. }
  628. /**
  629. * Implements hook_block_info();
  630. */
  631. function flag_lists_block_info() {
  632. return array(
  633. 'flag_lists' => array(
  634. 'info' => t('Lists'),
  635. 'cache' => DRUPAL_NO_CACHE,
  636. ),
  637. 'flag_lists_list' => array(
  638. 'info' => t('My lists'),
  639. 'cache' => DRUPAL_NO_CACHE,
  640. ),
  641. );
  642. }
  643. /**
  644. * Implements hook_block_configure().
  645. */
  646. function flag_lists_block_configure($delta = '') {
  647. $form = array();
  648. switch ($delta) {
  649. case 'flag_lists':
  650. $form = array(
  651. 'create_lists' => array(
  652. '#type' => 'checkbox',
  653. '#title' => t('Show link to add new list'),
  654. '#default_value' => variable_get('flag_lists_create_lists', 0),
  655. '#description' => t('Checking this adds a link to the create new list form.'),
  656. ),
  657. 'ops' => array(
  658. '#type' => 'checkbox',
  659. '#title' => t('Show edit and delete links'),
  660. '#default_value' => variable_get('flag_lists_ops', 0),
  661. '#description' => t('Checking this appends edit and delete links to each list name for users with access.'),
  662. ),
  663. 'include_flags' => array(
  664. '#type' => 'checkbox',
  665. '#title' => t('Include flag module flags'),
  666. '#default_value' => variable_get('flag_lists_include_flags', 0),
  667. '#description' => t('Checking this will append flag module flags to the list of lists.'),
  668. ),
  669. );
  670. break;
  671. }
  672. return $form;
  673. }
  674. /**
  675. * Implements hook_block_save().
  676. */
  677. function flag_lists_block_save($delta = '', $edit = array()) {
  678. switch ($delta) {
  679. case 'flag_lists':
  680. variable_set('flag_lists_create_lists', $edit['create_lists']);
  681. variable_set('flag_lists_ops', $edit['ops']);
  682. variable_set('flag_lists_include_flags', $edit['include_flags']);
  683. break;
  684. }
  685. }
  686. /**
  687. * Implements hook_block_view().
  688. */
  689. function flag_lists_block_view($delta = '') {
  690. $block = array();
  691. switch ($delta) {
  692. case 'flag_lists':
  693. if (user_access('create flag lists')) {
  694. $block = array(
  695. 'subject' => t('My lists'),
  696. 'content' => theme('flag_lists_list', array('node' => NULL, 'create' => variable_get('flag_lists_create_lists', 0), 'ops' =>variable_get('flag_lists_ops', 0), 'use_flags' => variable_get('flag_lists_include_flags', 0))),
  697. );
  698. }
  699. break;
  700. case 'flag_lists_list':
  701. if (user_access('create flag lists')) {
  702. global $user;
  703. $account = user_load($user->uid);
  704. $block = array(
  705. 'subject' => t('My lists'),
  706. 'content' => flag_lists_user_page($account),
  707. );
  708. }
  709. break;
  710. }
  711. return (!empty($block['content'])) ? $block : array();
  712. }
  713. /**
  714. * Implementation of hook_user_delete().
  715. */
  716. function flag_lists_user_delete($account) {
  717. // Remove personal flags by this user.
  718. $num_deleted = db_delete('flag_lists_flags')
  719. ->condition('uid', $account->uid)
  720. ->execute();
  721. }
  722. /**
  723. * Build a flag's messages.
  724. */
  725. function flag_lists_set_messages(&$flag) {
  726. // Get the parent flag. These are cached by the flag module.
  727. $pflag = flag_get_flag(NULL, $flag->pfid);
  728. $title = $flag->title;
  729. $lists_name = variable_get('flag_lists_name', t('list'));
  730. $flag->flag_short = $pflag->flag_short;
  731. $flag->flag_long = $pflag->flag_long;
  732. $flag->flag_message = $pflag->flag_message;
  733. $flag->unflag_short = $pflag->unflag_short;
  734. $flag->unflag_long = $pflag->unflag_long;
  735. $flag->unflag_message = $pflag->unflag_message;
  736. }
  737. /**
  738. * Implementation of hook_flag_access().
  739. *
  740. * Make sure a user can only see his/her own personal flags.
  741. */
  742. function flag_lists_flag_access($flag, $entity_id, $action, $account) {
  743. if (!empty($flag->module) && $flag->module == 'flag_lists') {
  744. switch ($action) {
  745. case 'flag':
  746. case 'unflag':
  747. $fid = db_select('flag_lists_flags', 'f')
  748. ->fields('f')
  749. ->condition('f.uid', $account->uid)
  750. ->execute()
  751. ->fetchField();
  752. if (!empty($fid)) {
  753. return array('flag_lists' => TRUE);
  754. }
  755. else {
  756. return array('flag_lists' => FALSE);
  757. }
  758. }
  759. }
  760. }
  761. /**
  762. * Implementation of hook_link().
  763. */
  764. // There may be a better way to keep flag lists out of the links, but this
  765. // works for now. @todo Find a better way to keep flags lists out of links.
  766. function flag_lists_link_alter(&$links, $node) {
  767. if (!variable_get('flag_lists_use_links', 1)) {
  768. foreach ($links as $name => $link) {
  769. if (stristr($name, 'flag-fl_')) {
  770. unset($links[$name]);
  771. }
  772. }
  773. }
  774. }
  775. /**
  776. * Implementation of hook_flag_alter().
  777. */
  778. function flag_lists_flag_alter(&$flag) {
  779. }
  780. /**
  781. * Implementation of hook_flag_delete().
  782. *
  783. * This is not in flag yet.
  784. */
  785. function flag_lists_flag_delete($flag) {
  786. // Template flag is being deleted. Clean up our tables.
  787. // Collect the sub-flag fids so we can delete counts and content records.
  788. $results = db_select('flag_lists_flags', 'f')
  789. ->fields('f', array('fid', 'name'))
  790. ->condition('pfid', $flag->fid)
  791. ->execute();
  792. foreach ($results as $fid) {
  793. db_delete('flag_lists_counts')
  794. ->condition('fid', $flag->fid)
  795. ->execute();
  796. db_delete('flag_lists_content')
  797. ->condition('fid', $flag->fid)
  798. ->execute();
  799. }
  800. // flag_lists_types uses the template flag name, not our own fid.
  801. db_delete('flag_lists_types')
  802. ->condition('name', $flag->name)
  803. ->execute();
  804. // Now delete the sub-flags.
  805. $num_deleted = db_delete('flag_lists_flags')
  806. ->condition('pfid', $flag->fid)
  807. ->execute();
  808. if (!empty($num_deleted)) {
  809. drupal_set_message(t('The template flag "@title" and all its sub-flags have been deleted.', array('@title' => $flag->title)));
  810. }
  811. }
  812. /**
  813. * Implementation of hook_views_api().
  814. */
  815. function flag_lists_views_api() {
  816. return array(
  817. 'api' => 2.0,
  818. 'path' => drupal_get_path('module', 'flag_lists') . '/includes',
  819. );
  820. }
  821. /**
  822. * Helper function to test if a flag is owned by the current user, or current
  823. * user can administer flags.
  824. */
  825. function flag_lists_is_owner($action, $name) {
  826. global $user;
  827. if (user_access('administer flags')) {
  828. return TRUE;
  829. }
  830. // If we don't have an fid, then we have the flag name.
  831. if (is_numeric($name)) {
  832. $query = db_select('flag_lists_flags', 'f')->condition('fid', $name);
  833. $query->addField('f', 'name');
  834. $name = $query->execute()->fetchField();
  835. }
  836. if (!user_access($action . ' own flag lists')) {
  837. return FALSE;
  838. }
  839. if (db_select('flag_lists_flags', 'f')->fields('f')->condition('f.name', $name)->condition('f.uid', $user->uid)->countQuery()->execute()->fetchField()) {
  840. return TRUE;
  841. }
  842. return FALSE;
  843. }
  844. /**
  845. * Get a single user's lists, and merge in flag module flags
  846. */
  847. function flag_lists_get_user_flags($content_type = NULL, $account = NULL, $use_flags = FALSE) {
  848. $flags = array();
  849. $lists = array();
  850. if (!isset($account)) {
  851. $account = $GLOBALS['user'];
  852. }
  853. // Get flag lists flags
  854. $query = db_select('flag_lists_flags', 'fl')
  855. ->fields('fl')
  856. ->condition('fl.uid', $account->uid);
  857. $query->leftJoin('flag', 'f', 'fl.pfid = f.fid');
  858. $query->leftJoin('flag_lists_types', 'ft', 'ft.name = f.name');
  859. $query->addField('ft', 'type');
  860. if ($content_type) {
  861. $query->condition('ft.type', $content_type);
  862. }
  863. $result = $query->execute();
  864. foreach ($result as $row) {
  865. if (!isset($lists[$row->name])) {
  866. $lists[$row->name] = flag_flag::factory_by_row($row);
  867. $lists[$row->name]->module = 'flag_lists';
  868. }
  869. else {
  870. $lists[$row->name]->types[] = $row->type;
  871. }
  872. }
  873. // Get regular flags.
  874. if ($use_flags) {
  875. $flags = flag_get_flags('node', $content_type, $account);
  876. // Strip out any list templates
  877. foreach ($flags as $key => $flag) {
  878. if (stristr($flag->name, 'fl_template') !== FALSE) {
  879. unset($flags[$key]);
  880. }
  881. }
  882. }
  883. $flags = array_merge($lists, $flags);
  884. return $flags;
  885. }
  886. /**
  887. * Theme function to return edit, delete links.
  888. *
  889. * @param $flag
  890. * The flag whose links are being built.
  891. */
  892. function theme_flag_lists_ops($variables) {
  893. $flag = $variables['flag'];
  894. $links = array(
  895. 'flags_edit' => array('title' => t('edit'), 'href' => 'flags/lists/edit/' . $flag->name, 'query' => drupal_get_destination()),
  896. 'flags_delete' => array('title' => t('delete'), 'href' => 'flags/lists/delete/' . $flag->name, 'query' => drupal_get_destination()),
  897. );
  898. return theme('links', array('links' => $links, 'attributes' => array('class' => 'flag_lists_ops')));
  899. }
  900. /**
  901. * Theme a list of lists
  902. *
  903. * @param $node
  904. * The listable node
  905. * @param boolean $create
  906. * Show the create list form.
  907. * @param boolean $ops
  908. * Show the edit / delete links for lists
  909. * @param boolean $use_flags
  910. * Show flags from the flag module
  911. * @return <type>
  912. */
  913. // @todo Separate out the code from the theming better.
  914. function theme_flag_lists_list($variables) {
  915. $node = $variables['node'];
  916. $create = $variables['create'];
  917. $ops = $variables['ops'];
  918. $use_flags = $variables['use_flags'];
  919. $items = array();
  920. // Make sure we have a node.
  921. if (is_object($node) && user_access('create flag lists')) {
  922. $content_type = $node->type;
  923. $entity_id = $node->nid;
  924. }
  925. // Or at least confirm we are on a node page and use has access.
  926. elseif (arg(0) == 'node' && is_numeric(arg(1)) && user_access('create flag lists')) {
  927. $entity_id = arg(1);
  928. $query = db_select('node')->condition('nid', $entity_id);
  929. $query->addField('node', 'type');
  930. $content_type = $query->execute()->fetchField();
  931. }
  932. else {
  933. return;
  934. }
  935. // Do we have a list template for this node type, or are we s
  936. if (!flag_lists_template_exists($content_type) && !$use_flags) {
  937. return;
  938. }
  939. global $user;
  940. if ($flags = flag_lists_get_user_flags($content_type, $user, $use_flags)) {
  941. // Build the list of lists for this node.
  942. foreach ($flags as $flag) {
  943. if ($flag->module == 'flag_lists') {
  944. $action = _flag_lists_is_flagged($flag, $entity_id, $user->uid, 0) ? 'unflag' : 'flag';
  945. }
  946. else {
  947. $action = $flag->is_flagged($entity_id) ? 'unflag' : 'flag';;
  948. }
  949. // Do we need the ops?
  950. if ($ops && $flag->module == 'flag_lists') {
  951. $ops_links = theme('flag_lists_ops', array('flag' => $flag));
  952. $link = $flag->theme($action, $entity_id) . $ops_links;
  953. }
  954. else {
  955. $link = $flag->theme($action, $entity_id);
  956. }
  957. // If it's a list, fix the link.
  958. if ($flag->module == 'flag_lists') {
  959. flag_lists_fix_link($link, $action);
  960. }
  961. $items[] = $link;
  962. }
  963. }
  964. if ($create && flag_lists_template_exists($content_type)) {
  965. $items[] = l(t('Make a new @name', array('@name' => variable_get('flag_lists_name', t('list')))), 'flag-lists/add/' . $content_type, array('query' => drupal_get_destination()));
  966. }
  967. // Return if nothing to display.
  968. if (empty($items) || !count($items)) {
  969. return;
  970. }
  971. drupal_add_css(drupal_get_path('module', 'flag_lists') . '/theme/flag_lists.css');
  972. return theme('item_list', array('items' => $items, 'type' => 'ul', 'attributes' => array('class' => 'flag-lists-links')));
  973. }
  974. // Do we still need this, and/or do we need our own cache?
  975. /**
  976. * Clear the flag cache.
  977. *
  978. * This is a less severe cache clear than provided by flag. All flag lists
  979. * users must be authorized, so we don't need to flush the page cache. For now,
  980. * flag lists titles won't be in the menu, so no need to clear that.
  981. */
  982. function _flag_lists_clear_cache() {
  983. // We're not using _flag_clear_cache because we probably don't need the menu
  984. // rebuild and don't need to clear the page cache.
  985. if (module_exists('views')) {
  986. views_invalidate_cache();
  987. }
  988. }
  989. /**
  990. * Update ALL flag lists with settings form values.
  991. */
  992. function flag_lists_rebuild() {
  993. $flags = flag_lists_get_flags();
  994. foreach ($flags as $flag) {
  995. flag_lists_set_messages($flag);
  996. $flag->link_type = 'toggle';
  997. flag_lists_save($flag);
  998. }
  999. }
  1000. /**
  1001. * Build array of all flag lists.
  1002. *
  1003. * @return If limit and header arguments are provided, the paged flags, otherwise
  1004. * an array of all flags.
  1005. */
  1006. function flag_lists_get_flags($limit = NULL, $header = NULL) {
  1007. $flags = array();
  1008. if ($limit) {
  1009. $query = db_select('flag_lists_flags', 'fl')
  1010. ->fields('fl')
  1011. ->groupBy('fl.fid')
  1012. ->extend('PagerDefault')
  1013. ->limit($limit);
  1014. $query->leftJoin('flag_types', 'ft', 'ft.fid = fl.pfid');
  1015. $query->addExpression('GROUP_CONCAT(ft.type)', 'types');
  1016. $result = $query->execute();
  1017. }
  1018. else {
  1019. $query = db_select('flag_lists_flags', 'fl')
  1020. ->fields('fl')
  1021. ->groupBy('fl.fid');
  1022. $query->leftJoin('flag_types', 'ft', 'ft.fid = fl.pfid');
  1023. $query->addExpression('GROUP_CONCAT(ft.type)', 'types');
  1024. $result = $query->execute();
  1025. }
  1026. foreach ($result as $row) {
  1027. $flags[$row->name] = flag_flag::factory_by_row($row);
  1028. $flags[$row->name]->types = explode(',', $row->types);
  1029. $flags[$row->name]->uid = $row->uid;
  1030. }
  1031. return $flags;
  1032. }
  1033. /**
  1034. * Get a specific flag.
  1035. *
  1036. * Using this instead of flag_get_flag() for performance.
  1037. */
  1038. function flag_lists_get_flag($fid) {
  1039. // If we don't have an fid, then we have the flag name.
  1040. if (!is_numeric($fid)) {
  1041. $query = db_select('flag_lists_flags')
  1042. ->condition('name', $fid);
  1043. $query->addField('flag_lists_flags', 'fid');
  1044. $fid = $query->execute()->fetchField();
  1045. }
  1046. $query = db_select('flag_lists_flags', 'fl')
  1047. ->fields('fl')
  1048. ->condition('fl.fid', $fid);
  1049. $query->leftJoin('flag_types', 'ft', 'ft.fid = fl.pfid');
  1050. $query->addField('ft', 'type');
  1051. $result = $query->execute();
  1052. foreach ($result as $row) {
  1053. if (!isset($flag->name)) {
  1054. $flag = flag_flag::factory_by_row($row);
  1055. }
  1056. else {
  1057. $flag->types[] = $row->type;
  1058. }
  1059. }
  1060. return $flag;
  1061. }
  1062. /**
  1063. * Get all flagged content in a flag.
  1064. *
  1065. * Using this instead of flag_get_flagged_content() because we need to make sure that we use flag_lists_get_flags()
  1066. *
  1067. * @param
  1068. * The flag name for which to retrieve flagged content.
  1069. */
  1070. function flag_lists_get_flagged_content($fid, $uid) {
  1071. $return = array();
  1072. $flag = flag_lists_get_flag($fid);
  1073. $result = db_select('flag_lists_content', 'f')
  1074. ->fields('f')
  1075. ->condition('f.fid', $flag->fid)
  1076. ->condition('f.uid', $uid)
  1077. ->execute();
  1078. foreach ($result as $row) {
  1079. $return[] = $row;
  1080. }
  1081. return $return;
  1082. }
  1083. /**
  1084. * Implementation of hook_flag_link().
  1085. *
  1086. * When Flag uses a link type provided by this module, it will call this
  1087. * implementation of hook_flag_link(). It returns a single link's attributes,
  1088. * using the same structure as hook_link(). Note that "title" is provided by
  1089. * the Flag configuration if not specified here.
  1090. *
  1091. * @param $flag
  1092. * The full flag object of for the flag link being generated.
  1093. * @param $action
  1094. * The action this link will perform. Either 'flag' or 'unflag'.
  1095. * @param $entity_id
  1096. * The ID of the node, comment, user, or other object being flagged.
  1097. * @return
  1098. * An array defining properties of the link.
  1099. */
  1100. function flag_lists_flag_link($flag, $action, $entity_id) {
  1101. return array();
  1102. }
  1103. /**
  1104. * Implementation of hook_flag_link_types().
  1105. */
  1106. function flag_lists_flag_link_types() {
  1107. return array(
  1108. 'fl_template' => array(
  1109. 'title' => t('Flag Lists toggle'),
  1110. 'description' => t('If you are creating a Flag lists template flag, you must select this link type.'),
  1111. ),
  1112. );
  1113. }
  1114. function flag_lists_flag_default_flags($name = 'fl_template') {
  1115. // return array(
  1116. // array(
  1117. // 'api_version' => 2,
  1118. // 'name' => $name,
  1119. // 'module' => 'flag_lists',
  1120. // 'content_type' => 'node',
  1121. // 'global' => 0,
  1122. // 'show_on_page' => 0,
  1123. // 'show_on_teaser' => 0,
  1124. // 'show_on_form' => 0,
  1125. // // The following UI labels aren't wrapped in t() because they are written
  1126. // // to the DB in English. They are passed to t() later, thus allowing for
  1127. // // multilingual sites.
  1128. // 'title' => 'Flag lists template',
  1129. // 'flag_short' => 'Add to your [flag_lists:title] [flag_lists:term]',
  1130. // 'flag_long' => 'Add this post to your [flag_lists:title] [flag_lists:term]',
  1131. // 'flag_message' => 'This post has been added to your [flag_lists:title] [flag_lists:term]',
  1132. // 'unflag_short' => 'Remove this from your [flag_lists:title] [flag_lists:term]',
  1133. // 'unflag_long' => 'Remove this post from your [flag_lists:title] [flag_lists:term]',
  1134. // 'unflag_message' => 'This post has been removed from your [flag_lists:title] [flag_lists:term]',
  1135. // 'types' => array(),
  1136. // 'link_type' => 'toggle',
  1137. // ),
  1138. // );
  1139. $flags = array();
  1140. // Exported flag: "Flag lists template".
  1141. $flags['fl_template'] = array(
  1142. 'entity_type' => 'node',
  1143. 'title' => 'Flag lists template',
  1144. 'global' => 0,
  1145. 'types' => array(),
  1146. 'flag_short' => 'Add to your [flag_lists:title] [flag_lists:term]',
  1147. 'flag_long' => 'Add this post to your [flag_lists:title] [flag_lists:term]',
  1148. 'flag_message' => 'This post has been added to your [flag_lists:title] [flag_lists:term]',
  1149. 'unflag_short' => 'Remove this from your [flag_lists:title] [flag_lists:term]',
  1150. 'unflag_long' => 'Remove this post from your [flag_lists:title] [flag_lists:term]',
  1151. 'unflag_message' => 'This post has been removed from your [flag_lists:title] [flag_lists:term]',
  1152. 'unflag_denied_text' => '',
  1153. 'link_type' => 'toggle',
  1154. 'weight' => 0,
  1155. 'api_version' => 3,
  1156. 'module' => 'flag_lists',
  1157. 'show_on_page' => 0,
  1158. 'show_on_teaser' => 0,
  1159. 'show_on_form' => 0,
  1160. 'status' => FALSE,
  1161. 'import_roles' => array(
  1162. 'flag' => array(),
  1163. 'unflag' => array(),
  1164. ),
  1165. );
  1166. return $flags;
  1167. }
  1168. /**
  1169. * Saves a flag to the database. It is a wrapper around update($flag) and insert($flag).
  1170. */
  1171. function flag_lists_save(&$flag, $account = NULL) {
  1172. if (!isset($account)) {
  1173. $account = $GLOBALS['user'];
  1174. }
  1175. if (isset($flag->fid)) {
  1176. flag_lists_update($flag);
  1177. $flag->is_new = FALSE;
  1178. module_invoke_all('flag_lists', $flag, $account);
  1179. }
  1180. else {
  1181. flag_lists_insert($flag);
  1182. $flag->is_new = TRUE;
  1183. module_invoke_all('flag_lists', $flag, $account);
  1184. }
  1185. // Clear the page cache for anonymous users.
  1186. // cache_clear_all('*', 'cache_page', TRUE);
  1187. }
  1188. /**
  1189. * Saves an existing flag to the database. Better use save($flag).
  1190. */
  1191. function flag_lists_update($flag) {
  1192. $num_updated = db_update('flag_lists_flags')
  1193. ->fields(array(
  1194. 'title' => $flag->title,
  1195. 'name' => $flag->name,
  1196. 'options' => $flag->get_serialized_options($flag),
  1197. ))
  1198. ->condition('fid', $flag->fid)
  1199. ->execute();
  1200. }
  1201. /**
  1202. * Saves a new flag to the database. Better use save($flag).
  1203. */
  1204. function flag_lists_insert($flag) {
  1205. $flag->fid = db_insert('flag_lists_flags')
  1206. ->fields(array(
  1207. 'pfid' => $flag->pfid,
  1208. 'uid' => $flag->uid,
  1209. 'entity_type' => $flag->entity_type,
  1210. 'name' => $flag->name,
  1211. 'title' => $flag->title,
  1212. 'options' => $flag->get_serialized_options($flag),
  1213. ))
  1214. ->execute();
  1215. $flag->name = 'flag_lists_' . $flag->uid . '_' . $flag->fid;
  1216. flag_lists_update($flag);
  1217. }
  1218. /**
  1219. * Delete a flag_lists flag.
  1220. *
  1221. */
  1222. function flag_lists_fl_delete($flag, $account = NULL) {
  1223. if (!isset($account)) {
  1224. $account = $GLOBALS['user'];
  1225. }
  1226. db_delete('flag_lists_counts')->condition('fid', $flag->fid)->execute();
  1227. db_delete('flag_lists_content')->condition('fid', $flag->fid)->execute();
  1228. db_delete('flag_lists_flags')->condition('fid', $flag->fid)->execute();
  1229. $flag->is_deleted = TRUE;
  1230. module_invoke_all('flag_lists', $flag, $account);
  1231. _flag_lists_clear_cache();
  1232. drupal_set_message(t('The @name @title has been deleted.', array('@name' => variable_get('flag_lists_name', t('list')), '@title' => $flag->title)));
  1233. }
  1234. /**
  1235. * Menu callback for (un)flagging a node.
  1236. *
  1237. * Used both for the regular callback as well as the JS version. We use this
  1238. * instead of the flag module's because our flags are not in the flags table.
  1239. */
  1240. function flag_lists_page($action = NULL, $flag_name = NULL, $entity_id = NULL) {
  1241. global $user;
  1242. // Shorten up the variables that affect the behavior of this page.
  1243. $js = isset($_REQUEST['js']);
  1244. $token = $_REQUEST['token'];
  1245. // Specifically $_GET to avoid getting the $_COOKIE variable by the same key.
  1246. $has_js = isset($_GET['has_js']);
  1247. // Check the flag token, then perform the flagging.
  1248. if (!flag_check_token($token, $entity_id)) {
  1249. $error = t('Bad token. You seem to have followed an invalid link.');
  1250. }
  1251. elseif ($user->uid == 0 && !$has_js) {
  1252. $error = t('You must have JavaScript and cookies enabled in your browser to flag content.');
  1253. }
  1254. else {
  1255. if (empty($flag_name) || !($flag = flag_lists_get_flag($flag_name))) {
  1256. // Flag does not exist.
  1257. $error = t('You are not allowed to flag, or unflag, this content.');
  1258. }
  1259. // Identify it as ours.
  1260. $flag->module = 'flag_lists';
  1261. flag_lists_do_flag($flag, $action, $entity_id);
  1262. }
  1263. // If an error was received, set a message and exit.
  1264. if (isset($error)) {
  1265. if ($js) {
  1266. drupal_add_http_header('Content-Type', 'text/javascript; charset=utf-8');
  1267. print drupal_to_js(array(
  1268. 'status' => FALSE,
  1269. 'errorMessage' => $error,
  1270. ));
  1271. exit;
  1272. }
  1273. else {
  1274. drupal_set_message($error);
  1275. drupal_access_denied();
  1276. return;
  1277. }
  1278. }
  1279. // If successful, return data according to the request type.
  1280. if ($js) {
  1281. drupal_add_http_header('Content-Type', 'text/javascript; charset=utf-8');
  1282. // $flag = flag_lists_get_flag($flag_name);
  1283. // $flag->link_type = 'toggle';
  1284. $sid = flag_get_sid($user->uid);
  1285. $new_action = _flag_lists_is_flagged($flag, $entity_id, $user->uid, $sid) ? 'unflag' : 'flag';
  1286. $new_link = $flag->theme($new_action, $entity_id, array("after_flagging" => TRUE));
  1287. flag_lists_fix_link($new_link, $new_action);
  1288. drupal_json_output(array(
  1289. 'status' => TRUE,
  1290. 'newLink' => $new_link,
  1291. // Further information for the benefit of custom JavaScript event handlers:
  1292. 'contentId' => $entity_id,
  1293. 'contentType' => $flag->content_type,
  1294. 'flagName' => $flag->name,
  1295. 'flagStatus' => $action,
  1296. ));
  1297. exit;
  1298. }
  1299. else {
  1300. $flag = flag_lists_get_flag($flag->fid);
  1301. drupal_set_message($flag->get_label($action . '_message', $entity_id));
  1302. drupal_goto();
  1303. }
  1304. }
  1305. function flag_lists_fix_link(&$link, $action) {
  1306. // This is a hack to let us use our own flag/unflag callbacks without having
  1307. // to override $flag->theme and creating our own flag_link type.
  1308. $link = str_replace('/flag/' . $action . '/', '/flag-lists/' . $action . '/', $link);
  1309. }
  1310. /**
  1311. * Flags, or unflags, an item.
  1312. *
  1313. * @param $action
  1314. * Either 'flag' or 'unflag'.
  1315. * @param $entity_id
  1316. * The ID of the item to flag or unflag.
  1317. * @param $account
  1318. * The user on whose behalf to flag. Leave empty for the current user.
  1319. * @param $skip_permission_check
  1320. * Flag the item even if the $account user doesn't have permission to do so.
  1321. * @return
  1322. * FALSE if some error occured (e.g., user has no permission, flag isn't
  1323. * applicable to the item, etc.), TRUE otherwise.
  1324. */
  1325. function flag_lists_do_flag($flag, $action, $entity_id, $account = NULL, $skip_permission_check = FALSE) {
  1326. if (!isset($account)) {
  1327. $account = $GLOBALS['user'];
  1328. }
  1329. if (!$account) {
  1330. return FALSE;
  1331. }
  1332. if (!$skip_permission_check) {
  1333. if (!$flag->access($entity_id, $action, $account)) {
  1334. // User has no permission to flag/unflag this object.
  1335. return FALSE;
  1336. }
  1337. }
  1338. else {
  1339. // We are skipping permission checks. However, at a minimum we must make
  1340. // sure the flag applies to this content type:
  1341. if (!$flag->applies_to_content_id($entity_id)) {
  1342. return FALSE;
  1343. }
  1344. }
  1345. // Clear various caches; We don't want code running after us to report
  1346. // wrong counts or false flaggings.
  1347. // flag_get_counts(NULL, NULL, TRUE);
  1348. // flag_get_user_flags(NULL, NULL, NULL, NULL, TRUE);
  1349. // Find out which user id to use.
  1350. $uid = $flag->global ? 0 : $account->uid;
  1351. $sid = flag_get_sid($uid);
  1352. // Anonymous users must always have a session id.
  1353. if ($sid == 0 && $account->uid == 0) {
  1354. return FALSE;
  1355. }
  1356. // Perform the flagging or unflagging of this flag. We invoke hook_flag here
  1357. // because we do our own flagging.
  1358. $flagged = _flag_lists_is_flagged($flag, $entity_id, $uid, $sid);
  1359. if ($action == 'unflag') {
  1360. if ($flagged) {
  1361. $fcid = _flag_lists_unflag($flag, $entity_id, $uid, $sid);
  1362. module_invoke_all('flag', 'unflag', $flag, $entity_id, $account, $fcid);
  1363. }
  1364. }
  1365. elseif ($action == 'flag') {
  1366. if (!$flagged) {
  1367. $fcid = _flag_lists_flag($flag, $entity_id, $uid, $sid);
  1368. module_invoke_all('flag', 'flag', $flag, $entity_id, $account, $fcid);
  1369. }
  1370. }
  1371. return TRUE;
  1372. }
  1373. /**
  1374. * Returns TRUE if a certain user has flagged this content.
  1375. *
  1376. *
  1377. * This method is similar to is_flagged() except that it does direct SQL and
  1378. * doesn't do caching. Use it when you want to not affect the cache, or to
  1379. * bypass it.
  1380. *
  1381. */
  1382. function _flag_lists_is_flagged($flag, $entity_id, $uid, $sid) {
  1383. $query = db_select('flag_lists_content')
  1384. ->condition('fid', $flag->fid)
  1385. ->condition('uid', $uid)
  1386. ->condition('sid', $sid)
  1387. ->condition('entity_id', $entity_id);
  1388. $query->addField('flag_lists_content', 'fid');
  1389. return $query->execute()->fetchField();
  1390. }
  1391. /**
  1392. * A low-level method to flag content.
  1393. *
  1394. * You probably shouldn't call this raw private method: call the
  1395. * flag_lists_do_flag() function instead.
  1396. *
  1397. */
  1398. function _flag_lists_flag($flag, $entity_id, $uid, $sid) {
  1399. $fcid = db_insert('flag_lists_content')
  1400. ->fields(array(
  1401. 'fid' => $flag->fid,
  1402. 'entity_type' => $flag->entity_type,
  1403. 'entity_id' => $entity_id,
  1404. 'uid' => $uid,
  1405. 'sid' => $sid,
  1406. 'timestamp' => REQUEST_TIME,
  1407. ))
  1408. ->execute();
  1409. _flag_lists_update_count($flag, $entity_id);
  1410. return $fcid;
  1411. }
  1412. /**
  1413. * A low-level method to unflag content.
  1414. *
  1415. * You probably shouldn't call this raw private method: call the
  1416. * flag_lists_do_flag() function instead.
  1417. *
  1418. */
  1419. function _flag_lists_unflag($flag, $entity_id, $uid, $sid) {
  1420. $query = db_select('flag_lists_content')
  1421. ->condition('fid', $flag->fid)
  1422. ->condition('entity_id', $entity_id)
  1423. ->condition('uid', $uid)
  1424. ->condition('sid', $sid);
  1425. $query->addField('flag_lists_content', 'fcid');
  1426. $fcid = $query->execute()->fetchField();
  1427. if ($fcid) {
  1428. db_delete('flag_lists_content')
  1429. ->condition('fcid', $fcid)
  1430. ->execute();
  1431. _flag_lists_update_count($flag, $entity_id);
  1432. }
  1433. return $fcid;
  1434. }
  1435. /**
  1436. * Updates the flag count for this content
  1437. */
  1438. function _flag_lists_update_count($flag, $entity_id) {
  1439. $count = db_select('flag_lists_content', 'f')
  1440. ->fields('f')
  1441. ->condition('fid', $flag->fid)
  1442. ->condition('entity_id', $entity_id)
  1443. ->countQuery()
  1444. ->execute()
  1445. ->fetchField();
  1446. if (empty($count)) {
  1447. $num_deleted = db_delete('flag_lists_counts')
  1448. ->condition('fid', $flag->fid)
  1449. ->condition('entity_id', $entity_id)
  1450. ->execute();
  1451. }
  1452. else {
  1453. $num_updated = db_update('flag_lists_counts')
  1454. ->fields(array(
  1455. 'count' => $count,
  1456. ))
  1457. ->condition('fid', $flag->fid)
  1458. ->condition('entity_id', $entity_id)
  1459. ->execute();
  1460. if (empty($num_updated)) {
  1461. db_insert('flag_lists_counts')
  1462. ->fields(array(
  1463. 'fid' => $flag->fid,
  1464. 'entity_type' => $flag->entity_type,
  1465. 'entity_id' => $entity_id,
  1466. 'count' => $count,
  1467. ))
  1468. ->execute();
  1469. }
  1470. }
  1471. }
  1472. /**
  1473. * Checks for a list template for a content type.
  1474. */
  1475. function flag_lists_template_exists($type) {
  1476. $query = db_select('flag_lists_types')
  1477. ->condition('type', $type);
  1478. $query->addField('flag_lists_types', 'type');
  1479. $exists = $query->execute()->fetchField();
  1480. if (!empty($exists)) {
  1481. return TRUE;
  1482. }
  1483. return FALSE;
  1484. }
  1485. /**
  1486. * Checks for a list title by node type.
  1487. */
  1488. function flag_lists_title_exists($title, $type) {
  1489. return db_query("SELECT COUNT(flf.fid) FROM {flag_lists_flags} flf LEFT JOIN {flag_types} ft ON flf.pfid=ft.fid WHERE flf.title=:title AND ft.type=:type AND flf.uid=:uid", array(':title' => $title, ':type' => $type, ':uid' => $GLOBALS['user']->uid))->fetchField();
  1490. }
  1491. /**
  1492. * Get a list of template flag names.
  1493. */
  1494. function flag_lists_get_templates() {
  1495. $templates = array();
  1496. $result = db_select('flag_lists_types', 'f')
  1497. ->fields('f', array(
  1498. 'name'
  1499. ))
  1500. ->distinct()
  1501. ->execute();
  1502. foreach ($result as $obj) {
  1503. $templates[] = flag_get_flag($obj->name);
  1504. }
  1505. return $templates;
  1506. }
  1507. /**
  1508. * Implements hook_token_info().
  1509. */
  1510. function flag_lists_token_info() {
  1511. $type = array(
  1512. 'name' => t('Flag lists'),
  1513. 'description' => t('Tokens related to flag lists.'),
  1514. 'needs-data' => 'flag_lists',
  1515. );
  1516. $flag_lists['term'] = array(
  1517. 'name' => t("Term"),
  1518. 'description' => t("The terminology used to name the lists, such as list, wishlist, favorites, etc."),
  1519. );
  1520. $flag_lists['title'] = array(
  1521. 'name' => t("Title"),
  1522. 'description' => t("The title of the list."),
  1523. );
  1524. return array(
  1525. 'types' => array('flag_lists' => $type),
  1526. 'tokens' => array('flag_lists' => $flag_lists),
  1527. );
  1528. }
  1529. /**
  1530. * Implements hook_tokens().
  1531. */
  1532. function flag_lists_tokens($type, $tokens, array $data = array(), array $options = array()) {
  1533. $replacements = array();
  1534. if ($type == 'flag_lists' && !empty($data['flag_lists'])) {
  1535. $flag_list = $data['flag_lists'];
  1536. foreach ($tokens as $name => $original) {
  1537. switch ($name) {
  1538. case 'title':
  1539. $replacements[$original] = $flag_list->title;
  1540. break;
  1541. case 'term':
  1542. $replacements[$original] = variable_get('flag_lists_name', t('list'));
  1543. break;
  1544. }
  1545. }
  1546. }
  1547. return $replacements;
  1548. }
  1549. /**
  1550. * Preprocess link title and text for the flag.tpl.php
  1551. *
  1552. * This seems to be the only place to do this
  1553. */
  1554. function flag_lists_preprocess_flag(&$variables) {
  1555. if (module_exists('token') && !empty($variables['flag']->module) && $variables['flag']->module == 'flag_lists') {
  1556. if (!empty($variables['link_text'])) {
  1557. $variables['link_text'] = token_replace($variables['link_text'], array('flag_lists' => $variables['flag']));
  1558. }
  1559. if (!empty($variables['link_title'])) {
  1560. $variables['link_title'] = token_replace($variables['link_title'], array('flag_lists' => $variables['flag']));
  1561. }
  1562. if (!empty($variables['message_text'])) {
  1563. $variables['message_text'] = token_replace($variables['message_text'], array('flag_lists' => $variables['flag']));
  1564. }
  1565. }
  1566. }
  1567. /**
  1568. * Implements hook_views_form_substitutions().
  1569. */
  1570. function flag_lists_views_form_substitutions() {
  1571. // Views check_plains the column label, so Flag lists needs to do the same
  1572. // in order for the replace operation to succeed.
  1573. $select_all_placeholder = check_plain('<!--flag-lists-ops-select-all-->');
  1574. $select_all = array(
  1575. '#type' => 'checkbox',
  1576. '#default_value' => FALSE,
  1577. '#attributes' => array('class' => array('flo-table-select-all')),
  1578. );
  1579. return array(
  1580. $select_all_placeholder => drupal_render($select_all),
  1581. );
  1582. }