view sites/all/modules/custom/digitalobjects/digitalobjects.module @ 0:015d06b10d37 default tip

initial
author dwinter
date Wed, 31 Jul 2013 13:49:13 +0200
parents
children
line wrap: on
line source

<?php
/* Implements hook_menu */


/* TODOS
 *
 *
 */
include('digitalobject_reader.php');
include('digitalobjects.import.php');

/**
 * Implementation of hook_enable().
 */
function digitalobjects_enable() {

  #adds a field for the current collection
  // Check if our field is not already created.
  if (!field_info_field('field_digitalobjects_cid')) {
    $field = array(
        'field_name' => 'field_digitalobjects_cid',
        'type' => 'digitalobjects_digitalcollection',
    );
    field_create_field($field);

    // Create the instance on the bundle.
    $instance = array(
        'field_name' => 'field_digitalobjects_cid',
        'entity_type' => 'user',
        'label' => 'Current collection',
        'bundle' => 'user',
        // If you don't set the "required" property then the field wont be required by default.
        'required' => False,
        'settings' => array(
            // Here you inform either or not you want this field showing up on the registration form.
            'user_register_form' => 1,
        ),
        'widget' => array(
            'type' => 'digitalcollection_default_textfield',
        ),
    );
    field_create_instance($instance);
  }
}

function digitalobjects_menu(){

    $items['digitalcollections/manageCurrent'] = array(
        'description' => 'show an object.',
        'page callback' => 'digitalobjects_digitalcollectionsManage',
        'access arguments'   => array('access content'),
        'file'               => 'digitalobjects.collections.inc',

    );


    $items['digitalobject'] = array(
        'description' => 'show an object.',
        'page callback' => 'digitalobjects_page',
        'access arguments'   => array('access content'),

    );
     $items['admin/config/media/digitalobjects'] = array(
            'title'              => 'Digitalobject',
            'description'        => 'Set path to the metadaprovider for digitalobjects',
            'weight'             => 10,
            'page callback'      => 'drupal_get_form',
            'page arguments'     => array('digitalobjects_settings'),
            'access arguments'   => array('administrate digitalobjects'),
            'file'               => 'digitalobjects.admin.inc',

        );



     $items['user/%/collections'] = array(
         'title'              => 'Collections',
         'description'        => 'Set path to the metadaprovider for digitalobjects',
         'weight'             => 10,
         'page callback'      => 'digitalobjects_user_admin_collection',
         'page arguments'     => array(1),
         'access arguments'   => array('manage private collections'),
         'file'               => 'digitalobjects.collections.inc',
         'type' => MENU_LOCAL_TASK,
     );



     $items['digitalobject/%/view'] = array(
        'title'              => 'View',
               'description' => 'show an object.',
        'page callback' => 'digitalobjects_page',
        'access arguments'   => array('access content'),
            'page arguments'     => array(1),

         'access arguments'   => array('access content'),
          'type' => MENU_LOCAL_TASK,

     );

     $items['digitalobject/%/edit'] = array(
         'title'              => 'Edit',
         'description'        => 'Set path to the metadaprovider for digitalobjects',
         'weight'             => 10,
         'page callback'      => 'digitalobjects_edit',
         'page arguments'     => array(1),

         'access arguments'   => array('administrate digitalobjects'),
         'type' => MENU_LOCAL_TASK,

     );

     $items['digitalobject/%/usage'] = array(
         'title'              => 'Usage',
         'description'        => 'Set path to the metadaprovider for digitalobjects',
         'weight'             => 10,
         'page callback'      => 'digitalobjects_show_usage',
         'page arguments'     => array(1),

         'access arguments'   => array('access content'),
         'type' => MENU_LOCAL_TASK,

     );

     $items['digitalobject/%/add'] = array(
         'title'              => 'Add',
         'description'        => 'Add an object to the current collection',
         'weight'             => 10,
         'page callback'      => 'digitalobjects_addToCurrentCollection',
         'page arguments'     => array(1),

         'access arguments'   => array('administrate digitalobjects'),
         'file'               => 'digitalobjects.collections.inc',
         'type' => MENU_LOCAL_TASK,

     );
    return $items;

}


function  digitalobjects_create_primary_node($objid){
$node = new stdClass();
$node->type = 'digitalobject';
node_object_prepare($node);
global $user;
#dpm($user);
#$node->uid=$user->uid;
$node->field_single_objid['und'][0]['objid']=$objid;

$node->title = $objid;

$node->language = LANGUAGE_NONE;
#$node = node_submit($node);

#$node->field_object_type['und'][0]=array('tid'=> '6');

$foo = taxonomy_get_term_by_name('primary');
foreach($foo as $term) {

  if(($term->vocabulary_machine_name) == 'digitalobject_types') {

    $node->field_object_type['und'][]['tid'] = $term->tid;
  }
}
$node = node_submit($node);

node_save($node);

return $node;
}

function digitalobjects_edit($objid){
  #suche ob es schon ein locales object zu dieser id gibt.
  $query = new EntityFieldQuery();
  $entities = $query->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', 'digitalobject')
  ->fieldCondition('field_single_objid', 'objid', $objid,"=")
  ->execute();

  if ($entities == null) #existiert noch nicht, dann anlegen
  {
    $node =digitalobjects_create_primary_node($objid);
    #$node->field_object_type['und'][0]=array('tid'=> 'autocreate',
    #'name' => "primary",
    #'vocabulary_machine_name' => 'digitialobject_types');

  } else {


  $nodes = node_load_multiple(array_keys($entities['node']));

  #gibt es eins, dann gib dieses zum editieren zurück
  #TODO falls mehrere existieren, was dann??

  #gehe durch alle gefundene
  foreach ($nodes as $node){
    #$typeTag = $entity->object_type['und']

    if (isset($node->field_object_type['und'])){
    $tid = $node->field_object_type['und'][0]['tid'];
    $tax =taxonomy_term_load($tid);
    if ($tax->name == "primary"){ #'primary gefunden'
      module_load_include('inc', 'node', 'node.pages');
      return drupal_get_form('digitalobject_node_form', $node);
    }}
  } # keine primary dann lege diese an:

  $node =digitalobjects_create_primary_node($objid);

  #$vals = array_values($nodes);
  #$node= array_shift($vals);
  }

  $node = node_submit($node);

  #node_save($node);
  module_load_include('inc', 'node', 'node.pages');

  return drupal_get_form('digitalobject_node_form', $node);

}


function digitalobjects_show_usage($objid){
  $query = new EntityFieldQuery();
  $entities = $query->entityCondition('entity_type', 'node')
  ->fieldCondition('field_objid', 'objid', $objid,"=")
  ->execute();


  $data = array();

  #treffer gefunden
  if (isset($entities['node'])){

  $nodes = node_load_multiple(array_keys($entities['node']));


  foreach ($nodes as $node){
    $data[$node->nid] = $node->title;

  }


  $output['digitalobjects_item_usage']= array(
      '#theme' => 'digitalobjects_item_usage',
      '#data' => $data);

  return $output;
  }

  return "<h2> object not used in a collection </h2>";
  }


function digitalobjects_page($keys = ''){


  #suche ob es schon ein locales object zu dieser id gibt.
  $query = new EntityFieldQuery();
  $entities = $query->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', 'digitalobject')
  ->fieldCondition('field_single_objid', 'objid', $keys,"=")
  ->execute();

  if ($entities == null) #existiert noch nicht, dann generische Anzeige
  {

  $output['digitalobjects_item']= array(
  '#theme' => 'digitalobjects_item',
  '#objid' => $keys);

  return $output;
  } else {
    $nodes = node_load_multiple(array_keys($entities['node']));

    #gibt es eins, dann gib dieses zum editieren zurück
    #TODO falls mehrere existieren, was dann??

    #gehe durch alle gefundene
    foreach ($nodes as $node){
      if (isset($node->field_object_type['und'])){
        $tid = $node->field_object_type['und'][0]['tid'];
        $tax =taxonomy_term_load($tid);

        if ($tax->name == "primary"){ #'primary gefunden'
          return node_view($node, $view_mode = 'full');
        }
      }
    }
    #keine primary node gefunden, generische ausgabe
    $output['digitalobjects_item']= array(
        '#theme' => 'digitalobjects_item',
        '#objid' => $keys);
    return $output;
  }
}

