materio_flag.module 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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/mylists'] = $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/createlist/%'] = $base+array(
  52. 'access arguments' => array('create flag lists'),
  53. 'access callback' => 'user_access',
  54. 'page callback' => 'materio_flag_createlist',
  55. 'page arguments' => array(2, 3),
  56. );
  57. return $items;
  58. }
  59. /**
  60. * Implements hook_block_info().
  61. */
  62. function materio_flag_block_info() {
  63. $blocks['materio_flag_mybookmarks'] = array(
  64. 'info' => t('My bookmarks'),
  65. 'cache' => DRUPAL_NO_CACHE
  66. );
  67. $blocks['materio_flag_mylists'] = array(
  68. 'info' => t('My Materio flag lists'),
  69. 'cache' => DRUPAL_NO_CACHE
  70. );
  71. return $blocks;
  72. }
  73. /**
  74. * Implements hook_block_view().
  75. */
  76. function materio_flag_block_view($delta = '') {
  77. global $user;
  78. $block = array();
  79. switch ($delta) {
  80. case 'materio_flag_mybookmarks':
  81. if(user_access('access mybookmarks block')){
  82. $userflags = flag_get_user_flags('node');
  83. //dsm($userflags, 'userflags');
  84. if(isset($userflags['bookmarks'])){
  85. $userbookmarks = array();
  86. foreach ($userflags['bookmarks'] as $nid => $flag) {
  87. $userbookmarks[] = node_load($nid);
  88. }
  89. $block['subject'] = t('My bookmarks (@len)', array("@len"=>count($userbookmarks)));
  90. $block['content'] = theme('materio_flag_mybookmarks_block', array("bookmarks"=>$userbookmarks, "viewmode"=>"bookmark"));
  91. }else{
  92. $block['subject'] = t('My bookmarks');
  93. $block['content'] = t('No bookmarks yet. Add bookmarks on clicking on results star');
  94. }
  95. drupal_add_js(drupal_get_path('module', 'materio_flag').'/js/materio_flag-ck.js');
  96. }
  97. break;
  98. case 'materio_flag_mylists':
  99. if(user_access('create flag lists')){
  100. $flags = flag_lists_get_user_flags(NULL, $user);
  101. // $userflags = flag_get_user_flags('node');
  102. // dsm($userflags, 'userflags');
  103. // dsm($flags, 'flags');
  104. foreach ($flags as $name => $flag) {
  105. $flaged_content = flag_lists_get_flagged_content($flag->fid, $user->uid);
  106. // dsm($flaged_content, 'flaged_content');
  107. $fcn = array();
  108. foreach ($flaged_content as $entity) {
  109. if($entity->entity_type == 'node'){
  110. $node = node_load($entity->entity_id);
  111. // dsm($node, 'node');
  112. $node->flag_names[] = $name;
  113. $fcn[] = $node;
  114. }
  115. }
  116. $lists[$name] = array(
  117. 'list' => $flag,
  118. 'content' => $fcn,
  119. );
  120. }
  121. if(isset($lists)){
  122. $block['subject'] = t('My !listname'.'s', array('!listname'=>variable_get('flag_lists_name', 'list')));
  123. $block['content'] = theme('materio_flag_mylists_block', array("lists"=>$lists, "viewmode"=>"bookmark"));
  124. // $block['content'] = theme('flag_lists_user_page', array('uid' => $user->uid));
  125. }else{
  126. $block['subject'] = t('My !listname'.'s', array('!listname'=>variable_get('flag_lists_name', 'list')));
  127. $block['content'] = t('No !listname yet. Add !listname on clicking on results star', array('!listname'=>variable_get('flag_lists_name', 'list')));
  128. }
  129. drupal_add_js(drupal_get_path('module', 'materio_flag').'/js/materio_flag-ck.js');
  130. }
  131. break;
  132. }
  133. return $block;
  134. }
  135. /**
  136. * Implements hook_entity_info_alter().
  137. */
  138. function materio_flag_entity_info_alter(&$entity_info) {
  139. $entity_info['node']['view modes']['bookmark'] = array(
  140. 'label' => t('Bookmark'),
  141. 'custom settings' => TRUE,
  142. );
  143. }
  144. /**
  145. * Implements hook_entity_view().
  146. *
  147. * Note this is broken for taxonomy terms. @see http://drupal.org/node/1067120
  148. */
  149. function materio_flag_entity_view($entity, $type, $view_mode, $langcode) {
  150. if($type == 'node'){
  151. // dsm($entity, 'entity');
  152. // Do we have a list template for this node type, or are we s
  153. if (!flag_lists_template_exists($entity->type)) {
  154. return;
  155. }
  156. global $user;
  157. if ($flags = flag_lists_get_user_flags($entity->type, $user)) {
  158. // dsm($flags, 'flags');
  159. # if flag name is provided we are on flaglists content list (block mylists)
  160. if(isset($entity->flag_names)){
  161. // TODO: limit flag link by current flag list
  162. foreach ($flags as $flag) {
  163. //dsm($flag->name, 'flag');
  164. if(in_array($flag->name, $entity->flag_names)){
  165. if ($flag->module == 'flag_lists') {
  166. $action = _flag_lists_is_flagged($flag, $entity->nid, $user->uid, 0) ? 'unflag' : 'flag';
  167. } else {
  168. $action = $flag->is_flagged($entity->nid) ? 'unflag' : 'flag';
  169. }
  170. $flag->module = 'materio_flag';
  171. $link = $flag->theme($action, $entity->nid);
  172. // If it's a list, fix the link.
  173. if ($flag->module == 'flag_lists') {
  174. flag_lists_fix_link($link, $action);
  175. }
  176. $items[] = array(
  177. 'data' => $link,
  178. 'class' => array('flag-lists-link', $action.'-action'),
  179. );
  180. // array_splice($entity->flag_names, array_search($flag->name, $entity->flag_names), 1);
  181. // dsm($entity->flag_names, 'entity->flag_name');
  182. // break;
  183. }
  184. }
  185. #normal display
  186. }else{
  187. // Build the list of lists for this node.
  188. foreach ($flags as $flag) {
  189. if ($flag->module == 'flag_lists') {
  190. $action = _flag_lists_is_flagged($flag, $entity->nid, $user->uid, 0) ? 'unflag' : 'flag';
  191. }
  192. else {
  193. $action = $flag->is_flagged($entity->nid) ? 'unflag' : 'flag';;
  194. }
  195. // dsm($flag, 'flag');
  196. // $flag->module = 'materio_flag';
  197. $link = $flag->theme($action, $entity->nid);
  198. // If it's a list, fix the link.
  199. if ($flag->module == 'flag_lists') {
  200. flag_lists_fix_link($link, $action);
  201. }
  202. $items[] = array(
  203. 'data' => $link,
  204. 'class' => array('flag-lists-link', $action.'-action'),
  205. );
  206. }
  207. #create new list
  208. $link = l(
  209. t('New @name', array('@name' => variable_get('flag_lists_name', t('list')))),
  210. 'flag-lists/add/' . $entity->type,
  211. array(
  212. // 'query' => drupal_get_destination(),
  213. 'attributes' => array(
  214. 'class' => array('flag-lists-create'),
  215. 'title' => t('create a new @name and use it.', array('@name'=>variable_get('flag_lists_name', t('list'))))
  216. )
  217. )
  218. );
  219. $create = array(
  220. 'data' => $link,// . '<i class="icon-plus"></i>',
  221. 'class' => array('flag-lists-create'),
  222. );
  223. }
  224. if( (!isset($items) || !count($items)) && !isset($create))
  225. return;
  226. $ops = array(
  227. 'node' => $entity,
  228. 'items' => $items,
  229. );
  230. if(isset($create))
  231. $ops['create'] = $create;
  232. drupal_add_js(drupal_get_path('module', 'materio_flag').'/js/materio_flag-ck.js');
  233. $flaglists_links = theme('materio_flag_mylists_entity_links', $ops);
  234. $entity->content['links']['flaglistslinks'] = array('#markup' => $flaglists_links,"#html"=>true);
  235. }
  236. }
  237. }
  238. /**
  239. * Implements hook_theme().
  240. */
  241. function materio_flag_theme($existing, $type, $theme, $path) {
  242. return array(
  243. 'materio_flag_mybookmarks_block' => array(
  244. 'arguments' => array(),
  245. 'template' => 'materio-flag-mybookmarks-block',
  246. 'path' => drupal_get_path('module', 'materio_flag').'/templates',
  247. ),
  248. 'materio_flag_mylists_block' => array(
  249. 'arguments' => array(),
  250. 'template' => 'materio-flag-mylists-block',
  251. 'path' => drupal_get_path('module', 'materio_flag').'/templates',
  252. ),
  253. 'materio_flag_mylists_entity_links' => array(
  254. 'variables' => array('node' => NULL, 'create' => NULL, 'items' => array()),
  255. ),
  256. );
  257. }
  258. function template_preprocess_materio_flag_mybookmarks_block($vars){
  259. // dsm($vars, 'vars');
  260. }
  261. function template_preprocess_materio_flag_mylists_block($vars){
  262. // dsm($vars, 'vars');
  263. }
  264. /**
  265. * theme_materio_flag_mylists_entity_links()
  266. *
  267. * see theme_flag_lists_list()
  268. */
  269. function theme_materio_flag_mylists_entity_links($vars){
  270. // $node = $vars['node'];
  271. $items = $vars['items'];
  272. // dsm($vars, 'vars HO');
  273. if(isset($vars['create']))
  274. $items[] = $vars['create'];
  275. return theme('item_list', array('items' => $items, 'type' => 'ul', 'attributes' => array('class' => 'flag-lists-entity-links')));
  276. }