Xataface  2.0alpha2
Xataface Application Framework
 All Data Structures Namespaces Files Functions Variables Groups Pages
Serializer.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  */
21 import( 'Dataface/Table.php');
22 import( 'Dataface/Error.php');
24 
25 
26  var $_table;
27 
29  $this->_table =& Dataface_Table::loadTable($tablename);
30  }
31 
32  static function number2db($value)
33  {
34  $larr = localeconv();
35  $search = array(
36  $larr['decimal_point'],
37  $larr['mon_decimal_point'],
38  $larr['thousands_sep'],
39  $larr['mon_thousands_sep'],
40  $larr['currency_symbol'],
41  $larr['int_curr_symbol']
42  );
43  $replace = array('.', '.', '', '', '', '');
44 
45  return str_replace($search, $replace, $value);
46  }
47 
48 
57  function serialize($fieldname, $value, $handleRepeat=true){
58 
59  // check to see if the input value is a placeholder. If it is, we should pass it
60  // through untouched.
61  if ( is_string($value) and preg_match('/^__(.*)__$/', $value)){
62  // This fixes an issue with addRelatedRecord();
63  return $value;
64  }
65 
66  if ( $value === null ){
67  return null;
68  }
69 
70  if ( strpos($fieldname, '.') !== false ){
71  // This is a related field.
72  $table =& $this->_table->getTableTableForField($fieldname);
73  list( $relname, $fieldname) = explode('.', $fieldname);
74  $serializer = new Dataface_Serializer($table->tablename);
75  $out = $serializer->serialize($fieldname, $value, $handleRepeat);
76 
77  return $out;
78 
79  }
80 
82  $field =& $table->getField($fieldname);
83  if ( PEAR::isError($field) ){
84  throw new Exception($field->getMessage());
85  }
86 
87  $delegate =& $table->getDelegate();
88  if ( $delegate !== null and method_exists($delegate, $fieldname."__serialize") ){
89  $val = call_user_func(array(&$delegate, $fieldname."__serialize"), $value);
90 
91  return $val;
92  }
93  $widget = $field['widget'];
94  $type = $widget['type'];
95 
96 
97  if ( $handleRepeat and $field['repeat'] and is_array($value) ){
98  foreach ($value as $key=>$val){
99  $value[$key] = $this->serialize($fieldname, $val, false);
100  }
101 
102  $value = implode($field['separator'], $value);
103 
104  }
105 
106  $evt = new stdClass;
107  $evt->table = $this->_table;
108  $evt->field =& $field;
109  $evt->value = $value;
110  $evt->done = false;
111  $this->_table->app->fireEvent('serialize_field_value', $evt);
112  if ( $evt->done ){
113  return $evt->value;
114  }
115 
116  if ($table->isDate( $fieldname ) ){
117 
118  if ( !isset($value) || !$value ) return null;
119  $params = $value; //$field['value'];
120  if ( is_string($params) and strtotime($params) ){
121  $timestamp = strtotime($params);
122  switch ($table->getType($fieldname)){
123  case 'date':
124  return date('Y-m-d', $timestamp);
125  case 'datetime':
126  case 'timestamp':
127  return date('Y-m-d h:i:s', $timestamp);
128  case 'time':
129  return date('h:i:s', $timestamp);
130  case 'year':
131  return date('Y', $timestamp);
132  }
133 
134  }
135  if ( !is_array($params) ) return null;
136 
137 
138  $datestr = str_pad($params['year'],4,"0",STR_PAD_LEFT).'-'.str_pad($params['month'],2,"0",STR_PAD_LEFT).'-'.str_pad($params['day'],2,"0",STR_PAD_LEFT);
139  $timestr = str_pad($params['hours'],2,"0",STR_PAD_LEFT).':'.str_pad($params['minutes'],2,"0",STR_PAD_LEFT).':'.str_pad($params['seconds'], 2,"0",STR_PAD_LEFT);
140 
141  switch ( $table->getType($fieldname) ){
142  case 'date':
143  return $datestr;
144  //return "FROM_UNIXTIME('$datestr')";
145  case 'datetime':
146  return $datestr.' '.$timestr;
147  //return "FROM_UNIXTIME('$datestr $timestr')";
148  case 'timestamp':
149  return str_pad($params['year'],4,"0",STR_PAD_LEFT).str_pad($params['month'],2,"0",STR_PAD_LEFT).str_pad($params['day'],2,"0",STR_PAD_LEFT).str_pad($params['hours'],2,"0",STR_PAD_LEFT).str_pad($params['minutes'],2,"0",STR_PAD_LEFT).str_pad($params['seconds'],2,"0",STR_PAD_LEFT);
150  case 'time':
151  return $timestr;
152  case 'year':
153  return str_pad($params['year'],4,"0",STR_PAD_LEFT);
154  }
155 
156  }
157 
158  //if ( $table->isInt( $fieldname ) ){
159  // if ( !$value ) return 0;
160  // return $value;
161  //}
162 
163  //if ( $table->isFloat( $fieldname) ){
164  // return self::number2db(doubleval($value));
165  //}
166 
167 
168 
169 
170  if ( is_array( $value ) ){
171  if ( $widget['type'] == 'table' or $widget['type'] == 'group'){
172  import( 'XML/Serializer.php');
173  $serializer = new XML_Serializer(array('typeHints'=>true));
174  $ser_res =& $serializer->serialize($value);
175  if (!PEAR::isError($ser_res) ){
176  return $serializer->getSerializedData();
177  }
178  }
179 
180  throw new Exception("Trying to serialize value for field '$fieldname' that we don't know what to do with. The value is an array and we don't know how to parse it.", E_USER_ERROR);
181 
182  } else {
183 
184 
185  return $value;
186  }
187 
188 
189 
190  }
191 
192 
193 
198  function unserialize($fieldname, $value){
199  throw new Exception("Not implemented yet.", E_USER_ERROR);
200 
201  }
202 
203 
208  function encrypt($fieldname, $value=null){
209  if ( !isset($value) ) $value = '';
210  if ( strpos($fieldname, '.') !== false ){
211  // This is a related field.
212  $table =& $this->_table->getTableTableForField($fieldname);
213  list( $relname, $fieldname) = explode('.', $fieldname);
214  $serializer = new Dataface_Serializer($table->tablename);
215  $out = $serializer->encrypt($fieldname, $value);
216 
217  return $out;
218 
219  }
220  $field = $this->_table->getField($fieldname);
221  if ( PEAR::isError($field) ){
222  error_log($field->getMessage()."\n".implode("\n", $field->getBacktrace()));
223  throw new Exception("Failed to encrypt field $fieldname. See error log for details.", E_USER_ERROR);
224 
225  }
226  if ( isset($field['encryption']) ){
227  switch(strtolower($field['encryption'])){
228  case 'md5':
229  return 'MD5('.$value.')';
230  case 'password':
231  return 'PASSWORD('.$value.')';
232  case 'sha1':
233  return 'SHA1('.$value.')';
234  case 'encrypt':
235  return 'ENCRYPT('.$value.')';
236  case 'aes_encrypt':
237  return 'AES_ENCRYPT('.$value.',\''.addslashes($field['aes_key']).'\')';
238  }
239  }
240  return $value;
241  }
242 
243 
244 
245 
246 
247 
248 
249 }