Completed
Push — master ( 29833c...518cf4 )
by
unknown
07:48
created

InvestformPresenter::actionSend()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 40
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 2
Metric Value
c 3
b 1
f 2
dl 0
loc 40
rs 8.5806
cc 4
eloc 18
nc 6
nop 3
1
<?php
2
3
/**
4
 * This file is part of the Investform module for webcms2.
5
 * Copyright (c) @see LICENSE
6
 */
7
8
namespace AdminModule\InvestformModule;
9
10
use Nette\Forms\Form;
11
use WebCMS\InvestformModule\Common\PdfPrinter;
12
use WebCMS\InvestformModule\Common\EmailSender;
13
use WebCMS\InvestformModule\Entity\Address;
14
15
/**
16
 * Description of
17
 *
18
 * @author Tomas Voslar <[email protected]>
19
 */
20
class InvestformPresenter extends BasePresenter
21
{
22
    private $investment;
23
24
    protected function startup()
25
    {
26
    	parent::startup();
27
    }
28
29
    protected function beforeRender()
30
    {
31
	   parent::beforeRender();
32
    }
33
34
    protected function createComponentGrid($name)
35
    {
36
        $grid = $this->createGrid($this, $name, "\WebCMS\InvestformModule\Entity\Investment");
37
38
        $grid->setFilterRenderType(\Grido\Components\Filters\Filter::RENDER_INNER);
39
        $grid->addFilterDateRange('created', 'Created');
40
41
        $grid->addColumnDate('created', 'Created', \Grido\Components\Columns\Date::FORMAT_DATETIME)
42
            ->setSortable();
43
        $grid->addColumnNumber('id', 'Contract id')->setSortable();
44
45
        $grid->addColumnText('pin', 'Business Id')->setCustomRender(function($item) {
46
            if ($item->getBusinessman()) {
47
                return $item->getBusinessman()->getBusinessId();
48
            } else {
49
                return $item->getPin();
50
            }
51
        });
52
53
        $grid->addColumnText('name', 'Name')->setCustomRender(function($item) {
54
            return $item->getAddress()->getName() . ' ' . $item->getAddress()->getLastname();
55
        });
56
        $grid->addColumnText('company', 'Company')->setCustomRender(function($item) {
57
            return $item->getCompany();
58
        });
59
        //TODO prekladac
60
        $grid->addColumnText('demand', 'Demand')->setCustomRender(function($item) {
0 ignored issues
show
Unused Code introduced by
The parameter $item is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
61
            return "Odesláno";
62
        });
63
        $grid->addColumnText('contract', 'Contract')->setCustomRender(function($item) {
64
            return $item->getContractSend() ? 'Odesláno' : 'Neodesláno';
65
        });
66
        $grid->addColumnText('contractClosed', 'Contract closed')->setCustomRender(function($item) {
67
            return $item->getContractClosed() ? 'Yes' : 'No';
68
        });
69
        $grid->addColumnText('contractPaid', 'Contract paid')->setCustomRender(function($item) {
70
            return $item->getContractPaid() ? 'Yes' : 'No';
71
        });
72
        $grid->addColumnText('clientContacted', 'Client contacted')->setCustomRender(function($item) {
73
            return $item->getClientContacted() ? 'Yes' : 'No';
74
        });
75
76
        $grid->addActionHref("send", 'Send', 'send', array('idPage' => $this->actualPage->getId()))->getElementPrototype()->addAttributes(array('class' => array('btn', 'btn-primary', 'ajax', 'purple')));
77
        $grid->addActionHref("download", 'Download', 'download', array('idPage' => $this->actualPage->getId()))->getElementPrototype()->addAttributes(array('class' => array('btn', 'btn-primary', 'purple')));
78
        $grid->addActionHref("update", 'Edit', 'update', array('idPage' => $this->actualPage->getId()))->getElementPrototype()->addAttributes(array('class' => array('btn', 'btn-primary', 'ajax', 'green')));
79
        $grid->addActionHref("delete", 'Delete', 'delete', array('idPage' => $this->actualPage->getId()))->getElementPrototype()->addAttributes(array('class' => array('btn', 'btn-danger'), 'data-confirm' => 'Are you sure you want to delete this item?'));
80
        $grid->addActionHref("closed", 'Contract closed', 'closed', array('idPage' => $this->actualPage->getId()))->getElementPrototype()->addAttributes(array('class' => array('btn', 'btn-primary', 'ajax', 'green')));
81
        $grid->addActionHref("paid", 'Contract Paid', 'paid', array('idPage' => $this->actualPage->getId()))->getElementPrototype()->addAttributes(array('class' => array('btn', 'btn-primary', 'ajax', 'green')));
82
        $grid->addActionHref("contacted", 'Contacted', 'contacted', array('idPage' => $this->actualPage->getId()))->getElementPrototype()->addAttributes(array('class' => array('btn', 'btn-primary', 'ajax', 'green')));
83
84
        // $operations = array('downloadGrid' => 'Download', 'deleteGrid' => 'Delete');
85
        // $grid->setOperation($operations, $this->handleGridOperations)
86
        //     ->setConfirm('deleteGrid', 'Are you sure you want to delete %i items?');
87
88
        return $grid;
89
    }
90
91
    /**
92
     * Common handler for grid operations.
93
     * @param string $operation
94
     * @param array $id
95
     */
96
    public function handleGridOperations($operation, $id)
97
    {
98
        if (!$id) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $id of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
99
            $this->flashMessage('No rows selected.', 'error');
100
        }
101
102
        $this->forward($operation, array(
103
            'idPage' => $this->actualPage->getId(),
104
            'id' => $id
105
        ));
106
    }
