Browse Source

personnale notes front ajax is working, remains to improve the display, do it like liked materiaux

Bachir Soussi Chiadmi 6 years ago
parent
commit
7d5bedcb0c

BIN
sites/all/modules/gui/materiobasemod/images/close.png


File diff suppressed because it is too large
+ 0 - 1
sites/all/modules/gui/materiobasemod/js/dist/materio_personalnotes.min.js


+ 119 - 1
sites/all/modules/gui/materiobasemod/js/materio_personalnotes.js

@@ -12,14 +12,132 @@ MaterioPersonalNotes = function(){
   function init(){
     trace('MaterioPersonalNotes :: init');
 
+    $(document)
+      .bind('resultscompleted resultschanged previewloaded', onResultsUpdated);
+      // .bind('init-scroller-pager', onInitScrollerPager)
+      // .bind('load-scroller-pager', onLoadScrollerPager)
+      // .bind('view-mode-changed', onViewModeChanged)
+      // .bind('history-state-change', onHistoryStateChange);
+
+
+
   };
 
 
+  function onResultsUpdated(event){
+    trace('MaterioPersonalNotes :: onResultsUpdated', event);
+    ajaxifyLinks(event.container);
+  };
 
 
-  init();
+  function ajaxifyLinks(container){
+    trace('MaterioPersonalNotes :: ajaxifyLinks', container);
+
+    container = ((container != null) ? container : 'body');
+
+    // trace('MaterioFlag :: typeof Drupal.flagLink', typeof Drupal.flagLink);
+    // if (typeof Drupal.flagLink != 'undefined')
+    //   Drupal.flagLink(container);
+
+    $('a.personal-note-link:not(.ajax-processed), ', container)
+      .bind('click', onClickLink)
+      .addClass('ajax-processed');
+
+    // $('a.personal-note-edit:not(.ajax-processed)', container)
+    //   .bind('click', {'action':'edit'}, onClickLink)
+    //   .addClass('ajax-processed');
+
+  };
+
+  /**
+  * onClickCreatLink(event)
+  */
+  function onClickLink(event){
+    // trace('MaterioPersonalNotes :: onClickLink | '+action+' | event', event);
+    event.preventDefault();
+    var $link = $(event.currentTarget);
+    var note_nid = $link.attr('note_nid');
+    var action = note_nid ? 'edit' : 'create';
+    var src_nid = $link.attr('src_nid');
+    trace('MaterioPersonalNotes :: src_nid', src_nid);
+    trace('MaterioPersonalNotes :: note_nid', note_nid);
 
+    var url = Drupal.settings.basePath
+      +Drupal.settings.pathPrefix
+      +'materio_personalnotes/form/'+action
+      +'/'+src_nid;
 
+    if(action == 'edit'){
+      url += '/'+note_nid;
+    }
+
+    $.getJSON(url, function(json){
+      trace('MaterioPersonalNotes :: '+action+' : json', json);
+      if(json.new_note_nid){
+        updateNoteLink($link, json.new_note_nid);
+      }
+      showNoteForm(json);
+    });
+    return false;
+  };
+
+  function updateNoteLink($link, new_note_nid){
+    trace('MaterioPersonalNotes :: updateNoteLink : new_note_nid', new_note_nid);
+    $link.attr('note_nid', new_note_nid);
+    $link.attr('href', Drupal.settings.pathPrefix+'node/'+new_note_nid+'/edit');
+  };
+
+  function showNoteForm(json){
+    trace('MaterioPersonalNotes :: showNoteForm');
+    // google analytics
+    $.event.trigger({
+      type:"record-stat",
+      categorie:"PersonalNotes",
+      action: 'show create form'
+    });
+
+    var $modal = $('<div id="modal" class="modal note-modal"/>').appendTo('body');
+    $modal
+      .css({
+        position:'absolute',
+        top:'40%', left:'50%',
+        marginLeft:'-150px', width:'500px',
+        zIndex:"99999"
+      })
+      .append($('<div class="close"></i><div>')
+        .bind('click', function(){
+          trace('close note');
+          $(this).parents('#modal').remove();
+        })
+      );
+    $modal
+      .append(json.rendered_form)
+        .find('textarea')
+          .focus()
+          .bind('change keyup paste', function(event){
+            // trace('text changed', $(this).val());
+            var note_nid = $(this).parents('form').find('input[name="note_nid"]').val();
+            saveNote($(this).val(), note_nid);
+          });
+    $modal
+      .find('input[type="submit"], .homepage-textfield').hide();
+      // TODO:  esc keypressed close the form
+  };
+
+  function saveNote(text, note_nid){
+    trace(text, 'MaterioPersonalNotes :: saveNote : text');
+    var url = Drupal.settings.basePath
+      +Drupal.settings.pathPrefix
+      +'materio_personalnotes/save'
+      +'/'+note_nid;
+
+    $.post(url, {'content':text}, function(data, status){
+      trace('MaterioPersonalNotes :: save : status', status);
+
+    });
+  };
+
+  init();
 };
 
 $(document).ready(function() {

+ 92 - 97
sites/all/modules/gui/materiobasemod/materio_personalnotes.module

@@ -23,23 +23,32 @@ function materio_personalnotes_permission() {
   return $perms;
 }
 
-// function materio_personalnotes_menu(){
-//   $items = array();
-//
-//   $base = array(
-//     'type' => MENU_CALLBACK,
-//     'file' => 'materio_personalnotes.pages.inc',
-//   );
-//
-//   $items['materio_personalnotes/registerblock'] = $base+array(
-//     'title' => 'Materio base user ajax',
-//     'page callback' => 'materio_personalnotes_registerblock',
-//     // 'page arguments' => array(),
-//     'access callback' => TRUE,
-//   );
-//
-//   return $items;
-// }
+function materio_personalnotes_menu(){
+  $items = array();
+
+  $base = array(
+    'type' => MENU_CALLBACK,
+    'file' => 'materio_personalnotes.pages.inc',
+  );
+
+  $items['materio_personalnotes/form/%/%'] = $base+array(
+    'access arguments' => array('create own personal notes'),
+    'access callback' => 'user_access',
+    'title' => 'Materio base create/edit note ajax',
+    'page callback' => 'materio_personalnotes_createeditnote',
+    'page arguments' => array(2,3),
+  );
+
+  $items['materio_personalnotes/save/%'] = $base+array(
+    'access arguments' => array('create own personal notes'),
+    'access callback' => 'user_access',
+    'title' => 'Materio base save note ajax',
+    'page callback' => 'materio_personalnotes_savenote',
+    'page arguments' => array(2),
+  );
+
+  return $items;
+}
 
 /**
  * Implements hook_menu_alter().
@@ -55,9 +64,9 @@ function materio_personalnotes_permission() {
  * Note this is broken for taxonomy terms. @see http://drupal.org/node/1067120
  */
 function materio_personalnotes_entity_view($entity, $type, $view_mode, $langcode) {
-  if($type == 'node'){
+  // dsm($entity, 'entity');
+  if($type == 'node' && $entity->type == "materiau"){
     if(user_access('create own personal notes') && $view_mode != 'print'){
-
       $entity->content['personalnotelink'] = materio_personalnotes_get_note_link($entity);
 
       // drupal_add_css(drupal_get_path('module', 'flag') . '/theme/flag.css');
@@ -68,28 +77,51 @@ function materio_personalnotes_entity_view($entity, $type, $view_mode, $langcode
 }
 
 function materio_personalnotes_get_note_link($entity){
-
-  // if note alredy exists link to it
-
-  // else create one
-  #create new list
-  $link = array(
-    '#path' => '/node/add/note',
-    '#query' => array(
-      // 'edit[field_target_content][und][0][target_id]' => $entity->title.'('.$entity->nid.')',
-      'target_id'=>$entity->nid,
-    ),
-    // get the content type from settings OR create the content type with module install
-    // TODO: add data (node nid) for pre-filled reference field of new note
-    '#attributes' => array(
-      'class' => array('personal-note-link', 'personal-note-create'),
-      'title' => t('create a note for @title.', array('@title'=>$entity->title)),
-      'nid' => $entity->nid,
-    ),
-    '#theme'=>'materio_personalnotes_note_link',
-  );
+  global $user;
+  // dsm($user);
+  // get note already created for this entity by current user
+  $query = new EntityFieldQuery();
+  $query
+    ->entityCondition('entity_type', 'node')
+    ->entityCondition('bundle', 'note')
+    // ->propertyCondition('status', NODE_PUBLISHED);
+    ->propertyCondition('uid', $user->uid)
+    ->fieldCondition('field_target_content_nid', 'value', $entity->nid)
+    ->addMetaData('account', user_load(1));
+  $result = $query->execute();
+  // dsm($result, 'result');
+
+  if (isset($result['node'])) {
+    // if note alredy exists link to it
+    $note = array_shift($result['node']);
+    // dsm($note, 'note');
+    $link = array(
+      '#path' => '/node/'.$note->nid.'/edit',
+      '#attributes' => array(
+        'class' => array('personal-note-link', 'personal-note-edit'),
+        'title' => t('Edit your @title\'s notes.', array('@title'=>$entity->title)),
+        'src_nid' => $entity->nid,
+        'note_nid' => $note->nid,
+      ),
+    );
+  }else{
+    // else create one
+    $link = array(
+      // get the content type from settings OR create the content type with module install
+      '#path' => '/node/add/note',
+      '#query' => array(
+        'target_id'=>$entity->nid,
+      ),
+      '#attributes' => array(
+        'class' => array('personal-note-link', 'personal-note-create'),
+        'title' => t('create a note for @title.', array('@title'=>$entity->title)),
+        'src_nid' => $entity->nid,
+      ),
+    );
+  }
 
   if(isset($link)){
+    $link['#theme'] = 'materio_personalnotes_note_link';
     // dsm($link, 'link');
     drupal_add_js(drupal_get_path('module', 'materio_personalnotes').'/js/dist/materio_personalnotes.min.js');
     return $link;
@@ -103,69 +135,17 @@ function materio_personalnotes_get_note_link($entity){
  */
 function materio_personalnotes_form_alter(&$form, &$form_state, $form_id) {
   // dsm($form_id);
-  /*
-   *if (isset($form['type']) && $form['type']['#value'] . '_node_settings' == $form_id) {
-   *  $form['workflow']['upload_' . $form['type']['#value']] = array(
-   *    '#type' => 'radios',
-   *    '#title' => t('Attachments'),
-   *    '#default_value' => variable_get('upload_' . $form['type']['#value'], 1),
-   *    '#options' => array(t('Disabled'), t('Enabled')),
-   * );
-   *}
-   */
-  if($form_id == "note_node_form" && $target_id = $_GET['target_id']){
-    dsm($_GET, 'get');
-    dsm($target_id, 'target_id');
-    dsm($form, 'form');
-    dsm($form_state, 'form_state');
-
-
-
-    // $form['#after_build'][] = 'materio_personalnotes_noteform_after_build';
-
-    // $form_state['field']['field_target_content']['und']['instance']['default_value'] = $target_id;
+  if($form_id == "note_node_form" && isset($_GET['target_id']) && $target_id = $_GET['target_id']){
+    // dsm($_GET, 'get');
+    // dsm($target_id, 'target_id');
+    // dsm($form, 'form');
+    // dsm($form_state, 'form_state');
+
     $form['field_target_content_nid']['und'][0]['value']['#default_value'] = $target_id;
     $form['field_target_content_nid']['und'][0]['value']['#type'] = 'hidden';
-    // $form_state['field']['field_target_content']['und']['items_count'] = 1;
   }
 }
 
-/**
- * Implements hook_node_presave().
- */
-function materio_personalnotes_node_presave($node) {
-  /*
-   *if ($node->nid && $node->moderate) {
-   *  // Reset votes when node is updated:
-   *  $node->score = 0;
-   *  $node->users = '';
-   *  $node->votes = 0;
-   *}
-   */
-  /* Your code here */
-}
-
-// function materio_personalnotes_noteform_after_build($form, &$form_state) {
-//   dsm($_GET, 'get');
-//   dsm($form, 'form');
-//   dsm($form_state, 'form_state');
-//
-//   $form_state['field_target_content']['und'][0]['target_id']['#default_value'] = $target_id;
-//
-//    // switch ($form['form_id']['#value']) {
-//    //
-//    //   case 'your_form':
-//    //
-//    //      $form['field_yournoderef']['nid']['nid']['#value'] = arg(3);
-//    //      $form['field_yournoderef']['#access']  = false;
-//    //      break;
-//    //
-//    //  }
-//
-//   return $form;
-//
-// }
-
 /**
  * Implements hook_theme().
  */
@@ -188,3 +168,18 @@ function theme_materio_personalnotes_note_link($vars){
     )
   );
 }
+
+
+/**
+ * Implements hook_block_info().
+ */
+// function materio_personalnotes_block_info() {
+//
+// TODO: add my notes block
+//   $blocks[''] = array(
+//     'info' => t(''),
+//     'cache' => DRUPAL_NO_CACHE
+//   );
+//
+//   return $blocks;
+// }

+ 109 - 59
sites/all/modules/gui/materiobasemod/materio_personalnotes.pages.inc

@@ -1,62 +1,112 @@
 <?php
 
 
-// 
-// function materio_personalnotes_registerblock(){
-//   $return = array();
-//   $block = block_load('materio_personalnotes','user_createaccount');
-//   $return['block'] = drupal_render(_block_get_renderable_array(_block_render_blocks(array($block))));
-//   drupal_json_output($return);
-// }
-//
-// function materio_personalnotes_loginandregisterblock(){
-//   $return = array();
-//   $block = block_load('materio_personalnotes','user_register');
-//   $return['block'] = drupal_render(_block_get_renderable_array(_block_render_blocks(array($block))));
-//   drupal_json_output($return);
-// }
-//
-// function materio_personalnotes_register_submit(){
-//   $return = array();
-//   $return['POST'] = $_POST;
-//
-//   $form_state = array( "values"=>$_POST);
-//   drupal_form_submit($_POST['form_id'], $form_state);
-//
-//   // $return['form_state'] = $form_state;
-//
-//   $return['errors'] = form_get_errors();
-//   if($return['errors']){
-//     unset ($_SESSION['messages']['error']);
-//   }else{
-//     $messages = drupal_get_messages('status');
-//     // $return['messages'] = $messages;
-//     // drupal_set_message(t("Congratulations, you juste created your free materiO' account, welcome !"), 'status');
-//     foreach ($messages['status'] as $msg) {
-//       drupal_set_message($msg, 'status');
-//     }
-//   }
-//   // after registration user is automaticly logged in, thank's to login tobogan module
-//
-//   drupal_json_output($return);
-// }
-//
-// function materio_personalnotes_login_submit(){
-//   $return = array();
-//   $return['POST'] = $_POST;
-//
-//   $form_state = array("values"=>$_POST);
-//   drupal_form_submit($_POST['form_id'], $form_state);
-//
-//   $return['errors'] = form_get_errors();
-//   if($return['errors'])
-//     unset ($_SESSION['messages']['error']);
-//
-//   // if user-login form succed we retreive the user uid on $form_state, then we can effectively loggin the user
-//   if($uid = $form_state['uid'])
-//     user_login_submit(array(), $form_state);
-//
-//   drupal_json_output($return);
-// }
-//
-//
+function materio_personalnotes_createeditnote($action = 'create', $src_nid = null, $note_nid = null){
+  $debug = false;
+
+  global $user;
+
+  $rep = array();
+
+  if(!$src_nid)
+    drupal_json_output(array('error'=>"src_nid arguments needed"));
+
+  $contenttype = 'note';
+
+  // check if request is ajax, if not rediret
+  if (!$debug && (!isset($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest')) {
+    switch ($action) {
+      case 'edit':
+        drupal_goto('node/'.$src_nid.'/edit');
+        break;
+      default:
+        drupal_goto('node/add/' . $contenttype . '?target_id='.$src_nid);
+        break;
+    }
+    exit ;
+  }
+
+
+  module_load_include('inc', 'node', 'node.pages');
+  switch ($action) {
+    case 'edit':
+      if(!$note_nid){
+        return "For edit action, note_nid is needed";
+      }else{
+        $note = node_load($note_nid);
+        $form = drupal_get_form('note_node_form', $note);
+      }
+      break;
+    default: // create
+      $new_note = (object) array(
+        'uid' => $user->uid,
+        'name' => (isset($user->name) ? $user->name : ''),
+        'type' => 'note',
+        'language' => LANGUAGE_NONE,
+        'field_target_content_nid' => array(
+          'und'=>array(
+            array(
+              'value'=>$src_nid
+            )
+          )
+        ),
+        'field_personal_note' => array(
+          'und'=>array(
+            array(
+              'value'=>$src_nid
+            )
+          )
+        )
+      );
+      node_object_prepare($new_note);
+      node_save($new_note);
+      $note_nid = $new_note->nid;
+      $rep['new_note_nid'] = $note_nid;
+
+      $form = drupal_get_form('note_node_form', $new_note);
+      // pre-fill the field_target_content_nid with src_nid argument
+      // $form['field_target_content_nid']['und'][0]['value']['#default_value'] = $src_id;
+      // $form['field_target_content_nid']['und'][0]['value']['#type'] = 'hidden';
+      break;
+  }
+
+  $form['note_nid'] = array(
+    '#type'=>'hidden',
+    '#value'=> $note_nid,
+    '#name' => 'note_nid',
+  );
+
+	$rendered_form = drupal_render($form);
+
+  $rep += array(
+		// 'form'=>$form,
+    'rendered_form'=>$rendered_form
+  );
+
+  if ($debug) {
+    dsm($rep, 'rep');
+    return "debug display";
+  }else{
+    drupal_json_output($rep);
+  }
+}
+
+
+function materio_personalnotes_savenote($note_nid = null){
+  $debug = false;
+
+  if(!$note_nid)
+    drupal_json_output(array('error'=>"note_nid arguments needed"));
+
+  $node = node_load($note_nid);
+  // $note->field_target_content_nid
+  $node->field_personal_note[$node->language][0]['value'] = $_POST['content'];
+  node_save($node);
+
+  if($debug){
+    dsm($node, "note node");
+    return 'debug display';
+  }else{
+    drupal_json_output(array('status'=>'note node saved'));
+  }
+}

+ 12 - 0
sites/all/themes/gui/materiobasetheme/css/styles.css

@@ -8327,6 +8327,18 @@ body.node-type-publication article.node-publication .field figure, body.node-typ
     margin-left: 1em;
     width: 70%; } }
 
+.note-modal .close {
+  position: absolute;
+  top: 0;
+  right: 0;
+  width: 8px;
+  height: 8px;
+  border-radius: 4px;
+  background-color: #fff !important;
+  background-image: url(../img/close2.png);
+  background-size: contain;
+  background-position: center; }
+
 /*
     __  ___
    /  |/  /__  ______________ _____ ____  _____

BIN
sites/all/themes/gui/materiobasetheme/img/close2.png


+ 18 - 2
sites/all/themes/gui/materiobasetheme/scss/styles.scss

@@ -3836,8 +3836,24 @@ body.node-type-publication{
   }
 }
 
-
-
+//     _   __      __
+//    / | / /___  / /____  _____
+//   /  |/ / __ \/ __/ _ \/ ___/
+//  / /|  / /_/ / /_/  __(__  )
+// /_/ |_/\____/\__/\___/____/
+
+.note-modal{
+  .close{
+    position: absolute;
+    top:0; right:0;
+    width:8px; height:8px;
+    border-radius: 4px;
+    background-color: #fff!important;
+    background-image: url(../img/close2.png);
+    background-size: contain;
+    background-position: center;
+  }
+}
 
 
 @import "misc.scss";

Some files were not shown because too many files changed in this diff