Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 364
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
zeroBSCRM_LogsMetaboxSetup
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
zeroBSCRM_permifyLogType
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
zeroBSCRM_setupLogTypes
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
72
zeroBS__Metabox_LogsV2
0.00% covered (danger)
0.00%
0 / 150
0.00% covered (danger)
0.00%
0 / 3
1332
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 html
0.00% covered (danger)
0.00%
0 / 148
0.00% covered (danger)
0.00%
0 / 1
1190
 save_data
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/*
3 * Jetpack CRM
4 * https://jetpackcrm.com
5 * V3.0
6 *
7 * Copyright 2020 Automattic
8 *
9 * Date: 20/02/2019
10 */
11defined( 'ZEROBSCRM_PATH' ) || exit( 0 );
12
13/*
14======================================================
15    Init Func
16    ====================================================== */
17
18function zeroBSCRM_LogsMetaboxSetup() {
19
20    // req. for custom log types
21    zeroBSCRM_setupLogTypes();
22}
23
24    add_action( 'after_zerobscrm_settings_init', 'zeroBSCRM_LogsMetaboxSetup' );
25
26/*
27======================================================
28    / Init Func
29    ====================================================== */
30
31/*
32======================================================
33    Declare Globals
34    ====================================================== */
35
36global $zeroBSCRM_logTypes; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
37$zeroBSCRM_logTypes = array( // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
38
39    'zerobs_customer' => array(
40
41        'note'                                  => array(
42            'label' => __( 'Note', 'zero-bs-crm' ),
43            'ico'   => 'fa-sticky-note-o',
44        ),
45        'call'                                  => array(
46            'label' => __( 'Call', 'zero-bs-crm' ),
47            'ico'   => 'fa-phone-square',
48        ),
49        'email'                                 => array(
50            'label' => __( 'Email', 'zero-bs-crm' ),
51            'ico'   => 'fa-envelope-o',
52        ),
53        'mail'                                  => array(
54            'label' => __( 'Mail', 'zero-bs-crm' ),
55            'ico'   => 'fa-envelope-o',
56        ),
57        'meeting'                               => array(
58            'label' => __( 'Meeting', 'zero-bs-crm' ),
59            'ico'   => 'fa-users',
60        ),
61        'quote__sent'                           => array(
62            'label' => __( 'Quote: Sent', 'zero-bs-crm' ),
63            'ico'   => 'fa-share-square-o',
64        ),
65        'quote__accepted'                       => array(
66            'label' => __( 'Quote: Accepted', 'zero-bs-crm' ),
67            'ico'   => 'fa-thumbs-o-up',
68        ),
69        'quote__refused'                        => array(
70            'label' => __( 'Quote: Refused', 'zero-bs-crm' ),
71            'ico'   => 'fa-ban',
72        ),
73        'invoice__sent'                         => array(
74            'label' => __( 'Invoice: Sent', 'zero-bs-crm' ),
75            'ico'   => 'fa-share-square-o',
76        ),
77        'invoice__part_paid'                    => array(
78            'label' => __( 'Invoice: Part Paid', 'zero-bs-crm' ),
79            'ico'   => 'fa-money',
80        ),
81        'invoice__paid'                         => array(
82            'label' => __( 'Invoice: Paid', 'zero-bs-crm' ),
83            'ico'   => 'fa-money',
84        ),
85        'invoice__refunded'                     => array(
86            'label' => __( 'Invoice: Refunded', 'zero-bs-crm' ),
87            'ico'   => 'fa-money',
88        ),
89        'transaction'                           => array(
90            'label' => __( 'Transaction', 'zero-bs-crm' ),
91            'ico'   => 'fa-credit-card',
92        ),
93        'feedback'                              => array(
94            'label' => __( 'Feedback', 'zero-bs-crm' ),
95            'ico'   => 'fa-commenting',
96        ),
97        'tweet'                                 => array(
98            'label' => __( 'Tweet', 'zero-bs-crm' ),
99            'ico'   => 'fa-twitter',
100        ),
101        'facebook_post'                         => array(
102            'label' => __( 'Facebook Post', 'zero-bs-crm' ),
103            'ico'   => 'fa-facebook-official',
104        ),
105        'other_contact'                         => array(
106            'label' => __( 'Other contact', 'zero-bs-crm' ),
107            'ico'   => 'fa-users',
108        ),
109        'created'                               => array(
110            'locked' => true,
111            'label'  => __( 'Created', 'zero-bs-crm' ),
112            'ico'    => 'fa-plus-circle',
113        ),
114        'updated'                               => array(
115            'locked' => true,
116            'label'  => __( 'Updated', 'zero-bs-crm' ),
117            'ico'    => 'fa-pencil-square-o',
118        ),
119        'quote_created'                         => array(
120            'locked' => true,
121            'label'  => __( 'Quote Created', 'zero-bs-crm' ),
122            'ico'    => 'fa-plus-circle',
123        ),
124        'invoice_created'                       => array(
125            'locked' => true,
126            'label'  => __( 'Invoice Created', 'zero-bs-crm' ),
127            'ico'    => 'fa-plus-circle',
128        ),
129        'event_created'                         => array(
130            'locked' => true,
131            'label'  => __( 'Task Created', 'zero-bs-crm' ),
132            'ico'    => 'fa-calendar',
133        ),
134        'task_created'                          => array(
135            'locked' => true,
136            'label'  => __( 'Task Created', 'zero-bs-crm' ),
137            'ico'    => 'fa-calendar',
138        ),
139        'transaction_created'                   => array(
140            'locked' => true,
141            'label'  => __( 'Transaction Created', 'zero-bs-crm' ),
142            'ico'    => 'fa-credit-card',
143        ),
144        'transaction_updated'                   => array(
145            'locked' => true,
146            'label'  => __( 'Transaction Updated', 'zero-bs-crm' ),
147            'ico'    => 'fa-credit-card',
148        ),
149        'transaction_deleted'                   => array(
150            'locked' => true,
151            'label'  => __( 'Transaction Deleted', 'zero-bs-crm' ),
152            'ico'    => 'fa-credit-card',
153        ),
154        'form_filled'                           => array(
155            'locked' => true,
156            'label'  => __( 'Form Filled', 'zero-bs-crm' ),
157            'ico'    => 'fa-wpforms',
158        ),
159        'api_action'                            => array(
160            'locked' => true,
161            'label'  => __( 'API Action', 'zero-bs-crm' ),
162            'ico'    => 'fa-random',
163        ),
164        'bulk_action__merge'                    => array(
165            'locked' => true,
166            'label'  => __( 'Bulk Action: Merge', 'zero-bs-crm' ),
167            'ico'    => 'fa-compress',
168        ),
169        'client_portal_user_created'            => array(
170            'locked' => true,
171            'label'  => __( 'Client Portal User Created', 'zero-bs-crm' ),
172            'ico'    => 'fa-id-card',
173        ),
174        'client_portal_access_changed'          => array(
175            'locked' => true,
176            'label'  => __( 'Client Portal Access Changed', 'zero-bs-crm' ),
177            'ico'    => 'fa-id-card',
178        ),
179        'status_change'                         => array(
180            'locked' => true,
181            'label'  => __( 'Status Change', 'zero-bs-crm' ),
182            'ico'    => 'fa-random',
183        ),
184        'contact_changed_details_via_portal'    => array(
185            'locked' => true,
186            'label'  => __( 'Contact Changed via Portal', 'zero-bs-crm' ),
187            'ico'    => 'fa-id-card',
188        ),
189        'contact_changed_details_via_wpprofile' => array(
190            'locked' => true,
191            'label'  => __( 'Contact Changed via WordPress Profile', 'zero-bs-crm' ),
192            'ico'    => 'fa-id-card',
193        ),
194        'contact_changed_details_via_woomyacc'  => array(
195            'locked' => true,
196            'label'  => __( 'Contact Changed via WooCommerce My Account', 'zero-bs-crm' ),
197            'ico'    => 'fa-id-card',
198        ),
199        'contact_changed_details_via_mailpoet'  => array(
200            'locked' => true,
201            'label'  => __( 'Contact Changed via MailPoet', 'zero-bs-crm' ),
202            'ico'    => 'fa-id-card',
203        ),
204        'subscriber_deleted_in_mailpoet'        => array(
205            'locked' => true,
206            'label'  => __( 'Subscriber deleted in MailPoet', 'zero-bs-crm' ),
207            'ico'    => 'fa-times',
208        ),
209        'contact_change_details_attempt'        => array(
210            'locked' => true,
211            'label'  => __( 'Attempted Contact detail change', 'zero-bs-crm' ),
212            'ico'    => 'fa-id-card',
213        ),
214
215    ),
216
217    'zerobs_company'  => array(
218
219        'note'    => array(
220            'label' => __( 'Note', 'zero-bs-crm' ),
221            'ico'   => 'fa-sticky-note-o',
222        ),
223        'call'    => array(
224            'label' => __( 'Call', 'zero-bs-crm' ),
225            'ico'   => 'fa-phone-square',
226        ),
227        'email'   => array(
228            'label' => __( 'Email', 'zero-bs-crm' ),
229            'ico'   => 'fa-envelope-o',
230        ),
231        'created' => array(
232            'locked' => true,
233            'label'  => __( 'Created', 'zero-bs-crm' ),
234            'ico'    => 'fa-plus-circle',
235        ),
236        'updated' => array(
237            'locked' => true,
238            'label'  => __( 'Updated', 'zero-bs-crm' ),
239            'ico'    => 'fa-pencil-square-o',
240        ),
241
242    ),
243
244);
245
246function zeroBSCRM_permifyLogType( $logTypeStr = '' ) {
247
248    return strtolower( str_replace( ' ', '_', str_replace( ':', '_', $logTypeStr ) ) );
249}
250
251function zeroBSCRM_setupLogTypes() {
252
253    global $zeroBSCRM_logTypes;
254
255    // hide log types for objects that are disabled
256    $hide_quotes       = zeroBSCRM_getSetting( 'feat_quotes' ) == -1;
257    $hide_invoices     = zeroBSCRM_getSetting( 'feat_invs' ) == -1;
258    $hide_transactions = zeroBSCRM_getSetting( 'feat_transactions' ) == -1;
259
260    foreach ( $zeroBSCRM_logTypes['zerobs_customer'] as $log_type => $log_type_value ) {
261        if (
262        $hide_quotes && str_starts_with( $log_type, 'quote' )
263        || $hide_invoices && str_starts_with( $log_type, 'invoice' )
264        || $hide_transactions && str_starts_with( $log_type, 'transaction' )
265        ) {
266            $zeroBSCRM_logTypes['zerobs_customer'][ $log_type ]['locked'] = true;
267        }
268    }
269
270    // apply filters
271    $zeroBSCRM_logTypes = apply_filters( 'zbs_logtype_array', $zeroBSCRM_logTypes );
272}
273
274/*
275======================================================
276    / Declare Globals
277    ====================================================== */
278
279/*
280======================================================
281    Logs (v3 DB3) Metabox
282    ====================================================== */
283
284class zeroBS__Metabox_LogsV2 extends zeroBS__Metabox {
285
286    public $objtypeid       = false; // child fills out e.g. ZBS_TYPE_CONTACT
287    public $metaboxLocation = 'normal';
288
289    /**
290     * The legacy object name (e.g. 'zerobs_customer')
291     *
292     * @var string
293     */
294    private $postType; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.PropertyNotSnakeCase
295
296    public function __construct( $plugin_file ) {
297
298        // call this
299        $this->initMetabox();
300    }
301
302    public function html( $obj, $metabox ) {
303
304        global $zbs;
305
306            // needs conversion to set this v3+
307            // // obj type (1 => zerobs_customer)
308            // we load from DAL defaults, if objTypeID passed (overriding anything passed, if empty/false)
309        if ( isset( $this->objtypeid ) ) {
310
311                $objTypeID = (int) $this->objtypeid;
312            if ( $objTypeID > 0 ) {
313
314                // obj type (1 => zerobs_customer)
315                $objTypeStr = $zbs->DAL->typeCPT( $objTypeID );
316                if ( ( ! isset( $this->postType ) || $this->postType == false ) && ! empty( $objTypeStr ) ) {
317                    $this->postType = $objTypeStr;
318                }
319            }
320        }
321
322            $objid = -1;
323        if ( is_array( $obj ) && isset( $obj['id'] ) ) {
324            $objid = $obj['id'];
325        }
326
327            #} Only load if is legit.
328            // if (in_array($this->postType,array('zerobs_customer'))){
329        if ( in_array( $this->objtypeid, array( ZBS_TYPE_CONTACT, ZBS_TYPE_COMPANY ) ) ) {
330
331                #} Proceed
332
333                #} Retrieve
334                $zbsLogs = $zbs->DAL->logs->getLogsForObj(
335                    array(
336
337                        'objtype'      => $this->objtypeid,
338                        'objid'        => $objid,
339
340                        'searchPhrase' => '',
341
342                        'incMeta'      => false,
343
344                        'sortByField'  => 'zbsl_created',
345                        'sortOrder'    => 'DESC',
346                        'page'         => 0,
347                        'perPage'      => 100,
348
349                        'ignoreowner'  => true,
350
351                    )
352                );
353
354            if ( ! is_array( $zbsLogs ) ) {
355                $zbsLogs = array();
356            }
357
358            ?>
359            <script type="text/javascript">var zbscrmjs_logsSecToken = '<?php echo esc_js( wp_create_nonce( 'zbscrmjs-ajax-nonce-logs' ) ); ?>';</script>
360
361                <table class="form-table wh-metatab wptbp" id="wptbpMetaBoxLogs">
362                    
363                    <tr>
364                                <td><h4><span id="zbsActiveLogCount"><?php echo esc_html( zeroBSCRM_prettifyLongInts( count( $zbsLogs ) ) ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase ?></span> <?php esc_html_e( 'Logs', 'zero-bs-crm' ); ?></h4></td>
365                                <td><button type="button" class="ui button black jpcrm-button" id="zbscrmAddLog"><?php esc_html_e( 'Add Log', 'zero-bs-crm' ); ?></button></td>
366                    </tr>
367
368                    <!-- this line will pop/close with "add log" button -->
369                    <tr id="zbsAddLogFormTR" style="display:none"><td colspan="2">
370
371
372                        <div id="zbsAddLogForm">
373
374                            <div id="zbsAddLogIco">
375                                <!-- this will change with select changing... -->
376                                <i class="fa fa-sticky-note-o" aria-hidden="true"></i>
377                            </div>
378
379                            <label for="zbsAddLogType"><?php esc_html_e( 'Activity Type', 'zero-bs-crm' ); ?>:</label>
380                            <select id="zbsAddLogType" class="form-control zbsUpdateTypeAdd">
381                            <?php
382                                global $zeroBSCRM_logTypes;
383                            if ( isset( $zeroBSCRM_logTypes[ $this->postType ] ) && count( $zeroBSCRM_logTypes[ $this->postType ] ) > 0 ) {
384                                foreach ( $zeroBSCRM_logTypes[ $this->postType ] as $logKey => $logType ) {
385
386                                    // not for locked logs
387                                    if ( isset( $logType['locked'] ) && $logType['locked'] ) {
388                                        // nope
389                                    } else {
390                                        ?>
391                                        <option value="<?php echo esc_attr( $logKey ); ?>"><?php esc_html_e( $logType['label'], 'zero-bs-crm' ); ?></option>
392                                        <?php
393                                    }
394                                }
395                            }
396
397                            ?>
398                            </select>
399
400                            <br />
401
402                            <label for="zbsAddLogMainDesc"><?php esc_html_e( 'Activity Description', 'zero-bs-crm' ); ?>:</label>
403                                                        <input type="text" class="form-control" id="zbsAddLogMainDesc" placeholder="e.g. <?php esc_attr_e( 'Called and talked to Todd about service x, seemed keen', 'zero-bs-crm' ); ?>" autocomplete="<?php echo esc_attr( jpcrm_disable_browser_autocomplete() ); ?>" />
404
405                            <label for="zbsAddLogDetailedDesc"><?php esc_html_e( 'Activity Detailed Notes', 'zero-bs-crm' ); ?>:</label>
406                                                        <textarea class="form-control" id="zbsAddLogDetailedDesc" autocomplete="<?php echo esc_attr( jpcrm_disable_browser_autocomplete() ); ?>"></textarea>
407
408                            <label for="zbsAddLogPinNote"><?php esc_html_e( 'Pin note', 'zero-bs-crm' ); ?>:</label>
409                            <input type="checkbox" id="zbsAddLogPinNote" />
410
411                            <div id="zbsAddLogActions">
412                                <div id="zbsAddLogUpdateMsg"></div>
413                                            <button type="button" class="jpcrm-button white-bg" id="zbscrmAddLogCancel"><?php esc_html_e( 'Cancel', 'zero-bs-crm' ); ?></button>
414                                            <button type="button" class="jpcrm-button" id="zbscrmAddLogSave"><?php esc_html_e( 'Save Log', 'zero-bs-crm' ); ?></button>
415                            </div>
416
417                        </div>
418
419
420
421                        <!-- edit log form is to be moved about by edit routines :) -->
422                        <div id="zbsEditLogForm">
423
424                            <div id="zbsEditLogIco">
425                                <!-- this will change with select changing... -->
426                                <i class="fa fa-sticky-note-o" aria-hidden="true"></i>
427                            </div>
428
429                            <label for="zbsEditLogType"><?php esc_html_e( 'Activity Type', 'zero-bs-crm' ); ?>:</label>
430                                                        <select id="zbsEditLogType" class="form-control zbsUpdateTypeEdit" autocomplete="<?php echo esc_attr( jpcrm_disable_browser_autocomplete() ); ?>">
431                                <?php
432                                global $zeroBSCRM_logTypes;
433                                if ( isset( $zeroBSCRM_logTypes[ $this->postType ] ) && count( $zeroBSCRM_logTypes[ $this->postType ] ) > 0 ) {
434                                    foreach ( $zeroBSCRM_logTypes[ $this->postType ] as $logKey => $logType ) {
435
436                                        // not for locked logs
437                                        if ( isset( $logType['locked'] ) && $logType['locked'] ) {
438                                            // nope
439                                        } else {
440                                            ?>
441                                        <option value="<?php echo esc_attr( $logKey ); ?>"><?php echo esc_html( $logType['label'] ); ?></option>
442                                            <?php
443                                        }
444                                    }
445                                }
446
447                                ?>
448                            </select>
449
450                            <br />
451
452                            <label for="zbsEditLogMainDesc"><?php esc_html_e( 'Activity Description', 'zero-bs-crm' ); ?>:</label>
453                                                        <input type="text" class="form-control" id="zbsEditLogMainDesc" placeholder="e.g. 'Called and talked to Todd about service x, seemed keen'" autocomplete="<?php echo esc_attr( jpcrm_disable_browser_autocomplete() ); ?>" />
454
455                            <label for="zbsEditLogDetailedDesc"><?php esc_html_e( 'Activity Detailed Notes', 'zero-bs-crm' ); ?>:</label>
456                                                        <textarea class="form-control" id="zbsEditLogDetailedDesc" autocomplete="<?php echo esc_attr( jpcrm_disable_browser_autocomplete() ); ?>"></textarea>
457
458                            <label for="zbsEditLogPinNote"><?php esc_html_e( 'Pin note', 'zero-bs-crm' ); ?>:</label>
459                            <input type="checkbox" id="zbsEditLogPinNote" />
460
461                            <div id="zbsEditLogActions">
462                                <div id="zbsEditLogUpdateMsg"></div>
463                                <button type="button" class="button button-info button-large" id="zbscrmEditLogCancel"><?php esc_html_e( 'Cancel', 'zero-bs-crm' ); ?></button>
464                                <button type="button" class="button button-primary button-large" id="zbscrmEditLogSave"><?php esc_html_e( 'Save Log', 'zero-bs-crm' ); ?></button>
465                            </div>
466
467                        </div>
468
469
470
471
472                    </td></tr>
473
474                    <tr><td colspan="2">
475
476                        <?php
477                        # Output logs (let JS do this!)
478
479                        #if (count($zbsLogs) > 0){ }
480
481                        ?>
482                        <div id="zbsAddLogOutputWrap"></div>
483
484
485                    </td></tr>
486
487                </table>
488
489
490            <style type="text/css">
491                #submitdiv {
492                    display:none;
493                }
494            </style>
495            <script type="text/javascript">
496
497                var zbsLogPerms = 
498                <?php
499                echo json_encode(
500                    array(
501                        'addedit' => zeroBSCRM_permsLogsAddEdit(),
502                        'delete'  => zeroBSCRM_permsLogsDelete(),
503                    )
504                );
505                ?>
506                                    ;
507
508                var zbsLogAgainstID = <?php echo esc_html( $objid ); ?>; var zbsLogProcessingBlocker = false;
509
510                <?php
511                if ( isset( $_GET['addlog'] ) && $_GET['addlog'] == '1' ) {
512
513                    // this just opens new log for those who've clicked through from another page
514                    echo 'var initialiseAddLog = true;';
515
516                }
517
518                #} Centralised log types :)
519                global $zeroBSCRM_logTypes;
520
521                #} Build array of locked logs
522                $lockedLogs = array();
523                if ( isset( $zeroBSCRM_logTypes[ $this->postType ] ) && count( $zeroBSCRM_logTypes[ $this->postType ] ) > 0 ) {
524                    foreach ( $zeroBSCRM_logTypes[ $this->postType ] as $logTypeKey => $logTypeDeet ) {
525                        if ( isset( $logTypeDeet['locked'] ) && $logTypeDeet['locked'] ) {
526                            $lockedLogs[ $logTypeKey ] = true;
527                        }
528                    }
529                }
530                echo 'var zbsLogsLocked = ' . json_encode( $lockedLogs ) . ';';
531
532                /*
533                var zbsLogsLocked = {
534                'created': true,
535                'updated': true,
536                'quote_created': true,
537                'invoice_created': true,
538                'form_filled': true
539
540                }; */
541
542                if ( isset( $zeroBSCRM_logTypes[ $this->postType ] ) && count( $zeroBSCRM_logTypes[ $this->postType ] ) > 0 ) {
543
544                    echo 'var zbsLogTypes = ' . json_encode( $zeroBSCRM_logTypes[ $this->postType ] ) . ';';
545
546                }
547                ?>
548
549                var zbsLogIndex = 
550                <?php
551
552                #} Array or empty
553                if ( count( $zbsLogs ) > 0 && is_array( $zbsLogs ) ) {
554
555                    $zbsLogsExpose = array();
556                    foreach ( $zbsLogs as $zbsLog ) {
557
558                        $retLine = $zbsLog;
559                        if ( isset( $retLine ) && isset( $retLine['longdesc'] ) ) {
560                            $retLine['longdesc'] = wp_kses( html_entity_decode( $retLine['longdesc'], ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 ), $zbs->acceptable_restricted_html );
561                        }
562
563                        $zbsLogsExpose[] = $retLine;
564
565                    }
566
567                    echo json_encode( $zbsLogsExpose );
568                } else {
569                    echo json_encode( array() );
570                }
571                ?>
572                ;
573
574                var zbsLogEditing = -1;
575
576                // def ico
577                var zbsLogDefIco = 'fa-sticky-note-o'; 
578
579                jQuery(function(){
580
581                    // build log ui
582                    zbscrmjs_buildLogs();
583
584                    // if url has addlogs=1, init open addlogs :)
585                    // passed down from php via var higher up in this file
586                    setTimeout(function(){
587                        if (typeof window.initialiseAddLog != "undefined"){
588                            jQuery('#zbscrmAddLog').trigger( 'click' );
589                        }
590                    },500);
591
592                    // add log button
593                    jQuery('#zbscrmAddLog').on( 'click', function(){
594
595
596                        if (jQuery(this).css('display') == 'block'){
597
598                            jQuery('#zbsAddLogFormTR').slideDown('400', function() {
599                                
600                            });
601
602                            jQuery(this).hide();
603
604                        } else {
605
606                            jQuery('#zbsAddLogFormTR').hide();
607
608                            jQuery(this).show();
609
610                        }
611
612
613                    });
614
615                    // cancel
616                    jQuery('#zbscrmAddLogCancel').on( 'click', function(){
617
618                            jQuery('#zbsAddLogFormTR').hide();
619
620                            jQuery('#zbscrmAddLog').show();
621
622                    });
623
624                    // save
625                    jQuery('#zbscrmAddLogSave').on( 'click', function(){
626
627                            //jQuery('#zbsAddLogFormTR').hide();
628
629                            //jQuery('#zbscrmAddLog').show();
630
631                            /* 
632                            zbsnagainstid
633                            zbsntype
634                            zbsnshortdesc
635                            zbsnlongdesc
636                            zbsnoverwriteid
637                            pinned
638                            */
639
640                            // get / check data
641                            var data = {sec:window.zbscrmjs_logsSecToken,zbsnobjtype:'<?php echo esc_html( $this->postType ); ?>'}; var errs = 0;
642                            if ((jQuery('#zbsAddLogType').val()).length > 0) data.zbsntype = jQuery('#zbsAddLogType').val();
643                            if ((jQuery('#zbsAddLogMainDesc').val()).length > 0) data.zbsnshortdesc = jQuery('#zbsAddLogMainDesc').val();
644                            if ((jQuery('#zbsAddLogDetailedDesc').val()).length > 0) 
645                                data.zbsnlongdesc = jQuery('#zbsAddLogDetailedDesc').val();
646                            else
647                                data.zbsnlongdesc = '';
648
649                            if (jQuery('#zbsAddLogPinNote').is(':checked')) {
650                                data.pinned = true;
651                            }
652
653                            // post id & no need for overwrite id as is new
654                            data.zbsnagainstid = parseInt(window.zbsLogAgainstID);
655
656                            // debug console.log('posting new note: ',data);
657
658                            // validate
659                            var msgOut = '';
660                            if (typeof data.zbsntype == "undefined" || data.zbsntype == '') {
661                                errs++;
662                                msgOut = 'Note Type is required!'; 
663                                jQuery('#zbsAddLogType').css('border','2px solid orange');
664                                setTimeout(function(){
665
666                                    jQuery('#zbsAddLogUpdateMsg').html('');
667                                    jQuery('#zbsAddLogType').css('border','1px solid #ddd');
668
669                                },1500);
670                            }
671                            if (typeof data.zbsnshortdesc == "undefined" || data.zbsnshortdesc == '') {
672                                errs++;
673                                if (msgOut == 'Note Type is required!') 
674                                    msgOut = 'Note Type and Description are required!'; 
675                                else
676                                    msgOut += 'Note Description is required!'; 
677                                jQuery('#zbsAddLogMainDesc').css('border','2px solid orange');
678                                setTimeout(function(){
679
680                                    jQuery('#zbsAddLogUpdateMsg').html('');
681                                    jQuery('#zbsAddLogMainDesc').css('border','1px solid #ddd');
682
683                                },1500);
684                            }
685
686                            if (errs === 0){
687
688                                // add action
689                                data.action = 'zbsaddlog';
690
691                                zbscrmjs_addNewNote(data,function(newLog){
692
693                                    // success
694
695                                        // msg
696                                        jQuery('#zbsAddLogUpdateMsg').html('Saved!');
697
698                                        // then hide form, build new log gui, clear form
699
700                                            // hide + clear form
701                                            jQuery('#zbsAddLogFormTR').hide();
702                                            jQuery('#zbscrmAddLog').show();
703                                            jQuery('#zbsAddLogType').val('note');
704                                            jQuery('#zbsAddLogMainDesc').val('');
705                                            jQuery('#zbsAddLogDetailedDesc').val('');
706                                            jQuery('#zbsAddLogPinNote').prop('checked', false);
707                                            jQuery('#zbsAddLogUpdateMsg').html('');
708
709                                        // add it (build example obj)
710                                        var newLogObj = {
711                                            id: newLog.logID,
712                                            created: '', //moment(),
713                                            type: newLog.zbsntype,
714                                            shortdesc: newLog.zbsnshortdesc,
715                                            longdesc: zbscrmjs_nl2br(newLog.zbsnlongdesc)
716                                        }
717                                        if (newLog.pinned) {
718                                            newLogObj.pinned = true;
719                                        }
720                                        zbscrmjs_addNewNoteLine(newLogObj,true);
721
722                                        // also add to window obj
723                                        window.zbsLogIndex.push(newLogObj);
724
725
726                                        // bind ui
727                                        setTimeout(function(){
728                                            zbscrmjs_bindNoteUIJS();
729                                            zbscrmjs_updateLogCount();
730                                        },0);
731
732
733                                },function(){
734
735                                    // failure
736
737                                        // msg + do nothing
738                                        jQuery('#zbsAddLogUpdateMsg').html('There was an error when saving this note!');
739
740                                });
741
742                            } else {
743                                if (typeof msgOut !== "undefined" && msgOut != '') jQuery('#zbsAddLogUpdateMsg').html(msgOut); 
744                            }
745
746                    });
747
748
749                    // note ico - works for both edit + add
750                    jQuery('#zbsAddLogType, #zbsEditLogType').on( 'change', function(){
751
752                        // get perm
753                        var logPerm = zbscrmjs_permify(jQuery(this).val()); // jQuery('#zbsAddLogType').val()
754
755                        var thisIco = window.zbsLogDefIco;
756                        // find ico
757                        if (typeof window.zbsLogTypes[logPerm] != "undefined") thisIco = window.zbsLogTypes[logPerm].ico;
758
759                        // override all existing classes with ones we want:
760                        if (jQuery(this).hasClass('zbsUpdateTypeAdd')) jQuery('#zbsAddLogIco i').attr('class','fa ' + thisIco);
761                        if (jQuery(this).hasClass('zbsUpdateTypeEdit')) jQuery('#zbsEditLogIco i').attr('class','fa ' + thisIco);
762
763                    });
764
765
766                });
767
768                function zbscrmjs_updateLogCount(){
769
770                    var count = 0; 
771                    if (window.zbsLogIndex.length > 0) count = parseInt(window.zbsLogIndex.length);
772                    jQuery('#zbsActiveLogCount').html(zbscrmjs_prettifyLongInts(count));
773
774                }
775
776                // build log ui
777                function zbscrmjs_buildLogs(){
778
779                    // get from obj
780                    var theseLogs = window.zbsLogIndex;
781
782
783                    jQuery.each(theseLogs,function(ind,ele){
784
785                        zbscrmjs_addNewNoteLine(ele);
786
787                    });
788
789                    // bind ui
790                    setTimeout(function(){
791                        zbscrmjs_bindNoteUIJS();
792                        zbscrmjs_updateLogCount();
793                    },0);
794
795                }
796
797                function zbscrmjs_addNewNoteLine(ele,prepflag,replaceExisting){
798
799                        // localise
800                        var logMeta = ele; if (typeof ele.meta != "undefined") logMeta = ele.meta;
801
802                        // get perm
803                        var logPerm = zbscrmjs_permify(logMeta.type);
804
805                        // classes (pinned)
806                        var classes = '';
807                        if ( typeof ele.pinned !== "undefined" && ele.pinned ){
808                            classes = ' jpcrm-pinned';
809                        }
810
811                        // build it
812                        var thisLogHTML = '<div class="zbsLogOut' + classes + '" data-logid="' + ele.id + '" id="zbsLogOutLine' + ele.id + '" data-logtype="' + logPerm + '">';
813
814
815                            // type ico
816                                
817                                var thisIco = window.zbsLogDefIco;
818                                // find ico
819                                if (typeof window.zbsLogTypes[logPerm] != "undefined") thisIco = window.zbsLogTypes[logPerm].ico;
820
821                                // output
822                                thisLogHTML += '<div class="zbsLogOutIco"><i class="fa ' + thisIco + '" aria-hidden="true"></i></div>';
823
824
825                            // created date
826                            if (typeof ele.created !== "undefined" && ele.created !== '') {
827                                
828                                // not req: var offsetStr = zbscrmjs_getTimeZoneOffset();
829                                // date, inc any timezone offset as set in wp: window.zbs_root.timezone_offset
830                                //console.log("Creating moment bare",[moment(ele.created + ' ' + offsetStr, 'YYYY-MM-DD HH:mm:ss Z'), moment(ele.created + ' ' + offsetStr, 'YYYY-MM-DD HH:mm:ss Z','en'), offsetStr, moment(ele.created, 'YYYY-MM-DD HH:mm:ss').utcOffset(offsetStr)]);
831                                //var createdMoment = moment(ele.created + ' ' + offsetStr, 'YYYY-MM-DD HH:mm:ss Z', 'en');
832                                //var createdMoment = moment(ele.created, 'YYYY-MM-DD HH:mm:ss').utcOffset(offsetStr);
833                                //var nowAdjusted = moment(); //.utcOffset(offsetStr);
834                                //console.log("compare",[createdMoment.format(),nowAdjusted.format(),createdMoment.from(nowAdjusted),createdMoment.fromNow()]);
835
836                                // this works best in the end, just add / - any offset
837                                var createdMoment = moment.unix(ele.createduts);
838                                thisLogHTML += '<div class="zbsLogOutCreated" data-zbscreated="' + ele.created + '" title="' + createdMoment.format('lll') + '">' + createdMoment.fromNow() + '</div>';
839
840                            } else {
841
842                                // empty created means just created obj
843                                var createdMoment = moment();
844                                thisLogHTML += '<div class="zbsLogOutCreated" data-zbscreated="' + createdMoment + '" title="' + createdMoment.format('lll') + '">' + createdMoment.fromNow() + '</div>';
845
846                            }
847
848                            // title
849
850                                var thisTitle = '';
851
852                                // find type
853                                var thisType = ucwords(logMeta.type);
854                                if (typeof window.zbsLogTypes[logPerm] != "undefined") thisType = window.zbsLogTypes[logPerm].label;
855
856                                // type
857                                if (typeof thisType !== "undefined") thisTitle += '<span>' + thisType + '</span>';
858
859                                // desc
860                                if (typeof logMeta.shortdesc !== "undefined") {
861
862                                    if (thisTitle != '') thisTitle += ': ';
863                                    thisTitle += jpcrm_strip_scripts(logMeta.shortdesc);
864
865                                }
866
867                                var logEditElements = '<div class="zbsLogOutEdits"><i class="fa fa-pencil-square-o zbsLogActionEdit" title="<?php esc_attr_e( 'Edit Log', 'zero-bs-crm' ); ?>"></i><i class="fa fa-thumb-tack jpcrm_log_pin" title="<?php esc_attr_e( 'Pin log to contact', 'zero-bs-crm' ); ?>"></i><i class="fa fa-thumb-tack jpcrm_log_unpin" title="<?php esc_attr_e( 'Unpin log from contact', 'zero-bs-crm' ); ?>"></i><i class="fa fa-trash-o zbsLogActionRemove last" title="<?php esc_attr_e( 'Delete Log', 'zero-bs-crm' ); ?>"></i><span></span></div>';
868                                thisLogHTML += '<div class="zbsLogOutTitle">' + thisTitle + logEditElements + '</div>';
869
870                            // desc
871                            if (typeof logMeta.longdesc !== "undefined" && logMeta.longdesc !== '' && logMeta.longdesc !== null) thisLogHTML += '<div class="zbsLogOutDesc">' + jpcrm_strip_scripts(logMeta.longdesc) + '</div>';
872
873                            thisLogHTML += '</div>';                            
874
875
876                        if (typeof replaceExisting == "undefined"){
877
878                            // normal
879
880                            // add it
881                            if (typeof prepflag !== "undefined")
882                                jQuery('#zbsAddLogOutputWrap').prepend(thisLogHTML);
883                            else
884                                jQuery('#zbsAddLogOutputWrap').append(thisLogHTML);
885
886
887                        } else {
888
889                            // replace existing
890                            jQuery('#zbsLogOutLine' + ele.id).replaceWith(thisLogHTML);
891
892                        }
893
894                }
895
896                function zbscrmjs_bindNoteUIJS(){
897
898                    // show hide edit controls
899                    jQuery('.zbsLogOut').on( 'mouseenter', function(){
900
901                        var logType = jQuery(this).attr('data-logtype');
902
903                        // only if not editing another :) + Log Type is not one we don't have in our set
904                        if (window.zbsLogEditing == -1 && typeof window.zbsLogTypes[logType] != "undefined"){
905
906                            // check if locked log or not! 
907                            if (typeof logType == "undefined") logType = '';
908
909                            // if log type empty, or has a key in window.zbsLogsLocked, don't allow edits
910                            // ... and finally check perms too 
911                            if (
912                                logType != '' && !window.zbsLogsLocked.hasOwnProperty(logType) && 
913                                window.zbsLogPerms.addedit // can add edit
914                                ){
915
916                                    // check if can delete
917                                    if (window.zbsLogPerms.delete){
918                                        // can
919                                        jQuery('.zbsLogActionRemove',jQuery(this)).css('display','inline-block');
920
921                                    } else {
922                                        // can't
923                                        jQuery('.zbsLogActionRemove',jQuery(this)).css('display','none');
924                                    }
925                                    
926                                // yep (overall)
927                                jQuery('.zbsLogOutEdits',jQuery(this)).css('display','inline-block');
928
929                            }
930
931                        }
932
933                    }).on( 'mouseleave', function(){
934
935                        jQuery('.zbsLogOutEdits',jQuery(this)).not('.stayhovered').css('display','none');
936
937                    });
938
939                    // bind del
940                    jQuery('.zbsLogOutEdits .zbsLogActionRemove').off('click').on( 'click', function(){
941
942                        if (window.zbsLogPerms.delete){
943
944                            // append "deleting"
945                            jQuery(this).closest('.zbsLogOutEdits').addClass('stayhovered');
946                            jQuery('span',jQuery(this).closest('.zbsLogOutEdits')).html('<?php esc_html_e( 'Deleting', 'zero-bs-crm' ); ?>...');
947
948                            var noteID = parseInt(jQuery(this).closest('.zbsLogOut').attr('data-logid'));
949
950                            if (noteID > 0){
951
952                                var thisEle = this;
953
954                                zbscrmjs_deleteNote(noteID,function(){
955
956                                    // success
957
958                                        // localise
959                                        var nID = noteID;
960
961                                        // append "deleted" and then vanish
962                                        jQuery('span',jQuery(thisEle).closest('.zbsLogOutEdits')).html('Deleted!...');
963
964                                        var that = thisEle;
965                                        setTimeout(function(){
966
967                                            // localise
968                                            var thisNoteID = nID;
969
970                                            // also del from window obj
971                                            zbscrmjs_removeItemFromLogIndx(thisNoteID);
972
973                                            // update count span
974                                            zbscrmjs_updateLogCount();
975
976                                            // slide up
977                                            jQuery(that).closest('.zbsLogOut').slideUp(400,function(){
978
979                                                // and remove itself?
980
981                                            });
982                                        },500);
983
984                                },function(){
985
986                                    //TODO: proper error msg
987                                    console.error('There was an issue retrieving this note for editing/deleting'); 
988
989                                });
990
991                            } else console.error('There was an issue retrieving this note for editing/deleting'); //TODO: proper error msg
992
993                        } // if perms
994
995                    });
996
997                    // bind pin
998                    jQuery('.zbsLogOutEdits .jpcrm_log_pin').off('click').on( 'click', function(){
999
1000                        // append "pinning"
1001                        jQuery(this).closest('.zbsLogOutEdits').addClass('stayhovered');
1002                        jQuery('span',jQuery(this).closest('.zbsLogOutEdits')).html('<?php esc_html_e( 'Pinning', 'zero-bs-crm' ); ?>...');
1003
1004                        var noteID = parseInt(jQuery(this).closest('.zbsLogOut').attr('data-logid'));
1005
1006                        if (noteID > 0){
1007
1008                            var thisEle = this;
1009
1010                            jpcrm_js_pin_note( noteID, function(){
1011
1012                                // success
1013
1014                                    // localise
1015                                    var nID = noteID;
1016
1017                                    // append "pinned"
1018                                    jQuery('span',jQuery(thisEle).closest('.zbsLogOutEdits')).html('<?php esc_html_e( 'Pinned', 'zero-bs-crm' ); ?>');
1019
1020                                    var that = thisEle;
1021                                    setTimeout(function(){
1022
1023                                        // add pinned
1024                                        jQuery(that).closest('.zbsLogOut').addClass( 'jpcrm-pinned' );
1025                                        jQuery('span',jQuery(that).closest('.zbsLogOutEdits')).html('');
1026                                        jQuery(that).closest('.zbsLogOutEdits').removeClass( 'stayhovered' );
1027                                        jQuery(that).closest('.zbsLogOutEdits').css('display','none');
1028
1029
1030                                    },500);
1031
1032                            },function(){
1033
1034                                //TODO: proper error msg
1035                                console.error('There was an issue pinning this log'); 
1036
1037                            });
1038
1039                        } else console.error('There was an issue pinning this log'); //TODO: proper error msg
1040
1041                    });
1042
1043
1044                    // bind unpin
1045                    jQuery('.zbsLogOutEdits .jpcrm_log_unpin').off('click').on( 'click', function(){
1046
1047                        // append "pinning"
1048                        jQuery(this).closest('.zbsLogOutEdits').addClass('stayhovered');
1049                        jQuery('span',jQuery(this).closest('.zbsLogOutEdits')).html('<?php esc_html_e( 'Removing Pin', 'zero-bs-crm' ); ?>...');
1050
1051                        var noteID = parseInt(jQuery(this).closest('.zbsLogOut').attr('data-logid'));
1052
1053                        if (noteID > 0){
1054
1055                            var thisEle = this;
1056
1057                            jpcrm_js_unpin_note( noteID, function(){
1058
1059                                // success
1060
1061                                    // localise
1062                                    var nID = noteID;
1063
1064                                    // append "pinned"
1065                                    jQuery('span',jQuery(thisEle).closest('.zbsLogOutEdits')).html('<?php esc_html_e( 'Unpinned', 'zero-bs-crm' ); ?>');
1066
1067                                    var that = thisEle;
1068                                    setTimeout(function(){
1069
1070                                        // remove pinned
1071                                        jQuery(that).closest('.zbsLogOut').removeClass( 'jpcrm-pinned' );
1072                                        jQuery('span',jQuery(that).closest('.zbsLogOutEdits')).html('');
1073                                        jQuery(that).closest('.zbsLogOutEdits').removeClass( 'stayhovered' );
1074                                        jQuery(that).closest('.zbsLogOutEdits').css('display','none');
1075
1076
1077                                    },500);
1078
1079                            },function(){
1080
1081                                //TODO: proper error msg
1082                                console.error('There was an issue pinning this log'); 
1083
1084                            });
1085
1086                        } else console.error('There was an issue pinning this log'); //TODO: proper error msg
1087
1088                    });
1089
1090                    // bind edit
1091                    jQuery('.zbsLogOutEdits .zbsLogActionEdit').off('click').on( 'click', function(){
1092
1093                        if (window.zbsLogPerms.addedit){
1094
1095                            // one at a time please sir...
1096                            if (window.zbsLogEditing == -1){
1097
1098                                // get edit id
1099                                var noteID = parseInt(jQuery(this).closest('.zbsLogOut').attr('data-logid'));
1100
1101                                // get edit obj
1102                                var editObj = zbscrmjs_retrieveItemFromIndex(noteID);
1103
1104                                // move edit box to before here
1105                                jQuery('#zbsEditLogForm').insertBefore('#zbsLogOutLine' + noteID);
1106
1107                                setTimeout(function(){
1108
1109                                    var lObj = editObj;
1110                                    if (typeof lObj.meta != "undefined") lObj = lObj.meta; // pre dal2
1111
1112                                    // update edit box texts etc.
1113                                    jQuery('#zbsEditLogMainDesc').val(lObj.shortdesc);
1114                                    jQuery('#zbsEditLogDetailedDesc').val(zbscrmjs_reversenl2br(lObj.longdesc));
1115                                    jQuery('#zbsEditLogPinNote').prop('checked', lObj.pinned);
1116                                    jQuery('#zbsEditLogType option').each(function(){
1117                                        if (zbscrmjs_permify(jQuery(this).text()) == lObj.type) {
1118                                            jQuery(this).attr('selected', 'selected');
1119                                            return false;
1120                                        }
1121                                        return true;
1122                                    });
1123                                
1124                                    // type ico
1125
1126                                        // get perm
1127                                        var logPerm = zbscrmjs_permify(lObj.type);
1128                                    
1129                                        var thisIco = window.zbsLogDefIco;
1130                                        // find ico
1131                                        if (typeof window.zbsLogTypes[logPerm] != "undefined") thisIco = window.zbsLogTypes[logPerm].ico;
1132
1133                                        // update
1134                                        jQuery('#zbsEditLogIco i').attr('class','fa ' + thisIco);
1135
1136
1137                                },10);
1138
1139                                // set edit vars
1140                                window.zbsLogEditing = noteID;
1141
1142                                // hide line / show edit
1143                                jQuery('#zbsLogOutLine' + noteID).slideUp();
1144                                jQuery('#zbsEditLogForm').slideDown();
1145
1146                                // bind
1147                                zbscrmjs_bindEditNote();
1148
1149                            }
1150                       
1151                        } // if perms
1152
1153                    });
1154
1155                }
1156
1157                function zbscrmjs_bindEditNote(){
1158
1159
1160                        // cancel
1161                        jQuery('#zbscrmEditLogCancel').on( 'click', function(){
1162
1163                                // get note id
1164                                var noteID = window.zbsLogEditing;
1165
1166                                // hide edit from
1167                                jQuery('#zbsEditLogForm').hide();
1168
1169                                // show back log
1170                                jQuery('#zbsLogOutLine' + noteID).show();
1171
1172                                // unset noteID
1173                                window.zbsLogEditing = -1;
1174
1175                        });
1176
1177                        // save
1178                        jQuery('#zbscrmEditLogSave').on( 'click', function(){
1179
1180                                if (window.zbsLogEditing > -1){
1181
1182                                        // get note id
1183                                        var noteID = window.zbsLogEditing;
1184
1185                                        //jQuery('#zbsEditLogFormTR').hide();
1186
1187                                        //jQuery('#zbscrmEditLog').show();
1188
1189                                        /* 
1190                                        zbsnagainstid
1191                                        zbsntype
1192                                        zbsnshortdesc            
1193                                        zbsnlongdesc
1194                                        zbsnoverwriteid
1195                                        */
1196
1197                                        // get / check data
1198                                        var data = {sec:window.zbscrmjs_logsSecToken,zbsnobjtype:'<?php echo $this->postType; ?>'}; var errs = 0;
1199
1200                                        // same as add code, but with note id:
1201                                        data.zbsnprevid = noteID;
1202
1203                                        if ((jQuery('#zbsEditLogType').val()).length > 0) data.zbsntype = jQuery('#zbsEditLogType').val();
1204                                        if ((jQuery('#zbsEditLogMainDesc').val()).length > 0) data.zbsnshortdesc = jQuery('#zbsEditLogMainDesc').val();
1205                                        if ((jQuery('#zbsEditLogDetailedDesc').val()).length > 0) 
1206                                            data.zbsnlongdesc = jQuery('#zbsEditLogDetailedDesc').val();
1207                                        else
1208                                            data.zbsnlongdesc = '';
1209
1210                                        if (jQuery('#zbsEditLogPinNote').is(':checked')) {
1211                                            data.pinned = true;
1212                                        }
1213
1214                                        // post id & no need for overwrite id as is new
1215                                        data.zbsnagainstid = parseInt(window.zbsLogAgainstID);
1216
1217                                        // validate
1218                                        var msgOut = '';
1219                                        if (typeof data.zbsntype == "undefined" || data.zbsntype == '') {
1220                                            errs++;
1221                                            msgOut = 'Note Type is required!'; 
1222                                            jQuery('#zbsEditLogType').css('border','2px solid orange');
1223                                            setTimeout(function(){
1224
1225                                                jQuery('#zbsEditLogUpdateMsg').html('');
1226                                                jQuery('#zbsEditLogType').css('border','1px solid #ddd');
1227
1228                                            },1500);
1229                                        }
1230                                        if (typeof data.zbsnshortdesc == "undefined" || data.zbsnshortdesc == '') {
1231                                            errs++;
1232                                            if (msgOut == 'Note Type is required!') 
1233                                                msgOut = 'Note Type and Description are required!'; 
1234                                            else
1235                                                msgOut += 'Note Description is required!'; 
1236                                            jQuery('#zbsEditLogMainDesc').css('border','2px solid orange');
1237                                            setTimeout(function(){
1238
1239                                                jQuery('#zbsEditLogUpdateMsg').html('');
1240                                                jQuery('#zbsEditLogMainDesc').css('border','1px solid #ddd');
1241
1242                                            },1500);
1243                                        }
1244
1245
1246                                        if (errs === 0){
1247
1248                                            // add action
1249                                            data.action = 'zbsupdatelog';
1250                                            zbscrmjs_updateNote(data,function(newLog){
1251
1252                                                // success
1253
1254                                                    // msg
1255                                                    jQuery('#zbsEditLogUpdateMsg').html('Changes Saved!');
1256
1257                                                    // then hide form, build new log gui, clear form
1258
1259                                                        // hide + clear form
1260                                                        jQuery('#zbsEditLogForm').hide();
1261                                                        jQuery('#zbsEditLogType').val('Note');
1262                                                        jQuery('#zbsEditLogMainDesc').val('');
1263                                                        jQuery('#zbsEditLogDetailedDesc').val('');
1264                                                        jQuery('#zbsEditLogPinNote').prop('checked', false);
1265                                                        jQuery('#zbsEditLogUpdateMsg').html('');
1266
1267                                                    // update it (build example obj)
1268                                                    var newLogObj = {
1269                                                        id: newLog.logID,
1270                                                        created: '',
1271                                                        meta: {
1272
1273                                                            type: newLog.zbsntype,
1274                                                            shortdesc: newLog.zbsnshortdesc,
1275                                                            // have to replace the nl2br for long desc:
1276                                                            longdesc: zbscrmjs_nl2br(newLog.zbsnlongdesc)
1277
1278                                                        }
1279                                                    }
1280                                                    if (newLog.pinned) {
1281                                                        newLogObj.pinned = true;
1282                                                    }
1283                                                    zbscrmjs_addNewNoteLine(newLogObj,true,true); // third param here is "replace existing"
1284
1285                                                    // also add to window obj in prev place
1286                                                    //window.zbsLogIndex.push(newLogObj);
1287                                                    zbscrmjs_replaceItemInLogIndx(newLog.logID,newLogObj);
1288
1289                                                    // unset noteID
1290                                                    window.zbsLogEditing = -1;
1291
1292                                                    // bind ui
1293                                                    setTimeout(function(){
1294                                                        zbscrmjs_bindNoteUIJS();
1295                                                        zbscrmjs_updateLogCount();
1296                                                    },0);
1297
1298
1299                                            },function(){
1300
1301                                                // failure
1302
1303                                                    // msg + do nothing
1304                                                    jQuery('#zbsEditLogUpdateMsg').html('There was an error when saving this note!');
1305
1306                                            });
1307
1308                                        } else {
1309                                            if (typeof msgOut !== "undefined" && msgOut != '') jQuery('#zbsEditLogUpdateMsg').html(msgOut); 
1310                                        }
1311
1312
1313                                } // if note id
1314
1315                        });
1316
1317
1318                }
1319
1320                function zbscrmjs_removeItemFromLogIndx(noteID){
1321
1322                    var logIndex = window.zbsLogIndex;
1323                    var newLogIndex = [];
1324
1325                    jQuery.each(logIndex,function(ind,ele){
1326
1327                        if (typeof ele.id != "undefined" && ele.id != noteID) newLogIndex.push(ele);
1328
1329                    });
1330
1331                    window.zbsLogIndex = newLogIndex;
1332
1333                    // fini
1334                    return window.zbsLogIndex;
1335
1336                }
1337
1338                function zbscrmjs_replaceItemInLogIndx(noteIDToReplace,newObj){
1339
1340                    var logIndex = window.zbsLogIndex;
1341                    var newLogIndex = [];
1342
1343                    jQuery.each(logIndex,function(ind,ele){
1344
1345                        if (typeof ele.id != "undefined")
1346                            if (ele.id != noteIDToReplace) 
1347                                newLogIndex.push(ele);
1348                            else
1349                                // is to replace
1350                                newLogIndex.push(newObj);
1351
1352                    });
1353
1354                    window.zbsLogIndex = newLogIndex;
1355
1356                    // fini
1357                    return window.zbsLogIndex;
1358
1359                }
1360
1361                function zbscrmjs_retrieveItemFromIndex(noteID){
1362
1363                    var logIndex = window.zbsLogIndex;
1364                    var logObj = -1;
1365
1366                    jQuery.each(logIndex,function(ind,ele){
1367
1368                        if (typeof ele.id != "undefined" && ele.id == noteID) logObj = ele;
1369
1370                    });
1371
1372                    return logObj;
1373                }
1374
1375                
1376
1377                // function assumes a legit dataArr :) (validate above)
1378                function zbscrmjs_addNewNote(dataArr,cb,errcb){
1379                    
1380                    // needs nonce. <!--#NONCENEEDED -->
1381
1382                    if (!window.zbsLogProcessingBlocker){
1383                        
1384                        // blocker
1385                        window.zbsLogProcessingBlocker = true;
1386
1387                        // msg
1388                        jQuery('#zbsAddLogUpdateMsg').html('<?php esc_html_e( 'Saving...', 'zero-bs-crm' ); ?>');
1389
1390                        // Send 
1391                            jQuery.ajax({
1392                                    type: "POST",
1393                                    url: ajaxurl, // admin side is just ajaxurl not wptbpAJAX.ajaxurl,
1394                                    "data": dataArr,
1395                                    dataType: 'json',
1396                                    timeout: 20000,
1397                                    success: function(response) {
1398
1399                                    // Debug  console.log("RESPONSE",response);
1400
1401                                    // blocker
1402                                    window.zbsLogProcessingBlocker = false;
1403
1404                                    // this also has true/false on update... 
1405                                    if (typeof response.processed != "undefined" && response.processed){
1406
1407                                        // callback
1408                                        // make a merged item... 
1409                                        var retArr = dataArr; dataArr.logID = response.processed;
1410                                        if (typeof cb == "function") cb(retArr);
1411
1412                                    } else {
1413
1414                                        // .. was an error :)
1415
1416                                        // callback
1417                                        if (typeof errcb == "function") errcb(response);
1418
1419                                    }
1420
1421
1422                                    },
1423                                    error: function(response){ 
1424
1425                                    // Debug  console.error("RESPONSE",response);
1426
1427                                    // blocker
1428                                    window.zbsLogProcessingBlocker = false;
1429
1430                                    // callback
1431                                    if (typeof errcb == "function") errcb(response);
1432
1433
1434
1435                                    }
1436
1437                            });
1438
1439
1440                    } else {
1441                        
1442                        // end of blocker
1443                        jQuery('#zbsAddLogUpdateMsg').html('... already processing!');
1444                        setTimeout(function(){
1445
1446                            jQuery('#zbsAddLogUpdateMsg').html('');
1447
1448                        },2000);
1449
1450                    }
1451
1452                }
1453
1454                // function assumes a legit dataArr :) (validate above)
1455                // is almost a clone of _addNote (homogenise later)
1456                function zbscrmjs_updateNote(dataArr,cb,errcb){
1457                    
1458                    // needs nonce. <!--#NONCENEEDED -->
1459
1460                    if (!window.zbsLogProcessingBlocker){
1461                        
1462                        // blocker
1463                        window.zbsLogProcessingBlocker = true;
1464
1465                        // msg
1466                        jQuery('#zbsEditLogUpdateMsg').html('<?php esc_html_e( 'Saving...', 'zero-bs-crm' ); ?>');
1467
1468                        // Send 
1469                            jQuery.ajax({
1470                                    type: "POST",
1471                                    url: ajaxurl, // admin side is just ajaxurl not wptbpAJAX.ajaxurl,
1472                                    "data": dataArr,
1473                                    dataType: 'json',
1474                                    timeout: 20000,
1475                                    success: function(response) {
1476
1477                                    // Debug  console.log("RESPONSE",response);
1478
1479                                    // blocker
1480                                    window.zbsLogProcessingBlocker = false;
1481
1482                                    // this also has true/false on update... 
1483                                    if (typeof response.processed != "undefined" && response.processed){
1484
1485                                        // callback
1486                                        // make a merged item... 
1487                                        var retArr = dataArr; dataArr.logID = response.processed;
1488                                        if (typeof cb == "function") cb(retArr);
1489
1490                                    } else {
1491
1492                                        // .. was an error :)
1493
1494                                        // callback
1495                                        if (typeof errcb == "function") errcb(response);
1496
1497                                    }
1498
1499
1500                                    },
1501                                    error: function(response){ 
1502
1503                                    // Debug  console.error("RESPONSE",response);
1504
1505                                    // blocker
1506                                    window.zbsLogProcessingBlocker = false;
1507
1508                                    // callback
1509                                    if (typeof errcb == "function") errcb(response);
1510
1511
1512
1513                                    }
1514
1515                            });
1516
1517
1518                    } else {
1519                        
1520                        // end of blocker
1521                        jQuery('#zbsEditLogUpdateMsg').html('... already processing!');
1522                        setTimeout(function(){
1523
1524                            jQuery('#zbsEditLogUpdateMsg').html('');
1525
1526                        },2000);
1527
1528                    }
1529
1530                }
1531
1532
1533                // function assumes a legit noteID + perms :) (validate above)
1534                function zbscrmjs_deleteNote(noteID,cb,errcb){
1535                    
1536                    // needs nonce. <!--#NONCENEEDED -->
1537
1538                    if (!window.zbsLogProcessingBlocker){
1539                        
1540                        // blocker
1541                        window.zbsLogProcessingBlocker = true;
1542
1543                        // -package
1544                        var dataArr = {
1545                            action : 'zbsdellog',
1546                            zbsnid : noteID,
1547                            sec:window.zbscrmjs_logsSecToken
1548                        };
1549
1550                        // Send 
1551                            jQuery.ajax({
1552                                    type: "POST",
1553                                    url: ajaxurl, // admin side is just ajaxurl not wptbpAJAX.ajaxurl,
1554                                    "data": dataArr,
1555                                    dataType: 'json',
1556                                    timeout: 20000,
1557                                    success: function(response) {
1558
1559                                    // Debug  console.log("RESPONSE",response);
1560
1561                                    // blocker
1562                                    window.zbsLogProcessingBlocker = false;
1563
1564                                    // this also has true/false on update... 
1565                                    if (typeof response.processed != "undefined" && response.processed){
1566
1567                                        // Debug console.log("SUCCESS");
1568
1569                                        // callback
1570                                        if (typeof cb == "function") cb(response);
1571
1572                                    } else {
1573
1574                                        // .. was an error :)
1575                                        // Debug console.log("ERRZ");                                    
1576
1577                                        // callback
1578                                        if (typeof errcb == "function") errcb(response);
1579
1580                                    }
1581
1582
1583                                    },
1584                                    error: function(response){ 
1585
1586                                    // Debug  console.error("RESPONSE",response);
1587
1588                                    // blocker
1589                                    window.zbsLogProcessingBlocker = false;
1590
1591                                    // callback
1592                                    if (typeof errcb == "function") errcb(response);
1593
1594
1595
1596                                    }
1597
1598                            });
1599
1600
1601                    } else {
1602                        
1603                        // end of blocker
1604
1605                    }
1606
1607                }
1608
1609
1610
1611
1612                // function assumes a legit noteID + perms :) (validate above)
1613                function jpcrm_js_pin_note(noteID,cb,errcb){
1614                    
1615                    // needs nonce. <!--#NONCENEEDED -->
1616
1617                    if (!window.zbsLogProcessingBlocker){
1618                        
1619                        // blocker
1620                        window.zbsLogProcessingBlocker = true;
1621
1622                        // -package
1623                        var dataArr = {
1624                            action : 'jpcrmpinlog',
1625                            zbsnid : noteID,
1626                            sec:window.zbscrmjs_logsSecToken
1627                        };
1628
1629                        // Send 
1630                            jQuery.ajax({
1631                                    type: "POST",
1632                                    url: ajaxurl, // admin side is just ajaxurl not wptbpAJAX.ajaxurl,
1633                                    "data": dataArr,
1634                                    dataType: 'json',
1635                                    timeout: 20000,
1636                                    success: function(response) {
1637
1638                                    // Debug  console.log("RESPONSE",response);
1639
1640                                    // blocker
1641                                    window.zbsLogProcessingBlocker = false;
1642
1643                                    // this also has true/false on update... 
1644                                    if (typeof response.processed != "undefined" && response.processed){
1645
1646                                        // Debug console.log("SUCCESS");
1647
1648                                        // callback
1649                                        if (typeof cb == "function") cb(response);
1650
1651                                    } else {
1652
1653                                        // .. was an error :)
1654                                        // Debug console.log("ERRZ");                                    
1655
1656                                        // callback
1657                                        if (typeof errcb == "function") errcb(response);
1658
1659                                    }
1660
1661
1662                                    },
1663                                    error: function(response){ 
1664
1665                                    // Debug  console.error("RESPONSE",response);
1666
1667                                    // blocker
1668                                    window.zbsLogProcessingBlocker = false;
1669
1670                                    // callback
1671                                    if (typeof errcb == "function") errcb(response);
1672
1673
1674
1675                                    }
1676
1677                            });
1678
1679
1680                    } else {
1681                        
1682                        // end of blocker
1683
1684                    }
1685
1686                }
1687
1688
1689                // function assumes a legit noteID + perms :) (validate above)
1690                function jpcrm_js_unpin_note(noteID,cb,errcb){
1691                    
1692                    // needs nonce. <!--#NONCENEEDED -->
1693
1694                    if (!window.zbsLogProcessingBlocker){
1695                        
1696                        // blocker
1697                        window.zbsLogProcessingBlocker = true;
1698
1699                        // -package
1700                        var dataArr = {
1701                            action : 'jpcrmunpinlog',
1702                            zbsnid : noteID,
1703                            sec:window.zbscrmjs_logsSecToken
1704                        };
1705
1706                        // Send 
1707                            jQuery.ajax({
1708                                    type: "POST",
1709                                    url: ajaxurl, // admin side is just ajaxurl not wptbpAJAX.ajaxurl,
1710                                    "data": dataArr,
1711                                    dataType: 'json',
1712                                    timeout: 20000,
1713                                    success: function(response) {
1714
1715                                    // Debug  console.log("RESPONSE",response);
1716
1717                                    // blocker
1718                                    window.zbsLogProcessingBlocker = false;
1719
1720                                    // this also has true/false on update... 
1721                                    if (typeof response.processed != "undefined" && response.processed){
1722
1723                                        // Debug console.log("SUCCESS");
1724
1725                                        // callback
1726                                        if (typeof cb == "function") cb(response);
1727
1728                                    } else {
1729
1730                                        // .. was an error :)
1731                                        // Debug console.log("ERRZ");                                    
1732
1733                                        // callback
1734                                        if (typeof errcb == "function") errcb(response);
1735
1736                                    }
1737
1738
1739                                    },
1740                                    error: function(response){ 
1741
1742                                    // Debug  console.error("RESPONSE",response);
1743
1744                                    // blocker
1745                                    window.zbsLogProcessingBlocker = false;
1746
1747                                    // callback
1748                                    if (typeof errcb == "function") errcb(response);
1749
1750
1751
1752                                    }
1753
1754                            });
1755
1756
1757                    } else {
1758                        
1759                        // end of blocker
1760
1761                    }
1762
1763                }
1764                
1765                </script>
1766                <?php
1767
1768        } // / if post type
1769    }
1770
1771    public function save_data( $objID, $obj ) {
1772
1773        // not req. ajax
1774
1775        return $obj;
1776    }
1777}
1778
1779/*
1780======================================================
1781    / Logs V2 - DB2 Metabox
1782    ====================================================== */
1783
1784    #} Mark as included :)
1785    define( 'ZBSCRM_INC_LOGSMB', true );