PageRenderTime 51ms CodeModel.GetById 12ms app.highlight 32ms RepoModel.GetById 1ms app.codeStats 1ms

/cubi/openbiz/bin/easy/element/OptionElement.php

http://openbiz-cubi.googlecode.com/
PHP | 280 lines | 201 code | 22 blank | 57 comment | 28 complexity | 13ace5039de4a8b21f29ce4b331e7181 MD5 | raw file
  1<?PHP
  2/**
  3 * PHPOpenBiz Framework
  4 *
  5 * LICENSE
  6 *
  7 * This source file is subject to the BSD license that is bundled
  8 * with this package in the file LICENSE.txt.
  9 *
 10 * @package   openbiz.bin.easy.element
 11 * @copyright Copyright (c) 2005-2011, Rocky Swen
 12 * @license   http://www.opensource.org/licenses/bsd-license.php
 13 * @link      http://www.phpopenbiz.org/
 14 * @version   $Id: OptionElement.php 3561 2011-03-30 06:15:47Z jixian2003 $
 15 */
 16
 17//include_once("InputElement.php");
 18
 19/**
 20 * OptionElement is the base class of element that render list (from Selection.xml)
 21 * Used by :
 22 *   - {@link AutoSuggest}
 23 *   - {@link Checkbox}
 24 *   - {@link ColumnList}
 25 *   - {@link EditCombobox}
 26 *   - {@link LabelList}
 27 *   - {@link Listbox}
 28 *
 29 * @package openbiz.bin.easy.element
 30 * @author Rocky Swen
 31 * @copyright Copyright (c) 2005-2009
 32 * @access public
 33 */
 34class OptionElement extends InputElement
 35{
 36    public $m_SelectFrom;
 37    public $m_SelectFromSQL;
 38    public $m_SelectedList;
 39
 40    /**
 41     * Read metadata info from metadata array and store to class variable
 42     *
 43     * @param array $xmlArr metadata array
 44     * @return void
 45     */
 46    protected function readMetaData(&$xmlArr)
 47    {
 48        parent::readMetaData($xmlArr);
 49        $this->m_SelectFrom = isset($xmlArr["ATTRIBUTES"]["SELECTFROM"]) ? $xmlArr["ATTRIBUTES"]["SELECTFROM"] : null;
 50        $this->m_SelectedList = isset($xmlArr["ATTRIBUTES"]["SELECTEDLIST"]) ? $xmlArr["ATTRIBUTES"]["SELECTEDLIST"] : null;
 51        $this->m_SelectFromSQL = isset($xmlArr["ATTRIBUTES"]["SELECTFROMSQL"]) ? $xmlArr["ATTRIBUTES"]["SELECTFROMSQL"] : null;
 52    }
 53
 54    /**
 55     * Get select from
 56     *
 57     * @return string
 58     */
 59    protected function getSelectFrom()
 60    {
 61        $formobj = $this->getFormObj();
 62        return Expression::evaluateExpression($this->m_SelectFrom, $formobj);
 63    }
 64
 65    protected function getSelectedList()
 66    {
 67        $formobj = $this->getFormObj();
 68        return Expression::evaluateExpression($this->m_SelectedList, $formobj);
 69    }
 70    
 71	protected function getSelectFromSQL()
 72    {
 73        $formobj = $this->getFormObj();
 74        return Expression::evaluateExpression($this->m_SelectFromSQL, $formobj);
 75    }
 76
 77    /**
 78     * Render, draw the control according to the mode
 79     *
 80     * @return string HTML text
 81     */
 82    public function render()
 83    {
 84        return "";
 85    }
 86    
 87    /**
 88     * Get from list
 89     *
 90     * @param array $list
 91     * @return void
 92     */
 93    public function getFromList(&$list, $selectFrom=null)
 94    {
 95        if (!$selectFrom) {
 96            $selectFrom = $this->getSelectFrom();
 97        }
 98        if (!$selectFrom) {
 99        	return $this->getSQLFromList($list);
100        }
101        $this->getXMLFromList($list, $selectFrom);
102        if ($list != null)
103            return;
104        $this->getDOFromList($list, $selectFrom);
105        if ($list != null)
106            return;
107        $this->getSimpleFromList($list, $selectFrom);
108        if ($list != null)
109            return;
110        
111        return;
112    }
113    
114    protected function getXMLFromList(&$list, $selectFrom)
115    {
116        $pos0 = strpos($selectFrom, "(");
117        $pos1 = strpos($selectFrom, ")");
118        if ($pos0>0 && $pos1 > $pos0)
119        {  // select from xml file
120            $xmlFile = substr($selectFrom, 0, $pos0);
121            $tag = substr($selectFrom, $pos0 + 1, $pos1 - $pos0-1);
122            $tag = strtoupper($tag);
123            $xmlFile = BizSystem::GetXmlFileWithPath ($xmlFile);
124            if (!$xmlFile) return false;
125
126            $xmlArr = &BizSystem::getXmlArray($xmlFile);
127            if ($xmlArr)
128            {
129                $i = 0;
130                if (!key_exists($tag, $xmlArr["SELECTION"]))
131                    return false;
132                if(!$xmlArr["SELECTION"][$tag][0]){
133                	$array = $xmlArr["SELECTION"][$tag];
134                	unset($xmlArr["SELECTION"][$tag]);
135                	$xmlArr["SELECTION"][$tag][0]=$array;
136                }
137                foreach($xmlArr["SELECTION"][$tag] as $node)
138                {
139                    $list[$i]['val'] = $node["ATTRIBUTES"]["VALUE"];
140                    $list[$i]['pic'] = $node["ATTRIBUTES"]["PICTURE"];
141                    if ($node["ATTRIBUTES"]["TEXT"])
142                    {
143                        $list[$i]['txt'] = $node["ATTRIBUTES"]["TEXT"];                        
144                    }
145                    else
146                    {
147                        $list[$i]['txt'] = $list[$i]['val'];
148                    }
149                    $i++;
150                    
151                }
152                $this->translateList($list, $tag);	// supprot multi-language
153            }
154            return true;
155        }
156        return false;
157    }
158    
159    protected function getDOFromList(&$list, $selectFrom)
160    {
161        $pos0 = strpos($selectFrom, "[");
162        $pos1 = strpos($selectFrom, "]");
163        if ($pos0 > 0 && $pos1 > $pos0)
164        {  // select from bizObj
165            // support BizObjName[BizFieldName] or 
166            // BizObjName[BizFieldName4Text:BizFieldName4Value] or 
167            // BizObjName[BizFieldName4Text:BizFieldName4Value:BizFieldName4Pic]
168            $bizObjName = substr($selectFrom, 0, $pos0);
169            $pos3 = strpos($selectFrom, ":");
170            if($pos3 > $pos0 && $pos3 < $pos1)
171            {
172                $fieldName = substr($selectFrom, $pos0 + 1, $pos3 - $pos0 - 1);
173                $fieldName_v = substr($selectFrom, $pos3 + 1, $pos1 - $pos3 - 1);
174            }
175            else
176            {
177                $fieldName = substr($selectFrom, $pos0 + 1, $pos1 - $pos0 - 1);
178                $fieldName_v = $fieldName;
179            }
180            $pos4 = strpos($fieldName_v, ":");
181            if($pos4){
182            	$fieldName_v_mixed = $fieldName_v;
183            	$fieldName_v = substr($fieldName_v_mixed,0,$pos4);
184            	$fieldName_p = substr($fieldName_v_mixed, $pos4+1, strlen($fieldName_v_mixed)-$pos4-1);
185            	unset($fieldName_v_mixed);
186            }
187            $commaPos = strpos($selectFrom, ",", $pos1);
188            if ($commaPos > $pos1)
189                $searchRule = trim(substr($selectFrom, $commaPos + 1));
190            
191            /* @var $bizObj BizDataObj */
192            $bizObj = BizSystem::getObject($bizObjName);
193            if (!$bizObj)
194                return false;
195
196            $recList = array();
197            $oldAssoc = $bizObj->m_Association;
198            $bizObj->m_Association = null;
199            QueryStringParam::reset();
200            $recList = $bizObj->directFetch($searchRule);
201            $bizObj->m_Association = $oldAssoc;
202
203            foreach ($recList as $rec)
204            {
205                $list[$i]['val'] = $rec[$fieldName_v];
206                $list[$i]['txt'] = $rec[$fieldName];
207                $list[$i]['pic'] = $rec[$fieldName_p];
208                $i++;
209            }
210           
211            return true;
212        }
213        return false;
214    }
215    
216    protected function getSimpleFromList(&$list, $selectFrom)
217    {
218        // in case of a|b|c
219        if (strpos($selectFrom, "[") > 0 || strpos($selectFrom, "(") > 0)
220            return;
221        $recList = explode('|',$selectFrom);
222        foreach ($recList as $rec)
223        {
224            $list[$i]['val'] = $rec;
225            $list[$i]['txt'] = $rec;
226            $list[$i]['pic'] = $rec;
227            $i++;
228        }
229    }
230    
231    public function getSQLFromList(&$list)
232    {
233    	$sql = $this->getSelectFromSQL();
234    	if (!$sql) return;
235    	$formObj = $this->getFormObj();
236    	$do = $formObj->getDataObj();
237    	$db = $do->getDBConnection();
238    	try {
239    		$resultSet = $db->query($sql);
240    		$recList = $resultSet->fetchAll();
241	    	foreach ($recList as $rec)
242	        {
243	            $list[$i]['val'] = $rec[0];
244	            $list[$i]['txt'] = isset($rec[1]) ? $rec[1] : $rec[0];
245	            $i++;
246	        }
247    	}
248    	catch (Exception $e)
249        {
250            BizSystem::log(LOG_ERR, "DATAOBJ", "Query Error: ".$e->getMessage());
251            $this->m_ErrorMessage = "Error in SQL query: ".$sql.". ".$e->getMessage();
252            throw new BDOException($this->m_ErrorMessage);
253            return null;
254        }
255    }
256    
257    protected function translateList(&$list, $tag)
258    {
259    	$module = $this->getModuleName($this->m_SelectFrom);
260        if (empty($module))
261            $module = $this->getModuleName($this->m_FormName);
262    	for ($i=0; $i<count($list); $i++)
263    	{
264    		$key = 'SELECTION_'.strtoupper($tag).'_'.$i.'_TEXT';
265    		$list[$i]['txt'] = I18n::t($list[$i]['txt'], $key, $module, $this->getTransLOVPrefix());
266    	}
267    }
268    
269    protected function getTransLOVPrefix()
270    {    	
271    	$nameArr = explode(".",$this->m_SelectFrom);
272    	for($i=1;$i<count($nameArr)-1;$i++)
273    	{
274    		$prefix .= strtoupper($nameArr[$i])."_";
275    	}    	
276    	return $prefix;
277    }  
278}
279
280?>