materio_flag.module 13 KB

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