|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package org.openpsa.sales |
|
4
|
|
|
* @author The Midgard Project, http://www.midgard-project.org |
|
5
|
|
|
* @copyright The Midgard Project, http://www.midgard-project.org |
|
6
|
|
|
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
use midcom\datamanager\datamanager; |
|
10
|
|
|
use midcom\datamanager\schemadb; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Salesproject edit/create/delete handler |
|
15
|
|
|
* |
|
16
|
|
|
* @package org.openpsa.sales |
|
17
|
|
|
*/ |
|
18
|
|
|
class org_openpsa_sales_handler_edit extends midcom_baseclasses_components_handler |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* The salesproject we're working on |
|
22
|
|
|
* |
|
23
|
|
|
* @var org_openpsa_sales_salesproject_dba |
|
24
|
|
|
*/ |
|
25
|
|
|
private $_salesproject; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param Request $request The request object |
|
29
|
|
|
* @param string $guid The salesproject GUID |
|
30
|
|
|
*/ |
|
31
|
1 |
|
public function _handler_edit(Request $request, $guid) |
|
32
|
|
|
{ |
|
33
|
1 |
|
$this->_salesproject = new org_openpsa_sales_salesproject_dba($guid); |
|
34
|
1 |
|
$this->_salesproject->require_do('midgard:update'); |
|
35
|
|
|
|
|
36
|
1 |
|
$schemadb = schemadb::from_path($this->_config->get('schemadb_salesproject')); |
|
37
|
1 |
|
$field =& $schemadb->get('default')->get_field('customer'); |
|
38
|
1 |
|
$field['type_config']['options'] = $this->list_groups($this->_salesproject); |
|
39
|
1 |
|
$dm = new datamanager($schemadb); |
|
40
|
1 |
|
$dm->set_storage($this->_salesproject); |
|
41
|
|
|
|
|
42
|
1 |
|
midcom::get()->head->set_pagetitle(sprintf($this->_l10n_midcom->get('edit %s'), $this->_l10n->get('salesproject'))); |
|
43
|
|
|
|
|
44
|
1 |
|
$workflow = $this->get_workflow('datamanager', ['controller' => $dm->get_controller()]); |
|
45
|
1 |
|
return $workflow->run($request); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param Request $request The request object |
|
50
|
|
|
* @param string $guid The customer GUID |
|
51
|
|
|
*/ |
|
52
|
1 |
|
public function _handler_new(Request $request, $guid = null) |
|
53
|
|
|
{ |
|
54
|
1 |
|
midcom::get()->auth->require_user_do('midgard:create', null, org_openpsa_sales_salesproject_dba::class); |
|
55
|
|
|
|
|
56
|
1 |
|
$this->_salesproject = new org_openpsa_sales_salesproject_dba; |
|
57
|
|
|
|
|
58
|
|
|
$defaults = [ |
|
59
|
1 |
|
'code' => org_openpsa_sales_salesproject_dba::generate_salesproject_number(), |
|
60
|
1 |
|
'owner' => midcom_connection::get_user() |
|
61
|
|
|
]; |
|
62
|
1 |
|
$schemadb = schemadb::from_path($this->_config->get('schemadb_salesproject')); |
|
63
|
|
|
|
|
64
|
1 |
|
if (!empty($guid)) { |
|
65
|
|
|
$field =& $schemadb->get('default')->get_field('customer'); |
|
66
|
|
|
try { |
|
67
|
|
|
$customer = new org_openpsa_contacts_group_dba($guid); |
|
68
|
|
|
$field['type_config']['options'] = [0 => '', $customer->id => $customer->official]; |
|
69
|
|
|
|
|
70
|
|
|
$defaults['customer'] = $customer->id; |
|
71
|
|
|
} catch (midcom_error $e) { |
|
72
|
|
|
$customer = new org_openpsa_contacts_person_dba($guid); |
|
73
|
|
|
$defaults['customerContact'] = $customer->id; |
|
74
|
|
|
$field['type_config']['options'] = $this->list_groups(new org_openpsa_sales_salesproject_dba, [$customer->id => true]); |
|
75
|
|
|
} |
|
76
|
|
|
$this->add_breadcrumb($this->router->generate('list_customer', ['guid' => $customer->guid]), |
|
77
|
|
|
sprintf($this->_l10n->get('salesprojects with %s'), $customer->get_label())); |
|
78
|
|
|
} |
|
79
|
1 |
|
$dm = new datamanager($schemadb); |
|
80
|
1 |
|
$dm->set_defaults($defaults); |
|
81
|
1 |
|
$dm->set_storage($this->_salesproject); |
|
82
|
|
|
|
|
83
|
1 |
|
midcom::get()->head->set_pagetitle($this->_l10n->get('create salesproject')); |
|
84
|
|
|
|
|
85
|
1 |
|
$workflow = $this->get_workflow('datamanager', [ |
|
86
|
1 |
|
'controller' => $dm->get_controller(), |
|
87
|
1 |
|
'save_callback' => [$this, 'save_callback'] |
|
88
|
|
|
]); |
|
89
|
1 |
|
return $workflow->run($request); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function save_callback() |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->router->generate('salesproject_view', ['guid' => $this->_salesproject->guid]); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Function for listing groups salesproject contacts are members of |
|
99
|
|
|
* |
|
100
|
|
|
* @param org_openpsa_sales_salesproject_dba $salesproject The salesproject we're working with |
|
101
|
|
|
* @param array $contacts Default contacts for nonpersistent objects |
|
102
|
|
|
*/ |
|
103
|
1 |
|
private function list_groups(org_openpsa_sales_salesproject_dba $salesproject, array $contacts = []) |
|
104
|
|
|
{ |
|
105
|
1 |
|
$ret = [0 => '']; |
|
106
|
|
|
|
|
107
|
|
|
// Make sure the currently selected customer (if any) is listed |
|
108
|
1 |
|
if ($salesproject->customer > 0) { |
|
109
|
|
|
// Make sure we can read the current customer for the name |
|
110
|
|
|
midcom::get()->auth->request_sudo('org.openpsa.helpers'); |
|
111
|
|
|
$this->load_group($ret, $salesproject->customer); |
|
112
|
|
|
midcom::get()->auth->drop_sudo(); |
|
113
|
|
|
} |
|
114
|
1 |
|
if (empty($contacts)) { |
|
115
|
1 |
|
$salesproject->get_members(); |
|
116
|
1 |
|
$contacts = $salesproject->contacts; |
|
|
|
|
|
|
117
|
|
|
|
|
118
|
1 |
|
if (empty($contacts)) { |
|
119
|
1 |
|
return $ret; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
$mc = midcom_db_member::new_collector(); |
|
124
|
|
|
$mc->add_constraint('uid', 'IN', array_keys($contacts)); |
|
125
|
|
|
// Skip magic groups and contact lists |
|
126
|
|
|
$mc->add_constraint('gid.name', 'NOT LIKE', '\_\_%'); |
|
127
|
|
|
$mc->add_constraint('gid.orgOpenpsaObtype', '<>', org_openpsa_contacts_group_dba::MYCONTACTS); |
|
128
|
|
|
$memberships = $mc->get_values('gid'); |
|
129
|
|
|
|
|
130
|
|
|
foreach ($memberships as $gid) { |
|
131
|
|
|
$this->load_group($ret, $gid); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
reset($ret); |
|
135
|
|
|
asort($ret); |
|
136
|
|
|
return $ret; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
private function load_group(array &$ret, $company_id) |
|
140
|
|
|
{ |
|
141
|
|
|
if (!array_key_exists($company_id, $ret)) { |
|
142
|
|
|
try { |
|
143
|
|
|
$company = org_openpsa_contacts_group_dba::get_cached($company_id); |
|
144
|
|
|
$ret[$company->id] = $company->get_label(); |
|
|
|
|
|
|
145
|
|
|
} catch (midcom_error $e) { |
|
146
|
|
|
$e->log(); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @param Request $request The request object |
|
153
|
|
|
* @param string $guid The salesproject GUID |
|
154
|
|
|
*/ |
|
155
|
|
|
public function _handler_delete(Request $request, $guid) |
|
156
|
|
|
{ |
|
157
|
|
|
$this->_salesproject = new org_openpsa_sales_salesproject_dba($guid); |
|
158
|
|
|
$workflow = $this->get_workflow('delete', [ |
|
159
|
|
|
'object' => $this->_salesproject, |
|
160
|
|
|
'recursive' => true |
|
161
|
|
|
]); |
|
162
|
|
|
return $workflow->run($request); |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|