<?php
// $Id: rdf_schema.module,v 1.2 2008/03/15 11:42:34 arto Exp $
/**
 * rdf_schema.module - Provides an RDFS vocabulary for Drupal data.
 *
 * @author Arto Bendiken <http://bendiken.net/>
 * @copyright Copyright (c) 2007-2008 Arto Bendiken. All rights reserved.
 * @license GPL <http://creativecommons.org/licenses/GPL/2.0/>
 * @package rdf.module
 * @subpackage rdf_schema.module
 */

//////////////////////////////////////////////////////////////////////////////
// Module settings

define('RDF_SCHEMA_URI', variable_get('rdf_schema_uri', url('rdf/schema', array('absolute' => TRUE)) . '/'));
define('RDF_SCHEMA_MODULE_URI', 'http://drupal.org/project/rdf#schema');

//////////////////////////////////////////////////////////////////////////////
// Core API hooks

/**
 * Implementation of hook_init().
 */
function rdf_schema_init() {
  require_once drupal_get_path('module', 'rdf_schema') . '/rdf_schema.inc';
}

/**
 * Implementation of hook_menu().
 */
function rdf_schema_menu() {
  return array(
    'rdf/schema' => array(
      'title' => 'RDF schema',
      'type' => MENU_CALLBACK,
      'access arguments' => array('access RDF data'),
      'page callback' => 'rdf_export',
      'page arguments' => array('rdf_schema_data', 'schema'),
      'file' => '../rdf_export/rdf_export.pages.inc', // HACK
    ),
  );
}

/**
 * Implementation of hook_form_alter().
 */
function rdf_schema_form_alter(&$form, $form_state, $form_id) {
  switch ($form_id) {
    // Administer >> Site configuration >> RDF settings
    case 'rdf_admin_settings':
      $form['schema'] = array('#type' => 'fieldset', '#title' => t('RDF Schema'), '#collapsible' => TRUE, '#collapsed' => FALSE, '#weight' => 10);
      $form['schema']['rdf_schema_uri'] = array('#type' => 'textfield', '#title' => t('RDFS base URI'), '#default_value' => RDF_SCHEMA_URI, '#maxlength' => 255, '#required' => TRUE, '#description' => t('') /* TODO */);
      break;

    // Administer >> User management >> Profiles >> Edit
    case 'profile_field_form':
      // The submitted value is stored using the callback function defined below.
      $form['rdf_mapping'] = array('#type' => 'fieldset', '#title' => t('RDF schema settings'), '#collapsible' => TRUE, '#collapsed' => FALSE, '#weight' => 1);
      $form['rdf_mapping']['rdf_property'] = array('#type' => 'textfield', '#title' => t('RDF property'), '#default_value' => isset($form['fid']['#value']) ? variable_get('rdf_schema_property_profile_' . $form['fid']['#value'], '') : '', '#maxlength' => 255, '#description' => t('') /* TODO */);
      $form['submit']['#weight'] = 40;
      $form['#submit'][] = 'rdf_schema_profile_field_form_submit';
      break;

    // Administer >> Content management >> Taxonomy >> Edit vocabulary
    case 'taxonomy_form_vocabulary':
      // The submitted value is stored in our implementation of hook_taxonomy()
      $form['rdf_mapping'] = array('#type' => 'fieldset', '#title' => t('RDF schema settings'), '#collapsible' => TRUE, '#collapsed' => FALSE, '#weight' => 1);
      $form['rdf_mapping']['rdf_property'] = array('#type' => 'textfield', '#title' => t('RDF property'), '#default_value' => isset($form['vid']['#value']) ? variable_get('rdf_schema_property_vocabulary_' . $form['vid']['#value'], '') : '', '#maxlength' => 255, '#description' => t('') /* TODO */);
      $form['submit']['#weight'] = 40;
      $form['delete']['#weight'] = 45;
      break;

    // Administer >> Content management >> Content types >> Edit
    case 'node_type_form':
      // The submitted value is auto-saved by node.module as a configuration variable with a name in the form 'rdf_schema_class_TYPE'
      $form['rdf_mapping'] = array('#type' => 'fieldset', '#title' => t('RDF schema settings'), '#collapsible' => TRUE, '#collapsed' => TRUE, '#weight' => 1);
      $form['rdf_mapping']['rdf_schema_class'] = array('#type' => 'textfield', '#title' => t('RDF class'), '#default_value' => '', '#maxlength' => 255, '#description' => t('') /* TODO */);
      break;

    // Administer >> Content management >> Content types >> (content type) >> Manage fields >> Configure
    case '_content_admin_field':
      // TODO
      break;
  }
}