107
108
    public function actionDownloadGrid()
109
    {
110
        $rows = $this->getParameter('id');
0 ignored issues
show
Unused Code introduced by
$rows is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
111
112
        // $zipSubfolder = 's' . date('Y-m-d-H-i-s');
113
114
        // foreach ($rows as $key => $value) {
115
        //     $investment = $this->em->getRepository('\WebCMS\InvestformModule\Entity\Investment')->find($value);
116
117
        //     $zip = new PdfPrinter($investment);            
118
119
        //     $zip->savePdfToZip($zipSubfolder);
120
        // }
121
        
122
        $this->flashMessage("Done.", 'success');
123
124
        $this->forward('default', array(
125
            'idPage' => $this->actualPage->getId()
126
        ));
127
    }
128
129
    public function actionDeleteGrid()
130
    {
131
        $rows = $this->getParameter('id');
132
        
133
        foreach ($rows as $key => $value) {
134
            $investment = $this->em->getRepository('\WebCMS\InvestformModule\Entity\Investment')->find($value);
135
136
            $this->em->remove($investment);
137
        }
138
139
        $this->em->flush();
140
141
        $this->flashMessage("Contracts has been deleted", 'success');
142
143
        $this->forward('default', array(
144
            'idPage' => $this->actualPage->getId()
145
        ));
146
    }
147
148
    public function createComponentForm($name)
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
149
    {
150
        $form = $this->createForm();
151
152
        $form->addText('phone', 'Phone');
153
        $form->addText('email', 'Email');
154
        $form->addText('birthdateNumber', 'Birthdate number');
155
        $form->addText('company', 'Company');
156
        $form->addText('registrationNumber', 'Registration number');
157
        $form->addText('investment', 'Investment amount');
158
        $form->addText('bankAccount', 'Bank account');
159
        $form->addSelect('investmentLength', 'Investment length', array(3 => 3, 5 => 5));
160
        $form->addText('pin', 'Pin');
161
162
        $address = $form->addContainer('Address');
163
        $address->addText('name', 'Name:');
164
        $address->addText('lastname', 'Lastname:');
165
        $address->addText('street', 'Street:');
166
        $address->addText('postcode', 'Postcode:');
167
        $address->addText('city', 'City:');
168
169
        $postalAddress = $form->addContainer('PostalAddress');
170
        $postalAddress->addText('name', 'Name:');
171
        $postalAddress->addText('lastname', 'Lastname:')
172
            ->addConditionOn($form['PostalAddress']['name'], Form::FILLED)
173
            ->addRule(Form::FILLED, 'Lastname is mandatory.');
174
        $postalAddress->addText('street', 'Street:')
175
            ->addConditionOn($form['PostalAddress']['name'], Form::FILLED)
176
            ->addRule(Form::FILLED, 'Street is mandatory.');
177
        $postalAddress->addText('postcode', 'Postcode:')
178
            ->addConditionOn($form['PostalAddress']['name'], Form::FILLED)
179
            ->addRule(Form::FILLED, 'Postcode is mandatory.');
180
        $postalAddress->addText('city', 'City:')
181
            ->addConditionOn($form['PostalAddress']['name'], Form::FILLED)
182
            ->addRule(Form::FILLED, 'City is mandatory.');
183
184
        if (is_object($this->investment->getAddress())) {
185
            $address->setDefaults($this->investment->getAddress()->toArray());    
186
        }
187
        
188
        if (is_object($this->investment->getPostalAddress())) {
189
            $postalAddress->setDefaults($this->investment->getPostalAddress()->toArray());
190
        }
191
192
        $form->addSubmit('save', 'Save');
193
        $form->setDefaults($this->investment->toArray());
194
195
        $form->onSuccess[] = callback($this, 'formSubmitted');
196
197
        return $form;
198
    }
