Created
March 1, 2010 19:38
-
-
Save apipkin/318725 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
abstract class Echofin_Model_Mapper_Abstract | |
{ | |
protected $_dbTable; | |
protected $_dbTableDefault; | |
protected $_model; | |
protected $_model_name; | |
protected $_find_active = false; | |
protected $_active_column_name = 'is_active'; | |
protected $_record_activity = true; | |
protected $_activity_type; | |
public function __construct() | |
{ | |
$this->setModelName(str_replace('_Mapper','',get_class($this))); | |
if(method_exists($this,'init')) | |
{ | |
return $this->init(); | |
} | |
return $this; | |
} | |
public function setActiveColumnName($value) | |
{ | |
if (strlen($value)) $this->_active_column_name = $value; | |
} | |
public function getActiveColumnName() | |
{ | |
return $this->_active_column_name; | |
} | |
public function getTableData() | |
{ | |
$model = $this->getModel(); | |
$data = array(); | |
$cols = $this->getDbTable()->info(Zend_Db_Table_Abstract::COLS); | |
foreach($model->getOptions() as $k => $v) { | |
if(in_array($k,$cols)) { | |
$data[$k] = $v; | |
} | |
} | |
return $data; | |
} | |
public function setModel(Echofin_Model_Abstract $model) | |
{ | |
$this->_model = $model; | |
return $this; | |
} | |
public function getModel() | |
{ | |
if($this->_model == null) | |
{ | |
$model_name = $this->getModelName(); | |
$this->_model = new $model_name(); | |
} | |
return $this->_model; | |
} | |
public function setModelName($name) | |
{ | |
if(class_exists($name)) | |
{ | |
$this->_model_name = $name; | |
} | |
return $this; | |
} | |
public function getModelName() | |
{ | |
if(empty($this->_mapper_name)) | |
{ | |
$this->_model_name = str_replace('_Model_Mapper_','_Model_',get_class($this)); | |
} | |
return $this->_model_name; | |
} | |
public function getPrimaryKey() | |
{ | |
$primary = $this->getDbTable()->info(Zend_Db_Table_Abstract::PRIMARY); | |
return current($primary); | |
} | |
public function hasIsActive() | |
{ | |
if(in_array('is_active',$this->getDbTable()->info(Zend_Db_Table_Abstract::COLS))) | |
{ | |
return true; | |
} | |
return false; | |
} | |
public function setFindActive($active) | |
{ | |
if($active === true) | |
{ | |
$this->_find_active = true; | |
return $this; | |
} | |
$this->_find_active = false; | |
return $this; | |
} | |
public function findActive() | |
{ | |
return $this->_find_active; | |
} | |
public function setRecordActivity($bool = true) | |
{ | |
$this->_record_activity = (int)$bool; | |
return $this; | |
} | |
public function getRecordActivity() | |
{ | |
return $this->_record_activity; | |
} | |
public function recordActivity() | |
{ | |
return $this->getRecordActivity(); | |
} | |
public function setActivityType($val) | |
{ | |
$this->_activity_type = $val; | |
return $this; | |
} | |
public function getActivityType() | |
{ | |
return $this->_activity_type; | |
} | |
/** | |
* Finds the component_id that's in the component table for | |
* this database table. This avoids hardcoding the id. | |
*/ | |
public function getComponentId() | |
{ | |
$dbtable = new Default_Model_DbTable_Components(); | |
$query = $dbtable->select() | |
->where("main_table = ?", $this->getTableName()); | |
$result = $dbtable->fetchRow($query); | |
if (count($result) > 0) { | |
return $result->id; | |
} | |
return -1; | |
} | |
public function setDbTable($dbTable = null) | |
{ | |
if($dbTable === null) | |
{ | |
$dbTable = str_replace('_Mapper_','_DbTable_', get_class($this)); | |
} | |
if(is_string($dbTable)) | |
{ | |
$dbTable = new $dbTable(); | |
} | |
if(!$dbTable instanceof Zend_Db_Table_Abstract) | |
{ | |
throw new Exception('Invalid table data gateway provided'); | |
} | |
$this->_dbTable = $dbTable; | |
return $this; | |
} | |
public function getDbTable() | |
{ | |
if(null === $this->_dbTable) | |
{ | |
$this->setDbTable($this->_dbTableDefault); | |
} | |
return $this->_dbTable; | |
} | |
public function getTableName() | |
{ | |
return $this->getDbTable()->info(Zend_Db_Table_Abstract::NAME); | |
} | |
public function save() | |
{ | |
$data = $this->getTableData(); | |
$id = $data['id']; | |
if(null === $id || 0 == (int)$id) { | |
unset($data['id']); | |
if(array_key_exists('date_created',$data)) | |
{ | |
$data['date_created'] = date('Y-m-d G:i:s'); | |
} | |
if(array_key_exists('date_modified',$data)) | |
{ | |
$data['date_modified'] = date('Y-m-d G:i:s'); | |
} | |
$id = $this->getDbTable()->insert($data); | |
$this->getModel()->find($id); | |
$this->updateActivity(Default_Model_Activity::CREATED); | |
return $id; | |
} else { | |
if(array_key_exists('date_created',$data)) | |
{ | |
unset($data['date_created']); | |
} | |
if(array_key_exists('date_modified',$data)) | |
{ | |
$data['date_modified'] = date('Y-m-d G:i:s'); | |
} | |
try | |
{ | |
$this->getDbTable()->update($data,array('id = ?' => $id)); | |
$this->getModel()->find($id); | |
$this->updateActivity(Default_Model_Activity::UPDATED); | |
return $id; | |
}catch(Exception $e){ | |
return 0; | |
} | |
} | |
return 0; | |
} | |
/** | |
* Saves the data as a new object | |
* @return | |
* @param object $data | |
*/ | |
public function saveNew() | |
{ | |
$this->getModel()->setId(null); | |
return self::save(); | |
} | |
/** | |
* Deletes the database row AS WELL AS the row in table `revisions` | |
* @return number of deleted records | |
* @param object $match | |
* @param object $field[optional] | |
*/ | |
public function delete($match, $field = 'id') | |
{ | |
$query = $this->getDbTable()->getAdapter()->quoteInto('`' . $field . '` = ?', $match); | |
$models = $this->getModel()->search($match, true, array($field)); | |
foreach($models as $model) { | |
$this->setModel($model); | |
$this->updateActivity(Default_Model_Activity::DELETED); | |
} | |
return $this->getDbTable()->delete($query); | |
} | |
/** | |
* Recycles | |
* @return true if success or false if fail | |
* @param object $model | |
*/ | |
public function recycle($model) | |
{ | |
$recycle = new Default_Model_RecycleBin(); | |
$recycle->setComponentId($this->getComponentId()); | |
$recycle->setComponentEntryId($model->getId()); | |
$recycle->setModelName($this->getModelName()); | |
$recycle->setFileName($model->getTitle()); | |
$recycle->setDateRecycled(date('Y-m-d H:i:s')); | |
if(!$recycle->save()) | |
{ | |
return false; | |
} | |
$this->updateActivity(Default_Model_Activity::RECYCLED); | |
return true; | |
} | |
public function find($id, $model) | |
{ | |
if($this->hasIsActive() && $this->findActive()) | |
{ | |
$query = $this->getDbTable()->select() | |
->where($this->getPrimaryKey() . ' = ?',$id) | |
->where('is_active = ?',(int)$this->findActive()); | |
$result = $this->getDbTable()->fetchAll($query); | |
}else{ | |
$result = $this->getDbTable()->find($id); | |
} | |
if(0 == count($result)) { return; } | |
$row = $result->current(); | |
$model->setOptions($row->toArray()); | |
} | |
public function fetchAll() | |
{ | |
$resultSet = array(); | |
$query = $this->getDbTable()->select(); | |
if($this->hasIsActive() && $this->findActive()) { | |
$query->where('is_active = ?',(int)$this->findActive()); | |
} | |
$query->order($this->getModel()->getSortArray()); | |
$resultSet = $this->getDbTable()->fetchAll($query); | |
$models = array(); | |
foreach($resultSet as $row) | |
{ | |
$model = new $this->_model_name(); | |
$model->setOptions($row->toArray()); | |
$models[] = $model; | |
} | |
if($this->getModel()->getFilter(Echofin_Model_Abstract::FILTER_RECYCLED)) { | |
return $this->filterOutRecycled($models); | |
} | |
return $models; | |
} | |
public function search($term, $strict = false, $fields = array()) | |
{ | |
if(empty($fields)) | |
{ | |
$fields = $this->getDbTable()->info(Zend_Db_Table_Abstract::COLS); | |
} | |
$query = $this->getDbTable()->select(); | |
foreach($fields as $field) | |
{ | |
if($strict) | |
{ | |
$query->orWhere('`' . $field . '` = ?', $term); | |
}else{ | |
$query->orWhere('`' .$field . '` LIKE ?', '%' . $term . '%'); | |
} | |
} | |
$resultSet = array(); | |
if($this->hasIsActive() && $this->findActive()) | |
{ | |
$query->where('`is_active` = ?',(int)$this->findActive()); | |
} | |
$query->order($this->getModel()->getSortArray()); | |
$resultSet = $this->getDbTable()->fetchAll($query); | |
$models = array(); | |
foreach($resultSet as $row) | |
{ | |
$model = new $this->_model_name(); | |
$model->setOptions($row->toArray()); | |
$models[] = $model; | |
} | |
if($this->getModel()->getFilter(Echofin_Model_Abstract::FILTER_RECYCLED)) { | |
return $this->filterOutRecycled($models); | |
} | |
return $models; | |
} | |
public function getSelectOptions($display = 'title') | |
{ | |
$resultSet = $this->getDbTable()->fetchAll(); | |
$models = array(); | |
foreach($resultSet as $row) | |
{ | |
$model = new $this->_model_name(); | |
$model->setOptions($row->toArray()); | |
$models[] = $model; | |
} | |
if($this->getModel()->getFilter(Echofin_Model_Abstract::FILTER_RECYCLED)) { | |
$models = $this->filterOutRecycled($models); | |
} | |
$options = array(); | |
try{ | |
foreach($models as $model) | |
{ | |
$d = 'get' . str_replace(' ','',ucwords(str_replace('_',' ',$display))); | |
$options[$model->getId()] = $model->$d(); | |
} | |
return $options; | |
} catch(Exception $e) { | |
try { | |
foreach($models as $model) | |
{ | |
$options[$model->getId()] = $model->getTitle(); | |
} | |
return $options; | |
} catch (Exception $e) { | |
foreach($models as $model) | |
{ | |
$options[$model->getId()] = $model->getName(); | |
} | |
return $options; | |
} | |
} | |
} | |
public function updateActivity($type = null) | |
{ | |
if(!$this->recordActivity() || $this->getModel()->getMapper()->getComponentId() < 1) | |
{ | |
return true; | |
} | |
if($type === null) | |
{ | |
$type = $this->getActivityType(); | |
} | |
if(!Zend_Auth::getInstance()->hasIdentity()) | |
{ | |
return false; | |
} | |
$activity = new Default_Model_Activity(); | |
$authData = Zend_Auth::getInstance()->getIdentity(); | |
$activity->setContactUserId($authData['userId']) | |
->setComponentEntryId($this->getModel()->getId()) | |
->setComponentId($this->getModel()->getMapper()->getComponentId()) | |
->setDateModified(date('Y-m-d G:i:s')) | |
->setTitle($this->getModel()->getTitle()) | |
->setType($type) | |
->save() | |
; | |
} | |
public function filterOutRecycled($models) | |
{ | |
if(is_array($models)) | |
{ | |
$cleanModels = array(); | |
foreach($models as $k => $model) | |
{ | |
if(!$this->itemIsRecycled($model)) | |
{ | |
$cleanModels[] = $model; | |
} | |
} | |
return $cleanModels; | |
} | |
if($this->itemIsRecycled($models)) | |
{ | |
return array(); | |
} | |
return $models; | |
} | |
public function itemIsRecycled(Echofin_Model_Abstract $model) | |
{ | |
$recycleBin = new Default_Model_RecycleBin(); | |
$recycleBin->setComponentId($model->getMapper()->getComponentId()); | |
$recycleBin->setComponentEntryId($model->getId()); | |
return $recycleBin->isRecycled(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment