[ Index ]

PHP Cross Reference of Akelos Framework

title

Body

[close]

/ -> AkBaseModel.php (source)

   1  <?php
   2  /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
   3  
   4  // +----------------------------------------------------------------------+
   5  // | Akelos Framework - http://www.akelos.org                             |
   6  // +----------------------------------------------------------------------+
   7  // | Copyright (c) 2002-2006, Akelos Media, S.L.  & Bermi Ferrer Martinez |
   8  // | Released under the GNU Lesser General Public License, see LICENSE.txt|
   9  // +----------------------------------------------------------------------+
  10  
  11  /**
  12   * @package ActiveRecord
  13   * @subpackage Base
  14   * @author Bermi Ferrer <bermi a.t akelos c.om>
  15   * @copyright Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org
  16   * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
  17   */
  18  
  19  defined('AK_LOG_EVENTS') ? null : define('AK_LOG_EVENTS', false);
  20  
  21  require_once (AK_LIB_DIR.DS.'Ak.php');
  22  require_once (AK_LIB_DIR.DS.'AkInflector.php');
  23  require_once (AK_LIB_DIR.DS.'AkObject.php');
  24  
  25  /**
  26   * This is the base class for all sort of models (Mailers or Active records)
  27   * It handles the naming conventions for models, as in PHP4 all methods appear lowercased
  28   * we to work around to find the real case of the methos to apply conventions.
  29   * 
  30   * See also <AkActiveRecord> and <AkActionMailer> as those are the ones you will usually inherit from
  31  *
  32  * @author Bermi Ferrer <bermi a.t akelos c.om>
  33  * @copyright Copyright (c) 2002-2005, Akelos Media, S.L. http://www.akelos.org
  34  * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
  35  */
  36  class AkBaseModel extends AkObject
  37  {
  38      var $_modelName;
  39  
  40      /**
  41      * Returns current model name
  42      */
  43      function getModelName()
  44      {
  45          if(!isset($this->_modelName)){
  46              if(!$this->setModelName()){
  47                  trigger_error(Ak::t('Unable to fetch current model name'),E_USER_NOTICE);
  48              }
  49          }
  50          return $this->_modelName;
  51      }
  52  
  53      /**
  54      * Sets current model name
  55      *
  56      * Use this option if the model name can't be guessed by the Active Record
  57      */
  58      function setModelName($model_name = null)
  59      {
  60          if(!empty($model_name)){
  61              $this->_modelName = $model_name;
  62          }else{
  63              $this->_modelName = $this->_getModelName(get_class($this));
  64          }
  65          return true;
  66      }
  67  
  68      /**
  69       * This method will nicely handle model names even  on 
  70       * PHP where class names are all lowercased
  71       */
  72      function _getModelName($class_name)
  73      {
  74          if(AK_PHP5){
  75              return $class_name;
  76          }
  77          $included_models = $this->_getIncludedModelNames();
  78          if(!in_array($class_name, $included_models)){
  79              $class_name = strtolower($class_name);
  80              foreach ($included_models as $included_model){
  81                  if($class_name == strtolower($included_model)){
  82                      return $included_model;
  83                  }
  84              }
  85  
  86              trigger_error(Ak::t('The Akelos Framework could not automatically configure your model name.'.
  87              ' This might be caused because your model file is not located on %path. Please call $this->setModelName("YourModelName");'.
  88              ' in your model constructor in order to make this work.',array('%path'=>AK_MODELS_DIR.DS)), E_USER_ERROR);
  89              return false;
  90          }
  91      }
  92  
  93  
  94  
  95      function getParentModelName()
  96      {
  97          if(!isset($this->_parentModelName)){
  98              if(!$this->setParentModelName()){
  99                  return false;
 100              }
 101          }
 102          return $this->_parentModelName;
 103      }
 104  
 105      function setParentModelName($model_name = null)
 106      {
 107          $got_errors = false;
 108          if(!empty($model_name)){
 109              $this->_parentModelName = $model_name;
 110          }else{
 111              $class_name = AkInflector::camelize(get_parent_class($this));
 112              if(!AK_PHP5){
 113                  $included_models = $this->_getIncludedModelNames();
 114                  if(!in_array($class_name, $included_models)){
 115                      $class_name = strtolower($class_name);
 116                      foreach ($included_models as $included_model){
 117                          if($class_name == strtolower($included_model)){
 118                              $this->_parentModelName = $included_model;
 119                              return true;
 120                          }
 121                      }
 122                      $got_errors = true;
 123                  }
 124              }
 125              if($got_errors || $class_name == 'AkActiveRecord'){
 126                  trigger_error(Ak::t('The Akelos Framework could not automatically configure your model name.'.
 127                  ' This might be caused because your model file is not located on %path. Please call $this->setParentModelName("YourParentModelName");'.
 128                  ' in your model constructor in order to make this work.',array('%path'=>AK_MODELS_DIR.DS)), E_USER_ERROR);
 129                  return false;
 130              }
 131              $this->_parentModelName = $class_name;
 132          }
 133          return true;
 134      }
 135  
 136      function t($string, $array = null)
 137      {
 138          return Ak::t($string, $array, AkInflector::underscore($this->getModelName()));
 139      }
 140  
 141      function _getIncludedModelNames()
 142      {
 143          $included_files = get_included_files();
 144          $models = array();
 145          foreach ($included_files as $file_name){
 146              if(strstr($file_name,AK_MODELS_DIR)){
 147                  $models[] = AkInflector::camelize(str_replace(array(AK_MODELS_DIR.DS,'.php'),'',$file_name));
 148              }
 149          }
 150          return $models;
 151      }
 152  }
 153  
 154  
 155  
 156  if(!defined('ADODB_OUTP')){
 157      define('ADODB_OUTP', 'ak_show_sql_debug');
 158      function ak_show_sql_debug($message){
 159          if(is_array($message)){
 160              foreach ($message as $msg){
 161                  return ak_show_sql_debug($msg);
 162              }
 163          }
 164          $skip = array(
 165          'SELECT LAST_INSERT_ID',
 166          'SHOW COLUMNS',
 167          'SHOW TABLES',
 168          'SET AUTOCOMMIT',
 169          );
 170  
 171          $message = preg_replace('/^\([a-z]+\): /', '',trim($message,"\n-"));
 172          if(preg_match('/^('. join("|", $skip) .')/', $message)){
 173              return;
 174          }
 175  
 176          if(AK_CLI){
 177              "\n$message;";
 178          }else{
 179              Ak::trace("$message");
 180          }
 181      }
 182  }
 183  
 184  ?>


Generated: Mon Oct 27 12:43:49 2008 Cross-referenced by PHPXref 0.6