Login | Register
My pages Projects Community openCollabNet

Discussions > commits > svn commit: r7 - trunk: . data exceptions forms forms/widgets

lawngnome
Discussion topic

Back to topic list

svn commit: r7 - trunk: . data exceptions forms forms/widgets

Reply

Author begby
Full name Jeff Dorsch
Date 2006-06-23 13:00:39 PDT
Message Author: begby Date: 2006-06-23 13:00:38-0700 New Revision: 7 Added: trunk/data/ trunk/data/lgdb.php trunk/data/lgpdo.php trunk/data/lgquerybuilder.php trunk/data/lgstatement.php trunk/exceptions/ trunk/exceptions/lgcomponentexception.php trunk/exceptions/lgcompositeexception.php trunk/exceptions/lgexception.php trunk/exceptions/lgformexception.php trunk/exceptions/lgwidgetexception.php trunk/forms/ trunk/forms/lgform.php trunk/forms/widgets/ trunk/forms/widgets/lgwidget.php trunk/forms/widgets/lgwidget_date.php trunk/forms/widgets/lgwidget_password.php trunk/forms/widgets/lgwidget_select.php trunk/forms/widgets/lgwidget_text.php trunk/forms/widgets/lgwidget_textarea.php trunk/lawngnome.php Log: "" Added: trunk/data/lgdb.php Url: http://lawngnome.tigris.org/source/browse/lawngnome/trunk/data/lgdb.php?view=auto&rev=7 ============================================================================== --- (empty file) +++ trunk/data/lgdb.php 2006-06-23 13:00:38-0700 @@ -0,0 +1,284 @@ + + * @package LawnGnome + * @subpackage db + * + * + */ +class LGDB +{ + + /** + * Lawngnome PDO singleton + * + * @var LGPDO + */ + protected static $LGPDO ; + + + /** + * Get the PDO singleton + * + * @return LGPDO + */ + public static function getPDO() + { + if( !isset( self::$LGPDO ) ) + { + self::$LGPDO = new LGPDO() ; + } + return self::$LGPDO ; + } + + + /** + * Run a simple query to return an array of row arrays using a single where clause + * + * @param string $table The table to query + * @param string $keyField The name of the field to search on + * @param mixed $keyValue Value to search for + * @param string $fieldClause An sql formatted list of fields to retrieve + * @return array + */ + public static function simpleSelect( $table, $keyField, $keyValue, $fieldClause = '*' ) + { + $sql = "SELECT {$fieldClause} FROM {$table} WHERE {$keyField} = :{$keyField}" ; + $stmt = self::getPDO()->prepare( $sql ) ; + $stmt->bindValue( ':'.$keyField, $keyValue ) ; + $stmt->execute() ; + return $stmt->fetchAll() ; + } + + + /** + * Run a simple query and return the first matching row + * + * @param string $table The table to query + * @param string $keyField The name of the field to search on + * @param mixed $keyValue Value to search for + * @param string $fieldClause + * @return array + */ + public static function simpleSelectFirst( $table, $keyField, $keyValue, $fieldClause = '*' ) + { + $sql = "SELECT {$fieldClause} FROM {$table} WHERE {$keyField} = :{$keyField}" ; + $stmt = $stmt = self::getPDO()->prepare( $sql ) ; + $stmt->bindValue( ':'.$keyField, $keyValue ) ; + $stmt->execute() ; + return $stmt->fetch() ; + } + + + + /** + * Simple update using an associative array of values keyed by fieldname + * + * @param string $table The table to query + * @param string $keyField The name of the field to search on + * @param mixed $keyValue Value to search for + * @return bool + */ + public static function simpleUpdate( $table, $values, $keyField, $keyValue ) + { + $fields = array_keys( $values ) ; + /*@var $stmt LGStatement*/ + $sql = self::buildUpdateSQL( $table, $fields )." WHERE {$keyField} = :{$keyField}" ; + $stmt = self::getPDO()->prepare( $sql ) ; + $stmt->bindNamedParams($values) ; + $stmt->bindValue( ':'.$keyField, $keyValue ) ; + return $stmt->execute() ; + } + + + /** + * Insert a row using an associative value array keyed by field name + * + * @param string $table The table to query + * @param array $values + * @return mixed The primary key of the new row or false on failure + */ + public static function simpleInsert( $table, $values ) + { + $fields = array_keys( $values ) ; + $sql = self::buildInsertSQL( $table, $fields ) ; + $stmt = self::getPDO()->prepare( $sql ) ; + $stmt->bindNamedParams($values) ; + if( !$stmt->execute() ) return false ; + return self::getPDO()->lastInsertId() ; + } + + + /** + * Delete a row or rows based on a single where param + * + * @param string $table The table to query + * @param string $keyField The name of the field to search on + * @param mixed $keyValue Value to search for + * @return bool + */ + public static function simpleDelete( $table, $keyField, $keyValue ) + { + $sql = "DELETE FROM {$table} WHERE {$keyField} = :{$keyField}" ; + $stmt = self::getPDO()->prepare( $sql ) ; + $stmt->bindValue( ':'.$keyField, $keyValue ) ; + return $stmt->execute() ; + } + + + + /** + * Build a prepared sql string using PDO style named parameters + * + * @param string $table + * @param array $fields Array of field names + * @return string + */ + public static function buildInsertSQL( $table, $fields ) + { + extract( self::parseFields($fields) ) ; + return "INSERT INTO {$table} ({$fnames}) VALUES ({$pholders})" ; + } + + + /** + * Enter description here... + * + * @param string $table + * @param array $fields Array of field names + * @return string + */ + public static function buildUpdateSQL( $table, $fields ) + { + + foreach( $fields as $field ) + { + $pairs[] = $field.'=:'.$field ; + } + $sqlpairs = implode(', ', $pairs) ; + + return "UPDATE {$table} SET {$sqlpairs}" ; + } + + + /** + * Get the meta info for a table + * + * @param string $table + * @todo Adapt this for different databases, only tested in MySQL + * @return array + */ + public static function getTableMeta( $table ) + { + $rows = self::getPDO()->query( "DESCRIBE {$table}") ; + foreach( $rows as $row ) + { + $metas[$row['Field']] = $row ; + } + return $metas ; + } + + + /** + * Take a list of fields and return an array containing field names + * and placeholder fields + * + * @param array $fields + * @return array + */ + protected static function parseFields( $fields ) + { + foreach( $fields as $field ) $pholders[] = ":{$field}" ; + $pholders = implode(', ', $pholders ) ; + $fnames= implode(', ', $fields ) ; + + $ret['fnames'] = $fnames ; + $ret['pholders'] = $pholders ; + return $ret ; + } + + + /** + * Encrypt a password for database storage using md5 and a 2 character + * hash + * + * @param string $pass + * @return string + */ + public static function EncryptPass( $pass ) + { + $jumble = md5(time() . getmypid()); + $mysalt = substr($jumble,0,2); + $encrypted = crypt($pass, $mysalt); + $encPassword .= trim($encrypted) ; + + return $encPassword ; + } + + + /** + * Check a password against one created with LGDB::EncryptPass + * + * @param string $encPass The encrypted password + * @param string $pass The password to compare + * @return bool + */ + public static function ComparePasswords( $encPass, $pass ) + { + if( $pass == trim(crypt( $pass, $encPass )) ) + { + return true ; + } + return false ; + } + + + + /** + * Format a unix timestamp into database format + * + * @param int $date + * @return string + */ + public static function ParseDate($date){ + // check if its a string, if not we are going to assume its a UNIX timestamp + if ( is_string($date) ){ + // Attempt to convert date string to UNIX timestamp + if ( ($date = strToTime($date)) === -1) { + throw new Exception( 'DB::ParseDate - Invalid Date String') ; + } + } + // Convert unix timestap to the format we need + $date = Date("Y-m-d", $date) ; + return "{d '" . $date . "'}" ; + } + + + + /** + * Format a unix timestamp into database datetime format + * + * @param int $date + * @return string + */ + public static function ParseDateTime($date){ + if ( is_string($date) ){ + if ( ($date = strToTime($date)) === -1) { + throw new Exception( 'DB::ParseDateTime - Invalid Date String') ; + } + } + $date = Date("Y-m-d G:i:s", $date) ; + return "{ts '" . $date . "'}" ; + } + +} + + +?> \ No newline at end of file Added: trunk/data/lgpdo.php Url: http://lawngnome.tigris.org/source/browse/lawngnome/trunk/data/lgpdo.php?view=auto&rev=7 ============================================================================== --- (empty file) +++ trunk/data/lgpdo.php 2006-06-23 13:00:38-0700 @@ -0,0 +1,38 @@ + + * @package LawnGnome + * @subpackage db + * + */ +class LGPDO extends PDO +{ + function __construct() + { + parent::__construct( LawnGnome::$dbURI, LawnGnome::$login, LawnGnome::$password ) ; + $this->setAttribute( PDO::ATTR_STATEMENT_CLASS, array('LGStatement', array($this) ) ) ; + } + + /** + * Prepare an SQL statement, this is only overridden for autocomplete purposes + * because this extended PDO returns an LGStatement + * + * @param string $sql + * @param array $driver_options + * @return LGStatement + */ + public function prepare( $sql ) + { + return parent::prepare( $sql ) ; + } + +} + + +?> \ No newline at end of file Added: trunk/data/lgquerybuilder.php Url: http://lawngnome.tigris.org/source/browse/lawngnome/trunk/data/lgquerybuilder.php?view=auto&rev=7 ============================================================================== --- (empty file) +++ trunk/data/lgquerybuilder.php 2006-06-23 13:00:38-0700 @@ -0,0 +1,378 @@ + + * @package LawnGnome + * @subpackage db + * + */ +class LGQueryBuilder +{ + + /** + * The current field we are setting up + * + * @var string + */ + private $curField ; + + + /** + * Array of where clauses + * + * @var array + */ + private $whereClauses = array() ; + + + /** + * Array of where parameter values + * + * @var array + */ + private $whereParams = array() ; + + /** + * Array of order by clauses + * + * @var array + */ + private $orderBy = array() ; + + /** + * The name of the model we are working with + * + * @var string + */ + private $modelName ; + + /** + * The limit clause + * + * @var string + */ + private $limit = '' ; + + + /** + * Params for the limit clause + * + * @var array + */ + private $limitParams = array() ; + + + /** + * PDO object for generating statments or running queries + * + * @var PDO + */ + private $pdo ; + + + /** + * Underlying LGModel object used to define the tables and base + * sql statement that querybuilder works on. + * + * @var LGModel + */ + private $model ; + + /** + * Constructor + * + * @param string $modelName + */ + public function __construct( LGModel $model, PDO $pdo ) + { + $this->model = $model ; + $this->modelName = get_class($model) ; + $this->pdo = $pdo ; + } + + + /** + * Get the underlying model + * + * @return LGModel + */ + public function getModel() + { + return $this->model ; + } + + + /** + * Override the getting of a variable and instead set the current field to the field + * name requested and return an instance of this in its place + * + * @param string $field + * @return LGQueryBuilder + */ + public function __get( $qualifiedField ) + { + $parts = explode( '_', $qualifiedField ) ; + $table = $parts[0] ; + $field = $parts[1] ; + $this->curField = $this->model->$table->getAsObject($field)->getSelectName() ; + return $this ; + } + + + /** + * Override setting a variable and just throw an exception, because thats cheating + * + * @param string $field + * @param mixed $value + * @throws Exception + */ + public function __set( $field, $value ) + { + throw new Exception( 'Cannot set a field value on a query' ) ; + } + + + /** + * Similar to __get currently + * + * @param string $field + * @param string $args + * @return LGQueryBuilder + */ + public function __call( $field, $args ) + { + $this->curField = $field ; + return $this ; + } + + + /** + * WHERE field = val + * + * @param mixed $val The value to compare + * @return LGQueryBuilder + */ + public function EQ( $val ) + { + $this->AddWhereClause( $this->curField, '=', $val ); + return $this ; + } + + + /** + * WHERE field <> val + * + * @param mixed $val + * @return LGQueryBuilder + */ + public function NEQ( $val ) + { + $this->AddWhereClause( $this->curField, '<>', $val ); + return $this ; + } + + /** + * WHERE field > val + * + * @param mixed $val + * @return LGQueryBuilder + */ + public function GT( $val ) + { + $this->AddWhereClause( $this->curField, '>', $val ); + return $this ; + } + + + /** + * WHERE field >= val + * + * @param mixed $val + * @return LGQueryBuilder + */ + public function GTE( $val ) + { + $this->AddWhereClause( $this->curField, '>=', $val ); + return $this ; + } + + + /** + * WHERE field < val + * + * @param mixed $val + * @return LGQueryBuilder + */ + public function LT( $val ) + { + $this->AddWhereClause( $this->curField, '<', $val ); + return $this ; + } + + + /** + * WHERE field <= val + * + * @param mixed $val + * @return LGQueryBuilder + */ + public function LTE( $val ) + { + $this->AddWhereClause( $this->curField, '<=', $val ); + return $this ; + } + + + + /** + * ORDER BY field dir + * + * @param string $field + * @param string $dir + * @return LGQueryBuilder + */ + public function ORDER_BY( $field, $dir = 'desc' ) + { + $this->orderBy[] = $field.' '.$dir ; + return $this ; + } + + + /** + * Limit the result set + * + * @param int $startRecord + * @param int $recCount + * @return LGQueryBuilder + */ + public function LIMIT( $startRecord, $recCount ) + { + $this->limit = "LIMIT {$startRecord}, {$recCount}" ; + //$this->limit = 'LIMIT :startRecord, :recCount' ; + $this->limitParams['startRecord'] = $startRecord ; + $this->limitParams['recCount'] = $recCount ; + return $this ; + } + + + /** + * field LIKE val + * + * @param mixed $val + * @return LGQueryBuilder + */ + public function LIKE( $val ) + { + $this->AddWhereClause( $this->curField, 'LIKE', $val ); + return $this ; + } + + /** + * Add a where clause to the internal array + * + * @param string $field + * @param string $operator + * @param string $val + */ + private function AddWhereClause( $field, $operator, $val ) + { + $this->whereClauses[] = $this->curField .' '.$operator.' ?' ; + $this->whereParams[] = $val ; + } + + + /** + * Reset all the query parameters + * + */ + public function Reset() + { + $this->whereClauses = array() ; + $this->whereParams = array() ; + $this->limitParams = array() ; + $this->limit = '' ; + $this->orderBy = array() ; + } + + + /** + * Run the query, this returns an array of records + * + * @return mixed + */ + public function RunQuery() + { + $whereClause = ($this->whereClauses) ? ' AND '.implode( ' AND ', $this->whereClauses ) : '' ; + $orderBy = ($this->orderBy) ? 'ORDER BY '.implode( ', ', $this->orderBy) : '' ; + $limit = $this->limit ; + //$selectFields = $this->selectFields ; + + + $sql = $this->model->getSQL() ; + + $sql.=" {$whereClause} + {$orderBy} + {$limit}" ; + + $stmt = $this->pdo->prepare($sql) ; + + + /* + if( $limit ) + { + $stmt->bindValue(':startRecord', $this->limitParams['startRecord'], PDO::PARAM_INT) ; + $stmt->bindValue(':recCount', $this->limitParams['recCount'], PDO::PARAM_INT) ; + } + */ + + if( !$stmt->execute( $this->whereParams ) ) + { + print_r($stmt->errorInfo() ) ; + } + + $recs = array() ; + + $class = $this->modelName ; + while( $row = $stmt->fetch() ) + { + $mdl = new $class() ; + $mdl->initValuesFromArray( $row ) ; + $recs[] = $mdl ; + } + return $recs ; + + + } + + + /** + * Delete the records that match the query, this does not automatically do cascading deletes on joined tables + * + */ + public function RunDelete() + { + $whereClause = ($this->whereClauses) ? implode( ' AND ', $this->whereClauses ) : '' ; + $table = $this->model->getTable() ; + $joins = $this->model->GetJoins() ; + + $sql = + "DELETE + FROM + {$table} + {$joins} + {$whereClause}" ; + + + db::RunQuery( $sql ) ; + } + +} + + +?> \ No newline at end of file Added: trunk/data/lgstatement.php Url: http://lawngnome.tigris.org/source/browse/lawngnome/trunk/data/lgstatement.php?view=auto&rev=7 ============================================================================== --- (empty file) +++ trunk/data/lgstatement.php 2006-06-23 13:00:38-0700 @@ -0,0 +1,51 @@ + + * @package LawnGnome + * @subpackage db + * + */ +class LGStatement extends PDOStatement +{ + + /** + * Lawngnome PDO object + * + * @var LGPDO + */ + public $dbh ; + + + /** + * Constructor + * + * @param LGPDO $dbh + */ + protected function __construct( LGPDO $dbh ) + { + $this->dbh = $dbh ; + $this->setFetchMode( PDO::FETCH_ASSOC ) ; + } + + + /** + * Bind an associatave array of values keyed by the fieldname, binds + * to named based placeholder + * + * @param array $values + */ + protected function bindNamedParams( $values ) + { + foreach( $values as $key => $value ) + { + $this->bindValue(':'.$key, $value) ; + } + } + +} + + +?> \ No newline at end of file Added: trunk/exceptions/lgcomponentexception.php Url: http://lawngnome.tigris.org/source/browse/lawngnome/trunk/exceptions/lgcomponentexception.php?view=auto&rev=7 ============================================================================== --- (empty file) +++ trunk/exceptions/lgcomponentexception.php 2006-06-23 13:00:38-0700 @@ -0,0 +1,17 @@ + + * @package LawnGnome + * @subpackage exceptions + * + */ +class LGComponentException extends LGException +{ + +} + +?> \ No newline at end of file Added: trunk/exceptions/lgcompositeexception.php Url: http://lawngnome.tigris.org/source/browse/lawngnome/trunk/exceptions/lgcompositeexception.php?view=auto&rev=7 ============================================================================== --- (empty file) +++ trunk/exceptions/lgcompositeexception.php 2006-06-23 13:00:38-0700 @@ -0,0 +1,17 @@ + + * @package LawnGnome + * @subpackage exceptions + * + */ +class LGCompositeException extends LGException +{ + +} + +?> \ No newline at end of file Added: trunk/exceptions/lgexception.php Url: http://lawngnome.tigris.org/source/browse/lawngnome/trunk/exceptions/lgexception.php?view=auto&rev=7 ============================================================================== --- (empty file) +++ trunk/exceptions/lgexception.php 2006-06-23 13:00:38-0700 @@ -0,0 +1,80 @@ + + * @package LawnGnome + * @subpackage exceptions + * + */ + +class LGException extends Exception +{ + + /** + * Formats error message + * + * @return string + */ + private function formatMessage() + { + $msg = '[' . $_SERVER['REMOTE_ADDR'] . ']' + . '[' . $this->getMessage() . ']' + . '[' . $this->getCode() . '] ' + . '[' . $this->getFile() . '] ' + . '[' . $this->getLine() . ']'; + + $msg .= "\n" . $this->getTraceAsString(); + + return $msg; + } + + /** + * Displays the exception + * + * @return LGException returns self (for fluent interfaces) + */ + public function display() + { + echo '
';
+		echo $this;
+		echo '
'; + return $this; + } + + /** + * Processes the exception + * + * @return LGException returns self (for fluent interfaces) + */ + public function process() + { + $debug = true ; + if ($debug) + { + $this->display(); + } + else + { + $this->log(); + } + return $this; + } + + /** + * Hide path information in error messages + * + * @return string + */ + public function __toString() + { + return str_replace(str_replace('/', DIRECTORY_SEPARATOR + , $_SERVER['DOCUMENT_ROOT']) + , "[DOCUMENT_ROOT]", parent::__toString()); + } +} + + +?> \ No newline at end of file Added: trunk/exceptions/lgformexception.php Url: http://lawngnome.tigris.org/source/browse/lawngnome/trunk/exceptions/lgformexception.php?view=auto&rev=7 ============================================================================== --- (empty file) +++ trunk/exceptions/lgformexception.php 2006-06-23 13:00:38-0700 @@ -0,0 +1,16 @@ + + * @package LawnGnome + * @subpackage exceptions + * + */ +class LGFormException extends LGException +{ + +} + +?> \ No newline at end of file Added: trunk/exceptions/lgwidgetexception.php Url: http://lawngnome.tigris.org/source/browse/lawngnome/trunk/exceptions/lgwidgetexception.php?view=auto&rev=7 ============================================================================== --- (empty file) +++ trunk/exceptions/lgwidgetexception.php 2006-06-23 13:00:38-0700 @@ -0,0 +1,17 @@ + + * @package LawnGnome + * @subpackage exceptions + * + */ +class LGWidgetException extends LGException +{ + +} + +?> \ No newline at end of file Added: trunk/forms/lgform.php Url: http://lawngnome.tigris.org/source/browse/lawngnome/trunk/forms/lgform.php?view=auto&rev=7 ============================================================================== --- (empty file) +++ trunk/forms/lgform.php 2006-06-23 13:00:38-0700 @@ -0,0 +1,204 @@ + + * @package LawnGnome + * @subpackage forms + * + */ +class LGForm implements IteratorAggregate +{ + + /** + * + * + * @var ArrayObject + */ + protected $widgets ; + + + /** + * + * + * @var LGModel + */ + private $model ; + + + + protected static $options = array() ; + + + public function __construct( LGModel $model ) + { + $this->model = $model ; + $this->widgets = new ArrayObject() ; + + $classes = $this->get_classes() ; + + if( sizeof($classes) == 0 ) + { + $this->AutoBuildAll(); + } + else + { + foreach( $classes as $class ) + { + $this->runOverrideMethod($class, 'defineForm') ; + } + } + } + + + private function get_classes() + { + $classes = array() ; + $class = get_class($this) ; + + while( $class != __CLASS__) + { + $classes[] = $class; + $class = get_parent_class($class) ; + } + return array_reverse( $classes ) ; + } + + + /** + * Enter description here... + * + * @return LGModel + */ + public function getModel() + { + return $this->model ; + } + + + protected function runOverrideMethod( $classname, $method ) + { + $class = new ReflectionClass( $classname ) ; + if( $class->hasMethod($method) ) + { + + $method = $class->getMethod( $method ) ; + $dclass = $method->getDeclaringClass() ; + if( $dclass->getName() == $classname) + { + + $method->invoke($this) ; + return true ; + } + } + return false ; + } + + + public function __get( $field) + { + if( !$this->__isset( $field ) ) + { + throw new LGFormException( 'Widget cannot be found' ) ; + } + return $this->widgets[$field] ; + } + + + + public function __set( $field, $value ) + { + if( !$this->__isset( $field ) ) + { + throw new LGFormException( 'Widget cannot be found' ) ; + } + $this->widget[$field]->setValue( $value ) ; + } + + + + public function __isset( $field ) + { + return $this->widgets->offsetExists($field) ; + } + + + public function __unset($field) + { + if( !$this->__isset( $field ) ) + { + throw new LGFormException( 'Widget cannot be found' ) ; + } + $this->removeWidget($field); + } + + + public function GetIterator() + { + return $this->widgets->getIterator() ; + } + + + public function addWidget( LGWidget $widget ) + { + $this->widgets[$widget->getFieldObject()->getName()] = $widget ; + } + + + public function removeWidget( $field ) + { + if( !$this->__isset( $field ) ) + { + throw new LGFormException( 'Widget cannot be found' ) ; + } + unset($this->widgets[$field]) ; + + } + + + public function __toString() + { + return $this->draw() ; + } + + + public function setValuesFromPost( $values = array() ) + { + + foreach( $values as $field => $value ) + { + if( $this->widgets->offsetExists($field)) + { + $this->$field->setValue($value) ; + } + } + } + + + protected function autoBuild( $table ) + { + foreach( $this->model->$table as $field ) + { + if( $field instanceof LGUpdateableField && !$field->isForeign() ) + { + $this->addWidget( LGWidget::factory( $field ) ); + } + } + } + + + protected function autoBuildAll() + { + foreach( $this->model->recursiveIterator() as $field ) + { + if( $field instanceof LGUpdateableField && !$field->isForeign() ) + { + $this->addWidget( LGWidget::factory( $field ) ); + } + } + } + +} + +?> \ No newline at end of file Added: trunk/forms/widgets/lgwidget.php Url: http://lawngnome.tigris.org/source/browse/lawngnome/trunk/forms/widgets/lgwidget.php?view=auto&rev=7 ============================================================================== --- (empty file) +++ trunk/forms/widgets/lgwidget.php 2006-06-23 13:00:38-0700 @@ -0,0 +1,299 @@ + + * @package LawnGnome + * @subpackage forms + * @subpackage widgets + * + */ +abstract class LGWidget implements IteratorAggregate +{ + + /** + * Enter description here... + * + * @var LGField + */ + protected $field ; + + + protected $title ; + + + /** + * HTML attributes that define the input + * + * @var Array + */ + private $attributes = array() ; + + public function __construct( LGField $fieldObject, $title = false, $attributeArray = array() ) + { + if( !$title ) + { + $this->setTitle( $fieldObject->getName() ) ; + } + else + { + $this->setTitle( $title ) ; + } + + $this->field = $fieldObject ; + $this->AddAttribute( 'name', true ) ; + $this->AddAttribute( 'class', false ); + $this->DefineAttributes(); + $this->name = $fieldObject->getName() ; + $this->SetAttributesFromArray( $attributeArray ) ; + + } + + + public static function factory( LGField $fieldObject ) + { + + switch( strtolower(get_class( $fieldObject )) ) + { + case 'lgfield_int' : + case 'lgfield_float' : + case 'lgfield_varchar' : + return new LGWidget_Text( $fieldObject ) ; + break ; + + case 'lgfield_text' : + return new LGWidget_Textarea( $fieldObject ) ; + break ; + + case 'lgfield_manytoone': + $widget = new LGWidget_Select( $fieldObject ) ; + $widget->SetOptions($fieldObject->getOptionsAsArray()); + return $widget ; + break ; + + case 'lgfield_date' : + return new LGWidget_Date( $fieldObject ) ; + break ; + + default : + return new LGWidget_Text( $fieldObject ) ; + break ; + } + + } + + + + abstract public function draw() ; + + + abstract protected function defineAttributes() ; + + + + /** + * Retrieve the value of an attribute + * + * @param string $attributeName + * @return mixed + */ + public function __get( $attributeName ) + { + if( isset( $this->attributes[$attributeName] ) ) + { + return $this->attributes[$attributeName]['value'] ; + } + else + { + throw new LGWidgetException( "Attempt to access a non-existant widget attribute" ) ; + } + } + + + /** + * Set an attribute value + * + * @param string $attributeName + * @param mixed $value + */ + public function __set( $attributeName, $value ) + { + if( isset( $this->attributes[$attributeName] ) ) + { + $this->attributes[$attributeName]['value'] = $value; + } + else + { + throw new LGWidgetException( "Attempt to set a non-existant widget attribute" ) ; + } + } + + + + /** + * Set an attribute value using function syntax + * + * @param string $attributeName + * @param array $attrs + * @return HTMLWidget + */ + public function __call( $attributeName, $attrs ) + { + + switch( count( $attrs ) ) + { + case 0: + $value = false ; + break ; + + case 1: + $value = $attrs[0] ; + break ; + + default: + $value = $attrs ; + break ; + } + + $this->$attributeName = $value ; + return $this ; + + } + + + /** + * Check if an attribute is set + * + * @param string $attributeName + * @return bool + */ + public function __isset( $attributeName ) + { + return isset( $this->attributes[$attributeName] ) ; + } + + + + /** + * Enter description here... + * + * @param mixed $value + */ + public function setValue( $value ) + { + $this->field->setValue($value); + } + + + + public function setTitle( $title ) + { + $this->title = $title ; + return $this ; + } + + + public function getTitle() + { + return $this->title ; + } + + + + /** + * Enter description here... + * + * @return LGField + */ + public function getFieldObject() + { + return $this->field ; + } + + + + /** + * IteratorAggregate implementation + * + * @return ArrayIterator + */ + public function getIterator() + { + return new ArrayObject($this->attributes) ; + } + + + /** + * Set the attributes from an array + * + * @param array $array + */ + public function setAttributesFromArray( $array = array() ) + { + foreach( $array as $attribute => $value ) + { + $this->$attribute = $value ; + } + } + + + /** + * Add an attribute + * + * @param string $attributeName + * @param bool $required + * @param mixed $default + */ + protected function addAttribute( $attributeName, $required = false, $default = null ) + { + $this->attributes[$attributeName] = array( 'required' => $required, 'value' => $default ) ; + } + + + /** + * Check to see if all required attributes are set + * + */ + protected function checkRequired() + { + foreach( $this as $attribute => $detail ) + { + if( $detail['required'] && $detail['value'] === null || $detail['value'] === false ) + { + throw new Exception( "Required attribute(s) not set for widget" ) ; + } + } + } + + + /** + * Get all set attributes in a string suitable for placing in an input element + * + * ex: 'attr="value" attr2="value2"' + * + * @return string + */ + protected function getAttributes() + { + foreach( $this as $attribute => $detail ) + { + if( $detail['value'] !== null && $detail['value'] !== false ) + { + $attr.= ' '.$attribute.'="'.$detail['value'].'"' ; + } + } + return $attr ; + } + + + + protected function getValue() + { + return htmlspecialchars($this->getFieldObject()->getValue()) ; + } + + +} + + +?> \ No newline at end of file Added: trunk/forms/widgets/lgwidget_date.php Url: http://lawngnome.tigris.org/source/browse/lawngnome/trunk/forms/widgets/lgwidget_date.php?view=auto&rev=7 ============================================================================== --- (empty file) +++ trunk/forms/widgets/lgwidget_date.php 2006-06-23 13:00:38-0700 @@ -0,0 +1,58 @@ + + * @package LawnGnome + * @subpackage forms + * @subpackage widgets + * + */ +class LGWidget_Date extends LGWidget +{ + + /** + * Draw the form input + * + * @return string + */ + public function draw() + { + $this->checkRequired() ; + + + $value = $this->getFieldObject()->getValue() ; + + + $month = date('m', strtotime($value) ) ; + $day = date('d', strtotime($value) ) ; + $year = date('Y', strtotime($value) ) ; + + $m = '' ; + $d = '' ; + $y = '' ; + + return $m .'/'.$d.'/'.$y ; + } + + + + + /** + * Setup the available attributes + * + */ + protected function DefineAttributes() + { + } + + + + public function setValue( $value ) + { + $date =$value['y'] .'-'.$value['m'].'-'.$value['d'] ; + $this->field->setValue($date); + } + +} + +?> \ No newline at end of file Added: trunk/forms/widgets/lgwidget_password.php Url: http://lawngnome.tigris.org/source/browse/lawngnome/trunk/forms/widgets/lgwidget_password.php?view=auto&rev=7 ============================================================================== --- (empty file) +++ trunk/forms/widgets/lgwidget_password.php 2006-06-23 13:00:38-0700 @@ -0,0 +1,37 @@ + + * @package LawnGnome + * @subpackage forms + * @subpackage widgets + * + */ +class LGWidget_Password extends LGWidget +{ + + /** + * Draw the form input + * + * @return string + */ + public function draw() + { + $this->checkRequired() ; + return 'GetAttributes().' value=""/>' ; + } + + + /** + * Setup the available attributes + * + */ + protected function defineAttributes() + { + $this->AddAttribute( 'size', false ) ; + $this->AddAttribute( 'maxlength', false ) ; + } + +} + +?> \ No newline at end of file Added: trunk/forms/widgets/lgwidget_select.php Url: http://lawngnome.tigris.org/source/browse/lawngnome/trunk/forms/widgets/lgwidget_select.php?view=auto&rev=7 ============================================================================== --- (empty file) +++ trunk/forms/widgets/lgwidget_select.php 2006-06-23 13:00:38-0700 @@ -0,0 +1,63 @@ + + * @package LawnGnome + * @subpackage forms + * @subpackage widgets + * + */ +class LGWidget_Select extends LGWidget +{ + + private $options = array(); + + + /** + * Draw the form input + * + * @return string + */ + public function draw() + { + $this->checkRequired() ; + + $value = $this->getFieldObject()->getValue() ; + + $input = "GetAttributes()}>" ; + foreach( $this->options as $key => $option ) + { + $sel = ($key == $value) ? ' selected' : '' ; + $input .= '\n" ; + } + $input .= "" ; + + return $input ; + } + + + /** + * Setup the available attributes + * + */ + protected function defineAttributes() + { + } + + + + public function SetOptions( $array ) + { + $this->options = $array ; + } + + public function __toString() + { + return $this->options[$this->getValue()] ; + } + + +} + +?> \ No newline at end of file Added: trunk/forms/widgets/lgwidget_text.php Url: http://lawngnome.tigris.org/source/browse/lawngnome/trunk/forms/widgets/lgwidget_text.php?view=auto&rev=7 ============================================================================== --- (empty file) +++ trunk/forms/widgets/lgwidget_text.php 2006-06-23 13:00:38-0700 @@ -0,0 +1,39 @@ + + * @package LawnGnome + * @subpackage forms + * @subpackage widgets + * + */ +class LGWidget_Text extends LGWidget +{ + + /** + * Draw the form input + * + * @return string + */ + public function draw() + { + $this->checkRequired() ; + + return 'GetAttributes().' value="'.$this->getValue().'"/>' ; + } + + + /** + * Setup the available attributes + * + */ + protected function defineAttributes() + { + $this->AddAttribute( 'size', false ) ; + $this->AddAttribute( 'maxlength', false ) ; + } + + +} + +?> \ No newline at end of file Added: trunk/forms/widgets/lgwidget_textarea.php Url: http://lawngnome.tigris.org/source/browse/lawngnome/trunk/forms/widgets/lgwidget_textarea.php?view=auto&rev=7 ============================================================================== --- (empty file) +++ trunk/forms/widgets/lgwidget_textarea.php 2006-06-23 13:00:38-0700 @@ -0,0 +1,39 @@ + + * @package LawnGnome + * @subpackage forms + * @subpackage widgets + * + */ +class LGWidget_Textarea extends LGWidget +{ + + /** + * Draw the form input + * + * @return string + */ + public function draw() + { + $this->checkRequired() ; + return 'GetAttributes().'>'.$this->getFieldObject()->getValue().'' ; + } + + + /** + * Setup the available attributes + * + */ + protected function defineAttributes() + { + $this->AddAttribute( 'cols', false, '40' ) ; + $this->AddAttribute( 'rows', false, '5' ) ; + } + +} + + +?> \ No newline at end of file Added: trunk/lawngnome.php Url: http://lawngnome.tigris.org/source/browse/lawngnome/trunk/lawngnome.php?view=auto&rev=7 ============================================================================== --- (empty file) +++ trunk/lawngnome.php 2006-06-23 13:00:38-0700 @@ -0,0 +1,109 @@ + + * @package LawnGnome + * @version .2 + * + * + */ +class LawnGnome +{ + + + /** + * PDO connection URI + * + * @var string + */ + public static $dbURI ; + + /** + * Database login + * + * @var string + */ + public static $login ; + + /** + * Database password + * + * @var string + */ + public static $password ; + + /** + * The path to the root of the lawngnome folder + * + * @var string + * @access privatw + */ + private static $basepath ; + + + + /** + * Set the basepath for lawngnome + * + */ + public static function init() + { + if( !isset(self::$basepath) ) + { + self::$basepath = dirname(__FILE__) ; + } + } + + + /** + * If not using __autoinclude and you are a lazy ass and like to + * include an assload of files in every page load that probably + * aren't being used, then call this method after init() + * + */ + public static function includeAll() + { + self::init() ; + + $path = str_replace( '.', DIRECTORY_SEPARATOR, strtolower($pkg) ) ; + $dir = new RecursiveDirectoryIterator( self::$basepath.DIRECTORY_SEPARATOR ) ; + foreach( new RecursiveIteratorIterator($dir) as $file ) + { + if( $dir->isDot() ) + continue ; + + $split = explode( '.', strtolower( (string) $file ) ) ; + //echo (string) $file .'
' ; + if( $split[1] == 'php' ) + { + require_once( (string) $file ) ; + } + + } + + } + + + /** + * Get the basepath of the lawngnome root + * + * @return string + */ + public static function basePath() + { + return self::$basepath ; + } + +} + + +?> \ No newline at end of file

« Previous message in topic | 1 of 1 | Next message in topic »

Messages

Show all messages in topic

svn commit: r7 - trunk: . data exceptions forms forms/widgets begby Jeff Dorsch 2006-06-23 13:00:39 PDT
Messages per page: