186 lines
5.2 KiB
Plaintext
186 lines
5.2 KiB
Plaintext
<?php
|
|
|
|
|
|
/**
|
|
* Implements hook_init().
|
|
*/
|
|
function materio_personalnotes_init() {
|
|
drupal_add_js(drupal_get_path('module', 'materio_personalnotes').'/js/materio_personalnotes.js');
|
|
}
|
|
|
|
|
|
/**
|
|
* Implements hook_permission().
|
|
*/
|
|
function materio_personalnotes_permission() {
|
|
$perms = array(
|
|
'create own personal notes' => array(
|
|
'title' => t('Create own personal notes'),
|
|
'description' => t('Create own personal notes'),
|
|
),
|
|
);
|
|
|
|
return $perms;
|
|
}
|
|
|
|
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().
|
|
*/
|
|
// function materio_personalnotes_menu_alter(&$items) {
|
|
// $items['user/%user']['access callback'] = 'user_access';
|
|
// $items['user/%user']['access arguments'] = array('view own user profile');
|
|
// }
|
|
|
|
/**
|
|
* Implements hook_entity_view().
|
|
*
|
|
* Note this is broken for taxonomy terms. @see http://drupal.org/node/1067120
|
|
*/
|
|
function materio_personalnotes_entity_view($entity, $type, $view_mode, $langcode) {
|
|
// dsm($entity, 'entity');
|
|
if($type == 'node' && $entity->type == "materiau" && in_array($view_mode, array('cardmedium', 'cardbig', 'cardfull'))){
|
|
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');
|
|
// drupal_add_js(drupal_get_path('module', 'flag') . '/theme/flag.js');
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
function materio_personalnotes_get_note_link($entity){
|
|
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;
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
/**
|
|
* Implements hook_form_alter().
|
|
*/
|
|
function materio_personalnotes_form_alter(&$form, &$form_state, $form_id) {
|
|
// dsm($form_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';
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Implements hook_theme().
|
|
*/
|
|
function materio_personalnotes_theme($existing, $type, $theme, $path) {
|
|
return array(
|
|
'materio_personalnotes_note_link' => array(
|
|
'variables' => array('path' => NULL, 'query' => array(), 'attributes' => array()),
|
|
),
|
|
);
|
|
}
|
|
|
|
function theme_materio_personalnotes_note_link($vars){
|
|
return l(
|
|
'<i class="fi-pencil"></i>',
|
|
$vars['path'],
|
|
array(
|
|
'attributes' => $vars['attributes'],
|
|
'html' => true,
|
|
'query' => $vars['query'],
|
|
)
|
|
);
|
|
}
|
|
|
|
|
|
/**
|
|
* Implements hook_block_info().
|
|
*/
|
|
// function materio_personalnotes_block_info() {
|
|
//
|
|
// TODO: add my notes block
|
|
// $blocks[''] = array(
|
|
// 'info' => t(''),
|
|
// 'cache' => DRUPAL_NO_CACHE
|
|
// );
|
|
//
|
|
// return $blocks;
|
|
// }
|