<?php
/**
* Copyright (c) 2008, SARL Adaltas. All rights reserved.
* Code licensed under the BSD License:
* http://porte.adaltas.com/en/developer/license.html
*/
/**
* PorteDojoStore
*
* Plugin to add dojo data store format as exportable option.
*
* @package Porte
* @subpackage plugin
* @author David Worms info(at)adaltas.com
* @copyright 2008 Adaltas
*/
class PorteDojoStore{
/**
* Plug method "toDojoStore" on "iterator_call_before" called by PorteIterator
*
* New method accept the following options:
* output (string) Accepted values include json
* label (string) Name of the property marked as a label
*
* @return
* @param $list Object
* @param $method Object
* @param $args Object
*/
public static function _iteratorCallBefore($iterator,$method,$args){
if($method=='toDojoStore'){
$model = $iterator->table->porte->models->{$iterator->table->type};
$isHierarchical = !empty($model['hierarchical']);
$return = array();
$return['identifier'] = $model['primary_key'];
if(count($args)>0 && isset($args[0]['label'])){
$return['label'] = $args[0]['label'];
}else if(isset($model['label'])){
$return['label'] = $model['label'];
}else{
$return['label'] = $return['identifier'];
}
$options = isset($args[0])?$args[0]:array();
$return['items'] = self::prepareItems($model,$iterator->toArray($options),$options);
if(count($args)>0 && isset($args[0]['output']) && $args[0]['output']=='json'){
$return = json_encode($return);
}
return $return;
}
}
/**
* Options may include:
* - types Array in the form of porte_type => dojo_type
*
* The type option implement the way item may be serialize and deserialize in
* the dojo.data.ItemFileReadStore and dojo.data.ItemFileWriteStore. For exemple,
* providing a type option as "date_property"=>"Date", "year_property":"Year" will
* generate of each items the property date_property as equals to "_type"=>'Date',
* "_value"=>"1978-10-09 00:00:00"
*
* @return
* @param $model Object
* @param $items Object
* @param $options Object[optional]
*/
public static function prepareItems($model,$items,$options=array()){
$types = isset($options['types'])?$options['types']:array();
foreach($items as $index=>&$item){
foreach($item as $key=>&$value){
if($key=='children'){
$value = self::prepareItems($model,$value,$options);
}else if(isset($model['properties'][$key])){
$property = $model['properties'][$key];
if(isset($types[$property['type']]))
$value = array('_type'=>$types[$property['type']],'_value'=>$value);
else if(is_array($value)){ //is_object($value)||
$value = json_encode($value);
}
}else if(is_array($value)){ //is_object($value)||
$value = json_encode($value);
}
}
}
return $items;
}
}
PorteEvents::connect('iterator_call_before',array('PorteDojoStore','_iteratorCallBefore'));
?>