Created
March 1, 2010 19:38
-
-
Save apipkin/318724 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 | |
/** | |
* Echofin_Model_Abstract | |
* | |
* @category Echofin | |
* @package Echofin_Model | |
*/ | |
abstract class Echofin_Model_Abstract { | |
/** | |
* Save method options | |
*/ | |
const SAVE = 'save'; | |
const SAVE_NEW = 'new'; | |
/** | |
* Filter Options | |
*/ | |
const FILTER_ACTIVE = 'filter_active'; | |
const FILTER_RECYCLED = 'filter_recycled'; | |
/** | |
* Link to the mapper of the model | |
* @var Echofin_Model_Mapper_Abstract | |
*/ | |
protected $_mapper; | |
/** | |
* Contains Mapper Name to generate new mapper if needed | |
* @var string|null | |
*/ | |
protected $_mapper_name; | |
/** | |
* add sort options for different mapper methods | |
* @var array | |
*/ | |
protected $_sort_array = array(); | |
/** | |
* stores the autosave id | |
* @var int | |
*/ | |
protected $_autosave_id = 0; | |
/** | |
* stores filters and values | |
* @var array | |
*/ | |
protected $_filters = array(); | |
/** | |
* Constructor | |
* | |
* Adds options to the Model | |
* | |
* @param mixed $options | |
* @return Echofin_Model_Abstract | |
*/ | |
public function __construct(array $options = null) | |
{ | |
if(is_array($options)) | |
{ | |
$this->setOptions($options); | |
} | |
if(method_exists($this,'init')) | |
{ | |
return $this->init(); | |
} | |
return $this; | |
} | |
/** | |
* Sets the Mapper Name | |
* | |
* @param string $val | |
* @return Echofin_Model_Abstract | |
*/ | |
public function setMapperName($val) | |
{ | |
if(class_exists($val)) | |
{ | |
$this->_mapper_name = $val; | |
}else{ | |
throw new Exception('Invalid Mapper Class.'); | |
} | |
return $this; | |
} | |
/** | |
* Returns the set Mapper Name | |
* - OR - | |
* Generates and returns a Mapper Name based off the current model | |
* | |
* @return string | |
*/ | |
public function getMapperName() | |
{ | |
if(empty($this->_mapper_name)) | |
{ | |
$this->_mapper_name = str_replace('_Model_','_Model_Mapper_',get_class($this)); | |
} | |
return $this->_mapper_name; | |
} | |
/** | |
* Sets the Mapper to a child of the Echofin_Model_Mapper_Abstract | |
* | |
* @param Echofin_Model_Mapper_Abstract $mapper | |
* @return Echofin_Model_Abstract | |
*/ | |
public function setMapper($mapper) | |
{ | |
$this->_mapper = $mapper; | |
return $this; | |
} | |
/** | |
* Returns the assigned mapper | |
* - OR - | |
* Generates a new mapper from the mapper name | |
* | |
* @return Echofin_Model_Mapper_Abstract | |
*/ | |
public function getMapper() | |
{ | |
if(null === $this->_mapper) | |
{ | |
$this->getMapperName(); | |
$this->setMapper(new $this->_mapper_name()); | |
} | |
return $this->_mapper; | |
} | |
/** | |
* Sets the Mapper to a child of the Echofin_Model_Mapper_Abstract | |
* | |
* @param Echofin_Model_Mapper_Abstract $mapper | |
* @return Echofin_Model_Abstract | |
*/ | |
public function setAutosaveId($id) | |
{ | |
$this->_autosave_id = (int)$id; | |
return $this; | |
} | |
/** | |
* Returns the assigned mapper | |
* - OR - | |
* Generates a new mapper from the mapper name | |
* | |
* @return Echofin_Model_Mapper_Abstract | |
*/ | |
public function getAutosaveId() | |
{ | |
return $this->_autosave_id; | |
} | |
/** | |
* Generic method that will try to find either a title or | |
* name for the current model | |
* | |
* @return string | |
*/ | |
public function getTitle(){ | |
if(property_exists($this,'_title')) | |
{ | |
return $this->_title; | |
} | |
if(property_exists($this,'_name')) | |
{ | |
return $this->_name; | |
} | |
return ''; | |
} | |
/** | |
* Generic method that tries to return a value for the | |
* common is_active property in models | |
* | |
* @return null|bool | |
*/ | |
public function isActive(){ | |
if(property_exists($this,'_is_active')) | |
{ | |
return $this->_is_active; | |
} | |
return null; | |
} | |
/** | |
* Loops through options and tries to match the key to a property and assign | |
* | |
* @param array $options | |
* @return Echofin_Model_Mapper_Abstract | |
*/ | |
public function setOptions(array $options) | |
{ | |
$methods = get_class_methods($this); | |
foreach($options as $property => $value) | |
{ | |
$method = 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $property))); | |
if (in_array($method, $methods)) | |
{ | |
$this->$method($value); | |
} | |
} | |
return $this; | |
} | |
/** | |
* Returns an array of properties | |
* | |
* @return array | |
*/ | |
public function getOptions() | |
{ | |
$options = array(); | |
$methods = get_class_methods($this); | |
$properties = get_object_vars($this); | |
foreach($properties as $name => $value) | |
{ | |
$method = 'get' . str_replace(' ', '', ucwords(str_replace('_', ' ', $name))); | |
if(!in_array($method,$methods)) { | |
continue; | |
} | |
$options[substr($name,1)] = $this->$method(); | |
} | |
return $options; | |
} | |
/** | |
* Returns the array of sort options | |
* | |
* @return array | |
*/ | |
public function getSortArray() | |
{ | |
return $this->_sort_array; | |
} | |
/** | |
* Adds a sort option to the sort array | |
* Key is used for override and removal | |
* | |
* @param string $val | |
* @param string $key | |
*/ | |
public function addSortOption($val,$key = null) | |
{ | |
if($key === null) | |
{ | |
$key = count($this->getSortArray()); | |
} | |
$this->_sort_array[$key] = $val; | |
return $this; | |
} | |
/** | |
* Removes the sort option from the sort array if it exits | |
* | |
* @param string $key | |
* @return Echofin_Model_Mapper_Abstract | |
*/ | |
public function removeSortOption($key) | |
{ | |
if(array_key_exists($key, $this->getSortArray())) | |
{ | |
unset($this->_sort_array[$key]); | |
} | |
return $this; | |
} | |
/** | |
* Grants the ability to turn on and off filters | |
* | |
* @param string $type | |
* @param bool $bool | |
* @return Echofin_Model_Mapper_Abstract | |
*/ | |
public function setFilter($type, $bool) | |
{ | |
$this->_filters[$type] = $bool; | |
return $this; | |
} | |
/** | |
* Returns the value of the filter or null if filter is not set | |
* | |
* @param string $type | |
* @return bool | null | |
*/ | |
public function getFilter($type) | |
{ | |
if(array_key_exists($type,$this->_filters)) { | |
return (int)$this->_filters[$type]; | |
} | |
return null; | |
} | |
public function removeFilter($type) | |
{ | |
if(isset($this->_filters[$type])) { | |
unset($this->_filters[$type]); | |
} | |
return $this; | |
} | |
/** | |
* Finds the a row within the mapper data set based on the id given | |
* Populates the current Model with the data from the row | |
* | |
* @param mixed $id | |
* @param bool $active | |
* | |
* @return Echofin_Model_Mapper_Abstract | |
*/ | |
public function find($id,$active = false) | |
{ | |
$this->getMapper()->setModel($this)->setFindActive($active); | |
$this->getMapper()->find($id, $this); | |
return $this; | |
} | |
/** | |
* Returns an array of Models populated with data from the mapper data set | |
* Array contains one model for each row | |
* | |
* @param bool $active | |
* @return array | |
*/ | |
public function fetchAll($active = false) | |
{ | |
$this->getMapper()->setModel($this)->setFindActive($active); | |
return $this->getMapper()->fetchAll(); | |
} | |
/** | |
* Provides a custom search option for the model | |
* Returns an array of Models populated with data from the mapper data set | |
* | |
* @param mixed $term | |
* @param bool $strict | |
* @param array $fields | |
* @param bool $active | |
* @return array | |
*/ | |
public function search($term, $strict = false, $fields = array(), $active = false) | |
{ | |
if($fields == '*') { | |
$fields = array(); | |
} | |
if(!is_array($fields)) { | |
$fields = array($fields); | |
} | |
$this->getMapper()->setModel($this); | |
$this->getMapper()->setFindActive($active); | |
return $this->getMapper()->search($term,$strict,$fields); | |
} | |
/** | |
* Returns a Primary Key => Display array based off fetchAll() | |
* | |
* @param string $display | |
* @param bool $active | |
* @return array | |
*/ | |
public function getSelectOptions($display = 'title', $active = false) | |
{ | |
$this->getMapper()->setFindActive($active); | |
return $this->getMapper()->getSelectOptions($display); | |
} | |
/** | |
* Commits the Model data to a Mapper data set | |
* Returns the primary key value from the save method | |
* | |
* @param array $options | |
* @param string $type | |
* @return false|mixed | |
*/ | |
public function save( array $options = null, $type = 'save') | |
{ | |
$this->getMapper()->setModel($this); | |
if (null !== $options) { | |
$this->setOptions($options); | |
} | |
$id = false; | |
switch($type) | |
{ | |
case self::SAVE_NEW: | |
$id = $this->getMapper()->saveNew(); | |
if($id > 0) { | |
$this->autosaveCleanup(); | |
} | |
break; | |
case self::SAVE: | |
$id = $this->getMapper()->save(); | |
if($id > 0) { | |
$this->autosaveCleanup(); | |
} | |
break; | |
} | |
return $id; | |
} | |
/** | |
* Save the Model as a new instance | |
* | |
* @param $options | |
* @return mixed | |
*/ | |
public function saveNew( array $options = null) | |
{ | |
return $this->save($options,self::SAVE_NEW); | |
} | |
/** | |
* Adds the model information to the recycle bin | |
* | |
* @return mixed | |
*/ | |
public function recycle() | |
{ | |
return $this->getMapper()->recycle($this); | |
} | |
/** | |
* Removes the row from the mapper data set | |
* | |
* @param mixed $match | |
* @param mixed $field | |
* @return bool | |
*/ | |
public function delete($match, $field = 'id') | |
{ | |
return $this->getMapper()->delete($match, $field); | |
} | |
public function fetchAllDrafts() | |
{ | |
$autosave = new Default_Model_Autosave(); | |
$drafts = $autosave->fetchByComponentId($this->getMapper()->getComponentId()); | |
$models = array(); | |
foreach($drafts as $draft) { | |
$model_name = get_class($this); | |
$model = new $model_name(); | |
$model->setOptions(Zend_Json::decode($draft->getSnapshot())); | |
$model->setAutosaveId($draft->getId()); | |
$models[] = $model; | |
} | |
return $models; | |
} | |
public function fetchAllUnclaimedDrafts() | |
{ | |
$autosave = new Default_Model_Autosave(); | |
$drafts = $autosave->fetchByComponentId($this->getMapper()->getComponentId()); | |
$models = array(); | |
foreach($drafts as $draft) { | |
if($draft->getComponentEntryId() > 0) { | |
continue; | |
} | |
$model_name = get_class($this); | |
$model = new $model_name(); | |
$model->setOptions(Zend_Json::decode($draft->getSnapshot())); | |
$model->setAutosaveId($draft->getId()); | |
$models[] = $model; | |
} | |
return $models; | |
} | |
public function fetchDrafts() | |
{ | |
$autosave = new Default_Model_Autosave(); | |
$drafts = $autosave->fetchEntries($this->getMapper()->getComponentId(),$this->getId()); | |
$models = array(); | |
foreach($drafts as $draft) { | |
$model_name = get_class($this); | |
$model = new $model_name(); | |
$model->setOptions(Zend_Json::decode($draft->getSnapshot())); | |
$model->setAutosaveId($draft->getId()); | |
$models[] = $model; | |
} | |
return $models; | |
} | |
public function hasDraft() | |
{ | |
$autosave = new Default_Model_Autosave(); | |
$autosave->fetchEntry($this->getMapper()->getComponentId(),$this->getId()); | |
if($autosave->getId() > 0){ | |
return true; | |
} | |
return false; | |
} | |
public function autosaveCleanup() | |
{ | |
if($this->_autosave_id > 0) { | |
$autosave = new Default_Model_Autosave(); | |
$autosave->delete($this->_autosave_id,'id'); | |
} | |
} | |
/** | |
* Generic method to build an array specifically for an HTML select input element | |
* | |
* @param Echofin_Model_Abstract $menu | |
* @param int $parent | |
* @param int $iteration | |
* | |
* @return array | |
*/ | |
protected function menuHierarchyBuilder($menu, $parent, $iteration) | |
{ | |
$array = array(); | |
$front = Zend_Controller_Front::getInstance(); | |
$requeset = $front->getRequest(); | |
foreach($menu[$parent] as $k => $v) | |
{ | |
$menuArray = array( | |
'label' => $v->getTitle(), | |
'order' => $v->getOrder(), | |
'module' => $requeset->getModuleName(), | |
'controller' => $requeset->getControllerName(), | |
'active' => $v->getIsActive(), | |
'action' => 'update', | |
'params' => array('id' => $k) | |
); | |
if(isset($menu[$k])) | |
{ | |
$menuArray['pages'] = $this->menuHierarchyBuilder($menu, $k, $iteration + 1); | |
} | |
$array[] = $menuArray; | |
} | |
return $array; | |
} | |
/** | |
* Generic method to build an array specifically for an HTML select input element | |
* | |
* @param object $menu | |
* @param object $parent | |
* @param object $iteration | |
* @param object $pre [optional] | |
* @return | |
*/ | |
protected function selectHierarchyBuilder($menu, $parent, $iteration, $pre = '-') | |
{ | |
$array = array(); | |
foreach($menu[$parent] as $k => $v) | |
{ | |
$title = ''; | |
for($i = 0; $i < $iteration; $i++) | |
{ | |
$title .= $pre; | |
} | |
$title .= $v->getTitle(); | |
$array[$k]['id'] = $k; | |
$array[$k]['title'] = $title; | |
if(isset($menu[$k])) | |
{ | |
$array = array_merge($array,$this->selectHierarchyBuilder($menu, $k, $iteration + 1, $pre)); | |
} | |
} | |
return $array; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment