materio_flag.module 16 KB

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