materio_personalnotes.module 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /**
  3. * Implements hook_init().
  4. */
  5. function materio_personalnotes_init() {
  6. drupal_add_js(drupal_get_path('module', 'materio_personalnotes').'/js/materio_personalnotes.js');
  7. }
  8. /**
  9. * Implements hook_permission().
  10. */
  11. function materio_personalnotes_permission() {
  12. $perms = array(
  13. 'create own personal notes' => array(
  14. 'title' => t('Create own personal notes'),
  15. 'description' => t('Create own personal notes'),
  16. ),
  17. );
  18. return $perms;
  19. }
  20. function materio_personalnotes_menu(){
  21. $items = array();
  22. $base = array(
  23. 'type' => MENU_CALLBACK,
  24. 'file' => 'materio_personalnotes.pages.inc',
  25. );
  26. $items['materio_personalnotes/form/%/%'] = $base+array(
  27. 'access arguments' => array('create own personal notes'),
  28. 'access callback' => 'user_access',
  29. 'title' => 'Materio base create/edit note ajax',
  30. 'page callback' => 'materio_personalnotes_createeditnote',
  31. 'page arguments' => array(2,3),
  32. );
  33. $items['materio_personalnotes/save/%'] = $base+array(
  34. 'access arguments' => array('create own personal notes'),
  35. 'access callback' => 'user_access',
  36. 'title' => 'Materio base save note ajax',
  37. 'page callback' => 'materio_personalnotes_savenote',
  38. 'page arguments' => array(2),
  39. );
  40. return $items;
  41. }
  42. /**
  43. * Implements hook_menu_alter().
  44. */
  45. // function materio_personalnotes_menu_alter(&$items) {
  46. // $items['user/%user']['access callback'] = 'user_access';
  47. // $items['user/%user']['access arguments'] = array('view own user profile');
  48. // }
  49. /**
  50. * Implements hook_entity_view().
  51. *
  52. * Note this is broken for taxonomy terms. @see http://drupal.org/node/1067120
  53. */
  54. function materio_personalnotes_entity_view($entity, $type, $view_mode, $langcode) {
  55. // dsm($entity, 'entity');
  56. if($type == 'node' && $entity->type == "materiau" && in_array($view_mode, array('cardmedium', 'cardbig', 'cardfull'))){
  57. if(user_access('create own personal notes') && $view_mode != 'print'){
  58. $entity->content['personalnotelink'] = materio_personalnotes_get_note_link($entity);
  59. // drupal_add_css(drupal_get_path('module', 'flag') . '/theme/flag.css');
  60. // drupal_add_js(drupal_get_path('module', 'flag') . '/theme/flag.js');
  61. }
  62. }
  63. }
  64. function materio_personalnotes_get_note_link($entity){
  65. global $user;
  66. // dsm($user);
  67. // get note already created for this entity by current user
  68. $query = new EntityFieldQuery();
  69. $query
  70. ->entityCondition('entity_type', 'node')
  71. ->entityCondition('bundle', 'note')
  72. // ->propertyCondition('status', NODE_PUBLISHED);
  73. ->propertyCondition('uid', $user->uid)
  74. ->fieldCondition('field_target_content_nid', 'value', $entity->nid)
  75. ->addMetaData('account', user_load(1));
  76. $result = $query->execute();
  77. // dsm($result, 'result');
  78. if (isset($result['node'])) {
  79. // if note alredy exists link to it
  80. $note = array_shift($result['node']);
  81. // dsm($note, 'note');
  82. $link = array(
  83. '#path' => '/node/'.$note->nid.'/edit',
  84. '#attributes' => array(
  85. 'class' => array('personal-note-link', 'personal-note-edit'),
  86. 'title' => t('Edit your @title\'s notes.', array('@title'=>$entity->title)),
  87. 'src_nid' => $entity->nid,
  88. 'note_nid' => $note->nid,
  89. ),
  90. );
  91. }else{
  92. // else create one
  93. $link = array(
  94. // get the content type from settings OR create the content type with module install
  95. '#path' => '/node/add/note',
  96. '#query' => array(
  97. 'target_id'=>$entity->nid,
  98. ),
  99. '#attributes' => array(
  100. 'class' => array('personal-note-link', 'personal-note-create'),
  101. 'title' => t('create a note for @title.', array('@title'=>$entity->title)),
  102. 'src_nid' => $entity->nid,
  103. ),
  104. );
  105. }
  106. if(isset($link)){
  107. $link['#theme'] = 'materio_personalnotes_note_link';
  108. // dsm($link, 'link');
  109. drupal_add_js(drupal_get_path('module', 'materio_personalnotes').'/js/dist/materio_personalnotes.min.js');
  110. return $link;
  111. }
  112. return;
  113. }
  114. /**
  115. * Implements hook_form_alter().
  116. */
  117. function materio_personalnotes_form_alter(&$form, &$form_state, $form_id) {
  118. // dsm($form_id);
  119. if($form_id == "note_node_form" && isset($_GET['target_id']) && $target_id = $_GET['target_id']){
  120. // dsm($_GET, 'get');
  121. // dsm($target_id, 'target_id');
  122. // dsm($form, 'form');
  123. // dsm($form_state, 'form_state');
  124. $form['field_target_content_nid']['und'][0]['value']['#default_value'] = $target_id;
  125. $form['field_target_content_nid']['und'][0]['value']['#type'] = 'hidden';
  126. }
  127. }
  128. /**
  129. * Implements hook_theme().
  130. */
  131. function materio_personalnotes_theme($existing, $type, $theme, $path) {
  132. return array(
  133. 'materio_personalnotes_note_link' => array(
  134. 'variables' => array('path' => NULL, 'query' => array(), 'attributes' => array()),
  135. ),
  136. );
  137. }
  138. function theme_materio_personalnotes_note_link($vars){
  139. return l(
  140. '<i class="fi-pencil"></i>',
  141. $vars['path'],
  142. array(
  143. 'attributes' => $vars['attributes'],
  144. 'html' => true,
  145. 'query' => $vars['query'],
  146. )
  147. );
  148. }
  149. /**
  150. * Implements hook_block_info().
  151. */
  152. // function materio_personalnotes_block_info() {
  153. //
  154. // TODO: add my notes block
  155. // $blocks[''] = array(
  156. // 'info' => t(''),
  157. // 'cache' => DRUPAL_NO_CACHE
  158. // );
  159. //
  160. // return $blocks;
  161. // }