Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php
/*********************************************************************
class.organization.php
Peter Rotich <peter@osticket.com>
Jared Hancock <jared@osticket.com>
Copyright (c) 2014 osTicket
http://www.osticket.com
Released under the GNU General Public License WITHOUT ANY WARRANTY.
See LICENSE.TXT for details.
vim: expandtab sw=4 ts=4 sts=4:
**********************************************************************/
require_once(INCLUDE_DIR . 'class.orm.php');
class OrganizationModel extends VerySimpleModel {
static $meta = array(
'table' => ORGANIZATION_TABLE,
'pk' => array('id'),
'joins' => array(
'users' => array(
'reverse' => 'UserAccountModel.org',
),
)
);
var $users;
static function objects() {
$qs = parent::objects();
return $qs;
}
function getId() {
return $this->id;
}
}
class Organization extends OrganizationModel {
function __construct($ht) {
parent::__construct($ht);
}
//XXX: Shouldn't getName use magic get method to figure this out?
function getName() {
return $this->name;
}
function getUpdateDate() {
return $this->updated;
}
function getCreateDate() {
return $this->created;
}
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
function addDynamicData($data) {
$of = OrganizationForm::getInstance($this->id);
foreach ($of->getFields() as $f)
if (isset($data[$f->get('name')]))
$of->setAnswer($f->get('name'), $data[$f->get('name')]);
$of->save();
return $of;
}
function getDynamicData() {
if (!isset($this->_entries)) {
$this->_entries = DynamicFormEntry::forOrganization($this->id)->all();
if (!$this->_entries) {
$g = OrganizationForm::getInstance($this->id);
$g->save();
$this->_entries[] = $g;
}
}
return $this->_entries;
}
function getForms($data=null) {
if (!isset($this->_forms)) {
$this->_forms = array();
foreach ($this->getDynamicData() as $cd) {
$cd->addMissingFields();
if(!$data
&& ($form = $cd->getForm())
&& $form->get('type') == 'O' ) {
foreach ($cd->getFields() as $f) {
if ($f->get('name') == 'name')
$f->value = $this->getName();
}
}
$this->_forms[] = $cd->getForm();
}
}
return $this->_forms;
}
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
function to_json() {
$info = array(
'id' => $this->getId(),
'name' => (string) $this->getName()
);
return JsonDataEncoder::encode($info);
}
function __toString() {
return (string) $this->getName();
}
function delete() {
return parent::delete();
}
static function fromVars($vars) {
if (!($org = Organization::lookup(array('name' => $vars['name'])))) {
$org = Organization::create(array(
'name' => $vars['name'],
'created' => new SqlFunction('NOW'),
'updated' => new SqlFunction('NOW'),
));
$org->save(true);
}
return $org;
}
static function fromForm($form) {
if(!$form) return null;
//Validate the form
$valid = true;
if (!$form->isValid())
$valid = false;
//Make sure the email is not in-use
if (($field=$form->getField('name'))
&& $field->getClean()
&& Organization::lookup(array('name' => $field->getClean()))) {
$field->addError('Organization with the same name already exists');
$valid = false;
}
return $valid ? self::fromVars($form->getClean()) : null;
}
}
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
class OragnizationForm extends DynamicForm {
static $instance;
static $form;
static function objects() {
$os = parent::objects();
return $os->filter(array('type'=>'O'));
}
static function getDefaultForm() {
if (!isset(static::$form)) {
if ($o = static::objects())
static::$form = $o[0];
else //TODO: Remove the code below and move it to task??
static::$form = self::__loadDefaultForm();
}
return static::$form;
}
static function getInstance($object_id=0) {
if (!isset(static::$instance))
static::$instance = static::getDefaultForm()->instanciate();
static::$instance->object_type = 'O';
if ($object_id)
static::$instance->object_id = $object_id;
return static::$instance;
}
static function __loadDefaultForm() {
require_once(INCLUDE_DIR.'class.i18n.php');
$i18n = new Internationalization();
$tpl = $i18n->getTemplate('form.yaml');
foreach ($tpl->getData() as $f) {
if ($f['type'] == 'O') {
$form = DynamicForm::create($f);
$form->save();
break;
}
}
$o =static::objects();
return $o[0];
}
}
//Organization::_inspect();
?>