Adding extra charsets for ActionMailer unit tests, if you're looking to parse incomin...
[akelos.git] / lib / AkBaseModel.php
blob72d3ad77b0364a51950d313ca3f94e871ebe8045
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
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 // +----------------------------------------------------------------------+
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>
19 defined('AK_LOG_EVENTS') ? null : define('AK_LOG_EVENTS', false);
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');
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.
30 * See also <AkActiveRecord> and <AkActionMailer> as those are the ones you will usually inherit from
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>
36 class AkBaseModel extends AkObject
38 var $_modelName;
40 /**
41 * Returns current model name
43 function getModelName()
45 if(!isset($this->_modelName)){
46 if(!$this->setModelName()){
47 trigger_error(Ak::t('Unable to fetch current model name'),E_USER_NOTICE);
50 return $this->_modelName;
53 /**
54 * Sets current model name
56 * Use this option if the model name can't be guessed by the Active Record
58 function setModelName($model_name = null)
60 if(!empty($model_name)){
61 $this->_modelName = $model_name;
62 }else{
63 $this->_modelName = $this->_getModelName(get_class($this));
65 return true;
68 /**
69 * This method will nicely handle model names even on
70 * PHP where class names are all lowercased
72 function _getModelName($class_name)
74 if(AK_PHP5){
75 return $class_name;
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;
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;
95 function getParentModelName()
97 if(!isset($this->_parentModelName)){
98 if(!$this->setParentModelName()){
99 return false;
102 return $this->_parentModelName;
105 function setParentModelName($model_name = null)
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;
122 $got_errors = true;
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;
131 $this->_parentModelName = $class_name;
133 return true;
136 function _getIncludedModelNames()
138 $included_files = get_included_files();
139 $models = array();
140 foreach ($included_files as $file_name){
141 if(strstr($file_name,AK_MODELS_DIR)){
142 $models[] = AkInflector::camelize(str_replace(array(AK_MODELS_DIR.DS,'.php'),'',$file_name));
145 return $models;