<?php
/*
* Copyright 2008 Blandware (http://www.blandware.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Utils to work with descriptors.
*
* @package AtleapLite
* @author Roman Puchkovskiy
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
*/
/**
* This class is used to pass data between form, dao and assoc array converting
* if necessary.
* <b>This class has following convention: when field names are mentioned,
* they are obj-fields, not db-fields</b>
*
* @package AtleapLite
*/
class Descriptor {
/**
* Mapping from obj-fields names to db-fields names
* @var array
* @access private
*/
var $objToDb;
/**
* Array of obj-fields
* @var array
* @access private
*/
var $objFields;
/**
* Array of db-fields
* @var array
* @access private
*/
var $dbFields;
/**
* Array of names of fields which are not needed in grid
* @var array
* @access private
*/
var $nonGridFields;
/**
* Array of names of fields which are needed in grid
* @var array
* @access private
*/
var $gridFields;
/**
* Array of names of fields which are represented with selects in form
* @var array assoc array from field names to field types
* @access private
*/
var $selectFields;
/**
* Array of names of fields which hold dates
* @var array
* @access private
*/
var $dateFields;
/**
* Array of names of fields which are not passed from form to dao and
* vice-versa
* @var array
* @access private
*/
var $nonTransferrableFields;
/**
* Constructor.
*
* @param array $map mapping from obj-fields to db-fields
* @param array $notInGrid optional fields that are not needed in grid
* @param array $selectFields optional assoc array from field names
* (of fields which are rendered using selects in form) to field types
* @param array $dateFields optional fields that hold dates
* @param array $noTransfer optional non-transferrable fields
*/
function Descriptor($map, $notInGrid = array(),
$selectFields = array(), $dateFields = array(),
$noTransfer = array()) {
$this->nonGridFields = $notInGrid;
$this->objToDb = $map;
$this->objFields = array();
$this->dbFields = array();
$this->gridFields = array();
foreach ($map as $k => $v) {
$this->objFields[] = $k;
$this->dbFields[] = $v;
if (!$this->isNotInGrid($k)) {
$this->gridFields[] = $k;
}
}
$this->selectFields = $selectFields;
$this->dateFields = $dateFields;
$this->nonTransferrableFields = $noTransfer;
}
/**
* Returns mapping from obj-fields to db-fields.
*
* @return array mapping
*/
function &getObjToDb() {
return $this->objToDb;
}
/**
* Returns names of obj-fields.
*
* @return array obj-fields
*/
function &getObjFields() {
return $this->objFields;
}
/**
* Returns names of db-fields.
*
* @return array db-fields
*/
function &getDbFields() {
return $this->dbFields;
}
/**
* Returns whether there's an obj-field with a given name.
*
* @param string $name name to check
* @return true if obj-field with given name exists
*/
function isObjField($name) {
return array_search($name, $this->objFields) !== false;
}
/**
* Returns whether there's a db-field with a given name.
*
* @param string $name name to check
* @return true if db-field with given name exists
*/
function isDbField($name) {
return array_search($name, $this->dbFields) !== false;
}
/**
* Returns names of fields that are not in grid.
*
* @return array names
*/
function &getNonGridFields() {
return $this->nonGridFields;
}
/**
* Returns whether there's a field with a given name which is not in grid.
*
* @param string $name name to check
* @return true if field is not in grid
*/
function isNotInGrid($name) {
return array_search($name, $this->nonGridFields) !== false;
}
/**
* Returns names of fields that are in grid.
*
* @return array names
*/
function &getGridFields() {
return $this->gridFields;
}
/**
* Converts obj-field name to corresponding db-field name.
*
* @param string $name field name
* @return converted field name
*/
function objToDb($name) {
return $this->objToDb[$name];
}
/**
* Converts db-field name to corresponding obj-field name.
*
* @param string $name field name
* @return converted field name
*/
function dbToObj($name) {
$k = array_search($name, $this->objToDb);
return $k === false ? null : $k;
}
/**
* Returns names of fields which are rendered in form using selects.
*
* @return array select fields
*/
function &getSelectFields() {
return $this->selectFields;
}
/**
* Returns whether there's a select field with a given name.
*
* @param string $name name to check
* @return true if select field with given name exists
*/
function isSelectField($name) {
return isset($this->selectFields[$name]);
}
/**
* Returns type of select field.
*
* @param string $name field name
* @return string type
*/
function getSelectFieldType($name) {
return $this->selectFields[$name];
}
/**
* Returns whether given field is a select field with int type.
*
* @param string $name field name
* @return bool true if field is select field and has int type
*/
function isSelectFieldTypeInt($name) {
return $this->getSelectFieldType($name) == 'int';
}
/**
* Returns whether given field is a select field with string type.
*
* @param string $name field name
* @return bool true if field is select field and has string type
*/
function isSelectFieldTypeString($name) {
return $this->getSelectFieldType($name) == 'string';
}
/**
* Returns names of fields which hold dates.
*
* @return array date fields
*/
function &getDateFields() {
return $this->dateFields;
}
/**
* Returns whether there's a date field with a given name.
*
* @param string $name name to check
* @return true if date field with given name exists
*/
function isDateField($name) {
return array_search($name, $this->dateFields) !== false;
}
/**
* Returns whether there's a field with a given name which is not
* non-transferrable.
*
* @param string $name name to check
* @return true if transferrable field with given name exists
*/
function isTransferrable($name) {
return array_search($name, $this->nonTransferrableFields) === false;
}
/**
* Transfers data from form object to dao object converting if needed.
*
* @param object $form form object
* @param object $dao dao object
*/
function formToDao(&$form, &$dao) {
foreach ($this->objToDb as $obj => $db) {
if ($this->isTransferrable($obj) && $form->elementExists($obj)) {
$value = $form->getElementValue($obj);
if ($this->isSelectField($obj)) {
$value = $value[0];
}
if ($this->isDateField($obj)) {
$value = dateToDbFormat($value);
}
$dao->$db = $value;
}
}
}
/**
* Transfers data from dao object to form object converting if needed.
*
* @param object $dao dao object
* @param object $form form object
*/
function daoToForm(&$dao, &$form) {
foreach ($this->objToDb as $obj => $db) {
if ($this->isTransferrable($obj) && $form->elementExists($obj)) {
$element =& $form->getElement($obj);
$value = $dao->$db;
if ($this->isDateField($obj)) {
$value = dateFromDbFormat($value);
}
$element->setValue($value);
}
}
}
/**
* Extracts data from form object to assoc array converting if needed.
*
* @param object $form form object
* @return array assoc array from obj-fields to their values
*/
function formToArray(&$form) {
$dto = array();
foreach ($this->objFields as $obj) {
if ($this->isTransferrable($obj) && $form->elementExists($obj)) {
$value = $form->getElementValue($obj);
if ($this->isSelectField($obj)) {
$value = $value[0];
}
$dto[$obj] = $value;
}
}
return $dto;
}
/**
* Transfers data from assoc array to form object converting if needed.
*
* @param object $dto assoc array from obj-fields to their values
* @param object $form form object
*/
function arrayToForm(&$dto, &$form) {
foreach ($this->objFields as $obj) {
if ($this->isTransferrable($obj) && $form->elementExists($obj)) {
$element =& $form->getElement($obj);
$element->setValue($dto[$obj]);
}
}
}
/**
* Transfers data from assoc array to dao object converting if needed.
*
* @param object $dto assoc array from obj-fields to their values
* @param object $dao form object
*/
function arrayToDao(&$dto, &$dao) {
foreach ($this->objToDb as $obj => $db) {
if ($this->isTransferrable($obj)) {
$value = $dto[$obj];
if ($this->isDateField($obj)) {
$value = dateToDbFormat($value);
}
$dao->$db = $value;
}
}
}
/**
* Extracts data from dao object to assoc array converting if needed.
*
* @param object $dao dao object
* @return array assoc array from obj-fields to their values
*/
function daoToArray(&$dao) {
$dto = array();
foreach ($this->objToDb as $obj => $db) {
$t = $this->isTransferrable($obj);
if ($this->isTransferrable($obj)) {
$value = $dao->$db;
if ($this->isDateField($obj)) {
$value = dateFromDbFormat($value);
}
$dto[$obj] = $value;
}
}
return $dto;
}
}
/**
* User entity descriptor
*/
$userDescriptor = new Descriptor(
array('id' => 'id', 'login' => 'login',
'password' => 'password', 'firstName' => 'first_name',
'patronymicName' => 'patronymic_name', 'lastName' => 'last_name',
'address' => 'address', 'phone' => 'phone', 'fax' => 'fax',
'email' => 'email', 'roleId' => 'role_id'),
array('id', 'password'),
array('roleId' => 'int'),
array(),
array('id', 'password'));
/**
* Role entity descriptor
*/
$roleDescriptor = new Descriptor(
array('id' => 'id', 'identifier' => 'identifier',
'title' => 'title', 'description' => 'description',
'admin' => 'admin'),
array('id'),
array(),
array(),
array('id'));
/**
* Page entity descriptor
*/
$pageDescriptor = new Descriptor(
array('id' => 'id', 'identifier' => 'identifier', 'language' => 'language',
'title' => 'title', 'description' => 'description',
'keywords' => 'keywords', 'layout' => 'layout', 'fixed' => 'fixed',
'menu' => 'menu', 'menuIndex' => 'menu_index',
'published' => 'published',
'modificationDate' => 'modification_date',
'publicationDate' => 'publication_date',
'expirationDate' => 'expiration_date',
'page_active' => 'page_active' // this is an alias
),
array('id'),
array('layout' => 'string', 'menu' => 'string', 'language' => 'string',
'page_active' => 'integer'),
array('modificationDate', 'publicationDate', 'expirationDate'),
array('id', 'fixed', 'page_active'));
/**
* Field entity descriptor
*/
$fieldDescriptor = new Descriptor(
array('id' => 'id', 'identifier' => 'identifier',
'pageId' => 'page_id', 'data' => 'data'),
array('id'),
array(),
array(),
array('id'));
/**
* Resource entity descriptor
*/
$resourceDescriptor = new Descriptor(
array('id' => 'id', 'title' => 'title', 'size' => 'size',
'download' => 'download', 'data' => 'data', 'fileName' => 'file_name',
'published' => 'published',
'modificationDate' => 'modification_date',
'publicationDate' => 'publication_date',
'expirationDate' => 'expiration_date',
'resource_active' => 'resource_active' // this is an alias
),
array('id', 'data'),
array('resource_active' => 'integer'),
array('modificationDate', 'publicationDate', 'expirationDate'),
array('id', 'data', 'size', 'resource_active'));
/**
* Category entity descriptor
*/
$categoryDescriptor = new Descriptor(
array('id' => 'id', 'title' => 'title',
'description' => 'description', 'parentId' => 'parent_id'),
array('id', 'parentId'),
array(),
array(),
array('id', 'parentId'));
/**
* Commodity entity descriptor
*/
$commodityDescriptor = new Descriptor(
array('id' => 'id', 'title' => 'title',
'description' => 'description', 'categoryId' => 'category_id',
'orderNumber' => 'order_number', 'imageId' => 'image_id'),
array('id', 'categoryId', 'imageId'),
array(),
array(),
array('id', 'categoryId', 'imageId'));
/**
* Returns user descriptor.
*
* @global object user descriptor
* @return object descriptor
*/
function &getUserDescriptor() {
global $userDescriptor;
return $userDescriptor;
}
/**
* Returns role descriptor.
*
* @global object role descriptor
* @return object descriptor
*/
function &getRoleDescriptor() {
global $roleDescriptor;
return $roleDescriptor;
}
/**
* Returns page descriptor.
*
* @global object page descriptor
* @return object descriptor
*/
function &getPageDescriptor() {
global $pageDescriptor;
return $pageDescriptor;
}
/**
* Returns field descriptor.
*
* @global object field descriptor
* @return object descriptor
*/
function &getFieldDescriptor() {
global $fieldDescriptor;
return $fieldDescriptor;
}
/**
* Returns resource descriptor.
*
* @global object resource descriptor
* @return object descriptor
*/
function &getResourceDescriptor() {
global $resourceDescriptor;
return $resourceDescriptor;
}
/**
* Returns category descriptor.
*
* @global object category descriptor
* @return object descriptor
*/
function &getCategoryDescriptor() {
global $categoryDescriptor;
return $categoryDescriptor;
}
/**
* Returns commodity descriptor.
*
* @global object commodity descriptor
* @return object descriptor
*/
function &getCommodityDescriptor() {
global $commodityDescriptor;
return $commodityDescriptor;
}
?>