materio_flag.module 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. <?php
  2. /**
  3. * @file
  4. * The Flag bookmark module.
  5. *
  6. * This module creates a default Flag when enabled.
  7. *
  8. */
  9. /**
  10. * Implements hook_permission().
  11. */
  12. function materio_flag_permission() {
  13. return array(
  14. 'access mybookmarks block' => array(
  15. 'title' => t('Show my bookmarks block'),
  16. 'description' => t('access own bookmarks block'),
  17. ),
  18. );
  19. }
  20. /**
  21. * Implements hook_menu().
  22. */
  23. function materio_flag_menu() {
  24. $items = array();
  25. $base = array(
  26. 'type' => MENU_CALLBACK,
  27. 'file' => 'materio_flag.pages.inc',
  28. );
  29. $items['materioflag/refresh/block/bookmarks'] = $base+array(
  30. 'access arguments' => array('flag bookmarks'),
  31. 'page callback' => 'materio_flag_refresh_block',
  32. 'page arguments' => array(3),
  33. );
  34. $items['bookmarks'] = $base+array(
  35. 'access arguments' => array('flag bookmarks'),
  36. 'page callback' => 'materio_flag_user_bookmarks',
  37. 'page arguments' => array(),
  38. );
  39. $items['materioflag/refresh/block/lists'] = $base+array(
  40. 'access arguments' => array('create flag lists'),
  41. 'page callback' => 'materio_flag_refresh_block',
  42. 'page arguments' => array(3),
  43. );
  44. $items['materioflag/createlist/form/%'] = $base+array(
  45. 'access arguments' => array('create flag lists'),
  46. 'access callback' => 'user_access',
  47. 'page callback' => 'materio_flag_createlist_form',
  48. 'page arguments' => array(3),
  49. );
  50. $items['materioflag/editlistform/%/%'] = $base+array(
  51. 'access arguments' => array('create flag lists'),
  52. 'access callback' => 'user_access',
  53. 'page callback' => 'materio_flag_editlist_form',
  54. 'page arguments' => array(2,3),
  55. );
  56. $items['materioflag/editlist/%/%'] = $base+array(
  57. 'access arguments' => array('create flag lists'),
  58. 'access callback' => 'user_access',
  59. 'page callback' => 'materio_flag_edit_list',
  60. 'page arguments' => array(2,3),
  61. );
  62. $items['materioflag/deletelist/%'] = $base+array(
  63. 'access arguments' => array('create flag lists'),
  64. 'access callback' => 'user_access',
  65. 'page callback' => 'materio_flag_delete_list',
  66. 'page arguments' => array(2,3),
  67. );
  68. $items['materioflag/nodelinks'] = $base+array(
  69. 'access arguments' => array('create flag lists'),
  70. 'access callback' => 'user_access',
  71. 'page callback' => 'materio_flag_nodelinks',
  72. // 'page arguments' => array(3),
  73. );
  74. $items['lists/%'] = $base+array(
  75. 'access arguments' => array('view flag lists'),
  76. 'access callback' => 'user_access',
  77. 'page callback' => 'materio_flag_user_lists',
  78. 'page arguments' => array(1),
  79. );
  80. $items['materioflag/ajax/list/%'] = $base+array(
  81. 'page callback' => 'materio_flag_ajax_list',
  82. 'access callback' => 'materio_access_ajax_flags',
  83. 'page arguments' => array(3, 4),
  84. );
  85. return $items;
  86. }
  87. function materio_access_ajax_flags(){
  88. return user_access('create flag lists') || user_access('flag bookmarks');
  89. }
  90. /**
  91. * Implements hook_menu_alter().
  92. */
  93. function materio_flag_menu_alter(&$items) {
  94. // Example - disable the page at node/add
  95. /*
  96. *$items['node/add']['access callback'] = FALSE;
  97. */
  98. // disable tabs on user page
  99. $items['user/%user/flags/lists']['access callback'] = false;
  100. $items['user/%user/flags/lists/%']['access callback'] = false;
  101. }
  102. /**
  103. * Implements hook_block_info().
  104. */
  105. function materio_flag_block_info() {
  106. $blocks['materio_flag_mybookmarks'] = array(
  107. 'info' => t('My bookmarks'),
  108. 'cache' => DRUPAL_NO_CACHE
  109. );
  110. $blocks['materio_flag_mylists'] = array(
  111. 'info' => t('My Materio flag lists'),
  112. 'cache' => DRUPAL_NO_CACHE
  113. );
  114. $blocks['materio_flag_mylists_nav'] = array(
  115. 'info' => t('My Materio flag lists navigation'),
  116. 'cache' => DRUPAL_NO_CACHE
  117. );
  118. return $blocks;
  119. }
  120. /**
  121. * Implements hook_block_view().
  122. */
  123. function materio_flag_block_view($delta = '') {
  124. global $user;
  125. $block = array();
  126. switch ($delta) {
  127. case 'materio_flag_mybookmarks':
  128. if(user_access('access mybookmarks block')){
  129. $userflags = flag_get_user_flags('node');
  130. //dsm($userflags, 'userflags');
  131. if(isset($userflags['bookmarks'])){
  132. $userbookmarks = array();
  133. foreach ($userflags['bookmarks'] as $nid => $flag) {
  134. $userbookmarks[] = node_load($nid);
  135. }
  136. // TODO: put this title generation on a theme function
  137. $subject = '<span class="listname">'.t('My bookmarks (@len) ', array("@len"=>count($userbookmarks))).'</span>';
  138. $block['subject'] = $subject . l('<i class="icon-resize-full"></i>', 'bookmarks', array('html'=>true, 'attributes' => array('class' => array('open-list')),)); //
  139. $block['content'] = theme('materio_flag_mybookmarks_block', array("bookmarks"=>$userbookmarks, "viewmode"=>"bookmark"));
  140. }else{
  141. $block['subject'] = t('My bookmarks');
  142. $block['content'] = t('No bookmarks yet. Add bookmarks on clicking on results star');
  143. }
  144. drupal_add_js(drupal_get_path('module', 'materio_flag').'/js/materio_flag.min.js');
  145. }
  146. break;
  147. case 'materio_flag_mylists':
  148. if(user_access('create flag lists')){
  149. $flags = flag_lists_get_user_flags(NULL, $user);
  150. // $userflags = flag_get_user_flags('node');
  151. // dsm($userflags, 'userflags');
  152. // dsm($flags, 'flags');
  153. foreach ($flags as $name => $flag) {
  154. $flag->path = url('lists/'.$flag->fid);
  155. $flaged_content = flag_lists_get_flagged_content($flag->fid, $user->uid);
  156. // dsm($flaged_content, 'flaged_content');
  157. $fcn = array();
  158. foreach ($flaged_content as $entity) {
  159. if($entity->entity_type == 'node'){
  160. $node = node_load($entity->entity_id);
  161. // dsm($node, 'node');
  162. // $node->flag_names[] = $name;
  163. $fcn[] = $node;
  164. }
  165. }
  166. $lists[$name] = array(
  167. 'list' => $flag,
  168. 'content' => $fcn,
  169. );
  170. }
  171. if(isset($lists)){
  172. // $block['subject'] = t('My !listname'.'s', array('!listname'=>t(variable_get('flag_lists_name', 'list'))));
  173. $block['subject'] = t('My '.variable_get('flag_lists_name', 'list').'s');
  174. $block['content'] = theme('materio_flag_mylists_block', array("lists"=>$lists, "viewmode"=>"bookmark"));
  175. // $block['content'] = theme('flag_lists_user_page', array('uid' => $user->uid));
  176. }else{
  177. $block['subject'] = t('My '. variable_get('flag_lists_name', 'list') .'s');
  178. $block['content'] = t('No !listname yet. Add !listname on clicking on results star', array('!listname'=>variable_get('flag_lists_name', 'list')));
  179. }
  180. drupal_add_js(drupal_get_path('module', 'materio_flag').'/js/materio_flag.min.js');
  181. }
  182. break;
  183. case 'materio_flag_mylists_nav':
  184. if(user_access('create flag lists')){
  185. $flags = flag_lists_get_user_flags(NULL, $user);
  186. foreach ($flags as $name => $flag) {
  187. $flaged_content = flag_lists_get_flagged_content($flag->fid, $user->uid);
  188. $flag->flaged_content = $flaged_content;
  189. $flags[$name] = $flag;
  190. }
  191. // if($flags){
  192. // $block['subject'] = t('My !listname'.'s', array('!listname'=>variable_get('flag_lists_name', 'list')));
  193. $block['subject'] = t('My '.variable_get('flag_lists_name', 'list').'s');
  194. $block['content'] = theme('materio_flag_mylists_nav_block', array("flags"=>$flags));
  195. // $block['content'] = theme('flag_lists_user_page', array('uid' => $user->uid));
  196. // }
  197. # what happend if no flags yet
  198. // else{
  199. // $block['subject'] = t('My '.variable_get('flag_lists_name', 'list').'s');
  200. // // $block['content'] = t('No !listname yet. Add !listname on clicking on results star', array('!listname'=>variable_get('flag_lists_name', 'list')));
  201. // }
  202. }
  203. break;
  204. }
  205. return $block;
  206. }
  207. /**
  208. * Implements hook_entity_info_alter().
  209. */
  210. function materio_flag_entity_info_alter(&$entity_info) {
  211. $entity_info['node']['view modes']['bookmark'] = array(
  212. 'label' => t('Bookmark'),
  213. 'custom settings' => TRUE,
  214. );
  215. }
  216. /**
  217. * Implements hook_entity_view().
  218. *
  219. * Note this is broken for taxonomy terms. @see http://drupal.org/node/1067120
  220. */
  221. function materio_flag_entity_view($entity, $type, $view_mode, $langcode) {
  222. if($type == 'node'){
  223. if(user_access('create flag lists') && $view_mode != 'print'){
  224. $entity->content['flaglistslinks'] = materio_flag_get_entity_links($entity, $type, $view_mode);
  225. drupal_add_css(drupal_get_path('module', 'flag') . '/theme/flag.css');
  226. drupal_add_js(drupal_get_path('module', 'flag') . '/theme/flag.js');
  227. // Do we have a list template for this node type, or are we s
  228. if (flag_lists_template_exists($entity->type)) {
  229. global $user;
  230. if ($flags = flag_lists_get_user_flags($entity->type, $user)) {
  231. foreach ($flags as $flag) {
  232. // dsm($flag, 'flag');
  233. if ( ($flag->module == 'flag_lists' && _flag_lists_is_flagged($flag, $entity->nid, $user->uid, 0)) || $flag->is_flagged($entity->nid) ) {
  234. $entity->flags[] = $flag;
  235. }
  236. }
  237. }
  238. }
  239. }
  240. }
  241. }
  242. function materio_flag_get_entity_links($entity, $type, $view_mode = null){
  243. // dsm($entity, 'entity');
  244. // Do we have a list template for this node type, or are we s
  245. if (!flag_lists_template_exists($entity->type)) {
  246. return;
  247. }
  248. global $user;
  249. $links = array();
  250. # if flag name is provided we are on flaglists content list (block mylists)
  251. if($view_mode == 'bookmark'){
  252. // TODO: define view mode in settings
  253. // if (isset($entity->flag_names) && $flags = flag_lists_get_user_flags($entity->type, $user)) {
  254. // // dsm($flags, 'flags');
  255. // // TODO: limit flag link by current flag list
  256. // foreach ($flags as $flag) {
  257. // //dsm($flag->name, 'flag');
  258. // if(in_array($flag->name, $entity->flag_names)){
  259. // if ($flag->module == 'flag_lists') {
  260. // $action = _flag_lists_is_flagged($flag, $entity->nid, $user->uid, 0) ? 'unflag' : 'flag';
  261. // } else {
  262. // $action = $flag->is_flagged($entity->nid) ? 'unflag' : 'flag';
  263. // }
  264. // $flag->module = 'materio_flag';
  265. // $link = $flag->theme($action, $entity->nid);
  266. // // If it's a list, fix the link.
  267. // if ($flag->module == 'flag_lists') {
  268. // flag_lists_fix_link($link, $action);
  269. // }
  270. // $items[] = array(
  271. // 'data' => $link,
  272. // 'class' => array('flag-lists-link', $action.'-action'),
  273. // );
  274. // // array_splice($entity->flag_names, array_search($flag->name, $entity->flag_names), 1);
  275. // // dsm($entity->flag_names, 'entity->flag_name');
  276. // // break;
  277. // }
  278. // }
  279. // }
  280. #normal display
  281. }else{
  282. if ($flags = flag_lists_get_user_flags($entity->type, $user)) {
  283. // dsm($flags, 'flags');
  284. // Build the list of lists for this node.
  285. foreach ($flags as $flag) {
  286. // dsm($flag, 'flag');
  287. if ($flag->module == 'flag_lists') {
  288. $action = _flag_lists_is_flagged($flag, $entity->nid, $user->uid, 0) ? 'unflag' : 'flag';
  289. }
  290. else {
  291. $action = $flag->is_flagged($entity->nid) ? 'unflag' : 'flag';;
  292. }
  293. $link = $flag->theme($action, $entity->nid);
  294. // If it's a list, fix the link.
  295. if ($flag->module == 'flag_lists') {
  296. flag_lists_fix_link($link, $action);
  297. }
  298. $items[] = array(
  299. 'data' => $link,
  300. 'class' => array('flag-lists-link', $action.'-action', 'fid-'.$flag->fid),
  301. );
  302. }
  303. }
  304. // dsm($items, 'items '.$entity->title);
  305. #create new list
  306. $link = l(
  307. '<i class="icon-plus"></i>&nbsp;<span>' . t('New @name', array('@name' => t(variable_get('flag_lists_name', 'list')))) . '</span>',
  308. 'flag-lists/add/' . $entity->type,
  309. array(
  310. 'attributes' => array(
  311. 'class' => array('flag-lists-create'),
  312. 'title' => t('create a new @name and use it.', array('@name'=>t(variable_get('flag_lists_name', 'list')))),
  313. 'nid' => $entity->nid,
  314. 'token' => flag_get_token($entity->nid),
  315. ),
  316. 'html' => TRUE,
  317. )
  318. );
  319. $create = array(
  320. 'data' => $link,
  321. 'class' => array('flag-lists-create'),
  322. );
  323. }
  324. if(isset($items)){
  325. $ops = array(
  326. '#node' => $entity,
  327. '#items' => $items,
  328. );
  329. }
  330. if(isset($create)){
  331. $ops['#create'] = $create;
  332. }
  333. if(isset($ops)){
  334. // dsm($ops, 'ops');
  335. drupal_add_js(drupal_get_path('module', 'materio_flag').'/js/materio_flag.min.js');
  336. $ops['#theme'] = "materio_flag_mylists_entity_links";
  337. return $ops;
  338. }
  339. return;
  340. }
  341. function _materio_flag_get_listpagetitle($flag){
  342. $cont = '<i class="icon-materio-folder"></i>';
  343. $cont .= '<span class="'.$flag->name.'">'.check_plain($flag->title).'</span>';
  344. if(flag_lists_is_owner('edit', $flag->fid)){
  345. $cont .= l('<i class="icon-wrench"></i>',
  346. 'flags/lists/edit/'.$flag->fid,
  347. array(
  348. 'html'=>true,
  349. 'attributes'=>array('class'=>array('edit-list', $flag->name)),
  350. )
  351. );
  352. }
  353. return $cont;
  354. }
  355. /**
  356. * Implements hook_theme().
  357. */
  358. function materio_flag_theme($existing, $type, $theme, $path) {
  359. return array(
  360. 'materio_flag_mybookmarks_block' => array(
  361. 'arguments' => array(),
  362. 'template' => 'materio-flag-mybookmarks-block',
  363. 'path' => drupal_get_path('module', 'materio_flag').'/templates',
  364. ),
  365. 'materio_flag_mylists_block' => array(
  366. 'arguments' => array(),
  367. 'template' => 'materio-flag-mylists-block',
  368. 'path' => drupal_get_path('module', 'materio_flag').'/templates',
  369. ),
  370. 'materio_flag_mylists_nav_block' => array(
  371. 'arguments' => array(),
  372. 'template' => 'materio-flag-mylists-nav-block',
  373. 'path' => drupal_get_path('module', 'materio_flag').'/templates',
  374. ),
  375. 'materio_flag_mylists_entity_links' => array(
  376. 'variables' => array('node' => NULL, 'create' => NULL, 'items' => array()),
  377. ),
  378. 'materio_flag_mylists_list' => array(
  379. 'template' => 'materio-flag-mylists-list',
  380. 'path' => drupal_get_path('module', 'materio_flag').'/templates',
  381. 'variables' => array(
  382. 'count' => 0,
  383. 'items' => array(),
  384. 'view_mode' => 'teaser',
  385. 'pager' => NULL,
  386. 'fid' => null,
  387. 'name' => null,
  388. 'title' => null,
  389. ),
  390. ),
  391. );
  392. }
  393. function template_preprocess_materio_flag_mybookmarks_block(&$vars){
  394. // dsm($vars, 'vars');
  395. }
  396. function template_preprocess_materio_flag_mylists_block(&$vars){
  397. // dsm($vars, 'vars');
  398. }
  399. function template_preprocess_materio_flag_mylists_nav_block(&$vars){
  400. // dsm($vars, 'vars');
  401. }
  402. /**
  403. * theme_materio_flag_mylists_entity_links()
  404. *
  405. * see theme_flag_lists_list()
  406. */
  407. function theme_materio_flag_mylists_entity_links($vars){
  408. // $node = $vars['node'];
  409. $items = $vars['items'];
  410. // dsm($vars, 'vars HO');
  411. if(isset($vars['create']))
  412. $items[] = $vars['create'];
  413. return theme('item_list', array('items' => $items, 'type' => 'ul', 'attributes' => array('class' => 'flag-lists-entity-links')));
  414. }
  415. function template_preprocess_materio_flag_mylists_list(&$vars) {
  416. $vars['list_count'] = format_plural(
  417. $vars['count'],
  418. '@name @title contains 1 item.', // in @sec seconds
  419. '@name @title contains @count items.', // in @sec seconds
  420. array(
  421. '@name' => t($vars['name']),
  422. '@title' => $vars['title'],
  423. )
  424. );
  425. // dsm($vars, '$vars');
  426. }