PATH:
home
/
letacommog
/
letaweb
/
protected
/
extensions
/
YiisyCrudAdmin
<?php /** * Short desc * * Created by JetBrains PhpStorm. * @author zadoev@gmail.com * @version 29.03.11 14:19 * @package ${END} */ /** * Base field description object related to any field type. */ class RDbBase { protected $_model; protected $_field; protected $_args = array(); public function __construct( $model, $field, $args = array() ) { $this->_model = $model; $this->_field = $field; $this->_args = $args; } public function __toString() { return $this->_model->{$this->_field}; } public function getFieldFilter() { return null; } public function asGridColumn() { return array( 'filter' => $this->getFieldFilter(), 'name' => $this->_field, 'value' => '(string)$data->getField("'.$this->_field.'")', ); } public function asViewAttribute() { return array( 'name' => $this->_field, 'value' => (string)$this, ); } public function asActiveForm($activeForm, $options = array('template' => '{label} {input} {error}','htmlOptions' => array()) ) { $template = array_key_exists('template', $options) ? $options['template'] :'{label} {input} {error}' ; $htmlOptions = array_key_exists('htmlOptions',$options) ? $options['htmlOptions'] : array(); $template = str_replace('{label}', $activeForm->labelEx($this->_model, $this->_field), $template); $template = str_replace('{input}', $this->renderInput($activeForm, $htmlOptions), $template); $template = str_replace('{error}', $activeForm->error($this->_model, $this->_field), $template); return $template; } protected function renderInput($activeForm,$htmlOptions) { return $activeForm->textField($this->_model, $this->_field, $htmlOptions); } public function asSearchForm($activeForm, $options) { $template = isset($options['template']) ? $options['template'] :'{label} {input}' ; $htmlOptions = isset($options['htmlOptions']) ? $options['htmlOptions'] : array(); $template = str_replace('{label}', $activeForm->label($this->_model, $this->_field), $template); $template = str_replace('{input}', $this->renderInput($activeForm,$htmlOptions), $template); return $template; } } class RDbText extends RDbBase { protected function renderInput($activeForm,$htmlOptions) { if ( $htmlOptions=== null ) $htmlOptions = array('rows'=>6, 'cols'=>50); return $activeForm->textArea($this->_model, $this->_field, $htmlOptions); } public function __toString() { $str = ''.parent::__toString(); $words = preg_split("/\s+/s", $str); if ( count($words) < 2 ) { if ( strlen($str) > 50 ) $out = substr($str, 0, 50).' ...'; else $out = $str; } else { $out = ''; foreach ( $words as $word ) { $out .= $word. ' '; if ( strlen( $out ) > 50 ) { $out .= ' ... '; break; } } } return $out; } } /** * Usually used for fields stored in database with 0 1 values, where 0 is false and 1 is true. * * Provide more user friendly filter and displaying for it. */ class RDbBoolean extends RDbBase { public function __toString() { return $this->_model->{$this->_field} ? 'true' : 'false'; } public function getFieldFilter() { return array('0' => 'false', '1' => 'true'); } protected function renderInput($activeForm,$htmlOptions) { return $activeForm->checkBox($this->_model, $this->_field,$htmlOptions); } } class RDbRelation extends RDbBase { public function getRelatedModel() { $relName = $this->_args[0];//get relation name $relations = $this->_model->relations();//get all relations in current model if ( ! isset($relations[$relName])) throw new CHttpException(500, "Can not find relation $relName"); $relInfo = $relations[$relName];// get relation info for current relation $relModelName = $relInfo[1];//??? not sure it's good @todo: DANGER, FIXME, need good way to retrive relation mode return call_user_func($relModelName.'::model');//related MOdelName::model() } public function getFieldFilter() { $model = $this->getRelatedModel(); $pk = $model->getTableSchema()->primaryKey ;//get primary key info if ( is_array($pk) ) throw new CHttpException("Composed primary key in related record not supported");//@todo: make nice return CHtml::listData($model->findAll(),$pk,'admin.repr'); } protected function renderInput($activeForm, $htmlOptions) { return $activeForm->dropDownList($this->_model, $this->_field, $this->getFieldFilter()); } public function __toString() { try { $relName = $this->_args[0]; if ( $this->_model->$relName === null ) return "{{ unknown id: ".$this->_model->{$this->_field}."}}"; return $this->_model->$relName->admin->getRepr(); } catch ( Exception $e) { return "RAdmin error: ".$e->getMessage(); } } } class RFieldsManager { public function __construct( $model ) { $fields = $model->getFieldsDescription(); foreach ( array_diff( array_keys($model->getAttributes()) , array_keys($fields) ) as $field) { $this->_fields[$field] = new RDbBase( $model, $field); } foreach ( $fields as $field => $class ) { $args = array(); if ( is_array($class) ) { $args = $class; $class = array_shift($args); } $this->_fields[$field ] = new $class( $model, $field, $args); } } public function getField($fieldName) { return $this->_fields[$fieldName]; } } abstract class RActiveRecord extends CActiveRecord { protected $_fieldsManager = null; public function getAdmin() { $adminClassName = get_class($this).'Admin'; if ( ! class_exists( $adminClassName,false ) ) return new RModelAdmin($this); return new $adminClassName($this); } public function getField( $fieldName ) { return $this->getFieldsManager()->getField($fieldName); } public function getFieldsManager() { if ( $this->_fieldsManager == null ) $this->_fieldsManager = new RFieldsManager($this); return $this->_fieldsManager; } /** * * example of return: * <code> * array( * 'fieldName1' => 'FieldType', * 'is_admin' => 'RDbBoolean', * 'description' => 'RDbText', * ); * </code> * * @abstract * @return array */ abstract function getFieldsDescription(); }
[+]
..
[-] RController.php
[edit]
[-] RActiveRecord.php.orig
[edit]
[+]
fields
[-] RModelAdmin.php
[edit]
[+]
views
[-] RActiveRecord.php
[edit]