199
200
    public function formSubmitted($form)
201
    {
202
        $values = $form->getValues();
203
204
        $this->investment->setPhone($values->phone);
205
        $this->investment->setEmail($values->email);
206
        $this->investment->setBirthdateNumber($values->birthdateNumber);
207
        $this->investment->setCompany($values->company);
208
        $this->investment->setPin($values->pin);
209
        $this->investment->setBankAccount(str_replace('_', '', $values->bankAccount));
210
        $this->investment->setRegistrationNumber($values->registrationNumber);
211
        $this->investment->setInvestment($values->investment);
212
        $this->investment->setInvestmentLength($values->investmentLength);
213
214
        $address = $this->investment->getAddress();
215
        $address->setName($values->Address->name);
216
        $address->setLastname($values->Address->lastname);
217
        $address->setStreet($values->Address->street);
218
        $address->setPostcode($values->Address->postcode);
219
        $address->setCity($values->Address->city);
220
221
        $postalAddress = $this->investment->getPostalAddress();
222
        if(!is_object($postalAddress)) {
223
            $postalAddress = new Address;
224
            
225
            $this->investment->setPostalAddress($postalAddress);
226
            $this->em->persist($postalAddress);
227
        }
228
229
        $postalAddress->setName($values->PostalAddress->name);
230
        $postalAddress->setLastname($values->PostalAddress->lastname);
231
        $postalAddress->setStreet($values->PostalAddress->street);
232
        $postalAddress->setPostcode($values->PostalAddress->postcode);
233
        $postalAddress->setCity($values->PostalAddress->city);
234
235
        $this->em->flush();
236
237
        $this->flashMessage('Contract has been updated.', 'success');
238
239
        $roles = $this->user->getIdentity()->getRoles();
240
        if (count(array_intersect(array('superadmin', 'admin'), $roles)) > 0) {
241
            $this->forward('default', array(
242
                'idPage' => $this->actualPage->getId()
243
            ));
244
        } else {
245
            $this->forward('Businessman:default', array(
246
                'idPage' => $this->actualPage->getId()
247
            ));
248
        }
249
        
250
    }
251
252
    public function actionUpdate($id, $idPage)
253
    {
254
        $this->reloadContent();
255
256
        $this->investment = $this->em->getRepository('\WebCMS\InvestformModule\Entity\Investment')->find($id);
257
258
        $this->template->idPage = $idPage;
0 ignored issues
show
Documentation introduced by
The property $template is declared private in Nette\Application\UI\Control. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Bug introduced by
Accessing idPage on the interface Nette\Templating\ITemplate suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
259
    }
260
261
    public function actionSend($id, $idPage, $from = NULL)
262
    {
263
        $investment = $this->em->getRepository('\WebCMS\InvestformModule\Entity\Investment')->find($id);
264
265
        if ($investment->getBirthdateNumber()) {
266
            
267
            $emailSender = new EmailSender($this->settings, $investment, 'contract');
268
            $emailSender->send();
269
270
            $investment->setContractSend(true);
271
            $this->em->flush();
272
273
            $this->flashMessage('Contract has been sent to the client\'s email address.', 'success');
274
275
        } else {
276
277
            $this->flashMessage("Please fill client's birthdate number.", 'error');
278
279
        }
280
281
        if ($from == 'businessman') {
282
            
283
            $this->forward('Businessman:detail', array(
284
                'id' => $investment->getBusinessman()->getId(),
285
                'idPage' => $idPage
286
            ));
287
288
        } elseif ($from == 'company') {
289
            
290
            //TODO
291
292
        } else {
293
294
            $this->forward('default', array(
295
                'idPage' => $this->actualPage->getId()
296
            ));
297
298
        }
299
        
300
    }
301
302
    public function actionDelete($id)
303
    {
304
        $investment = $this->em->getRepository('\WebCMS\InvestformModule\Entity\Investment')->find($id);
305
306
        $this->em->remove($investment);
307
        $this->em->flush();
308
309
        $this->flashMessage('Investment has been removed.', 'success');
310
311
        $this->forward('default', array(
312
            'idPage' => $this->actualPage->getId()
313
        ));
314
    }
315
316
    public function actionDownload($id)
317
    {        
318
        $investment = $this->em->getRepository('\WebCMS\InvestformModule\Entity\Investment')->find($id);
319
        $pdfPrinter = new PdfPrinter($investment);
320
321
        $this->sendResponse($pdfPrinter->printPdfContract(true));
0 ignored issues
show
Documentation introduced by
$pdfPrinter->printPdfContract(true) is of type null|string, but the function expects a object<Nette\Application\IResponse>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
322
    }
323
324
    public function actionContacted($id, $idPage)
0 ignored issues
show
Unused Code introduced by
The parameter $idPage is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
325
    {
326
        $investment = $this->em->getRepository('\WebCMS\InvestformModule\Entity\Investment')->find($id);
327
        $investment->setClientContacted($investment->getClientContacted() ? false : true);
328
329
        $this->em->flush();
330
331
        $this->flashMessage('Parameter has been changed.', 'success');
332
        $this->forward('default', array(
333
            'idPage' => $this->actualPage->getId()
334
        ));
335
    }
336
337 View Code Duplication
    public function actionPaid($id, $idPage)
0 ignored issues
show
Unused Code introduced by
The parameter $idPage is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
338
    {
339
        $investment = $this->em->getRepository('\WebCMS\InvestformModule\Entity\Investment')->find($id);
340
341
        if ($investment->getContractClosed()) {
342
            $investment->setContractPaid($investment->getContractPaid() ? false : true);
343
344
            $this->em->flush();
345
346
            $this->flashMessage('Parameter has been changed.', 'success');
347
        } else {
348
            $this->flashMessage('Contract must be closed first.', 'error');
349
        }
350
        $this->forward('default', array(
351
            'idPage' => $this->actualPage->getId()
352
        ));
353
    }
354
355 View Code Duplication
    public function actionClosed($id, $idPage)
0 ignored issues
show
Unused Code introduced by
The parameter $idPage is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
356
    {
357
        $investment = $this->em->getRepository('\WebCMS\InvestformModule\Entity\Investment')->find($id);
358
359
        if ($investment->getContractSend()) {
360
            $investment->setContractClosed($investment->getContractClosed() ? false : true);
361
362
            $this->em->flush();
363
364
            $this->flashMessage('Parameter has been changed.', 'success');
365
        } else {
366
            $this->flashMessage('Contract must be sent first.', 'error');
367
        }
368
        $this->forward('default', array(
369
            'idPage' => $this->actualPage->getId()
370
        ));
371
    }
372
373
    public function actionDefault($idPage)
374
    {
375
376
    }
377
378
    public function renderDefault($idPage)
379
    {
380
    	$this->reloadContent();
381
    	$this->template->idPage = $idPage;
0 ignored issues
show
Documentation introduced by
The property $template is declared private in Nette\Application\UI\Control. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Bug introduced by
Accessing idPage on the interface Nette\Templating\ITemplate suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
382
    }
383
}
384