/* implements digitalobject_theme */

function digitalobjects_theme(){

    return array(
        'digitalobjects_item'  => array(
            'variables' => array('objid' => NULL),
            'file' => 'digitalobjects.item.inc',
            'template' => 'digitalobjects-item',
        ),
        'digitalobjects_item_short'  => array(
            'variables' => array('objid' => NULL),
            'file' => 'digitalobjects.item.inc',
            'template' => 'digitalobjects-item-short',
        ),
        'digitalobjects_item_tools'  => array(
            'variables' => array('objid' => NULL),
            'template' => 'digitalobjects-item-tools',
        ),
        'digitalobjects_item_usage'  => array(
            'variables' => array('data' => NULL),
            'template' => 'digitalobjects-item-usage',
        ),

        'digitalobjects_currentCollection_block'  => array(
            'variables' => array('node' => NULL),
            'template' => 'digitalobjects-current-collection',
        ),
     /*   'digitalobjects_user_admin_page' => array(
         'variables' => array('digitalobjects_user_admin','uid' =>1),
        'template'  => 'digitalobjects-user-admin-form'
         ),*/
            );
}

function digitalobjects_forms($form_id, $args) {

  $forms['digitalobjects_user_admin_form']= array(
      'callback' => 'digitalobjects_user_admin',
      'callback arguments' => array('digitalobjects_user_admin_page'),
  );
  return $forms;
};




function digitalobjects_user_admin(array $form, array &$form_state) {

  $form['digitalcollection'] = array(
      '#type' => 'textfield',
      '#title' => t('Active collection'),
      '#size' => 15,
      '#default_value' => 'xxxx',
      '#attributes' => array('title' => t('Enter the terms id of the current collection.')),
  );


  $form['userid'] = array(
      '#type' => 'hidden',
  );
  #$form['actions'] = array('#type' => 'actions');
  $form['submit'] = array('#type' => 'submit', '#value' => t('Save'));
  $form['submit']['#submit'][] = 'digitalobjects_user_admin_form_submit';

  return $form;
}
/* implements a new field type for the digitalobject id */

function digitalobjects_field_info(){

  return array('digitalobjects_digitalobject' => array (
      'label' => t('Digital Object ID'),
      'description' => t('This field stores a digital object ID'),
      'settings' => array('max_length' =>20),
     'instance_settings' => array('text_processing' => 0),
      'default_widget' => 'digitalobject_default_textfield',
      'default_formatter' => 'digitalobjects_fullmetadata',
    ),
      'digitalobjects_digitalcollection' => array (
          'label' => t('Digital Collection ID'),
          'description' => t('This field stores a digital object ID'),
          'settings' => array('max_length' =>20),
          'instance_settings' => array('text_processing' => 0),
          'default_widget' => 'digitalcollection_default_textfield',
          'default_formatter' => 'digitalcollection_fullmetadata',
          ),

  );
}

function digitalobjects_field_formatter_info() {

  return array(
      // This formatter shows the obejct with fullmetadata
      'digitalobjects_fullmetadata' => array(
          'label' => t('Fullmetadata formatter'),
          'field types' => array('digitalobjects_digitalobject'),
      ),
      'digitalobjects_label' => array(
          'label' => t('Lable formatter'),
          'field types' => array('digitalobjects_digitalobject'),
      )
  );
  }






 function digitalobjects_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
    $element = array();


    switch ($display['type']) {
      // This formatter simply outputs the field as text and with a color.
      case 'digitalobjects_fullmetadata':
        foreach ($items as $delta => $item) {
          if ($item['objid']!=""){
            $element[$delta]['#markup'] = theme('digitalobjects_item_tools', array("objid" => $item['objid']));
            $element[$delta]['#markup'] .= theme('digitalobjects_item', array("objid" => $item['objid']));

          }
        }
        break;

      case 'digitalobjects_label':
        foreach ($items as $delta => $item) {
          if ($item['objid']!=""){

            $element[$delta]['#markup'] = theme('digitalobjects_item_short', array("objid" => $item['objid']));
            $element[$delta]['#markup'] .= theme('digitalobjects_item_tools', array("objid" => $item['objid']));
          }
        }
        break;
    }
    return $element;
 }


