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
16 public $available_views = array();
19 private $configuration;
21 public $design_name = '';
22 public $active_view = null;
23 public $type_name = 'note';
30 public function __construct(&$connection, &$configuration)
32 $this->connection
=& $connection;
33 $this->configuration
=& $configuration;
35 $this->available_views
= array
40 $this->view_header
= "if (doc.value.metadata.deleted.val == __DELETED__ && doc.value.metadata.archived.val == __ARCHIVED__ && doc.value._type == '{$this->type_name}') {";
41 $this->view_footer
= "}";
43 $this->map_key
= 'doc.value.metadata.created.val';
45 $this->map_values
= array
47 'title' => 'doc.value.title',
48 'tags' => 'doc.value.tags',
49 'creator' => 'doc.value.metadata.creator',
50 'created' => 'doc.value.metadata.created'
54 public function __get($id)
56 return $this->get_object($id);
59 public function get_object($id)
61 $content_db = $this->configuration
['content_db'];
62 return $this->connection
->$content_db->document
->$id;
65 public function generate_view($name, $filter=array())
67 if (! in_array($name, $this->available_views
))
72 $this->active_view
= $name;
73 return $this->build_view($filter);
76 public function count($filter=null)
78 $this->active_view
= 'list';
79 $content_db = $this->configuration
['content_db'];
81 if (! is_null($filter))
83 return $this->connection
->$content_db->view
->temp( $this->build_view($filter), array( 'count' => 0 ))->total_rows
;
86 return $this->connection
->$content_db->view
->execute("{$this->design_name}/{$this->active_view}", array( 'count' => 0 ))->total_rows
;
89 public function all($filter=null, $args=null)
91 $this->active_view
= 'list';
92 $content_db = $this->configuration
['content_db'];
94 if (! is_null($filter))
96 return $this->connection
->$content_db->view
->temp( $this->build_view($filter), $args );
99 return $this->connection
->$content_db->view
->execute("{$this->design_name}/{$this->active_view}", $args);
102 public function get_by_tags(array $tags, array $skip_docs=array(), array $additional_map_values=array(), $include_archived=false)
106 $tags = $GLOBALS['ajatus']->helpers
->tag
->tags_to_id($tags);
108 $map_values = $this->map_values
;
109 if (! empty($additional_map_values))
111 foreach ($additional_map_values as $key => $value)
113 $map_values[$key] = $value;
117 $all_results = $GLOBALS['ajatus']->helpers
->tag
->related($tags, $skip_docs, $map_values, $include_archived);
119 if ($all_results->total_rows
> 0)
121 foreach ($all_results->rows
as $row)
123 if ($row->value
->_type
== $this->type_name
)
133 public function build_view($data=array())
135 if (! isset($data['map_key']))
137 $data['map_key'] = $this->get_active_map_key();
140 if (! isset($data['map_values']))
142 $data['map_values'] = $this->get_active_map_values();
145 return ajatus_types
::build_view($data, $this->view_header
, $this->view_footer
);
148 public function get_active_view()
150 if (is_null($this->active_view
))
152 $this->active_view
= $this->available_views
[0];
155 return $this->active_view
;
158 public function get_active_map_key()
160 if (is_null($this->active_view
))
162 $this->get_active_view();
165 if (count($this->available_views
) == 1)
167 return $this->map_key
;
170 return $this->map_key
[$this->active_view
];
173 public function get_active_map_values()
175 if (is_null($this->active_view
))
177 $this->get_active_view();
180 if (count($this->available_views
) == 1)
182 return $this->map_values
;
185 if (is_string($this->map_values
[$this->active_view
]))
187 $key = $this->map_values
[$this->active_view
];
188 return $this->map_values
[$key];
191 $values = $this->map_values
[$this->active_view
];
193 if (is_array($values[0]))
195 foreach ($values[0] as $i => $value)
197 if (is_string($value))
199 $values[0][$i] = $this->map_values
[$value];