4 * Ajatus - Distributed CRM
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
16 // public function view($name='all');
22 private $configuration;
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';
63 $value_count = count($values);
66 foreach ($values as $vk => $vv)
68 $map_values .= json_encode($vk) . ": {$vv}";
69 if ($i < $value_count)
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)
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;
134 $data[$key] = json_encode($value);
141 static function generate_sub_header_and_footer($data)
145 $data_count = count($data);
146 foreach ($data as $key => $keydata)
151 $separator = $key == 'and' ?
' && ' : ' || ';
153 if ( is_array($keydata)
157 $cnt = count($keydata);
159 foreach ($keydata as $kd_arr)
161 if (strtoupper($kd_arr[1]) == 'LIKE')
163 $block .= "{$kd_arr[0]}.match(/{$kd_arr[2]}(.*?)/i)";
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]}";
175 $block .= $separator;
202 static function build_view($data=array(), $header='', $footer='')
204 $data = ajatus_types
::prepare_view_data($data);
206 $view = 'function(doc){';
208 $view .= '__SUB_HEADER__';
209 $view .= 'map( __MAP_KEY__, {';
210 $view .= '__MAP_VALUES__';
212 $view .= '__SUB_FOOTER__';
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";
227 static function build_double_view($data=array(), $header='', $footer='', $loop=array())
229 $data = ajatus_types
::prepare_view_data($data);
235 if (isset($loop['key']))
237 $loop_key = $loop['key'];
239 if (isset($loop['start']))
241 $loop_start = $loop['start'];
244 if (isset($loop['end']))
246 $loop_end = $loop['end'];
249 $view = 'function(doc){';
251 $view .= 'map( [__MAP_KEY__, 0], {';
252 $view .= '__MAP_VALUES__';
255 $view .= '__SUB_HEADER__';
256 $view .= $loop_start;
257 $view .= 'map( [__MAP_KEY1__, 1], {';
258 $view .= '__MAP_VALUES1__';
261 $view .= '__SUB_FOOTER__';
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);