function digitalobjects_field_widget_info() {
  #TODO:both types should check if field is valid
   return array(
       'digitalobject_default_textfield' => array(
           'label' => t('digitalobject id'),
           'field types' => array('digitalobjects_digitalobject'),
       ),
       'digitalcollection_default_textfield' => array(
           'label' => t('digitalobject id'),
           'field types' => array('digitalobjects_digitalcollection'),
       )
       );
  }

 function digitalobjects_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {


   $field_name = $field['field_name'];
   $field_type = $field['type'];


   $value = isset($items[$delta]['objid']) ? $items[$delta]['objid'] : '';

   $widget = $element;
   $widget['#delta'] = $delta;

   switch ($instance['widget']['type']) {



     case 'digitalobject_default_textfield':
       $widget += array(
       '#type' => 'textfield',
       '#default_value' => $value,
       '#size' => 20,
       '#maxlength' => 20,
           );
       $element['objid'] = $widget;
           break;

     case 'digitalcollection_default_textfield':
             $widget += array(
             '#type' => 'textfield',
             '#default_value' => $value,
             '#size' => 20,
             '#maxlength' => 20,
             );
             break;


   }




   return $element;
 }


 function digitalobjects_field_is_empty($item, $field){
   return empty($item['objid']);
 }



function digitalobjects_user_admin_form_submit($form,$formstate){
  $value = $formstate['values']['digitalcollection'];
  $userid = $formstate['values']['userid'];

  $user=user_load($userid);


  $user->field_digitalobjects_cid['und'][0]['objid'] = $value;

  user_save($user);

}

function digitalobjects_block_info(){
  $blocks['digitalobjects_currentCollection'] = array(
      'info' => t('Current Collection'),
      'visibility' => BLOCK_VISIBILITY_PHP,
      'pages' => '<?php global $user; if ($user->uid == 0) return FALSE; else return TRUE; ?>'
  );
  return $blocks;
}

function digitalobjects_block_view($delta){

  switch($delta){

  case 'digitalobjects_currentCollection':
    /* display current cullection */

    global $user;

    $user_full=user_load($user->uid);

    if (isset($user_full->field_digitalobjects_cid['und'][0]['objid'])){
    $val = $user_full->field_digitalobjects_cid['und'][0]['objid'];
    } else {
      return;
    }
    $node = node_load($val);

    #$title = $node=>title;

   $block['content'] = theme('digitalobjects_currentCollection_block',array('node' => $node));

   $form['submit'] = array('#type' => 'submit', '#value' => t('Save'));
   $form['submit']['#submit'][] = 'digitalobjects_user_admin_collection';

   $submitForm=drupal_get_form('digitalobjects_change_collection_button_form');
   $block['content'] .= drupal_render($submitForm);
  }


  return $block;
}

function digitalobjects_change_collection_button_form(){
  include_once 'digitalobjects.collections.inc';
  global $user;
  $form['userid'] = array(

      '#type' => 'hidden',
      '#value' => $user->uid
  );

  $form['submit'] = array('#type' => 'submit', '#value' => t('change'));
  $form['submit']['#submit'][] = 'digitalobjects_user_admin_collection_submit';

  return $form;
}

function digitalobjects_permission() {
  return array(
      'manage private collections' => array(
          'title' => t('Manage private collections'),
          'description' => t('Allow users create and manage private collections.'),
      ),
      'administrate digitalobjects' => array(
          'title' => t('Administrate digitalobjects'),
          'description' => t('Can add commentaries to digital objects.'),
      ),
  );
}


function digitalobjects_pathologic_alter(&$url_params, $parts, $settings){
#bilder ohne jegliche pfad angaben werden auf sites/default/files/..
  if (preg_match('~^([^/]*)\.(png|gif|jpe?g)$~', $url_params['path'])){

    $url_params['path'] = 'sites/default/files/' . $url_params['path'];
  }

  if (preg_match('~^./([^/]*)\.pt$~', $url_params['path'],$matches)){

    $url_params['path'] = 'harriot/maps/' . $matches[1] . '.pt';
  }

}