Completed
Branch CASC/preview-page-2 (893921)
by
unknown
27:19 queued 19:03
created

ConfirmDeletion::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\domain\services\admin\events\data;
4
5
use EE_Admin_Page;
6
use EE_Error;
7
use EED_Batch;
8
use EEH_URL;
9
use EventEspresso\admin_pages\events\form_sections\ConfirmEventDeletionForm;
10
use EventEspresso\core\services\orm\tree_traversal\NodeGroupDao;
11
12
/**
13
 * Class ConfirmDeletion
14
 *
15
 * Description
16
 *
17
 * @package     Event Espresso
18
 * @author         Mike Nelson
19
 * @since         $VID:$
20
 *
21
 */
22
class ConfirmDeletion
23
{
24
    /**
25
     * @var NodeGroupDao
26
     */
27
    private $dao;
28
29
    /**
30
     * ConfirmDeletion constructor.
31
     * @param NodeGroupDao $dao
32
     */
33
    public function __construct(
34
        NodeGroupDao $dao
35
    )
36
    {
37
38
        $this->dao = $dao;
39
    }
40
41
    /**
42
     * @since $VID:$
43
     * @param \Events_Admin_Page $admin_page
44
     * @throws EE_Error
45
     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
46
     * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
47
     * @throws \EventEspresso\core\exceptions\UnexpectedEntityException
48
     * @throws \InvalidArgumentException
49
     * @throws \ReflectionException
50
     */
51
    public function handle(\Events_Admin_Page $admin_page)
52
    {
53
        $request_data = $admin_page->get_request_data();
54
        $deletion_job_code = isset($request_data['deletion_job_code']) ? sanitize_key($request_data['deletion_job_code']) : '';
55
        $models_and_ids_to_delete = $this->dao->getModelsAndIdsFromGroup($deletion_job_code);
56
        $form = new ConfirmEventDeletionForm($models_and_ids_to_delete['Event']);
57
        // Initialize the form from the request, and check if its valid.
58
        $form->receive_form_submission($request_data);
59
        if ($form->is_valid()) {
60
            // Redirect the user to the deletion batch job.
61
            EEH_URL::safeRedirectAndExit(
62
                EE_Admin_Page::add_query_args_and_nonce(
63
                    array(
64
                        'page' => 'espresso_batch',
65
                        'batch' => EED_Batch::batch_job,
66
                        'deletion_job_code' => $deletion_job_code,
67
                        'job_handler' => urlencode('EventEspressoBatchRequest\JobHandlers\ExecuteBatchDeletion'),
68
                        'return_url' => urlencode(
69
                            add_query_arg(
70
                                [
71
                                    'status' => 'trash'
72
                                ],
73
                                EVENTS_ADMIN_URL
74
                            )
75
                        )
76
                    ),
77
                    admin_url()
78
                )
79
            );
80
        }
81
        // Dont' use $form->submission_error_message() because it adds the form input's label in front
82
        // of each validation error which ends up looking quite confusing.
83
        $validation_errors = $form->get_validation_errors_accumulated();
84
        foreach ($validation_errors as $validation_error) {
85
            EE_Error::add_error(
86
                $validation_error->getMessage(),
87
                __FILE__,
88
                __FUNCTION__,
89
                __LINE__
90
            );
91
        }
92
93
        EEH_URL::safeRedirectAndExit(
94
            EE_Admin_Page::add_query_args_and_nonce(
95
                [
96
                    'action' => 'preview_deletion',
97
                    'deletion_job_code' => $deletion_job_code
98
                ],
99
                $admin_page->admin_base_url()
100
            )
101
        );
102
    }
103
}
104
// End of file ConfirmDeletion.php
105
// Location: EventEspresso\core\domain\services\admin\events\data/ConfirmDeletion.php
106