Added possibility to do LIKE filters
[ajatus.git] / plugins / ajatus / types.php
blobb1256054a627c64d846c1fe3ceb0a61bc27779b8
1 <?php
2 /**
3 * This file is part of
4 * Ajatus - Distributed CRM
5 *
6 * Copyright (c) 2008 Jerry Jalava <jerry.jalava@gmail.com>
7 * Copyright (c) 2008 Nemein Oy <http://nemein.com>
8 * Website: http://ajatus.info
9 * Licensed under the GPL license
10 * http://www.gnu.org/licenses/gpl.html
14 // interface ajatus_types_interface
15 // {
16 // public function view($name='all');
17 // }
19 class ajatus_types
21 private $connection;
22 private $configuration;
23 private $cache;
25 public function __construct(&$connection, $configuration)
27 $this->connection =& $connection;
28 $this->configuration =& $configuration;
31 public function __get($type)
33 return $this->load_type($type);
36 private function &load_type($name)
38 if (isset($this->cache[$name]))
40 return $this->cache[$name];
43 $classname = "ajatus_types_{$name}";
45 if (! class_exists($classname))
47 throw new ajatus_type_exception("No type {$name} found!");
50 $this->cache[$name] = new $classname(&$this->connection, &$this->configuration);
52 return $this->cache[$name];
55 static function prepare_map_values(array $values)
57 if (! isset($values['_type']))
59 $values['_type'] = 'doc.value._type';
62 $map_values = '';
63 $value_count = count($values);
65 $i = 1;
66 foreach ($values as $vk => $vv)
68 $map_values .= json_encode($vk) . ": {$vv}";
69 if ($i < $value_count)
71 $map_values .= ', ';
74 $i++;
77 return $map_values;
80 static function prepare_view_data($data=array())
82 if (! isset($data['deleted']))
84 $data['deleted'] = false;
87 if (! isset($data['archived']))
89 $data['archived'] = false;
92 if (! isset($data['map_key']))
94 $data['map_key'] = 'null';
97 if (! isset($data['map_values']))
99 $data['map_values'] = 'doc';
102 if ( isset($data['filter'])
103 && is_array($data['filter']))
105 list($data['sub_header'], $data['sub_footer']) = ajatus_types::generate_sub_header_and_footer($data['filter']);
106 unset($data['filter']);
109 if (! isset($data['sub_header']))
111 $data['sub_header'] = '';
114 if (! isset($data['sub_footer']))
116 $data['sub_footer'] = '';
119 foreach ($data as $key => $value)
121 if ( preg_match('/^map_values/', $key)
122 && is_array($value))
124 $data[$key] = ajatus_types::prepare_map_values($value);
126 else if( preg_match('/^map_key/', $key)
127 || $key == 'sub_header'
128 || $key == 'sub_footer')
130 $data[$key] = $value;
132 else
134 $data[$key] = json_encode($value);
138 return $data;
141 static function generate_sub_header_and_footer($data)
143 $header = 'if (';
145 $data_count = count($data);
146 foreach ($data as $key => $keydata)
148 if ( $key != 'and'
149 || $key != 'or')
151 $separator = $key == 'and' ? ' && ' : ' || ';
153 if ( is_array($keydata)
154 && !empty($keydata))
156 $block = '( ';
157 $cnt = count($keydata);
158 $i = 1;
159 foreach ($keydata as $kd_arr)
161 if (strtoupper($kd_arr[1]) == 'LIKE')
163 $block .= "{$kd_arr[0]}.match(/{$kd_arr[2]}(.*?)/i)";
165 else
167 $kd_arr[1] = $kd_arr[1] == '=' ? '==' : $kd_arr[1];
168 $kd_arr[2] = json_encode($kd_arr[2]);
170 $block .= "{$kd_arr[0]} {$kd_arr[1]} {$kd_arr[2]}";
173 if ($i < $cnt)
175 $block .= $separator;
178 $i++;
181 $block .= ' )';
182 $header .= $block;
184 if ($data_count > 1)
186 $header .= ' && ';
192 $header .= ') {';
193 $footer = '}';
195 return array
197 $header,
198 $footer
202 static function build_view($data=array(), $header='', $footer='')
204 $data = ajatus_types::prepare_view_data($data);
206 $view = 'function(doc){';
207 $view .= $header;
208 $view .= '__SUB_HEADER__';
209 $view .= 'map( __MAP_KEY__, {';
210 $view .= '__MAP_VALUES__';
211 $view .= '});';
212 $view .= '__SUB_FOOTER__';
213 $view .= $footer;
214 $view .= '}';
216 foreach ($data as $key => $value)
218 $replace_key = '__' . strtoupper($key) . '__';
219 $view = str_replace($replace_key, $value, $view);
222 // echo "Builded view: \n{$view}\n";
224 return $view;
227 static function build_double_view($data=array(), $header='', $footer='', $loop=array())
229 $data = ajatus_types::prepare_view_data($data);
231 $loop_start = '';
232 $loop_end = '';
233 $loop_key = 'i';
235 if (isset($loop['key']))
237 $loop_key = $loop['key'];
239 if (isset($loop['start']))
241 $loop_start = $loop['start'];
242 $loop_end = '}';
244 if (isset($loop['end']))
246 $loop_end = $loop['end'];
249 $view = 'function(doc){';
250 $view .= $header;
251 $view .= 'map( [__MAP_KEY__, 0], {';
252 $view .= '__MAP_VALUES__';
253 $view .= '});';
254 $view .= $footer;
255 $view .= '__SUB_HEADER__';
256 $view .= $loop_start;
257 $view .= 'map( [__MAP_KEY1__, 1], {';
258 $view .= '__MAP_VALUES1__';
259 $view .= '});';
260 $view .= $loop_end;
261 $view .= '__SUB_FOOTER__';
262 $view .= '}';
264 foreach ($data as $key => $value)
266 $replace_key = '__' . strtoupper($key) . '__';
267 $value = sprintf($value, $loop_key);
268 $view = str_replace($replace_key, $value, $view);
271 return $view;