materio_flag.module 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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('access mybookmarks block'),
  37. 'page callback' => 'materio_flag_refresh_block_bookmarks',
  38. // 'page arguments' => array(),
  39. );
  40. return $items;
  41. }
  42. /**
  43. * Implements hook_block_info().
  44. */
  45. function materio_flag_block_info() {
  46. $blocks['materio_flag_mybookmarks'] = array(
  47. 'info' => t('My bookmarks'),
  48. 'cache' => DRUPAL_NO_CACHE
  49. );
  50. $blocks['materio_flag_mylists'] = array(
  51. 'info' => t('My Materio flag lists'),
  52. 'cache' => DRUPAL_NO_CACHE
  53. );
  54. return $blocks;
  55. }
  56. /**
  57. * Implements hook_block_view().
  58. */
  59. function materio_flag_block_view($delta = '') {
  60. global $user;
  61. $block = array();
  62. switch ($delta) {
  63. case 'materio_flag_mybookmarks':
  64. if(user_access('access own bookmarks block')){
  65. $userflags = flag_get_user_flags('node');
  66. // dsm($userflags, 'userflags');
  67. if(isset($userflags['bookmarks'])){
  68. $userbookmarks = array();
  69. foreach ($userflags['bookmarks'] as $nid => $flag) {
  70. $userbookmarks[] = node_load($nid);
  71. }
  72. $block['subject'] = t('My bookmarks (@len)', array("@len"=>count($userbookmarks)));
  73. $block['content'] = theme('materio_flag_mybookmarks_block', array("bookmarks"=>$userbookmarks, "viewmode"=>"bookmark"));
  74. }else{
  75. $block['subject'] = t('My bookmarks');
  76. $block['content'] = t('No bookmarks yet. Add bookmarks on clicking on results star');
  77. }
  78. drupal_add_js(drupal_get_path('module', 'materio_flag').'/js/materio_flag-ck.js');
  79. }
  80. break;
  81. case 'materio_flag_mylists':
  82. if(user_access('create flag lists')){
  83. $flags = flag_lists_get_user_flags(NULL, $user);
  84. // $userflags = flag_get_user_flags('node');
  85. // dsm($userflags, 'userflags');
  86. // dsm($flags, 'flags');
  87. foreach ($flags as $name => $flag) {
  88. $flaged_content = flag_lists_get_flagged_content($flag->fid, $user->uid);
  89. // dsm($flaged_content, 'flaged_content');
  90. $fcn = array();
  91. foreach ($flaged_content as $entity) {
  92. if($entity->entity_type == 'node'){
  93. $fcn[] = node_load($entity->entity_id);
  94. }
  95. }
  96. $lists[$name] = array(
  97. 'list' => $flag,
  98. 'content' => $fcn,
  99. );
  100. }
  101. if(isset($lists)){
  102. $block['subject'] = t('My !listname', array('!listname'=>variable_get('flag_lists_name', 'list')));
  103. $block['content'] = theme('materio_flag_mylists_block', array("lists"=>$lists, "viewmode"=>"bookmark"));
  104. // $block['content'] = theme('flag_lists_user_page', array('uid' => $user->uid));
  105. }else{
  106. $block['subject'] = t('My !listname', array('!listname'=>variable_get('flag_lists_name', 'list')));
  107. $block['content'] = t('No !listname yet. Add !listname on clicking on results star', array('!listname'=>variable_get('flag_lists_name', 'list')));
  108. }
  109. }
  110. break;
  111. }
  112. return $block;
  113. }
  114. /**
  115. * Implements hook_entity_info_alter().
  116. */
  117. function materio_flag_entity_info_alter(&$entity_info) {
  118. // Set the controller class for nodes to an alternate implementation of the
  119. // DrupalEntityController interface.
  120. /*
  121. *$entity_info['node']['controller class'] = 'MyCustomNodeController';
  122. */
  123. /* Your code here */
  124. $entity_info['node']['view modes']['bookmark'] = array(
  125. 'label' => t('Bookmark'),
  126. 'custom settings' => TRUE,
  127. );
  128. }
  129. /**
  130. * Implements hook_entity_prepare_view().
  131. */
  132. function materio_flag_entity_prepare_view($entities, $type) {
  133. // Load a specific node into the user object for later theming.
  134. /*
  135. *if ($type == 'user') {
  136. * $nodes = mymodule_get_user_nodes(array_keys($entities));
  137. * foreach ($entities as $uid => $entity) {
  138. * $entity->user_node = $nodes[$uid];
  139. * }
  140. *}
  141. */
  142. /* Your code here */
  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. // $userlists = flag_lists_get_user_flags();
  153. // if (isset($userlists)) {
  154. // $entity->content['links']['flaglists'] = array(
  155. // '#theme' => 'links',
  156. // '#links' => $userlists,
  157. // '#attributes' => array('class' => array('links', 'inline')),
  158. // );
  159. // }
  160. // $create = theme('flag_lists_list', array(
  161. // 'node' => $entity,
  162. // 'create' => 0,
  163. // 'ops' => 0,
  164. // 'use_flags' => 1)
  165. // );
  166. // dsm($create, 'create');
  167. $flaglists_links = theme('materio_flag_mylists_entity_links', array(
  168. 'node' => $entity,
  169. 'create' => 0,
  170. 'ops' => 0,
  171. 'use_flags' => 1)
  172. );
  173. $entity->content['links']['flaglistslinks'] = array('#markup' => $flaglists_links,"#html"=>true);
  174. // dsm($entity, 'entity');
  175. }
  176. }
  177. /**
  178. * Implements hook_theme().
  179. */
  180. function materio_flag_theme($existing, $type, $theme, $path) {
  181. return array(
  182. 'materio_flag_mybookmarks_block' => array(
  183. 'arguments' => array(),
  184. 'template' => 'materio-flag-mybookmarks-block',
  185. 'path' => drupal_get_path('module', 'materio_flag').'/templates',
  186. ),
  187. 'materio_flag_mylists_block' => array(
  188. 'arguments' => array(),
  189. 'template' => 'materio-flag-mylists-block',
  190. 'path' => drupal_get_path('module', 'materio_flag').'/templates',
  191. ),
  192. 'materio_flag_mylists_entity_links' => array(
  193. 'variables' => array('node' => NULL, 'create' => NULL, 'ops' => NULL, 'use_flags' => NULL),
  194. ),
  195. );
  196. }
  197. function template_preprocess_materio_flag_mybookmarks_block($vars){
  198. // dsm($vars, 'vars');
  199. }
  200. function template_preprocess_materio_flag_mylists_block($vars){
  201. // dsm($vars, 'vars');
  202. }
  203. function theme_materio_flag_mylists_entity_links($vars){
  204. // dsm($vars, 'vars');
  205. return;
  206. $node = $vars['node'];
  207. $create = $vars['create'];
  208. $ops = $vars['ops'];
  209. $use_flags = $vars['use_flags'];
  210. $items = array();
  211. // Make sure we have a node.
  212. if (is_object($node) && user_access('create flag lists')) {
  213. $content_type = $node->type;
  214. $entity_id = $node->nid;
  215. }
  216. else {
  217. return;
  218. }
  219. // Do we have a list template for this node type, or are we s
  220. if (!flag_lists_template_exists($content_type) && !$use_flags) {
  221. return;
  222. }
  223. global $user;
  224. if ($flags = flag_lists_get_user_flags($content_type, $user, $use_flags)) {
  225. // Build the list of lists for this node.
  226. foreach ($flags as $flag) {
  227. if ($flag->module == 'flag_lists') {
  228. $action = _flag_lists_is_flagged($flag, $entity_id, $user->uid, 0) ? 'unflag' : 'flag';
  229. }
  230. else {
  231. $action = $flag->is_flagged($entity_id) ? 'unflag' : 'flag';;
  232. }
  233. // Do we need the ops?
  234. if ($ops && $flag->module == 'flag_lists') {
  235. $ops_links = theme('flag_lists_ops', array('flag' => $flag));
  236. $link = $flag->theme($action, $entity_id) . $ops_links;
  237. }
  238. else {
  239. $link = $flag->theme($action, $entity_id);
  240. }
  241. // If it's a list, fix the link.
  242. if ($flag->module == 'flag_lists') {
  243. flag_lists_fix_link($link, $action);
  244. }
  245. $items[] = $link;
  246. }
  247. }
  248. }