Added possibility to add additional map_values to queries.
[ajatus.git] / plugins / ajatus / helpers / metadata.php
blobddaa5bafd54ed6701f5ab0022a4f14b1c86a14d5
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 class ajatus_helpers_metadata
16 private $keys = array();
17 private $defaults = array();
18 private $metadata;
20 public function __construct(&$metadata)
22 $this->keys = array
24 'created', 'creator', 'revised', 'revisor', 'archived', 'deleted'
27 $this->defaults = array
29 'creator' => array
31 'widget' => array
33 'name' => 'text',
34 'config' => null
36 'value' => ''
38 'created' => array
40 'widget' => array
42 'name' => 'date',
43 'config' => array( 'use_time' => true )
45 'value' => ''
47 'revisor' => array
49 'widget' => array
51 'name' => 'text',
52 'config' => null
54 'value' => ''
56 'revised' => array
58 'widget' => array
60 'name' => 'date',
61 'config' => array( 'use_time' => true )
63 'value' => ''
65 'archived' => array
67 'widget' => array
69 'name' => 'boolean',
70 'config' => null
72 'value' => false
74 'deleted' => array
76 'widget' => array
78 'name' => 'boolean',
79 'config' => null
81 'value' => false
85 $this->metadata =& $metadata;
87 $this->validate();
90 public function __get($key)
92 return $this->read($key);
95 public function __set($key, $value)
97 $this->update($key, $value);
100 public function read($key)
102 if (! in_array($key, $this->keys))
104 throw new ajatus_metadata_exception("No key '{$key}' found!");
107 return $this->metadata->$key->val;
110 public function update($key, $value)
112 if (! in_array($key, $this->keys))
114 throw new ajatus_metadata_exception("No key '{$key}' found!");
117 $this->metadata->$key->val = $value;
120 private function validate()
122 foreach ($this->keys as $key)
124 $defaults = $this->defaults[$key];
125 if (! isset($this->metadata->$key))
127 $this->metadata->$key = new StdClass;
128 $this->metadata->$key->val = $defaults->value;
129 $this->metadata->$key->widget = new StdClass;
130 $this->metadata->$key->widget->name = $defaults['widget']['name'];
131 $this->metadata->$key->widget->config = new StdClass;
132 if (is_array($defaults['widget']['config']))
134 foreach ($defaults['widget']['config'] as $ck => $cv)
136 $this->metadata->$key->widget->config->$ck = $cv;