Ejemplo n.º 1
0
 /**
  * Constructor
  *
  * Initialize DataMapper.
  */
 function DataMapper()
 {
     $this->_assign_libraries();
     $this->_load_languages();
     $this->_load_helpers();
     // Determine model name
     if (empty($this->model)) {
         $this->model = singular(get_class($this));
     }
     // Load stored config settings by reference
     foreach (array_keys(DataMapper::$config) as $key) {
         // Only if they're not already set
         if (empty($this->{$key})) {
             $this->{$key} =& DataMapper::$config[$key];
         }
     }
     // Load model settings if not in common storage
     if (!array_key_exists($this->model, DataMapper::$common)) {
         // If model is 'datamapper' then this is the initial autoload by CodeIgniter
         if ($this->model == 'datamapper') {
             // Load config settings
             $this->config->load('datamapper', TRUE, TRUE);
             // Get and store config settings
             DataMapper::$config = $this->config->item('datamapper');
             return;
         }
         // Determine table name
         if (empty($this->table)) {
             $this->table = plural(get_class($this));
         }
         // Add prefix to table
         $this->table = $this->prefix . $this->table;
         // Convert validation into associative array by field name
         $associative_validation = array();
         foreach ($this->validation as $validation) {
             // Populate associative validation array
             $associative_validation[$validation['field']] = $validation;
         }
         $this->validation = $associative_validation;
         // Get and store the table's field names and meta data
         $fields = $this->db->field_data($this->table);
         // Store only the field names and ensure validation list includes all fields
         foreach ($fields as $field) {
             // Populate fields array
             $this->fields[] = $field->name;
             // Add validation if current field has none
             if (!array_key_exists($field->name, $this->validation)) {
                 $this->validation[$field->name] = array('field' => $field->name, 'label' => '', 'rules' => array());
             }
         }
         // Store common model settings
         DataMapper::$common[$this->model]['table'] = $this->table;
         DataMapper::$common[$this->model]['fields'] = $this->fields;
         DataMapper::$common[$this->model]['validation'] = $this->validation;
     }
     // Load stored common model settings by reference
     foreach (array_keys(DataMapper::$common[$this->model]) as $key) {
         $this->{$key} =& DataMapper::$common[$this->model][$key];
     }
     // Clear object properties to set at default values
     $this->clear();
 }
