materio_flag.module 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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'=>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. // # code...
  154. // }
  155. $block['subject'] = t('My !listname'.'s', array('!listname'=>variable_get('flag_lists_name', 'list')));
  156. $block['content'] = theme('materio_flag_mylists_nav_block', array("flags"=>$flags));
  157. // $block['content'] = theme('flag_lists_user_page', array('uid' => $user->uid));
  158. }
  159. break;
  160. }
  161. return $block;
  162. }
  163. /**
  164. * Implements hook_entity_info_alter().
  165. */
  166. function materio_flag_entity_info_alter(&$entity_info) {
  167. $entity_info['node']['view modes']['bookmark'] = array(
  168. 'label' => t('Bookmark'),
  169. 'custom settings' => TRUE,
  170. );
  171. }
  172. /**
  173. * Implements hook_entity_view().
  174. *
  175. * Note this is broken for taxonomy terms. @see http://drupal.org/node/1067120
  176. */
  177. function materio_flag_entity_view($entity, $type, $view_mode, $langcode) {
  178. if($type == 'node'){
  179. if(user_access('create flag lists')){
  180. $flaglists_links = materio_flag_get_entity_links($entity, $type, $view_mode);
  181. // dsm($flaglists_links, 'flaglists_links');
  182. $entity->content['links']['flaglistslinks'] = array('#markup' => $flaglists_links,"#html"=>true);
  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. }
  186. }
  187. }
  188. function materio_flag_get_entity_links($entity, $type, $view_mode = null){
  189. // dsm($entity, 'entity');
  190. // Do we have a list template for this node type, or are we s
  191. if (!flag_lists_template_exists($entity->type)) {
  192. return;
  193. }
  194. global $user;
  195. # if flag name is provided we are on flaglists content list (block mylists)
  196. if($view_mode == 'bookmark'){
  197. // TODO: define view mode in settings
  198. // if (isset($entity->flag_names) && $flags = flag_lists_get_user_flags($entity->type, $user)) {
  199. // // dsm($flags, 'flags');
  200. // // TODO: limit flag link by current flag list
  201. // foreach ($flags as $flag) {
  202. // //dsm($flag->name, 'flag');
  203. // if(in_array($flag->name, $entity->flag_names)){
  204. // if ($flag->module == 'flag_lists') {
  205. // $action = _flag_lists_is_flagged($flag, $entity->nid, $user->uid, 0) ? 'unflag' : 'flag';
  206. // } else {
  207. // $action = $flag->is_flagged($entity->nid) ? 'unflag' : 'flag';
  208. // }
  209. // $flag->module = 'materio_flag';
  210. // $link = $flag->theme($action, $entity->nid);
  211. // // If it's a list, fix the link.
  212. // if ($flag->module == 'flag_lists') {
  213. // flag_lists_fix_link($link, $action);
  214. // }
  215. // $items[] = array(
  216. // 'data' => $link,
  217. // 'class' => array('flag-lists-link', $action.'-action'),
  218. // );
  219. // // array_splice($entity->flag_names, array_search($flag->name, $entity->flag_names), 1);
  220. // // dsm($entity->flag_names, 'entity->flag_name');
  221. // // break;
  222. // }
  223. // }
  224. // }
  225. #normal display
  226. }else{
  227. if ($flags = flag_lists_get_user_flags($entity->type, $user)) {
  228. // dsm($flags, 'flags');
  229. // Build the list of lists for this node.
  230. foreach ($flags as $flag) {
  231. // dsm($flag, 'flag');
  232. if ($flag->module == 'flag_lists') {
  233. $action = _flag_lists_is_flagged($flag, $entity->nid, $user->uid, 0) ? 'unflag' : 'flag';
  234. }
  235. else {
  236. $action = $flag->is_flagged($entity->nid) ? 'unflag' : 'flag';;
  237. }
  238. $link = $flag->theme($action, $entity->nid);
  239. // If it's a list, fix the link.
  240. if ($flag->module == 'flag_lists') {
  241. flag_lists_fix_link($link, $action);
  242. }
  243. $items[] = array(
  244. 'data' => $link,
  245. 'class' => array('flag-lists-link', $action.'-action'),
  246. );
  247. }
  248. }
  249. #create new list
  250. $link = l(
  251. '<span>' . t('New @name', array('@name' => variable_get('flag_lists_name', t('list')))) . '</span>&nbsp;<i class="icon-plus"></i>',
  252. 'flag-lists/add/' . $entity->type,
  253. array(
  254. // 'query' => drupal_get_destination(),
  255. 'attributes' => array(
  256. 'class' => array('flag-lists-create'),
  257. 'title' => t('create a new @name and use it.', array('@name'=>variable_get('flag_lists_name', t('list')))),
  258. 'nid' => $entity->nid,
  259. 'token' => flag_get_token($entity->nid),
  260. ),
  261. 'html' => TRUE,
  262. )
  263. );
  264. $create = array(
  265. 'data' => $link,
  266. 'class' => array('flag-lists-create'),
  267. );
  268. }
  269. // if( (!isset($items) || !count($items)) && !isset($create))
  270. // return;
  271. if(isset($items)){
  272. $ops = array(
  273. 'node' => $entity,
  274. 'items' => $items,
  275. );
  276. }
  277. if(isset($create)){
  278. $ops['create'] = $create;
  279. // drupal_add_css(drupal_get_path('module', 'flag') . '/theme/flag.css');
  280. // drupal_add_js(drupal_get_path('module', 'flag') . '/theme/flag.js');
  281. }
  282. if(isset($ops)){
  283. // dsm($ops, 'ops');
  284. drupal_add_js(drupal_get_path('module', 'materio_flag').'/js/materio_flag-ck.js');
  285. return theme('materio_flag_mylists_entity_links', $ops);
  286. }
  287. return;
  288. }
  289. /**
  290. * Implements hook_theme().
  291. */
  292. function materio_flag_theme($existing, $type, $theme, $path) {
  293. return array(
  294. 'materio_flag_mybookmarks_block' => array(
  295. 'arguments' => array(),
  296. 'template' => 'materio-flag-mybookmarks-block',
  297. 'path' => drupal_get_path('module', 'materio_flag').'/templates',
  298. ),
  299. 'materio_flag_mylists_block' => array(
  300. 'arguments' => array(),
  301. 'template' => 'materio-flag-mylists-block',
  302. 'path' => drupal_get_path('module', 'materio_flag').'/templates',
  303. ),
  304. 'materio_flag_mylists_nav_block' => array(
  305. 'arguments' => array(),
  306. 'template' => 'materio-flag-mylists-nav-block',
  307. 'path' => drupal_get_path('module', 'materio_flag').'/templates',
  308. ),
  309. 'materio_flag_mylists_entity_links' => array(
  310. 'variables' => array('node' => NULL, 'create' => NULL, 'items' => array()),
  311. ),
  312. 'materio_flag_mylists_list' => array(
  313. 'template' => 'materio-flag-mylists-list',
  314. 'path' => drupal_get_path('module', 'materio_flag').'/templates',
  315. 'variables' => array(
  316. 'count' => 0,
  317. 'items' => array(),
  318. 'view_mode' => 'teaser',
  319. 'pager' => NULL,
  320. 'fid' => null,
  321. 'name' => null,
  322. 'title' => null,
  323. ),
  324. ),
  325. );
  326. }
  327. function template_preprocess_materio_flag_mybookmarks_block(&$vars){
  328. // dsm($vars, 'vars');
  329. }
  330. function template_preprocess_materio_flag_mylists_block(&$vars){
  331. // dsm($vars, 'vars');
  332. }
  333. function template_preprocess_materio_flag_mylists_nav_block(&$vars){
  334. // dsm($vars, 'vars');
  335. }
  336. /**
  337. * theme_materio_flag_mylists_entity_links()
  338. *
  339. * see theme_flag_lists_list()
  340. */
  341. function theme_materio_flag_mylists_entity_links($vars){
  342. // $node = $vars['node'];
  343. $items = $vars['items'];
  344. // dsm($vars, 'vars HO');
  345. if(isset($vars['create']))
  346. $items[] = $vars['create'];
  347. return theme('item_list', array('items' => $items, 'type' => 'ul', 'attributes' => array('class' => 'flag-lists-entity-links')));
  348. }
  349. function template_preprocess_materio_flag_mylists_list(&$vars) {
  350. $vars['list_count'] = format_plural(
  351. $vars['count'],
  352. '@name @title contains 1 item.', // in @sec seconds
  353. '@name @title contains @count items.', // in @sec seconds
  354. array(
  355. '@name' => $vars['name'],
  356. '@title' => $vars['title'],
  357. )
  358. );
  359. // dsm($vars, '$vars');
  360. }