<?php
// $Id$

/**
 *  List of contexts that are NOT defined by this module
 */
function mapi_profile_standard() {
  $list = array();

  // gets the list of contexts available, skipping the mapi_profile
  foreach (module_implements('mapi_context') as $module) {
    // skip doing the mapi_profile section
    if ($module == 'mapi_profile') {
      continue;
    }
    $func = $module .'_mapi_context';
    $results = $func('list', null, null, null);
    if (is_string($results)) {
      $results = array($results);
    }
    if (is_array($results)) {
      foreach ($results as $value) {
        if (is_string($value)) {
          $list[] = $value;
        }
      }
    }
  }

  return $list;
}

/**
 *  Returns a list of user defined contexts
 */
function mapi_profile_user() {
  $list = array();

  // user-defined profiles
  $results = db_query("SELECT name FROM {mapi_contexts}");
  while ($object = db_fetch_object($results)) {
    $list[] = $object->name;
  }
  return $list;
}

/**
 *  Returns a list of the defined contexts
 */
function mapi_profile_defined() {
  $list = array();

  // user-defined profiles
  $results = db_query("SELECT DISTINCT(context) FROM {mapi_profiles}");
  while ($object = db_fetch_object($results)) {
    $list[] = $object->context;
  }
  return $list;
}


/**
 *  Returns a list of contexts which are available to use.
 */
function mapi_profile_free() {
  // loads all the contexts within the database
  $list = array();
  $result = db_query("SELECT DISTINCT(context) FROM {mapi_profiles}");
  while ($object = db_fetch_object($result)) {
    $list[] = $object->context;
  }

  // get the list of contexts not currently used
  return array_diff(mapi_context_list(), $list);
}

/**
 *
 */
function mapi_profile_used_extensions($context) {
  $profile = mapi_profile_load($context);

  // build actual extension list
  $exts = array();
  foreach ($profile as $object) {
    $exts = array_merge($exts, explode('|', $object->exts));
  }

  return $exts;
}

/**
 *  Returns a list of extensions which are available for use within a context
 */
function mapi_profile_free_extensions($context) {
  return array_diff(mapi_extension_list(), mapi_profile_used_extensions($context));
}

/**
 *  Gets the details for the profile for an extension
 */
/*
function mapi_profile_details($context, $ext) {
  $profile = mapi_profile_load($context);
  foreach ($profile as $object) {
    if (in_array($ext, explode('|', $object->exts))) {
      return array('presenter' => $object->presenter, 'settings' => $object->settings);
    }
  }
  return array('presenter' => false, 'settings' => false);
}
*/

/**
 *
 */
/*
function mapi_profile_create($context, $exts, $presenter, $derivative, $settings) {
  $profile = mapi_profile_load($context);

  // check context existance
  if (!in_array($context, mapi_context_list())) {
    return false;
  }

  // do basic check for presenter and derivative existance
  if ((!in_array($presenter, mapi_presenter_list()) && $presenter !='generic') || (!in_array($derivative, mapi_derivative_list()) && $derivative != 'original')) {
    return false;
  }


  // make sure no overlaps on extensions within contexts
  if (count(array_intersect(mapi_profile_used_extensions($context), $exts))) {
    return false;
  }

  $settings = (is_array($settings) ? $settings : array());

  // save profile into database
  $pid = db_next_id("{mapi_profiles}_pid");
  db_query("INSERT INTO {mapi_profiles} (pid, context, exts, presenter, derivative, settings) VALUES (%d, '%s', '%s', '%s', '%s', '%s')",
    $pid, $context, '|'. implode('|', $exts) .'|', $presenter, $derivative, serialize($settings));

  // reload, and return profile
  return mapi_profile_load($context, true);
}
*/

/**
 *  Deletes a profile based on the context
 */
/*
function mapi_profile_delete($context) {
  db_query("DELETE FROM {mapi_profiles} WHERE context='%s'");

  // refresh cache
  mapi_profile_load($context, true);
}
*/

/**
 *  Updates the profile for the context
 */
/*
function mapi_profile_update($context, $profile) {
  if (!in_array($context, mapi_context_list()) || !is_array($profile)) {
    return false;
  }

  // update current context profiles
  foreach ($profile as $pid => $item) {
    if ($pid) {
      db_query("UPDATE {mapi_profiles} SET presenter='%s', derivative='%s', settings='%s' WHERE pid = %d",
        $item['presenter'], $item['derivative'], serialize($item['settings']), $pid);
    }
    else {
      $pid = db_next_id("{mapi_profiles}_pid");
      db_query("INSERT INTO {mapi_profiles} (pid, context, exts, presenter, derivative, settings) VALUES (%d, '%s', '%s', '%s', '%s', '%s')",
        $pid, $context, '|'. implode('|', $item['exts']) .'|', $item['presenter'], $item['derivative'], serialize($item['settings']));
    }
  }

  // remove those context which are NOT listed in the profile.
  $results = db_query("SELECT pid FROM {mapi_profiles} WHERE context='%s'", $context);
  while ($object = db_fetch_object($results)) {
    if (!array_key_exists($object->pid, $profile)) {
      db_query("DELETE FROM {mapi_profiles} WHERE pid = %d", $object->pid);
    }
  }

  return mapi_profile_load($context);
}
*/