Ejemplo n.º 2
0
 public function DataMapper($id = NULL)
 {
     $this->_dmz_assign_libraries();
     $this_class = strtolower(get_class($this));
     $is_dmz = $this_class == 'datamapper';
     if ($is_dmz) {
         $this->_load_languages();
         $this->_load_helpers();
     }
     // this is to ensure that singular is only called once per model
     if (isset(DataMapper::$common[DMZ_CLASSNAMES_KEY][$this_class])) {
         $common_key = DataMapper::$common[DMZ_CLASSNAMES_KEY][$this_class];
     } else {
         DataMapper::$common[DMZ_CLASSNAMES_KEY][$this_class] = $common_key = singular($this_class);
     }
     // Determine model name
     if (empty($this->model)) {
         $this->model = $common_key;
     }
     // Load stored config settings by reference
     foreach (DataMapper::$config as $config_key => &$config_value) {
         // Only if they're not already set
         if (property_exists($this, $config_key)) {
             $this->{$config_key} =& $config_value;
         }
     }
     // Load model settings if not in common storage
     if (!isset(DataMapper::$common[$common_key])) {
         // If model is 'datamapper' then this is the initial autoload by CodeIgniter
         if ($is_dmz) {
             // Load config settings
             $this->config->load('datamapper', TRUE, TRUE);
             // Get and store config settings
             DataMapper::$config = $this->config->item('datamapper');
             // now double check that all required config values were set
             foreach (DataMapper::$_dmz_config_defaults as $config_key => $config_value) {
                 if (empty(DataMapper::$config[$config_key])) {
                     DataMapper::$config[$config_key] = $config_value;
                 }
             }
             DataMapper::_load_extensions(DataMapper::$global_extensions, DataMapper::$config['extensions']);
             unset(DataMapper::$config['extensions']);
             return;
         }
         // load language file, if requested and it exists
         if (!empty($this->lang_file_format)) {
             $lang_file = str_replace(array('${model}', '${table}'), array($this->model, $this->table), $this->lang_file_format);
             $deft_lang = $this->config->item('language');
             $idiom = $deft_lang == '' ? 'english' : $deft_lang;
             if (file_exists(APPPATH . 'language/' . $idiom . '/' . $lang_file . '_lang' . EXT)) {
                 $this->lang->load($lang_file, $idiom);
             }
         }
         $loaded_from_cache = FALSE;
         // Load in the production cache for this model, if it exists
         if (!empty(DataMapper::$config['production_cache'])) {
             // check if it's a fully qualified path first
             if (!is_dir($cache_folder = DataMapper::$config['production_cache'])) {
                 // if not, it's relative to the application path
                 $cache_folder = APPPATH . DataMapper::$config['production_cache'];
             }
             if (file_exists($cache_folder) && is_dir($cache_folder) && is_writeable($cache_folder)) {
                 $cache_file = $cache_folder . '/' . $common_key . EXT;
                 if (file_exists($cache_file)) {
                     include $cache_file;
                     if (isset($cache)) {
                         DataMapper::$common[$common_key] =& $cache;
                         unset($cache);
                         // allow subclasses to add initializations
                         if (method_exists($this, 'post_model_init')) {
                             $this->post_model_init(TRUE);
                         }
                         // Load extensions (they are not cacheable)
                         $this->_initiate_local_extensions($common_key);
                         $loaded_from_cache = TRUE;
                     }
                 }
             }
         }
         if (!$loaded_from_cache) {
             // Determine table name
             if (empty($this->table)) {
                 $this->table = strtolower(plural(get_class($this)));
             }
             // Add prefix to table
             $this->table = $this->prefix . $this->table;
             $this->_field_tracking = array('get_rules' => array(), 'matches' => array(), 'intval' => array('id'));
             // Convert validation into associative array by field name
             $associative_validation = array();
             foreach ($this->validation as $name => $validation) {
                 if (is_string($name)) {
                     $validation['field'] = $name;
                 } else {
                     $name = $validation['field'];
                 }
                 // clean up possibly missing fields
                 if (!isset($validation['rules'])) {
                     $validation['rules'] = array();
                 }
                 // Populate associative validation array
                 $associative_validation[$name] = $validation;
                 if (!empty($validation['get_rules'])) {
                     $this->_field_tracking['get_rules'][] = $name;
                 }
                 // Check if there is a "matches" validation rule
                 if (isset($validation['rules']['matches'])) {
                     $this->_field_tracking['matches'][$name] = $validation['rules']['matches'];
                 }
             }
             // set up id column, if not set
             if (!isset($associative_validation['id'])) {
                 // label is set below, to prevent caching language-based labels
                 $associative_validation['id'] = array('field' => 'id', 'rules' => array('integer'));
             }
             $this->validation = $associative_validation;
             // Force all other has_one ITFKs to integers on get
             foreach ($this->has_one as $related => $rel_props) {
                 $field = $related . '_id';
                 if (in_array($field, $this->fields) && (!isset($this->validation[$field]) || !isset($this->validation[$field]['get_rules'])) && (!isset($this->validation[$related]) || !isset($this->validation[$related]['get_rules']))) {
                     // assume an int
                     $this->_field_tracking['intval'][] = $field;
                 }
             }
             // Get and store the table's field names and meta data
             $fields = $this->db->field_data($this->table);
             // Store only the field names and ensure validation list includes all fields
             foreach ($fields as $field) {
                 // Populate fields array
                 $this->fields[] = $field->name;
                 // Add validation if current field has none
                 if (!isset($this->validation[$field->name])) {
                     // label is set below, to prevent caching language-based labels
                     $this->validation[$field->name] = array('field' => $field->name, 'rules' => array());
                 }
             }
             // convert simple has_one and has_many arrays into more advanced ones
             foreach (array('has_one', 'has_many') as $arr) {
                 foreach ($this->{$arr} as $related_field => $rel_props) {
                     // process the relationship
                     $this->_relationship($arr, $rel_props, $related_field);
                 }
             }
             // allow subclasses to add initializations
             if (method_exists($this, 'post_model_init')) {
                 $this->post_model_init(FALSE);
             }
             // Store common model settings
             foreach (array('table', 'fields', 'validation', 'has_one', 'has_many', '_field_tracking') as $item) {
                 DataMapper::$common[$common_key][$item] = $this->{$item};
             }
             // store the item to the production cache
             $this->production_cache();
             // Load extensions last, so they aren't cached.
             $this->_initiate_local_extensions($common_key);
         }
         // Finally, localize the labels here (because they shouldn't be cached
         // This also sets any missing labels.
         $validation =& DataMapper::$common[$common_key]['validation'];
         foreach ($validation as $field => &$val) {
             // Localize label if necessary
             $val['label'] = $this->localize_label($field, isset($val['label']) ? $val['label'] : FALSE);
         }
         unset($validation);
     }
     // Load stored common model settings by reference
     foreach (DataMapper::$common[$common_key] as $key => &$value) {
         $this->{$key} =& $value;
     }
     // Clear object properties to set at default values
     $this->clear();
     if (!empty($id) && is_numeric($id)) {
         $this->get_by_id(intval($id));
     }
 }
 /**
  * Constructor
  *
  * Initialize DataMapper.
  */
 function DataMapper($id = NULL)
 {
     $this->_assign_libraries();
     $this->_load_languages();
     $this->_load_helpers();
     $common_key = singular(get_class($this));
     // Determine model name
     if (empty($this->model)) {
         $this->model = $common_key;
     }
     // Load stored config settings by reference
     foreach (array_keys(DataMapper::$config) as $key) {
         // Only if they're not already set
         if (empty($this->{$key})) {
             $this->{$key} =& DataMapper::$config[$key];
         }
     }
     // Load model settings if not in common storage
     if (!array_key_exists($common_key, DataMapper::$common)) {
         // If model is 'datamapper' then this is the initial autoload by CodeIgniter
         if ($this->model == 'datamapper') {
             // Load config settings
             $this->config->load('datamapper', TRUE, TRUE);
             // Get and store config settings
             DataMapper::$config = $this->config->item('datamapper');
             DataMapper::_load_extensions(DataMapper::$global_extensions, DataMapper::$config['extensions']);
             unset(DataMapper::$config['extensions']);
             return;
         }
         $loaded_from_cache = FALSE;
         // Load in the production cache for this model, if it exists
         if (!empty(DataMapper::$config['production_cache'])) {
             // attempt to load the production cache file
             $cache_folder = APPPATH . DataMapper::$config['production_cache'];
             if (file_exists($cache_folder) && is_dir($cache_folder) && is_writeable($cache_folder)) {
                 $cache_file = $cache_folder . '/' . $common_key . EXT;
                 if (file_exists($cache_file)) {
                     include $cache_file;
                     if (isset($cache)) {
                         DataMapper::$common[$common_key] =& $cache;
                         unset($cache);
                         // allow subclasses to add initializations
                         if (method_exists($this, 'post_model_init')) {
                             $this->post_model_init(TRUE);
                         }
                         // Load extensions (they are not cacheable)
                         $this->_initiate_local_extensions($common_key);
                         $loaded_from_cache = TRUE;
                     }
                 }
             }
         }
         if (!$loaded_from_cache) {
             // Determine table name
             if (empty($this->table)) {
                 $this->table = plural(get_class($this));
             }
             // Add prefix to table
             $this->table = $this->prefix . $this->table;
             // Convert validation into associative array by field name
             $associative_validation = array();
             foreach ($this->validation as $name => $validation) {
                 if (is_string($name)) {
                     $validation['field'] = $name;
                 } else {
                     $name = $validation['field'];
                 }
                 // clean up possibly missing fields
                 if (!isset($validation['rules'])) {
                     $validation['rules'] = array();
                 }
                 if (!isset($validation['label'])) {
                     $validation['label'] = $name;
                 }
                 // TODO: enable Localization of label
                 // Populate associative validation array
                 $associative_validation[$name] = $validation;
             }
             // set up id column, if not set
             if (!isset($associative_validation['id'])) {
                 $associative_validation['id'] = array('field' => 'id', 'label' => 'Identifier', 'rules' => array('integer'), 'get_rules' => array('intval'));
             }
             $this->validation = $associative_validation;
             // Get and store the table's field names and meta data
             $fields = $this->db->field_data($this->table);
             // Store only the field names and ensure validation list includes all fields
             foreach ($fields as $field) {
                 // Populate fields array
                 $this->fields[] = $field->name;
                 // Add validation if current field has none
                 if (!array_key_exists($field->name, $this->validation)) {
                     $this->validation[$field->name] = array('field' => $field->name, 'label' => '', 'rules' => array());
                 }
             }
             // convert simple has_one and has_many arrays into more advanced ones
             foreach (array('has_one', 'has_many') as $arr) {
                 $new = array();
                 foreach ($this->{$arr} as $key => $value) {
                     // allow for simple (old-style) associations
                     if (is_int($key)) {
                         $key = $value;
                     }
                     // convert value into array if necessary
                     if (!is_array($value)) {
                         $value = array('class' => $value);
                     } else {
                         if (!isset($value['class'])) {
                             // if already an array, ensure that the class attribute is set
                             $value['class'] = $key;
                         }
                     }
                     if (!isset($value['other_field'])) {
                         // add this model as the model to use in queries if not set
                         $value['other_field'] = $this->model;
                     }
                     if (!isset($value['join_self_as'])) {
                         // add this model as the model to use in queries if not set
                         $value['join_self_as'] = $value['other_field'];
                     }
                     if (!isset($value['join_other_as'])) {
                         // add the key as the model to use in queries if not set
                         $value['join_other_as'] = $key;
                     }
                     $new[$key] = $value;
                 }
                 // replace the old array
                 $this->{$arr} = $new;
             }
             // allow subclasses to add initializations
             if (method_exists($this, 'post_model_init')) {
                 $this->post_model_init(FALSE);
             }
             // Store common model settings
             foreach (array('table', 'fields', 'validation', 'has_one', 'has_many') as $item) {
                 DataMapper::$common[$common_key][$item] = $this->{$item};
             }
             // if requested, store the item to the production cache
             if (!empty(DataMapper::$config['production_cache'])) {
                 // attempt to load the production cache file
                 $cache_folder = APPPATH . DataMapper::$config['production_cache'];
                 if (file_exists($cache_folder) && is_dir($cache_folder) && is_writeable($cache_folder)) {
                     $cache_file = $cache_folder . '/' . $common_key . EXT;
                     $cache = "<" . "?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed'); \n";
                     $cache .= '$cache = ' . var_export(DataMapper::$common[$common_key], TRUE) . ';';
                     if (!($fp = @fopen($cache_file, 'w'))) {
                         show_error('Error creating production cache file: ' . $cache_file);
                     }
                     flock($fp, LOCK_EX);
                     fwrite($fp, $cache);
                     flock($fp, LOCK_UN);
                     fclose($fp);
                     @chmod($cache_file, FILE_WRITE_MODE);
                 }
             }
             // Load extensions last, so they aren't cached.
             $this->_initiate_local_extensions($common_key);
         }
     }
     // Load stored common model settings by reference
     foreach (array_keys(DataMapper::$common[$common_key]) as $key) {
         $this->{$key} =& DataMapper::$common[$common_key][$key];
     }
     // Clear object properties to set at default values
     $this->clear();
     if (!empty($id) && is_numeric($id)) {
         $this->get_by_id(intval($id));
     }
 }