Add analytics
[bus.git] / busui / owa / owa_coreAPI.php
blob:a/busui/owa/owa_coreAPI.php -> blob:b/busui/owa/owa_coreAPI.php
  <?php
   
  //
  // Open Web Analytics - An Open Source Web Analytics Framework
  //
  // Copyright 2006 Peter Adams. All rights reserved.
  //
  // Licensed under GPL v2.0 http://www.gnu.org/copyleft/gpl.html
  //
  // Unless required by applicable law or agreed to in writing, software
  // distributed under the License is distributed on an "AS IS" BASIS,
  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  // See the License for the specific language governing permissions and
  // limitations under the License.
  //
  // $Id$
  //
   
  require_once(OWA_BASE_DIR.'/owa_lib.php');
   
  /**
  * OWA Core API
  *
  * @author Peter Adams <peter@openwebanalytics.com>
  * @copyright Copyright &copy; 2006 Peter Adams <peter@openwebanalytics.com>
  * @license http://www.gnu.org/copyleft/gpl.html GPL v2.0
  * @category owa
  * @package owa
  * @version $Revision$
  * @since owa 1.0.0
  */
   
  class owa_coreAPI {
   
  public static function &singleton($params = array()) {
   
  static $api;
   
  if(!isset($api)):
  $api = new owa_coreAPI();
  endif;
   
  if(!empty($params)):
  $api->params = $params;
  endif;
   
  return $api;
  }
   
  public static function setupStorageEngine($type) {
   
  if (!class_exists('owa_db')) {
  require_once(OWA_BASE_CLASSES_DIR.'owa_db.php');
  }
   
  if ($type) {
   
  $connection_class = "owa_db_" . $type;
   
  if (!class_exists($connection_class)) {
  $connection_class_path = OWA_PLUGINS_DIR.'/db/' . $connection_class . ".php";
   
  if (!require_once($connection_class_path)) {
  owa_coreAPI::error(sprintf('Cannot locate proper db class at %s.', $connection_class_path));
  return false;
  }
  }
   
  }
   
  return true;
   
  }
   
  public static function &dbSingleton() {
   
  static $db;
   
  if (!isset($db)) {
   
  $db_type = owa_coreAPI::getSetting('base', 'db_type');
  $ret = owa_coreAPI::setupStorageEngine($db_type);
   
  if (!$ret) {
  owa_coreAPI::error(sprintf('Cannot locate proper db class at %s. Exiting.', $connection_class_path));
  return;
  } else {
  $connection_class = 'owa_db_'.$db_type;
  $db = new $connection_class(
  owa_coreAPI::getSetting('base','db_host'),
  owa_coreAPI::getSetting('base','db_name'),
  owa_coreAPI::getSetting('base','db_user'),
  owa_coreAPI::getSetting('base','db_password'),
  owa_coreAPI::getSetting('base','db_force_new_connections'),
  owa_coreAPI::getSetting('base','db_make_persistant_connections')
  );
  }
  }
   
  return $db;
  }
   
  public static function &configSingleton($params = array()) {
   
  static $config;
   
  if(!isset($config)):
   
  if (!class_exists('owa_settings')):
  require_once(OWA_BASE_CLASS_DIR.'settings.php');
  endif;
   
  $config = owa_coreAPI::supportClassFactory('base', 'settings');
   
  endif;
   
  return $config;
  }
   
  public static function &errorSingleton() {
   
  static $e;
   
  if(!$e) {
   
  if (!class_exists('owa_error')):
  require_once(OWA_BASE_CLASS_DIR.'error.php');
  endif;
   
  $e = owa_coreAPI::supportClassFactory('base', 'error');
   
  }
   
  return $e;
  }
   
  public static function getSetting($module, $name) {
   
  $s = &owa_coreAPI::configSingleton();
  return $s->get($module, $name);
  }
   
  public static function setSetting($module, $name, $value, $persist = false) {
   
  $s = &owa_coreAPI::configSingleton();
   
  if ($persist === true) {
  $s->persistSetting($module, $name, $value);
  } else {
  $s->setSetting($module, $name, $value);
  }
   
  }
   
  public static function persistSetting($module, $name, $value) {
   
  $s = &owa_coreAPI::configSingleton();
  $s->persistSetting($module, $name, $value);
   
  }
   
  public static function getSiteSetting($site_id, $name) {
   
  $site = owa_coreAPI::entityFactory('base.site');
  $site->load( $site->generateId( $site_id ) );
  if ( $site->wasPersisted() ) {
   
  $settings = $site->get('settings');
  if (!empty($settings)) {
  if ( array_key_exists($name, $settings) ) {
  return $settings[$name];
  }
  }
  }
  }
   
  public static function persistSiteSetting($site_id, $name, $value) {
   
  $site = owa_coreAPI::entityFactory('base.site');
  $site->load( $site->generateId( $site_id ) );
  if ( $site->wasPersisted() ) {
  $settings = $site->get('settings');
  if ( ! $settings ) {
  $settings = array();
  }
  $settings[$name] = $value;
  $site->set('settings', $settings);
  $site->update();
  }
  }
   
  public static function getSiteSettings($site_id) {
   
  $site = owa_coreAPI::entityFactory('base.site');
  $site->load( $site->generateId( $site_id ) );
  if ( $site->wasPersisted() ) {
   
  $settings = $site->get('settings');
   
  if ( $settings ) {
  return $settings;
  } else {
  return array();
  }
  }
   
  }
   
  public static function getAllRoles() {
   
  $caps = owa_coreAPI::getSetting('base', 'capabilities');
  return array_keys($caps);
  }
   
  public static function &getCurrentUser() {
   
  $s = &owa_coreAPI::serviceSingleton();
  return $s->getCurrentUser();
  }
   
  /**
  * check to see if the current user has a capability
  * always returns a bool
  */
  public static function isCurrentUserCapable($capability) {
   
  $cu = &owa_coreAPI::getCurrentUser();
  owa_coreAPI::debug("Current User Role: ".$cu->getRole());
  owa_coreAPI::debug("Current User Authentication: ".$cu->isAuthenticated());
  $ret = $cu->isCapable($capability);
  owa_coreAPI::debug("Is current User capable: ".$ret);
  return $ret;
  }
   
  public static function isCurrentUserAuthenticated() {
   
  $cu = &owa_coreAPI::getCurrentUser();
  return $cu->isAuthenticated();
  }
   
  public static function &serviceSingleton() {
   
  static $s;
   
  if(empty($s)) {
   
  if (!class_exists('owa_service')) {
  require_once(OWA_BASE_CLASS_DIR.'service.php');
  }
   
  $s = owa_coreAPI::supportClassFactory('base', 'service');
   
  }
   
  return $s;
  }
   
  public static function &cacheSingleton($params = array()) {
   
  static $cache;
   
  if ( !isset ( $cache ) ) {
  $cache_type = owa_coreAPI::getSetting('base', 'cacheType');
   
  switch ($cache_type) {
   
  case "memcached":
  $implementation = array('owa_memcachedCache', OWA_BASE_CLASS_DIR.'memcachedCache.php');
  break;
  default:
  $implementation = array('owa_fileCache', OWA_BASE_CLASS_DIR.'fileCache.php');
   
  }
   
  if ( ! class_exists( $implementation[0] ) ) {
  require_once( $implementation[1] );
  }
  // make this plugable
  $cache = new $implementation[0];
  }
   
  return $cache;
  }
   
  public static function requestContainerSingleton() {
   
  static $request;
   
  if(!isset($request)):
   
  if (!class_exists('owa_requestContainer')):
  require_once(OWA_DIR.'owa_requestContainer.php');
  endif;
   
  $request = owa_lib::factory(OWA_DIR, '', 'owa_requestContainer');
   
  endif;
   
  return $request;
   
  }
   
  public static function moduleRequireOnce($module, $class_dir, $file) {
   
  if (!empty($class_dir)) {
   
  $class_dir .= DIRECTORY_SEPARATOR;
   
  }
   
  $full_file_path = OWA_BASE_DIR.'/modules/'.$module.DIRECTORY_SEPARATOR.$class_dir.$file.'.php';
   
  if (file_exists($full_file_path)) {
  return require_once($full_file_path);
  } else {
  owa_coreAPI::debug("moduleRequireOnce says no file found at: $full_file_path");
  return false;
  }
  }
   
  public static function moduleFactory($modulefile, $class_suffix = null, $params = '', $class_ns = 'owa_') {
   
  list($module, $file) = explode(".", $modulefile);
  $class = $class_ns.$file.$class_suffix;
  //print $class;
  // Require class file if class does not already exist
  if(!class_exists($class)):
  owa_coreAPI::moduleRequireOnce($module, '', $file);
  endif;
   
  $obj = owa_lib::factory(OWA_BASE_DIR.'/modules/'.$module, '', $class, $params);
   
  //if (isset($obj->module)):
  $obj->module = $module;
  //endif;
   
  return $obj;
  }
   
  public static function moduleGenericFactory($module, $sub_directory, $file, $class_suffix = null, $params = '', $class_ns = 'owa_') {
   
  $class = $class_ns.$file.$class_suffix;
   
  // Require class file if class does not already exist
  if(!class_exists($class)):
  owa_coreAPI::moduleRequireOnce($module, $sub_directory, $file);
  endif;
   
  $obj = owa_lib::factory(OWA_DIR.'modules'.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$sub_directory, '', $class, $params);
   
  return $obj;
  }
   
  /**
  * Produces Module Classes (module.php)
  *
  * @return Object module class object
  */
  public static function moduleClassFactory($module) {
   
  if (!class_exists('owa_module')):
  require_once(OWA_BASE_CLASSES_DIR.'owa_module.php');
  endif;
   
  require_once(OWA_BASE_DIR.'/modules/'.$module.'/module.php');
   
  return owa_lib::factory(OWA_BASE_CLASSES_DIR.$module, 'owa_', $module.'Module');
   
  }
   
   
  public static function updateFactory($module, $filename, $class_ns = 'owa_') {
   
  require_once(OWA_BASE_CLASS_DIR.'update.php');
   
  //$obj = owa_coreAPI::moduleGenericFactory($module, 'updates', $filename, '_update');
  $class = $class_ns.$module.'_'.$filename.'_update';
   
  // Require class file if class does not already exist
  if(!class_exists($class)):
  owa_coreAPI::moduleRequireOnce($module, 'updates', $filename);
  endif;
   
  $obj = owa_lib::factory(OWA_DIR.'modules'.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.'updates', '', $class);
   
  $obj->module_name = $module;
  if (!$obj->schema_version) {
  $obj->schema_version = $filename;
  }
  return $obj;
  }
   
  public static function subViewFactory($subview, $params = array()) {
   
  list($module, $class) = explode(".", $subview);
  //print_r($module.' ' . $class);
  //owa_lib::moduleRequireOnce($module, $class);