Xataface  2.0alpha2
Xataface Application Framework
 All Data Structures Namespaces Files Functions Variables Groups Pages
ValuelistTool.php
Go to the documentation of this file.
1 <?php
2 /*-------------------------------------------------------------------------------
3  * Xataface Web Application Framework
4  * Copyright (C) 2005-2008 Web Lite Solutions Corp (shannah@sfu.ca)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *-------------------------------------------------------------------------------
20  */
22 
23 
24  var $_valuelists = array();
25  private $_valuelistsConfig = null;
26 
28 
29  $this->_loadValuelistsIniFile();
30 
31 
32  }
33 
35  return DATAFACE_SITE_PATH.'/valuelists.ini';
36  }
37 
39  return file_exists($this->_valuelistsIniFilePath());
40  }
41 
42 
52  private function &_loadValuelistsIniFile(){
53  if ( !isset($this->_valuelistsConfig) ){
54  import( 'Dataface/ConfigTool.php');
55  $configTool =& Dataface_ConfigTool::getInstance();
56  $this->_valuelistsConfig =& $configTool->loadConfig('valuelists');
57  }
58  return $this->_valuelistsConfig;
59  }
60 
74  private function _loadValuelist($name){
75  if ( !isset($this->_valuelists) ){
76  $this->_valuelists = array();
77  }
78 
79  if ( !isset($this->_valuelists[$name]) ){
80  $conf =& $this->_loadValuelistsIniFile();
81  if ( isset($conf[$name]) ){
82  $vllist = $conf[$name];
83  $vlname = $name;
84  $valuelists =& $this->_valuelists;
85  $valuelists[$vlname] = array();
86  foreach ( $vllist as $key=>$value ){
87  if ( $key == '__sql__' ) {
88  // we perform the sql query specified to produce our valuelist.
89  // the sql query should return two columns only. If more are
90  // returned, only the first two will be used. If one is returned
91  // it will be used as both the key and value.
92  $res = df_query($value, null, true, true);
93  if ( is_array($res) ){
94  //while ($row = mysql_fetch_row($res) ){
95  foreach ($res as $row){
96  $valuekey = $row[0];
97  $valuevalue = count($row)>1 ? $row[1] : $row[0];
98  $valuelists[$vlname][$valuekey] = $valuevalue;
99 
100  if ( count($row)>2 ){
101  $valuelists[$vlname.'__meta'][$valuekey] = $row[2];
102  }
103  }
104  //mysql_free_result($res);
105  } else {
106  throw new Exception("Valuelist query '".$value."' failed. ", E_USER_NOTICE);
107  }
108 
109  } else {
110  $valuelists[$vlname][$key] = $value;
111  }
112  }
113  }
114  }
115 
116  }
117 
118 
120  if ( !isset( $this->_valuelists ) ){
121  $this->_valuelists = array();
122  }
123  $valuelists =& $this->_valuelists;
124 
125  if ( $this->_hasValuelistsIniFile() ){
126 
127 
128  $conf = parse_ini_file( $this->_valuelistsIniFilePath(), true);
129 
130  foreach ( $conf as $vlname=>$vllist ){
131  $valuelists[$vlname] = array();
132  if ( is_array( $vllist ) ){
133  foreach ( $vllist as $key=>$value ){
134 
135  if ( $key == '__sql__' ) {
136  // we perform the sql query specified to produce our valuelist.
137  // the sql query should return two columns only. If more are
138  // returned, only the first two will be used. If one is returned
139  // it will be used as both the key and value.
140  $res = df_query($value, null, true, true);
141  if ( is_array($res) ){
142  //while ($row = mysql_fetch_row($res) ){
143  foreach ($res as $row){
144  $valuekey = $row[0];
145  $valuevalue = count($row)>1 ? $row[1] : $row[0];
146  $valuelists[$vlname][$valuekey] = $valuevalue;
147 
148  if ( count($row)>2 ){
149  $valuelists[$vlname.'__meta'][$valuekey] = $row[2];
150  }
151  }
152  } else {
153  throw new Exception('Valuelist sql query failed: '.$value.': '.mysql_error(), E_USER_NOTICE);
154  }
155 
156  } else {
157  $valuelists[$vlname][$key] = $value;
158  }
159  }
160  }
161 
162 
163  }
164 
165  }
166  }
167 
168 
169  public static function &getInstance(){
170  static $instance = 0;
171  if ( $instance === 0 ){
172  $instance = new Dataface_ValuelistTool();
173  }
174  return $instance;
175  }
176 
177  function &getValuelist($name){
178  if ( !is_a($this, 'Dataface_ValuelistTool') ){
180  } else {
181  $vlt =& $this;
182  }
183  $vlt->_loadValuelist($name);
184  if ( isset($vlt->_valuelists[$name] ) ){
185 
186  return $vlt->_valuelists[$name];
187  }
188 
189  throw new Exception("Request for valuelist '$name' that does not exist in Dataface_ValuelistTool::getValuelist().", E_USER_ERROR);
190  }
191 
192 
193  function hasValuelist($name){
194  if ( !is_a($this, 'Dataface_ValuelistTool') ){
196  } else {
197  $vlt =& $this;
198  }
199  return isset( $vlt->_valuelistsConfig[$name]);
200  }
201 
207  function &valuelists(){
208  if ( !is_a($this, 'Dataface_ValuelistTool') ){
210  } else {
211  $vlt =& $this;
212  }
213  $out =& $vlt->_valuelists;
214 
215  }
216 
229  function addValueToValuelist(&$table, $valuelistName, $value, $key=null, $checkPerms=false){
230 
231  import( 'Dataface/ConfigTool.php');
232  $configTool =& Dataface_ConfigTool::getInstance();
233  $conf = $configTool->loadConfig('valuelists', $table->tablename);
234 
235  $relname = $valuelistName.'__valuelist';
236  //$conf = array($relname=>$conf);
237  $table->addRelationship( $relname, $conf[$valuelistName]);
238  $rel =& $table->getRelationship($relname);
239  $fields =& $rel->fields();
240  if ( count($fields) > 1 ) {
241  $valfield = $fields[1];
242  $keyfield = $fields[0];
243  }
244  else {
245  $valfield = $fields[0];
246  $keyfield = $fields[0];
247  }
248 
249  $record = new Dataface_Record($table->tablename);
250  $rrecord = new Dataface_RelatedRecord($record, $relname);
251  if ( $checkPerms and !$rrecord->checkPermission('edit', array('field'=>$valfield)) ){
253  }
254  $rrecord->setValue($valfield, $value);
255  if (isset($key) and isset($keyfield) ){
256  if ( $checkPerms and !$rrecord->checkPermission('edit', array('field'=>$keyfield)) ){
258  }
259  $rrecord->setValue($keyfield, $key);
260  }
261  import('Dataface/IO.php');
262  $io = new Dataface_IO($table->tablename);
263  $res = $io->addRelatedRecord($rrecord);
264  if ( PEAR::isError($res) ) return $res;
265  return array('key'=>$rrecord->val($keyfield), 'value'=>$rrecord->val($valfield));
266 
267  }
268 
278  function &asRelationship(&$table, $valuelistName){
279  import( 'Dataface/ConfigTool.php');
280  $configTool =& Dataface_ConfigTool::getInstance();
281  $conf = $configTool->loadConfig('valuelists', $table->tablename);
282  if ( !@$conf[$valuelistName]['__sql__'] ){
283  $out = null;
284  return $out;
285  }
286 
287  $relname = $valuelistName.'__valuelist';
288  //$conf = array($relname=>$conf);
289  $table->addRelationship( $relname, $conf[$valuelistName]);
290  $rel =& $table->getRelationship($relname);
291  $rel->_schema['action']['visible']=0;
292  return $rel;
293 
294  }
295 
296 
297 
298 
299 }