Completed
Push — master ( 21d646...c8d5cb )
by Andreas
26:18
created

org_openpsa_sales_handler_edit   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Test Coverage

Coverage 47.89%

Importance

Changes 0
Metric Value
eloc 70
dl 0
loc 145
ccs 34
cts 71
cp 0.4789
rs 10
c 0
b 0
f 0
wmc 14

6 Methods

Rating   Name   Duplication   Size   Complexity  
A save_callback() 0 3 1
A _handler_new() 0 38 3
A _handler_edit() 0 15 1
A _handler_delete() 0 8 1
A list_groups() 0 34 5
A load_group() 0 8 3
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;
0 ignored issues
show
Bug Best Practice introduced by
The property contacts does not exist on org_openpsa_sales_salesproject_dba. Since you implemented __get, consider adding a @property annotation.
Loading history...
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();
0 ignored issues
show
Bug introduced by
The method get_label() does not exist on midcom_core_dbaobject. It seems like you code against a sub-type of midcom_core_dbaobject such as org_openpsa_calendar_event_dba or org_openpsa_invoices_invoice_dba or net_nemein_tag_tag_dba or org_openpsa_invoices_invoice_item_dba or midcom_db_parameter or org_openpsa_calendar_event_member_dba or org_openpsa_sales_salesproject_offer_dba or midcom_db_topic or net_nemein_tag_link_dba or org_openpsa_invoices_billing_data_dba or org_openpsa_directmarketing_campaign_member_dba or org_openpsa_contacts_group_dba or midcom_db_person or midcom_db_member or org_openpsa_projects_project or org_openpsa_calendar_event_resource_dba or midcom_db_group or org_openpsa_projects_task_dba or org_openpsa_documents_document_dba. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

144
                /** @scrutinizer ignore-call */ 
145
                $ret[$company->id] = $company->get_label();
Loading history...
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