Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 243
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
zeroBSCRM_Delete
0.00% covered (danger)
0.00%
0 / 242
0.00% covered (danger)
0.00%
0 / 4
4290
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 43
0.00% covered (danger)
0.00%
0 / 1
650
 loadObject
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
30
 catchPost
0.00% covered (danger)
0.00%
0 / 80
0.00% covered (danger)
0.00%
0 / 1
342
 drawView
0.00% covered (danger)
0.00%
0 / 114
0.00% covered (danger)
0.00%
0 / 1
306
1<?php
2/*
3 * Jetpack CRM
4 * https://jetpackcrm.com
5 * V2.52+
6 *
7 * Copyright 2020 Automattic
8 *
9 * Date: 26/02/18
10 */
11
12defined( 'ZEROBSCRM_PATH' ) || exit( 0 );
13
14class zeroBSCRM_Delete {
15
16    private $objID = false;
17
18    /**
19     * Contact object array if retrievable, otherwise false.
20     *
21     * @var array|false
22     */
23    private $obj;
24    private $objTypeID = false; // ZBS_TYPE_CONTACT - v3.0+
25
26    // following now FILLED OUT by objTypeID above, v3.0+
27    private $objType      = false; // 'contact'
28    private $singular     = false;
29    private $plural       = false;
30    private $listViewSlug = false;
31    private $langLabels   = false;
32
33    private $stage     = 1; // 1 = 'are you sure', 2 = 'deleted'
34    private $canDelete = 1; // if no perms -1
35
36    // this only applies to contacts (v3.0)
37    private $killChildren = false;
38
39    function __construct( $args = array() ) {
40
41        #} =========== LOAD ARGS ==============
42        $defaultArgs = array(
43
44            'objID'        => false,
45            'objTypeID'    => false,   // 5
46
47            // these are now retrieved from DAL centralised vars by objTypeID above, v3.0+
48            // ... unless hard typed here.
49            'objType'      => false,   // transaction
50            'singular'     => false,  // Transaction
51            'plural'       => false,      // Transactions
52            'listViewSlug' => false,    // manage-transactions
53            'langLabels'   => array(),
54
55        );
56        foreach ( $defaultArgs as $argK => $argV ) {
57            $this->$argK = $argV;
58            if ( is_array( $args ) && isset( $args[ $argK ] ) ) {
59                if ( is_array( $args[ $argK ] ) ) {
60                    $newData = $this->$argK;
61                    if ( ! is_array( $newData ) ) {
62                        $newData = array();
63                    } foreach ( $args[ $argK ] as $subK => $subV ) {
64                        $newData[ $subK ] = $subV;
65                    }$this->$argK = $newData;
66                } else {
67                    $this->$argK = $args[ $argK ]; }
68            }
69        }
70        #} =========== / LOAD ARGS =============
71
72        // NOTE: here these vars are passed like:
73        // $this->objID
74        // .. NOT
75        // $objID
76
77        global $zbs;
78
79        // we load from DAL defaults, if objTypeID passed (overriding anything passed, if empty/false)
80        if ( isset( $this->objTypeID ) ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
81
82            $objTypeID = (int) $this->objTypeID;
83            if ( $objTypeID > 0 ) {
84
85                // obj type (contact)
86                $objTypeStr = $zbs->DAL->objTypeKey( $objTypeID );
87                if ( ( ! isset( $this->objType ) || $this->objType == false ) && ! empty( $objTypeStr ) ) {
88                    $this->objType = $objTypeStr;
89                }
90
91                // singular
92                $objSingular = $zbs->DAL->typeStr( $objTypeID );
93                if ( ( ! isset( $this->singular ) || $this->singular == false ) && ! empty( $objSingular ) ) {
94                    $this->singular = $objSingular;
95                }
96
97                // plural
98                $objPlural = $zbs->DAL->typeStr( $objTypeID, true );
99                if ( ( ! isset( $this->plural ) || $this->plural == false ) && ! empty( $objPlural ) ) {
100                    $this->plural = $objPlural;
101                }
102
103                // listViewSlug
104                $objSlug = $zbs->DAL->listViewSlugFromObjID( $objTypeID );
105                if ( ( ! isset( $this->listViewSlug ) || $this->listViewSlug == false ) && ! empty( $objSlug ) ) {
106                    $this->listViewSlug = $objSlug;
107                }
108            }
109        }
110
111        // if objid - load $post
112        $this->loadObject();
113
114        // check perms
115        if ( ! zeroBSCRM_permsObjType( $this->objTypeID ) ) {
116            $this->canDelete = false;
117        }
118
119        // check if it actually exists
120        if ( ! is_array( $this->obj ) || ! isset( $this->obj['id'] ) ) {
121            $this->canDelete = false;
122        }
123
124        // anything to save?
125        if ( $this->canDelete ) {
126            $this->catchPost();
127        }
128    }
129
130    // automatically, generically, loads the single obj
131    public function loadObject() {
132
133        // if objid - load $post
134        if ( isset( $this->objID ) && ! empty( $this->objID ) && $this->objID > 0 ) {
135
136            global $zbs;
137            // DAL3 we can use generic getSingle
138            if ( $this->objTypeID > 0 ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
139                // this gets $zbs->DAL->contacts->getSingle()
140                $this->obj = $zbs->DAL->getObjectLayerByType( $this->objTypeID )->getSingle( $this->objID );
141            } else {
142                // DAL2
143                // customer currently only
144                $this->obj = zeroBS_getCustomer( $this->objID );
145            }
146        }
147    }
148
149    public function catchPost() {
150
151        // If post, fire do_action
152            // DAL3 this gets postType switched to objType
153        if ( isset( $_POST['zbs-delete-form-master'] ) && $_POST['zbs-delete-form-master'] == $this->objTypeID ) {
154
155            // CHECK NONCE
156            if ( wp_verify_nonce( $_POST['zbs-delete-nonce'], 'delete-nonce' ) ) {
157
158                // got any extras? e.g. kill children?
159                if ( isset( $_POST['zbs-delete-kill-children'] ) ) {
160                    if ( $_POST['zbs-delete-kill-children'] == 'no' ) {
161                        $this->killChildren = false;
162                    }
163                    if ( $_POST['zbs-delete-kill-children'] == 'yes' ) {
164                        $this->killChildren = true;
165                    }
166                }
167
168                // verify + delete
169                if ( isset( $_POST['zbs-delete-id'] ) && $_POST['zbs-delete-id'] == $this->objID ) {
170
171                        global $zbs;
172
173                        // got orphans?
174                        $saveOrphans = true;
175                    if ( $this->killChildren ) {
176                        $saveOrphans = false;
177                    }
178
179                        // legit, delete
180                    switch ( $this->objTypeID ) {
181
182                        case ZBS_TYPE_CONTACT:
183                            // delete
184                            $deleted = $zbs->DAL->contacts->deleteContact(
185                                array(
186                                    'id'          => $this->objID,
187                                    'saveOrphans' => $saveOrphans,
188                                )
189                            );
190
191                            break;
192
193                        case ZBS_TYPE_COMPANY:
194                            // delete
195                            $deleted = $zbs->DAL->companies->deleteCompany(
196                                array(
197                                    'id'          => $this->objID,
198                                    'saveOrphans' => $saveOrphans,
199                                )
200                            );
201
202                            break;
203
204                        case ZBS_TYPE_QUOTE:
205                            // delete
206                            $deleted = $zbs->DAL->quotes->deleteQuote(
207                                array(
208                                    'id'          => $this->objID,
209                                    'saveOrphans' => $saveOrphans,
210                                )
211                            );
212
213                            break;
214
215                        case ZBS_TYPE_INVOICE:
216                            // delete
217                            $deleted = $zbs->DAL->invoices->deleteInvoice(
218                                array(
219                                    'id'          => $this->objID,
220                                    'saveOrphans' => $saveOrphans,
221                                )
222                            );
223
224                            break;
225
226                        case ZBS_TYPE_TRANSACTION:
227                            // delete
228                            $deleted = $zbs->DAL->transactions->deleteTransaction(
229                                array(
230                                    'id'          => $this->objID,
231                                    'saveOrphans' => $saveOrphans,
232                                )
233                            );
234
235                            break;
236
237                        case ZBS_TYPE_FORM:
238                            // delete
239                            $deleted = $zbs->DAL->forms->deleteForm(
240                                array(
241                                    'id'          => $this->objID,
242                                    'saveOrphans' => $saveOrphans,
243                                )
244                            );
245
246                            break;
247
248                        case ZBS_TYPE_TASK:
249                            // for now always kill links
250                            $saveOrphans = false;
251
252                            // delete
253                            $deleted = $zbs->DAL->events->deleteEvent(
254                                array(
255                                    'id'          => $this->objID,
256                                    'saveOrphans' => $saveOrphans,
257                                )
258                            );
259
260                            break;
261
262                        case ZBS_TYPE_QUOTETEMPLATE:
263                            // delete
264                            $deleted = $zbs->DAL->quotetemplates->deleteQuotetemplate(
265                                array(
266                                    'id'          => $this->objID,
267                                    'saveOrphans' => $saveOrphans,
268                                )
269                            );
270
271                            break;
272
273                    }
274
275                        // fire it
276                        do_action( 'zerobs_delete_' . $this->objType, $this->objID, $this->obj );
277
278                        // set $stage +1 (as only get here if posted ^)
279                        $this->stage = 2;
280
281                }
282            }
283        }
284    }
285
286    public function drawView() {
287
288        // check
289        if ( empty( $this->objType ) || empty( $this->listViewSlug ) || empty( $this->singular ) || empty( $this->plural ) ) {
290
291            return 'Error.';
292        }
293
294        global $zbs;
295
296        ?><div id="zbs-delete-master-wrap">
297                <form method="post" id="zbs-delete-form">
298
299            <div id="zbs-edit-warnings-wrap">
300                <?php
301                #} Pre-loaded msgs, because I wrote the helpers in php first... should move helpers to js and fly these
302
303                echo zeroBSCRM_UI2_messageHTML( 'warning hidden', 'Error Retrieving ' . $this->plural, 'There has been a problem retrieving your ' . $this->singular . ', if this issue persists, please ask your administrator to reach out to Jetpack CRM.', 'disabled warning sign', 'zbsCantLoadData' );
304                echo zeroBSCRM_UI2_messageHTML( 'warning hidden', 'Error Retrieving ' . $this->singular, 'There has been a problem retrieving your ' . $this->singular . ', if this issue persists, please ask your administrator to reach out to Jetpack CRM.', 'disabled warning sign', 'zbsCantLoadDataSingle' );
305
306                ?>
307            </div>
308            <!-- main view: list + sidebar -->
309            <div id="zbs-edit-wrap" class="ui grid" style="padding-top:5em">
310
311                <?php
312
313                if ( count( $zbs->pageMessages ) > 0 ) {
314
315                    #} Updated Msgs
316                    // was doing like this, but need control over styling
317                    // do_action( 'zerobs_updatemsg_contact');
318                    // so for now just using global :)
319                    echo '<div class="row" style="padding-bottom: 0 !important;"><div class="sixteen wide column" id="zbs-edit-notification-wrap">';
320
321                    foreach ( $zbs->pageMessages as $msg ) {
322
323                        // for now these can be any html :)
324                        echo $msg;
325
326                    }
327
328                        echo '</div></div>';
329
330                }
331
332                ?>
333
334                <div class="row">
335
336                    <?php
337
338                    if ( ! $this->canDelete ) {
339
340                        // no perms msg
341
342                        ?>
343                                    <div class="two wide column"></div>
344                                    <div class="twelve wide column">
345                                    <?php echo zeroBSCRM_UI2_messageHTML( 'warning', 'Restricted', 'You cannot delete this ' . $this->singular . '.', 'disabled warning sign', 'zbsCantDelete' ); ?>
346                                    </div>
347                                    <div class="two wide column"></div>
348                            <?php
349
350                    } else {
351
352                        // switch based on stage
353                        switch ( $this->stage ) {
354
355                            case 2:
356                                // deleted
357                                ?>
358
359                                    <div class="two wide column"></div>
360
361                                    <div class="twelve wide column">
362
363                                        <div class="ui icon big message">
364                                            <i class="trash icon"></i>
365                                            <div class="content">
366                                            <div class="header">
367                                                <?php esc_html_e( 'Deleted', 'zero-bs-crm' ); ?>
368                                            </div>
369                                            <p><?php echo esc_html( $this->singular ) . ' ' . esc_html__( 'was successfully deleted.', 'zero-bs-crm' ); ?></p>
370                                            <p>
371                                            <?php
372
373                                            // delete / back buttons
374                                            $backUrl = jpcrm_esc_link( 'list', -1, $this->objTypeID );
375
376                                                // output
377                                                echo '<a href="' . esc_url( $backUrl ) . '" class="ui green button right floated">' . esc_html__( 'Back to', 'zero-bs-crm' ) . ' ' . esc_html( $this->plural ) . '</a>';
378
379                                            ?>
380                                            </p>
381                                            </div>
382                                        </div>
383                                    </div>
384
385                                    <div class="two wide column"></div>
386
387                                    <?php
388
389                                break;
390
391                            case 1:
392                                // are you sure?
393                                ?>
394
395                                    <div class="two wide column"></div>
396
397                                    <div class="twelve wide column">
398
399                                        <input type="hidden" name="zbs-delete-id" value="<?php echo esc_attr( $this->objID ); ?>" />
400                                        <input type="hidden" name="zbs-delete-form-master" value="<?php echo esc_attr( $this->objTypeID ); ?>" />
401
402                                        <div class="ui icon big warning message">
403                                            <i class="trash icon"></i>
404                                            <div class="content">
405                                            <div class="header">
406                                                <?php esc_html_e( 'Are you sure?', 'zero-bs-crm' ); ?>
407                                            </div>
408                                            <p><?php echo esc_html__( 'Are you sure you want to delete this', 'zero-bs-crm' ) . ' ' . esc_html( $this->singular ) . '?'; ?></p>
409                                            <?php
410
411                                            $objectTypesWithChildren = array(
412                                                ZBS_TYPE_CONTACT,
413                                                ZBS_TYPE_COMPANY,
414                                            );
415
416                                            // contact needs extra check:
417                                            if ( in_array( $this->objTypeID, $objectTypesWithChildren ) ) {
418
419                                                // ouput explanation (what children will go)
420                                                switch ( $this->objTypeID ) {
421
422                                                    case ZBS_TYPE_CONTACT:
423                                                    case ZBS_TYPE_COMPANY:
424                                                        ?>
425                                                        <?php
426                                                        echo '<p>';
427                                                        esc_html_e( 'Shall I also delete the associated Contacts, Invoices, Quotes, Transactions, and Tasks?', 'zero-bs-crm' );
428                                                        echo '<br>';
429                                                        esc_html_e( '(This cannot be undone!)', 'zero-bs-crm' );
430                                                        echo '</p>';
431                                                        break;
432                                                }
433                                                ?>
434                                                    <p>
435                                                        <select name="zbs-delete-kill-children">
436                                                            <option value="no"><?php esc_html_e( 'No, leave them', 'zero-bs-crm' ); ?></option>
437                                                            <option value="yes"><?php esc_html_e( 'Yes, remove everything', 'zero-bs-crm' ); ?></option>
438                                                        </select>
439                                                    </p>
440                                                    <?php
441                                            }
442
443                                            ?>
444                                            <p>
445                                            <?php
446
447                                            // delete / back buttons
448                                            $backUrl = jpcrm_esc_link( 'edit', $this->objID, $this->objTypeID );
449
450                                                // output
451                                                echo '<button type="submit" class="ui orange button right floated"><i class="trash alternate icon"></i> ' . esc_html__( 'Delete', 'zero-bs-crm' ) . ' ' . esc_html( $this->singular ) . '</button>';
452                                                echo '<a href="' . esc_url( $backUrl ) . '" class="ui green button right floated"><i class="angle double left icon"></i> ' . esc_html__( 'Back to', 'zero-bs-crm' ) . ' ' . esc_html( $this->singular ) . ' (' . esc_html__( 'Cancel', 'zero-bs-crm' ) . ')</a>';
453
454                                            ?>
455                                            </p>
456                                            </div>
457                                        </div>
458                                    </div>
459
460                                    <div class="two wide column"></div>
461
462                                    <?php
463
464                                break;
465
466                            default:
467                                // smt broken!
468                                echo 'Error!';
469
470                                break;
471
472                        }
473                    } // / can delete
474
475                    ?>
476
477                </div>
478
479                <!-- could use this for mobile variant?) 
480                <div class="two column mobile only row" style="display:none"></div>
481                -->
482            </div> <!-- / mainlistview wrap -->
483
484            <input type="hidden" name="zbs-delete-nonce" value="<?php echo esc_attr( wp_create_nonce( 'delete-nonce' ) ); ?>" />
485        </form></div>
486
487        <script type="text/javascript">
488
489            jQuery(function($){
490                console.log("======= DELETE VIEW UI =========");
491            });
492
493            // General options for edit page
494            var zbsDeleteSettings = {
495
496                objid: <?php echo esc_html( $this->objID ); ?>,
497                objdbname: '<?php echo esc_html( $this->objType ); ?>'
498
499            };
500            var zbsObjectViewLinkPrefixCustomer = '<?php echo jpcrm_esc_link( 'view', -1, 'zerobs_customer', true ); ?>';
501            var zbsObjectEditLinkPrefixCustomer = '<?php echo jpcrm_esc_link( 'edit', -1, 'zerobs_customer', true ); ?>';
502            var zbsObjectViewLinkPrefixCompany = '<?php echo jpcrm_esc_link( 'view', -1, 'zerobs_company', true ); ?>';
503            var zbsListViewLink = '<?php echo jpcrm_esc_link( $this->listViewSlug ); ?>';
504            var zbsClick2CallType = parseInt('<?php echo esc_html( zeroBSCRM_getSetting( 'clicktocalltype' ) ); ?>');
505            var zbsEditViewLangLabels = {
506
507                    'today': '<?php echo esc_html( zeroBSCRM_slashOut( __( 'Today', 'zero-bs-crm' ) ) ); ?>',
508
509                    <?php
510                    $labelCount = 0;
511                    if ( count( $this->langLabels ) > 0 ) {
512                        foreach ( $this->langLabels as $labelK => $labelV ) {
513
514                            if ( $labelCount > 0 ) {
515                                echo ',';
516                            }
517
518                            echo esc_html( $labelK ) . ":'" . esc_html( zeroBSCRM_slashOut( $labelV ) ) . "'";
519
520                            ++$labelCount;
521
522                        }
523                    }
524                    ?>
525
526            };
527            <?php
528            #} Nonce for AJAX
529                    echo "var zbscrmjs_secToken = '" . esc_js( wp_create_nonce( 'zbscrmjs-ajax-nonce' ) ) . "';";
530            ?>
531                    </script>
532                    <?php
533    } // /draw func
534} // class