/**
 * Implementation of hook_taxonomy().
 */
function rdf_schema_taxonomy($op, $type, $array = NULL) {
  if ($type == 'vocabulary') {
    switch ($op) {
      case 'insert':
      case 'update':
        if (!empty($array['rdf_property'])) {
          variable_set('rdf_schema_property_vocabulary_' . $array['vid'], $array['rdf_property']);
        }
        else {
          variable_del('rdf_schema_property_vocabulary_' . $array['vid']);
        }
        break;
      case 'delete':
        variable_del('rdf_schema_property_vocabulary_' . $array['vid']);
        break;
    }
  }
}

//////////////////////////////////////////////////////////////////////////////
// Forms API callbacks

function rdf_schema_profile_field_form_submit($form, &$form_state) {
  extract($form_state['values'], EXTR_SKIP | EXTR_REFS);

  variable_set('rdf_schema_property_profile_' . $fid, $rdf_property);
}

//////////////////////////////////////////////////////////////////////////////
// RDF API hooks

/**
 * Implementation of hook_rdf_namespaces().
 */
function rdf_schema_rdf_namespaces() {
  $namespaces = array('drupal' => RDF_SCHEMA_URI);
  foreach (rdf_schema_get_entities() as $entity) {
    $namespaces[$entity] = RDF_SCHEMA_URI . $entity . '#';
  }
  return $namespaces;
}

/**
 * Implementation of hook_rdf_contexts().
 */
function rdf_schema_rdf_contexts() {
  return array(RDF_SITE_URI, RDF_SCHEMA_URI);
}

/**
 * Implementation of hook_rdf_classes().
 */
function rdf_schema_rdf_classes() {
  return array(
    'user' => array(
      'title'   => t('User'),
      'module'  => 'user',
      'table'   => 'users',
      'query'   => 'SELECT uid FROM {users} WHERE uid > 0',
      'uri'     => 'user/%uid',
      'enabled' => TRUE,
    ),
    'vocabulary' => array(
      'title'   => t('Taxonomy vocabulary'),
      'module'  => 'taxonomy',
      'table'   => 'vocabulary',
      'query'   => 'SELECT vid FROM {vocabulary}',
      // NOTE: Drupal vocabularies don't actually have dereferenceable URIs
      'uri'     => 'taxonomy/vocabulary/%vid',
      'enabled' => FALSE, // TODO
    ),
    'term' => array(
      'title'   => t('Taxonomy term'),
      'module'  => 'taxonomy',
      'table'   => 'term_data',
      'query'   => 'SELECT tid FROM {term_data}',
      'uri'     => 'taxonomy/term/%tid',
      'enabled' => FALSE, // TODO
    ),
    'node' => array(
      'title'   => t('Node'),
      'module'  => 'node',
      'table'   => 'node',
      'query'   => 'SELECT nid FROM {node}',
      'uri'     => 'node/%nid',
      'load'    => 'node_load',
      'enabled' => TRUE,
    ),
    'comment' => array(
      'title'   => t('Comment'),
      'module'  => 'comment',
      'table'   => 'comments',
      'query'   => 'SELECT nid, cid FROM {comments}',
      'uri'     => 'node/%nid#comment-%cid',
      'enabled' => FALSE, // TODO
    ),
    'path' => array(
      'title'   => t('URL alias'),
      'module'  => 'path',
      'table'   => 'url_alias',
      'query'   => 'SELECT dst FROM {url_alias}',
      'uri'     => '%dst',
      'enabled' => FALSE, // TODO
    ),
    'variable' => array(
      'title'   => t('Setting'),
      'module ' => 'system',
      'table'   => 'variable',
      'query'   => 'SELECT name FROM {variable}',
      'uri'     => 'rdf/variable/%name',
      'enabled' => FALSE, // TODO
    ),
  );
}

/**
 * Implementation of hook_rdf_properties().
 */
function rdf_schema_rdf_properties() {
  return array(
    // TODO
  );
}


/**
 * Implementation of hook_rdf_resources().
 */
function rdf_schema_rdf_resources($context) {
  switch ($context) {
    case NULL:
      return array_merge(rdf_schema_get_resources(), rdf_schema_get_classes());
    case RDF_SITE_URI:
      return rdf_schema_get_resources();
    case RDF_SCHEMA_URI:
      return rdf_schema_get_classes();
  }
}
