Initial Commit
This commit is contained in:
119
app/extensions/components/EActiveRecord.php
Normal file
119
app/extensions/components/EActiveRecord.php
Normal file
@@ -0,0 +1,119 @@
|
||||
<?php
|
||||
/**
|
||||
* EActiveRecord class
|
||||
*
|
||||
* Some cool methods to share amount your models
|
||||
*
|
||||
* @author Antonio Ramirez <amigo.cobos@gmail.com>
|
||||
* @link http://www.ramirezcobos.com/
|
||||
* @link http://www.2amigos.us/
|
||||
* @copyright 2013 2amigOS! Consultation Group LLC
|
||||
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
|
||||
*/
|
||||
class EActiveRecord extends CActiveRecord
|
||||
{
|
||||
/**
|
||||
* default form ID for the current model. Defaults to get_class()+'-form'
|
||||
*/
|
||||
private $_formId;
|
||||
|
||||
public function setFormId($value)
|
||||
{
|
||||
$this->_formId = $value;
|
||||
}
|
||||
|
||||
public function getFormId()
|
||||
{
|
||||
if (null !== $this->_formId)
|
||||
return $this->_formId;
|
||||
else
|
||||
{
|
||||
$this->_formId = strtolower(get_class($this)) . '-form';
|
||||
return $this->_formId;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* default grid ID for the current model. Defaults to get_class()+'-grid'
|
||||
*/
|
||||
private $_gridId;
|
||||
|
||||
public function setGridId($value)
|
||||
{
|
||||
$this->_gridId = $value;
|
||||
}
|
||||
|
||||
public function getGridId()
|
||||
{
|
||||
if (null !== $this->_gridId)
|
||||
return $this->_gridId;
|
||||
else
|
||||
{
|
||||
$this->_gridId = strtolower(get_class($this)) . '-grid';
|
||||
return $this->_gridId;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* default list ID for the current model. Defaults to get_class()+'-list'
|
||||
*/
|
||||
private $_listId;
|
||||
|
||||
public function setListId($value)
|
||||
{
|
||||
$this->_listId = $value;
|
||||
}
|
||||
|
||||
public function getListId()
|
||||
{
|
||||
if (null !== $this->_listId)
|
||||
return $this->_listId;
|
||||
else
|
||||
{
|
||||
$this->_listId = strtolower(get_class($this)) . '-list';
|
||||
return $this->_listId;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Logs the record update information.
|
||||
* Updates the four columns: create_user_id, create_date, last_update_user_id and last_update_date.
|
||||
*/
|
||||
protected function logUpdate()
|
||||
{
|
||||
$userId = php_sapi_name() === 'cli'
|
||||
? -1
|
||||
: Yii::app()->user->id;
|
||||
|
||||
foreach (array('create_user_id' => $userId, 'create_date' => time()) as $attribute => $value)
|
||||
$this->updateLogAttribute($attribute, $value, (!($userId===-1 || Yii::app()->user->isGuest) && $this->isNewRecord));
|
||||
|
||||
foreach (array('last_update_user_id' => $userId, 'last_update_date' => time()) as $attribute => $value)
|
||||
$this->updateLogAttribute($attribute, $value, (!($userId===-1 || Yii::app()->user->isGuest) && !$this->isNewRecord));
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to update attributes
|
||||
* @param $attribute
|
||||
* @param $value
|
||||
* @param $check
|
||||
*/
|
||||
protected function updateLogAttribute($attribute, $value, $check)
|
||||
{
|
||||
|
||||
if ($this->hasAttribute($attribute) && $check)
|
||||
$this->$attribute = $value;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* updates the log fields before saving
|
||||
* @return boolean
|
||||
*/
|
||||
public function beforeSave()
|
||||
{
|
||||
$this->logUpdate();
|
||||
return parent::beforeSave();
|
||||
}
|
||||
|
||||
}
|
||||
106
app/extensions/components/EController.php
Normal file
106
app/extensions/components/EController.php
Normal file
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
/**
|
||||
* EController class
|
||||
*
|
||||
* Has some useful methods for your Controllers
|
||||
*
|
||||
* @author Antonio Ramirez <amigo.cobos@gmail.com>
|
||||
* @link http://www.ramirezcobos.com/
|
||||
* @link http://www.2amigos.us/
|
||||
* @copyright 2013 2amigOS! Consultation Group LLC
|
||||
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
|
||||
*/
|
||||
class EController extends CController
|
||||
{
|
||||
public $meta_keywords = array();
|
||||
public $meta_description = array();
|
||||
public $breadcrumbs;
|
||||
|
||||
|
||||
/**
|
||||
* Gets a param
|
||||
* @param $name
|
||||
* @param null $defaultValue
|
||||
* @return mixed
|
||||
*/
|
||||
public function getActionParam($name, $defaultValue = null)
|
||||
{
|
||||
return Yii::app()->request->getParam($name, $defaultValue );
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the requested data model.
|
||||
* @param string the model class name
|
||||
* @param integer the model ID
|
||||
* @param array additional search criteria
|
||||
* @param boolean whether to throw exception if the model is not found. Defaults to true.
|
||||
* @return CActiveRecord the model instance.
|
||||
* @throws CHttpException if the model cannot be found
|
||||
*/
|
||||
protected function loadModel($class, $id, $criteria = array(), $exceptionOnNull = true)
|
||||
{
|
||||
if (empty($criteria))
|
||||
$model = CActiveRecord::model($class)->findByPk($id);
|
||||
else
|
||||
{
|
||||
$finder = CActiveRecord::model($class);
|
||||
$c = new CDbCriteria($criteria);
|
||||
$c->mergeWith(array(
|
||||
'condition' => $finder->tableSchema->primaryKey . '=:id',
|
||||
'params' => array(':id' => $id),
|
||||
));
|
||||
$model = $finder->find($c);
|
||||
}
|
||||
if (isset($model))
|
||||
return $model;
|
||||
else if ($exceptionOnNull)
|
||||
throw new CHttpException(404, 'Unable to find the requested object.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs the AJAX validation.
|
||||
* @param CModel the model to be validated
|
||||
*/
|
||||
protected function performAjaxValidation($model)
|
||||
{
|
||||
if (isset($_POST['ajax']) && $_POST['ajax'] === $model->formId)
|
||||
{
|
||||
echo CActiveForm::validate($model);
|
||||
Yii::app()->end();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Outputs (echo) json representation of $data, prints html on debug mode.
|
||||
* NOTE: json_encode exists in PHP > 5.2, so it's safe to use it directly without checking
|
||||
* @param array $data the data (PHP array) to be encoded into json array
|
||||
* @param int $opts Bitmask consisting of JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_FORCE_OBJECT.
|
||||
*/
|
||||
public function renderJson($data, $opts=null)
|
||||
{
|
||||
if(YII_DEBUG && isset($_GET['debug']) && is_array($data))
|
||||
{
|
||||
foreach($data as $type => $v)
|
||||
printf('<h1>%s</h1>%s', $type, is_array($v) ? json_encode($v, $opts) : $v);
|
||||
}
|
||||
else
|
||||
{
|
||||
header('Content-Type: application/json; charset=UTF-8');
|
||||
echo json_encode($data, $opts);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility function to ensure the base url.
|
||||
* @param $url
|
||||
* @return string
|
||||
*/
|
||||
public function baseUrl( $url = '' )
|
||||
{
|
||||
static $baseUrl;
|
||||
if ($baseUrl === null)
|
||||
$baseUrl = Yii::app()->request->baseUrl;
|
||||
return $baseUrl . '/' . ltrim($url, '/');
|
||||
}
|
||||
|
||||
}
|
||||
43
app/extensions/components/EWebApplication.php
Normal file
43
app/extensions/components/EWebApplication.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
* EWebApplication class
|
||||
*
|
||||
* @author Antonio Ramirez <amigo.cobos@gmail.com>
|
||||
* @link http://www.ramirezcobos.com/
|
||||
* @link http://www.2amigos.us/
|
||||
* @copyright 2013 2amigOS! Consultation Group LLC
|
||||
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
|
||||
*/
|
||||
class EWebApplication extends CWebApplication {
|
||||
|
||||
/**
|
||||
* This function is here because we aren't creating a locale file for every client.
|
||||
* Thus we provide a fallback to "en".
|
||||
*/
|
||||
public function getLocale($localeID = null) {
|
||||
try {
|
||||
return parent::getLocale($localeID);
|
||||
} catch (Exception $e) {
|
||||
return CLocale::getInstance('en');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* We were getting tons of errors in the logs from OPTIONS requests for the URI "*"
|
||||
* As it turns out, the requests were from localhost (::1) and are apparently a way
|
||||
* that Apache polls its processes to see if they're alive. This function causes
|
||||
* Yii to respond without logging errors.
|
||||
*/
|
||||
public function runController($route) {
|
||||
try {
|
||||
parent::runController($route);
|
||||
} catch (CHttpException $e) {
|
||||
if (@$_SERVER['REQUEST_METHOD'] == 'OPTIONS' && @$_SERVER['REQUEST_URI'] == '*') {
|
||||
Yii::app()->end('Hello, amigo!');
|
||||
} else
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user