Completed
Branch CASC/base (79f9d1)
by
unknown
16:50 queued 08:50
created

ConfirmDeletion   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 83
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A handle() 0 51 4
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\exceptions\InvalidDataTypeException;
11
use EventEspresso\core\exceptions\InvalidInterfaceException;
12
use EventEspresso\core\exceptions\UnexpectedEntityException;
13
use EventEspresso\core\services\orm\tree_traversal\NodeGroupDao;
14
use Events_Admin_Page;
15
use InvalidArgumentException;
16
use ReflectionException;
17
18
/**
19
 * Class ConfirmDeletion
20
 *
21
 * Controller-like logic for redirecting to the batch job for deleting model objects if the request data for
22
 * ConfirmEventDeletionForm is valid, otherwise providing an error message and redirecting back to the deletion preview
23
 * page (the forms system takes care of stashing the invalid form submission data and then populating that form with
24
 * it).
25
 *
26
 * @package     Event Espresso
27
 * @author         Mike Nelson
28
 * @since         $VID:$
29
 *
30
 */
31
class ConfirmDeletion
32
{
33
    /**
34
     * @var NodeGroupDao
35
     */
36
    private $dao;
37
38
    /**
39
     * ConfirmDeletion constructor.
40
     * @param NodeGroupDao $dao
41
     */
42
    public function __construct(
43
        NodeGroupDao $dao
44
    ) {
45
46
        $this->dao = $dao;
47
    }
48
49
    /**
50
     * Redirects to the batch job for deleting events if the form submission is valid, otherwise back to the deletion
51
     * preview page.
52
     * @since $VID:$
53
     * @param $request_data
54
     * @param $admin_base_url
55
     * @throws EE_Error
56
     * @throws InvalidArgumentException
57
     * @throws InvalidDataTypeException
58
     * @throws InvalidInterfaceException
59
     * @throws ReflectionException
60
     * @throws UnexpectedEntityException
61
     */
62
    public function handle($request_data, $admin_base_url)
63
    {
64
        $deletion_job_code = isset($request_data['deletion_job_code']) ? sanitize_key($request_data['deletion_job_code']) : '';
65
        $models_and_ids_to_delete = $this->dao->getModelsAndIdsFromGroup($deletion_job_code);
66
        $form = new ConfirmEventDeletionForm($models_and_ids_to_delete['Event']);
67
        // Initialize the form from the request, and check if its valid.
68
        $form->receive_form_submission($request_data);
69
        if ($form->is_valid()) {
70
            // Redirect the user to the deletion batch job.
71
            EEH_URL::safeRedirectAndExit(
72
                EE_Admin_Page::add_query_args_and_nonce(
73
                    array(
74
                        'page' => 'espresso_batch',
75
                        'batch' => EED_Batch::batch_job,
76
                        'deletion_job_code' => $deletion_job_code,
77
                        'job_handler' => urlencode('EventEspressoBatchRequest\JobHandlers\ExecuteBatchDeletion'),
78
                        'return_url' => urlencode(
79
                            add_query_arg(
80
                                [
81
                                    'status' => 'trash'
82
                                ],
83
                                EVENTS_ADMIN_URL
84
                            )
85
                        )
86
                    ),
87
                    admin_url()
88
                )
89
            );
90
        }
91
        // Dont' use $form->submission_error_message() because it adds the form input's label in front
92
        // of each validation error which ends up looking quite confusing.
93
        $validation_errors = $form->get_validation_errors_accumulated();
94
        foreach ($validation_errors as $validation_error) {
95
            EE_Error::add_error(
96
                $validation_error->getMessage(),
97
                __FILE__,
98
                __FUNCTION__,
99
                __LINE__
100
            );
101
        }
102
103
        EEH_URL::safeRedirectAndExit(
104
            EE_Admin_Page::add_query_args_and_nonce(
105
                [
106
                    'action' => 'preview_deletion',
107
                    'deletion_job_code' => $deletion_job_code
108
                ],
109
                $admin_base_url
110
            )
111
        );
112
    }
113
}
114
// End of file ConfirmDeletion.php
115
// Location: EventEspresso\core\domain\services\admin\events\data/ConfirmDeletion.php
116