materio_flag.module 15 KB

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