Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 606
0.00% covered (danger)
0.00%
0 / 38
CRAP
n/a
0 / 0
zeroBSCRM_IA_NewCustomerLog
0.00% covered (danger)
0.00%
0 / 52
0.00% covered (danger)
0.00%
0 / 1
756
zeroBSCRM_IA_NewCompanyLog
0.00% covered (danger)
0.00%
0 / 50
0.00% covered (danger)
0.00%
0 / 1
756
zeroBSCRM_IA_NewQuoteLog
0.00% covered (danger)
0.00%
0 / 49
0.00% covered (danger)
0.00%
0 / 1
1260
zeroBSCRM_IA_AcceptedQuoteLog
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 1
272
zeroBSCRM_IA_NewInvoiceLog
0.00% covered (danger)
0.00%
0 / 49
0.00% covered (danger)
0.00%
0 / 1
1332
zeroBSCRM_IA_NewTransactionLog
0.00% covered (danger)
0.00%
0 / 51
0.00% covered (danger)
0.00%
0 / 1
1332
zeroBSCRM_IA_NewEventLog
0.00% covered (danger)
0.00%
0 / 53
0.00% covered (danger)
0.00%
0 / 1
1406
zeroBSCRM_IA_NewLogCatchContactsDB2
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
182
zeroBSCRM_IA_NewClientPortalUserLog
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 1
210
zeroBSCRM_IA_NewCustomerClientPortal
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 1
462
zeroBSCRM_IA_ContactSegmentCompiler
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
90
zeroBSCRM_IA_quoteSegmentCompiler
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
12
zeroBSCRM_IA_invoiceSegmentCompiler
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
12
zeroBSCRM_IA_transactionSegmentCompiler
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
12
zeroBSCRM_IA_CustomerStatusChangePortalAndLog
0.00% covered (danger)
0.00%
0 / 69
0.00% covered (danger)
0.00%
0 / 1
702
zeroBSCRM_IA_CustomerStatusChangeAutoLog
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 1
272
zeroBSCRM_IA_NewCustomerWPHook
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
20
zeroBSCRM_IA_EditCustomerWPHook
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
20
zeroBSCRM_IA_EditCustomerVitalsWPHook
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
20
zeroBSCRM_IA_EditCustomerEmailWPHook
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
20
zeroBSCRM_IA_DeleteCustomerWPHook
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
20
zeroBSCRM_IA_BeforeDeleteCustomerWPHook
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
20
zeroBSCRM_IA_NewCompanyWPHook
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
20
zeroBSCRM_IA_DeleteCompanyWPHook
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
20
zeroBSCRM_IA_NewQuoteWPHook
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
20
zeroBSCRM_IA_AcceptedQuoteWPHook
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
20
zeroBSCRM_IA_DeleteQuoteWPHook
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
20
zeroBSCRM_IA_NewInvoiceWPHook
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
20
zeroBSCRM_IA_EditInvoiceWPHook
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
20
zeroBSCRM_IA_DeleteInvoiceWPHook
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
20
zeroBSCRM_IA_NewTransactionWPHook
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
20
zeroBSCRM_IA_DeleteTransactionWPHook
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
20
zeroBSCRM_IA_NewEventWPHook
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
20
zeroBSCRM_IA_UpdateEventWPHook
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
20
zeroBSCRM_IA_DeleteEventWPHook
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
20
zeroBSCRM_IA_NewClientPortalUserHook
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
20
zeroBSCRM_IA_DeleteFormWPHook
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
20
zeroBSCRM_IA_DeleteSegmentWPHook
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2/*
3 * Jetpack CRM
4 * https://jetpackcrm.com
5 * V1.1.15
6 *
7 * Copyright 2020 Automattic
8 *
9 * Date: 30/08/16
10 */
11
12defined( 'ZEROBSCRM_PATH' ) || exit( 0 );
13
14/*
15======================================================
16    Setup Internal Automator Recipes:
17    ====================================================== */
18
19    // Note on contact.vitals.update vs contact.update,
20    // ... if contact.vitals.update fires, it will fire AS WELL as contact.update.
21    // ... if contact.email.update fires, it will fire AS WELL as contact.update.
22
23    #} Set IA Recipes (CREATED)
24    zeroBSCRM_AddInternalAutomatorRecipe( 'contact.new', 'zeroBSCRM_IA_NewCustomerClientPortal', array() );
25    zeroBSCRM_AddInternalAutomatorRecipe( 'contact.new', 'zeroBSCRM_IA_NewCustomerLog', array() );
26    zeroBSCRM_AddInternalAutomatorRecipe( 'contact.status.update', 'zeroBSCRM_IA_CustomerStatusChangePortalAndLog', array() );
27    zeroBSCRM_AddInternalAutomatorRecipe( 'contact.status.update', 'zeroBSCRM_IA_CustomerStatusChangeAutoLog', array() );
28    zeroBSCRM_AddInternalAutomatorRecipe( 'contact.new', 'zeroBSCRM_IA_ContactSegmentCompiler', array() ); // for edit + new
29    zeroBSCRM_AddInternalAutomatorRecipe( 'contact.update', 'zeroBSCRM_IA_ContactSegmentCompiler', array() ); // for edit + new
30    zeroBSCRM_AddInternalAutomatorRecipe( 'company.new', 'zeroBSCRM_IA_NewCompanyLog', array() );
31    zeroBSCRM_AddInternalAutomatorRecipe( 'quote.new', 'zeroBSCRM_IA_NewQuoteLog', array() );
32    zeroBSCRM_AddInternalAutomatorRecipe( 'quote.new', 'zeroBSCRM_IA_quoteSegmentCompiler', array() );
33    zeroBSCRM_AddInternalAutomatorRecipe( 'quote.update', 'zeroBSCRM_IA_quoteSegmentCompiler', array() );
34    zeroBSCRM_AddInternalAutomatorRecipe( 'quote.accepted', 'zeroBSCRM_IA_AcceptedQuoteLog', array() );
35    zeroBSCRM_AddInternalAutomatorRecipe( 'invoice.new', 'zeroBSCRM_IA_NewInvoiceLog', array() );
36    zeroBSCRM_AddInternalAutomatorRecipe( 'invoice.new', 'zeroBSCRM_IA_invoiceSegmentCompiler', array() );
37    zeroBSCRM_AddInternalAutomatorRecipe( 'invoice.update', 'zeroBSCRM_IA_invoiceSegmentCompiler', array() );
38    zeroBSCRM_AddInternalAutomatorRecipe( 'log.new', 'zeroBSCRM_IA_NewLogCatchContactsDB2', array() );
39    zeroBSCRM_AddInternalAutomatorRecipe( 'transaction.new', 'zeroBSCRM_IA_NewTransactionLog', array() );
40    zeroBSCRM_AddInternalAutomatorRecipe( 'transaction.new', 'zeroBSCRM_IA_transactionSegmentCompiler', array() );
41    zeroBSCRM_AddInternalAutomatorRecipe( 'transaction.update', 'zeroBSCRM_IA_transactionSegmentCompiler', array() );
42    zeroBSCRM_AddInternalAutomatorRecipe( 'event.new', 'zeroBSCRM_IA_NewEventLog', array() );
43    zeroBSCRM_AddInternalAutomatorRecipe( 'clientwpuser.new', 'zeroBSCRM_IA_NewClientPortalUserLog', array() );
44
45    #} Set IA  Recipes (UPDATED)
46    #} - WH commented out, you need to have a corresponding function for any of these you add:
47    #// zeroBSCRM_AddInternalAutomatorRecipe('status.change','zeroBSCRM_IA_StatusChange',array());
48
49    // WP Hook tie-ins (for Mike [and 3rd party developers!], mostly).
50    zeroBSCRM_AddInternalAutomatorRecipe( 'contact.new', 'zeroBSCRM_IA_NewCustomerWPHook', array() );
51    zeroBSCRM_AddInternalAutomatorRecipe( 'contact.update', 'zeroBSCRM_IA_EditCustomerWPHook', array() );
52    zeroBSCRM_AddInternalAutomatorRecipe( 'contact.status.update', 'zeroBSCRM_IA_EditCustomerWPHook', array() );
53    zeroBSCRM_AddInternalAutomatorRecipe( 'contact.vitals.update', 'zeroBSCRM_IA_EditCustomerVitalsWPHook', array() );
54    zeroBSCRM_AddInternalAutomatorRecipe( 'contact.email.update', 'zeroBSCRM_IA_EditCustomerEmailWPHook', array() );
55    zeroBSCRM_AddInternalAutomatorRecipe( 'contact.delete', 'zeroBSCRM_IA_DeleteCustomerWPHook', array() );
56    zeroBSCRM_AddInternalAutomatorRecipe( 'company.new', 'zeroBSCRM_IA_NewCompanyWPHook', array() );
57    zeroBSCRM_AddInternalAutomatorRecipe( 'company.delete', 'zeroBSCRM_IA_DeleteCompanyWPHook', array() );
58    zeroBSCRM_AddInternalAutomatorRecipe( 'quote.new', 'zeroBSCRM_IA_NewQuoteWPHook', array() );
59    zeroBSCRM_AddInternalAutomatorRecipe( 'quote.accepted', 'zeroBSCRM_IA_AcceptedQuoteWPHook', array() );
60    zeroBSCRM_AddInternalAutomatorRecipe( 'quote.update', 'zeroBSCRM_IA_EditInvoiceWPHook', array() );
61    zeroBSCRM_AddInternalAutomatorRecipe( 'quote.status.update', 'zeroBSCRM_IA_EditInvoiceWPHook', array() );
62    zeroBSCRM_AddInternalAutomatorRecipe( 'quote.delete', 'zeroBSCRM_IA_DeleteQuoteWPHook', array() );
63    zeroBSCRM_AddInternalAutomatorRecipe( 'invoice.new', 'zeroBSCRM_IA_NewInvoiceWPHook', array() );
64    zeroBSCRM_AddInternalAutomatorRecipe( 'invoice.update', 'zeroBSCRM_IA_EditInvoiceWPHook', array() );
65    zeroBSCRM_AddInternalAutomatorRecipe( 'invoice.status.update', 'zeroBSCRM_IA_EditInvoiceWPHook', array() );
66    zeroBSCRM_AddInternalAutomatorRecipe( 'invoice.delete', 'zeroBSCRM_IA_DeleteInvoiceWPHook', array() );
67    zeroBSCRM_AddInternalAutomatorRecipe( 'transaction.new', 'zeroBSCRM_IA_NewTransactionWPHook', array() );
68    zeroBSCRM_AddInternalAutomatorRecipe( 'transaction.delete', 'zeroBSCRM_IA_DeleteTransactionWPHook', array() );
69    zeroBSCRM_AddInternalAutomatorRecipe( 'event.new', 'zeroBSCRM_IA_NewEventWPHook', array() );
70    zeroBSCRM_AddInternalAutomatorRecipe( 'event.update', 'zeroBSCRM_IA_UpdateEventWPHook', array() );
71    zeroBSCRM_AddInternalAutomatorRecipe( 'event.delete', 'zeroBSCRM_IA_DeleteEventWPHook', array() );
72    zeroBSCRM_AddInternalAutomatorRecipe( 'clientwpuser.new', 'zeroBSCRM_IA_NewClientPortalUserHook', array() );
73    zeroBSCRM_AddInternalAutomatorRecipe( 'segment.delete', 'zeroBSCRM_IA_DeleteSegmentWPHook', array() );
74    zeroBSCRM_AddInternalAutomatorRecipe( 'contact.before.delete', 'zeroBSCRM_IA_BeforeDeleteCustomerWPHook', array() );
75    // don't need to expose tbh
76    // zeroBSCRM_AddInternalAutomatorRecipe('form.delete','zeroBSCRM_IA_DeleteFormWPHook',array());
77
78// DAL3.0 + can use these:
79/*
80    company.new
81    company.update
82    company.status.update
83
84    quote.new
85    quote.update
86
87    log.update
88
89    event.new
90    event.update
91
92    form.new
93    form.update
94
95    invoice.new
96    invoice.update
97    invoice.status.update
98
99    transaction.new
100    transaction.update
101    transaction.status.update
102
103    quotetemplate.new
104    quotetemplate.update
105
106*/
107
108/*
109======================================================
110    / Setup Internal Automator Recipes:
111    ====================================================== */
112
113/*
114======================================================
115    Internal Automator Recipe Functions
116    ====================================================== */
117
118    #} Adds a "created" log to users (if setting)
119function zeroBSCRM_IA_NewCustomerLog( $obj = array() ) {
120
121    # if setting
122    $autoLogThis = zeroBSCRM_getSetting( 'autolog_customer_new' );
123
124    if ( $autoLogThis > 0 ) {
125
126        #} Retrieve necessary info:
127        $zbsNoteAgainstPostID = -1;
128        if ( is_array( $obj ) && isset( $obj['id'] ) ) {
129            $zbsNoteAgainstPostID = (int) $obj['id'];
130        }
131        if ( isset( $zbsNoteAgainstPostID ) && ! empty( $zbsNoteAgainstPostID ) ) {
132
133            #} First check if an override is passed...
134            if ( isset( $obj['automatorpassthrough'] ) && is_array( $obj['automatorpassthrough'] ) && isset( $obj['automatorpassthrough']['note_override'] ) && is_array( $obj['automatorpassthrough']['note_override'] ) && isset( $obj['automatorpassthrough']['note_override']['type'] ) ) {
135
136                #} An overriding note has been passed, just use that
137
138                    #} Add log
139                    $newLogID = zeroBS_addUpdateContactLog( $zbsNoteAgainstPostID, -1, -1, $obj['automatorpassthrough']['note_override'] );
140
141            } else {
142
143                #} No override, use default processing...
144
145                #} Set Deets
146                $newCustomerName = '';
147                if ( is_array( $obj ) && isset( $obj['id'] ) && isset( $obj['customerMeta'] ) && is_array( $obj['customerMeta'] ) ) {
148                    $newCustomerName = zeroBS_customerName( $obj['id'], $obj['customerMeta'], false, true );
149                }
150                $noteShortDesc = 'Customer Created';
151                if ( ! empty( $newCustomerName ) ) {
152                    $noteShortDesc = $newCustomerName;
153                }
154                $note_long_description = '';
155
156                // Custom short desc for external source creations
157                if ( isset( $obj['extsource'] ) && ! empty( $obj['extsource'] ) ) {
158
159                    $uid = '';
160
161                    // We seem to pass either the external source array (woo)
162                    // ... or a string 'pay', so lets select the right one...
163                    if ( is_array( $obj['extsource'] ) && isset( $obj['extsource']['source'] ) ) {
164
165                        $source_key = $obj['extsource']['source'];
166
167                        // if we have this we also have UID
168                        $uid = $obj['extsource']['uid'];
169
170                    } else {
171                        $source_key = $obj['extsource'];
172                    }
173
174                    switch ( $source_key ) {
175
176                        case 'pay':
177                            $note_long_description = __( 'Created from PayPal', 'zero-bs-crm' ) . '<i class="fa fa-paypal"></i>';
178
179                            break;
180
181                        case 'woo':
182                            $note_long_description = __( 'Created from WooCommerce Order', 'zero-bs-crm' ) . ' <i class="fa fa-shopping-cart"></i>';
183
184                            break;
185
186                        case 'env':
187                            $note_long_description = __( 'Created from Envato', 'zero-bs-crm' ) . '<i class="fa fa-envira"></i>';
188
189                            break;
190
191                        case 'form':
192                            $note_long_description = __( 'Created from Form Capture', 'zero-bs-crm' ) . '<i class="fa fa-wpforms"></i>';
193
194                            break;
195
196                        case 'csv':
197                            $note_long_description = __( 'Created from CSV Import', 'zero-bs-crm' ) . '<i class="fa fa-file-text"></i>';
198
199                            break;
200
201                        case 'gra':
202                            $note_long_description = __( 'Created from Gravity Forms', 'zero-bs-crm' ) . '<i class="fa fa-wpforms"></i>';
203
204                            break;
205
206                        default:
207                            // Generic fallback (shouldn't ever fire)
208                            $note_long_description = __( 'Created from External Source', 'zero-bs-crm' ) . ' <i class="fa fa-users"></i>';
209
210                            break;
211
212                    }
213
214                    // allow extension override via filter (e.g. WooSync)
215                    $note_long_description = apply_filters( 'jpcrm_new_contact_log', $note_long_description, $source_key, $uid );
216
217                }
218
219                #} Add log
220                $newLogID = zeroBS_addUpdateContactLog(
221                    $zbsNoteAgainstPostID,
222                    -1,
223                    -1,
224                    array(
225                        'type'      => 'Created',
226                        'shortdesc' => $noteShortDesc,
227                        'longdesc'  => $note_long_description,
228                    )
229                );
230
231            } # / end of if no override
232
233        }
234    }
235}
236    #} Adds a "created" log to users (if setting)
237function zeroBSCRM_IA_NewCompanyLog( $obj = array() ) {
238
239    # if setting
240    $autoLogThis = zeroBSCRM_getSetting( 'autolog_company_new' );
241
242    if ( $autoLogThis > 0 ) {
243
244        #} Retrieve necessary info:
245        $zbsNoteAgainstPostID = -1;
246        if ( is_array( $obj ) && isset( $obj['id'] ) ) {
247            $zbsNoteAgainstPostID = (int) $obj['id'];
248        }
249        if ( isset( $zbsNoteAgainstPostID ) && ! empty( $zbsNoteAgainstPostID ) ) {
250
251            #} First check if an override is passed...
252            if ( isset( $obj['automatorpassthrough'] ) && is_array( $obj['automatorpassthrough'] ) && isset( $obj['automatorpassthrough']['note_override'] ) && is_array( $obj['automatorpassthrough']['note_override'] ) && isset( $obj['automatorpassthrough']['note_override']['type'] ) ) {
253
254                #} An overriding note has been passed, just use that
255
256                    #} Add log
257                    $newLogID = zeroBS_addUpdateLog( $zbsNoteAgainstPostID, -1, -1, $obj['automatorpassthrough']['note_override'], 'zerobs_company' );
258
259            } else {
260
261                #} No override, use default processing...
262
263                #} Set Deets
264                $newCompanyName = '';
265                if ( is_array( $obj ) && isset( $obj['id'] ) && isset( $obj['companyMeta'] ) && is_array( $obj['companyMeta'] ) ) {
266                    $newCompanyName = zeroBS_companyName( $obj['id'], $obj['companyMeta'], false, true );
267                }
268                $noteShortDesc = 'Company Created';
269                if ( ! empty( $newCompanyName ) ) {
270                    $noteShortDesc = $newCompanyName;
271                }
272                $note_long_description = '';
273
274                // Custom short desc for external source creations
275                if ( isset( $obj['extsource'] ) && ! empty( $obj['extsource'] ) ) {
276
277                    // We seem to pass either the external source array (woo)
278                    // ... or a string 'pay', so lets select the right one...
279                    if ( is_array( $obj['extsource'] ) && isset( $obj['extsource']['source'] ) ) {
280
281                        $source_key = $obj['extsource']['source'];
282
283                        // if we have this we also have UID
284                        $uid = $obj['extsource']['uid'];
285
286                    } else {
287                        $source_key = $obj['extsource'];
288                    }
289
290                    switch ( $source_key ) {
291
292                        case 'pay':
293                            $note_long_description = 'Created from PayPal <i class="fa fa-paypal"></i>';
294
295                            break;
296
297                        case 'woo':
298                            if ( isset( $uid ) ) {
299                                $note_long_description = sprintf( __( 'Created from WooCommerce Order #%s', 'zero-bs-crm' ), $uid ) . ' <i class="fa fa-shopping-cart"></i>';
300                            } else {
301                                $note_long_description = __( 'Created from WooCommerce Order', 'zero-bs-crm' ) . ' <i class="fa fa-shopping-cart"></i>';
302                            }
303
304                            break;
305
306                        case 'env':
307                            $note_long_description = 'Created from Envato <i class="fa fa-envira"></i>';
308
309                            break;
310
311                        case 'form':
312                            $note_long_description = 'Created from Form Capture <i class="fa fa-wpforms"></i>';
313
314                            break;
315
316                        case 'csv':
317                            $note_long_description = 'Created from CSV Import <i class="fa fa-file-text"></i>';
318
319                            break;
320
321                        default:
322                            #} Generic for now (SHOULD NEVER CALL)
323                            $note_long_description = 'Created from External Source <i class="fa fa-users"></i>';
324
325                            break;
326
327                    }
328                }
329
330                #} Add log
331                $newLogID = zeroBS_addUpdateLog(
332                    $zbsNoteAgainstPostID,
333                    -1,
334                    -1,
335                    array(
336                        'type'      => 'Created',
337                        'shortdesc' => $noteShortDesc,
338                        'longdesc'  => $note_long_description,
339                    ),
340                    'zerobs_company'
341                );
342
343            } # / end of if no override
344
345        }
346    }
347}
348
349    #} Adds a "created" log to customer (of quotes) (if setting)
350function zeroBSCRM_IA_NewQuoteLog( $obj = array() ) {
351
352    # if setting
353    $autoLogThis = zeroBSCRM_getSetting( 'autolog_quote_new' );
354
355    if ( $autoLogThis > 0 ) {
356
357            // 3.0+
358            #} Retrieve necessary info:
359            $noteAgainstIDs = array(
360                'contacts'  => array(),
361                'companies' => array(),
362            );
363            if ( is_array( $obj ) && isset( $obj['againstids'] ) && is_array( $obj['againstids'] ) ) {
364
365                // trusting they're correctly passed...
366                if ( isset( $obj['againstids']['contacts'] ) && is_array( $obj['againstids']['contacts'] ) ) {
367                    $noteAgainstIDs['contacts'] = $obj['againstids']['contacts'];
368                }
369                if ( isset( $obj['againstids']['companies'] ) && is_array( $obj['againstids']['companies'] ) ) {
370                    $noteAgainstIDs['companies'] = $obj['againstids']['companies'];
371                }
372            }
373            $quoteID = '';
374            if ( is_array( $obj ) && isset( $obj['id'] ) ) {
375                $quoteID = $obj['id'];
376            }
377            $quoteTitle = '';
378            if ( is_array( $obj ) && is_array( $obj['data'] ) && isset( $obj['data']['title'] ) && ! empty( $obj['data']['title'] ) ) {
379                $quoteTitle = $obj['data']['title'];
380            }
381            $quoteValue = '';
382            if ( is_array( $obj ) && is_array( $obj['data'] ) && isset( $obj['data']['value'] ) && ! empty( $obj['data']['value'] ) ) {
383                $quoteValue = zeroBSCRM_formatCurrency( $obj['data']['value'] );
384            }
385            $extsource = '';
386            if ( is_array( $obj ) && is_array( $obj['extsource'] ) && isset( $obj['extsource']['source'] ) && ! empty( $obj['extsource']['source'] ) ) {
387                $extsource = $obj['extsource']['source'];
388            }
389            $extsourceID = '';
390            if ( is_array( $obj ) && is_array( $obj['extsource'] ) && isset( $obj['extsource']['uid'] ) && ! empty( $obj['extsource']['uid'] ) ) {
391                $extsourceID = $obj['extsource']['uid'];
392            }
393
394            // build str
395            $note_long_description = '';
396            $noteShortDesc         = '';
397            if ( ! empty( $quoteID ) ) {
398                $noteShortDesc .= '#' . $quoteID;
399            }
400            if ( ! empty( $quoteTitle ) ) {
401                $noteShortDesc .= $quoteTitle;
402            }
403            if ( ! empty( $quoteValue ) ) {
404                $noteShortDesc .= ' (' . $quoteValue . ')';
405            }
406            if ( ! empty( $extsource ) && ! empty( $extsourceID ) ) {
407                $note_long_description = __( 'Created by', 'zero-bs-crm' ) . ' ' . zeroBS_getExternalSourceTitle( $extsource, $extsourceID );
408            }
409
410            if ( is_array( $noteAgainstIDs['contacts'] ) && count( $noteAgainstIDs['contacts'] ) > 0 ) {
411                foreach ( $noteAgainstIDs['contacts'] as $cID ) {
412
413                    #} Add log
414                    $newLogID = zeroBS_addUpdateLog(
415                        $cID,
416                        -1,
417                        -1,
418                        array(
419                            'type'      => __( 'Quote Created', 'zero-bs-crm' ),
420                            'shortdesc' => $noteShortDesc,
421                            'longdesc'  => $note_long_description,
422                        ),
423                        'zerobs_customer'
424                    );
425
426                }
427            }
428    }
429}
430
431    // Adds an "accepted" log to contact (of quote) (if setting)
432function zeroBSCRM_IA_AcceptedQuoteLog( $obj = array() ) {
433
434    # if setting
435    $autoLogThis = zeroBSCRM_getSetting( 'autolog_quote_accepted' );
436
437    if ( $autoLogThis > 0 ) {
438
439        global $zbs;
440
441            // retrieve quote
442            $quoteID = '';
443        if ( is_array( $obj ) && isset( $obj['id'] ) ) {
444            $quoteID = $obj['id'];
445        }
446            $quote = $zbs->DAL->quotes->getQuote( $quoteID );
447
448        if ( is_array( $quote ) && count( $quote['contact'] ) > 0 ) {
449
450            // get signed by if passed
451            $signedBy = '';
452            if ( is_array( $obj ) && is_array( $obj['data'] ) && isset( $obj['data']['signed'] ) && ! empty( $obj['data']['signed'] ) ) {
453                $signedBy = $obj['data']['signed'];
454            }
455            // could get `ip` but not really user friendly/needed
456
457            // build str
458            $note_long_description = '';
459            $noteShortDesc         = '';
460            if ( ! empty( $quoteID ) ) {
461                $noteShortDesc .= '#' . $quoteID;
462            }
463            if ( ! empty( $quoteTitle ) ) {
464                $noteShortDesc .= $quote['title'];
465            }
466            if ( ! empty( $quoteValue ) ) {
467                $noteShortDesc .= ' (' . zeroBSCRM_formatCurrency( $quote['value'] ) . ')';
468            }
469            if ( ! empty( $signedBy ) ) {
470                $note_long_description = __( 'Signed', 'zero-bs-crm' ) . ' ' . $signedBy;
471            }
472
473            if ( is_array( $quote['contact'] ) ) {
474                foreach ( $quote['contact'] as $contact ) {
475
476                    #} Add log
477                    $newLogID = zeroBS_addUpdateLog(
478                        $contact['id'],
479                        -1,
480                        -1,
481                        array(
482                            'type'      => __( 'Quote: Accepted', 'zero-bs-crm' ),
483                            'shortdesc' => $noteShortDesc,
484                            'longdesc'  => $note_long_description,
485                        ),
486                        'zerobs_customer'
487                    );
488
489                }
490            }
491        }
492    }
493}
494
495    #} Adds a "created" log to customer (of quotes) (if setting)
496function zeroBSCRM_IA_NewInvoiceLog( $obj = array() ) {
497
498    # if setting
499    $autoLogThis = zeroBSCRM_getSetting( 'autolog_invoice_new' );
500
501    if ( $autoLogThis > 0 ) {
502
503            #} Retrieve necessary info:
504            $noteAgainstIDs = array(
505                'contacts'  => array(),
506                'companies' => array(),
507            );
508            if ( is_array( $obj ) && isset( $obj['againstids'] ) && is_array( $obj['againstids'] ) ) {
509
510                // trusting they're correctly passed...
511                if ( isset( $obj['againstids']['contacts'] ) && is_array( $obj['againstids']['contacts'] ) ) {
512                    $noteAgainstIDs['contacts'] = $obj['againstids']['contacts'];
513                }
514                if ( isset( $obj['againstids']['companies'] ) && is_array( $obj['againstids']['companies'] ) ) {
515                    $noteAgainstIDs['companies'] = $obj['againstids']['companies'];
516                }
517            }
518            $invoiceID = '';
519            if ( is_array( $obj ) && isset( $obj['id'] ) ) {
520                $invoiceID = $obj['id'];
521            }
522            $invoiceRef = '';
523            if ( is_array( $obj ) && is_array( $obj['data'] ) && isset( $obj['data']['id_override'] ) && ! empty( $obj['data']['id_override'] ) ) {
524                $invoiceRef = $obj['data']['id_override'];
525            }
526            $invoiceValue = '';
527            if ( is_array( $obj ) && is_array( $obj['data'] ) && isset( $obj['data']['total'] ) && ! empty( $obj['data']['total'] ) ) {
528                $invoiceValue = zeroBSCRM_formatCurrency( $obj['data']['total'] );
529            }
530            $extsource = '';
531            if ( is_array( $obj ) && is_array( $obj['extsource'] ) && isset( $obj['extsource']['source'] ) && ! empty( $obj['extsource']['source'] ) ) {
532                $extsource = $obj['extsource']['source'];
533            }
534            $extsourceID = '';
535            if ( is_array( $obj ) && is_array( $obj['extsource'] ) && isset( $obj['extsource']['uid'] ) && ! empty( $obj['extsource']['uid'] ) ) {
536                $extsourceID = $obj['extsource']['uid'];
537            }
538
539            // build str
540            $note_long_description = '';
541            $noteShortDesc         = '';
542            if ( ! empty( $invoiceID ) && empty( $invoiceRef ) ) {
543                $noteShortDesc .= '#' . $invoiceID;
544            }
545            if ( ! empty( $invoiceRef ) ) {
546                $noteShortDesc .= $invoiceRef;
547            }
548            if ( ! empty( $invoiceValue ) ) {
549                $noteShortDesc .= ' (' . $invoiceValue . ')';
550            }
551            if ( ! empty( $extsource ) && ! empty( $extsourceID ) ) {
552                $note_long_description = __( 'Created by', 'zero-bs-crm' ) . ' ' . zeroBS_getExternalSourceTitle( $extsource, $extsourceID );
553            }
554
555            if ( is_array( $noteAgainstIDs['contacts'] ) && count( $noteAgainstIDs['contacts'] ) > 0 ) {
556                foreach ( $noteAgainstIDs['contacts'] as $cID ) {
557
558                    #} Add log
559                    $newLogID = zeroBS_addUpdateLog(
560                        $cID,
561                        -1,
562                        -1,
563                        array(
564                            'type'      => __( 'Invoice Created', 'zero-bs-crm' ),
565                            'shortdesc' => $noteShortDesc,
566                            'longdesc'  => $note_long_description,
567                        ),
568                        'zerobs_customer'
569                    );
570
571                }
572            }
573    }
574}
575
576    #} Adds a "created" log to customer (of trans) (if setting)
577function zeroBSCRM_IA_NewTransactionLog( $obj = array() ) {
578
579    $newLogID = false;
580
581    #} if setting
582    $autoLogThis = zeroBSCRM_getSetting( 'autolog_transaction_new' );
583
584    if ( $autoLogThis > 0 ) {
585
586            #} Retrieve necessary info:
587            $noteAgainstIDs = array(
588                'contacts'  => array(),
589                'companies' => array(),
590            );
591            if ( is_array( $obj ) && isset( $obj['againstids'] ) && is_array( $obj['againstids'] ) ) {
592
593                // trusting they're correctly passed...
594                if ( isset( $obj['againstids']['contacts'] ) && is_array( $obj['againstids']['contacts'] ) ) {
595                    $noteAgainstIDs['contacts'] = $obj['againstids']['contacts'];
596                }
597                if ( isset( $obj['againstids']['companies'] ) && is_array( $obj['againstids']['companies'] ) ) {
598                    $noteAgainstIDs['companies'] = $obj['againstids']['companies'];
599                }
600            }
601            $transactionID = '';
602            if ( is_array( $obj ) && isset( $obj['id'] ) ) {
603                $transactionID = $obj['id'];
604            }
605            $transactionRef = '';
606            if ( is_array( $obj ) && is_array( $obj['data'] ) && isset( $obj['data']['ref'] ) && ! empty( $obj['data']['ref'] ) ) {
607                $transactionRef = $obj['data']['ref'];
608            }
609            $transactionValue = '';
610            if ( is_array( $obj ) && is_array( $obj['data'] ) && isset( $obj['data']['total'] ) && ! empty( $obj['data']['total'] ) ) {
611                $transactionValue = zeroBSCRM_formatCurrency( $obj['data']['total'] );
612            }
613            $extsource = '';
614            if ( is_array( $obj ) && is_array( $obj['extsource'] ) && isset( $obj['extsource']['source'] ) && ! empty( $obj['extsource']['source'] ) ) {
615                $extsource = $obj['extsource']['source'];
616            }
617            $extsourceID = '';
618            if ( is_array( $obj ) && is_array( $obj['extsource'] ) && isset( $obj['extsource']['uid'] ) && ! empty( $obj['extsource']['uid'] ) ) {
619                $extsourceID = $obj['extsource']['uid'];
620            }
621
622            // build str
623            $note_long_description = '';
624            $noteShortDesc         = '';
625            if ( ! empty( $transactionID ) && empty( $transactionRef ) ) {
626                $noteShortDesc .= '#' . $transactionID;
627            }
628            if ( ! empty( $transactionRef ) ) {
629                $noteShortDesc .= $transactionRef;
630            }
631            if ( ! empty( $transactionValue ) ) {
632                $noteShortDesc .= ' (' . $transactionValue . ')';
633            }
634            if ( ! empty( $extsource ) && ! empty( $extsourceID ) ) {
635                $note_long_description = __( 'Created by', 'zero-bs-crm' ) . ' ' . zeroBS_getExternalSourceTitle( $extsource, $extsourceID );
636            }
637
638            if ( is_array( $noteAgainstIDs['contacts'] ) && count( $noteAgainstIDs['contacts'] ) > 0 ) {
639                foreach ( $noteAgainstIDs['contacts'] as $cID ) {
640
641                    #} Add log
642                    $newLogID = zeroBS_addUpdateLog(
643                        $cID,
644                        -1,
645                        -1,
646                        array(
647                            'type'      => __( 'Transaction Created', 'zero-bs-crm' ),
648                            'shortdesc' => $noteShortDesc,
649                            'longdesc'  => $note_long_description,
650                        ),
651                        'zerobs_customer'
652                    );
653
654                }
655            }
656    } // / if autolog
657
658    return $newLogID;
659}
660
661    #} Adds a "created" log to customer (of task) (if setting)
662function zeroBSCRM_IA_NewEventLog( $obj = array() ) {
663
664    $newLogID = false;
665
666    #} if setting
667    $autoLogThis = zeroBSCRM_getSetting( 'autolog_event_new' );
668
669    #} if has id
670    $zbsNoteAgainstPostID = -1;
671    if ( is_array( $obj ) && isset( $obj['againstid'] ) && $obj['againstid'] > 0 ) {
672        $zbsNoteAgainstPostID = (int) $obj['againstid'];
673    }
674
675    if ( $autoLogThis > 0 && isset( $zbsNoteAgainstPostID ) && ! empty( $zbsNoteAgainstPostID ) ) {
676
677            #} Retrieve necessary info:
678            $noteAgainstIDs = array(
679                'contacts'  => array(),
680                'companies' => array(),
681            );
682            if ( is_array( $obj ) && isset( $obj['againstids'] ) && is_array( $obj['againstids'] ) ) {
683
684                // trusting they're correctly passed...
685                if ( isset( $obj['againstids']['contacts'] ) && is_array( $obj['againstids']['contacts'] ) ) {
686                    $noteAgainstIDs['contacts'] = $obj['againstids']['contacts'];
687                }
688                if ( isset( $obj['againstids']['companies'] ) && is_array( $obj['againstids']['companies'] ) ) {
689                    $noteAgainstIDs['companies'] = $obj['againstids']['companies'];
690                }
691            }
692            $taskID = '';
693            if ( is_array( $obj ) && isset( $obj['id'] ) ) {
694                $taskID = $obj['id'];
695            }
696            $taskTitle = '';
697            if ( is_array( $obj ) && is_array( $obj['data'] ) && isset( $obj['data']['title'] ) && ! empty( $obj['data']['title'] ) ) {
698                $taskTitle = $obj['data']['title'];
699            }
700            $taskDescription = '';
701            if ( is_array( $obj ) && is_array( $obj['data'] ) && isset( $obj['data']['desc'] ) && ! empty( $obj['data']['desc'] ) ) {
702                $taskDescription = $obj['data']['desc'];
703            }
704            $taskStart = '';
705            if ( is_array( $obj ) && is_array( $obj['data'] ) && isset( $obj['data']['start'] ) && ! empty( $obj['data']['start'] ) ) {
706                $taskStart = zeroBSCRM_locale_utsToDatetime( $obj['data']['start'] );
707            }
708
709            // build str
710            $note_long_description = '';
711            $noteShortDesc         = '';
712            if ( ! empty( $taskID ) && empty( $taskTitle ) ) {
713                $noteShortDesc .= '#' . $taskID;
714            }
715            if ( ! empty( $taskTitle ) ) {
716                $noteShortDesc .= $taskTitle;
717            }
718            if ( ! empty( $taskDescription ) ) {
719                $note_long_description = $taskDescription;
720            }
721            if ( ! empty( $taskStart ) ) {
722
723                // pad if filled
724                if ( ! empty( $note_long_description ) ) {
725                    $note_long_description .= '<br />';
726                }
727
728                // add starting date
729                $note_long_description .= __( 'Starts at ', 'zero-bs-crm' ) . ' ' . $taskStart;
730
731            }
732
733            if ( is_array( $noteAgainstIDs['contacts'] ) && count( $noteAgainstIDs['contacts'] ) > 0 ) {
734                foreach ( $noteAgainstIDs['contacts'] as $cID ) {
735
736                    #} Add log
737                    $newLogID = zeroBS_addUpdateLog(
738                        $cID,
739                        -1,
740                        -1,
741                        array(
742                            'type'      => __( 'Task Created', 'zero-bs-crm' ),
743                            'shortdesc' => $noteShortDesc,
744                            'longdesc'  => $note_long_description,
745                        ),
746                        'zerobs_customer'
747                    );
748
749                }
750            }
751    }
752
753    return $newLogID;
754}
755
756/**
757 * Catches new logs and updates contact 'last contacted' if contact type log
758 *
759 * @param array $obj Array containing log details.
760 */
761function zeroBSCRM_IA_NewLogCatchContactsDB2( $obj = array() ) {
762
763    global $zbs;
764
765    // for now, only contacts
766
767    if ( is_array( $obj ) && isset( $obj['logagainsttype'] ) && ( $obj['logagainsttype'] === 'zerobs_customer' || $obj['logagainsttype'] === ZBS_TYPE_CONTACT ) ) {
768
769        // check if 'contact' type
770        $log_type = '';
771        if ( is_array( $obj ) && isset( $obj['logtype'] ) ) {
772            $log_type = $obj['logtype'];
773        }
774
775        if ( ! empty( $log_type ) && in_array( $log_type, $zbs->DAL->logs->contact_log_types, true ) ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
776
777            // checks out..proceed
778
779            // retrieve
780            $contact_id = -1;
781            if ( is_array( $obj ) && isset( $obj['logagainst'] ) ) {
782                $contact_id = (int) $obj['logagainst'];
783            }
784
785            if ( ! empty( $contact_id ) && $contact_id > 0 ) {
786
787                // update contact
788                $zbs->DAL->contacts->setContactLastContactUTS( $contact_id, time() ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
789            }
790        } // is a contact type log
791
792    } // is log against contact
793}
794
795    #} Adds a "created" log to customer (if setting)
796function zeroBSCRM_IA_NewClientPortalUserLog( $obj = array() ) {
797
798    # if setting
799    $autoLogThis = zeroBSCRM_getSetting( 'autolog_clientportal_new' );
800
801    if ( $autoLogThis > 0 ) {
802
803        #} Retrieve necessary info:
804        $zbsNoteAgainstPostID = -1;
805        if ( is_array( $obj ) && isset( $obj['againstid'] ) && $obj['againstid'] > 0 ) {
806            $zbsNoteAgainstPostID = (int) $obj['againstid'];
807        }
808        #TRANSITIONTOMETANO
809        $userID = -1;
810        if ( is_array( $obj ) && isset( $obj['id'] ) && $obj['id'] > 0 ) {
811            $userID = (int) $obj['id'];
812        }
813        $userEmail = '';
814        if ( is_array( $obj ) && isset( $obj['userEmail'] ) ) {
815            $userEmail = $obj['userEmail'];
816        }
817        $noteShortDesc = '';
818        if ( ! empty( $userEmail ) ) {
819            $noteShortDesc = __( 'Created with email', 'zero-bs-crm' ) . ': ' . $userEmail;
820        }
821
822        if ( $userID > 0 ) {
823            $noteShortDesc .= ' (#' . $userID . ')';
824        }
825
826        if ( isset( $zbsNoteAgainstPostID ) && ! empty( $zbsNoteAgainstPostID ) ) {
827
828            #} Add log
829            $newLogID = zeroBS_addUpdateContactLog(
830                $zbsNoteAgainstPostID,
831                -1,
832                -1,
833                array(
834                    'type'      => 'Client Portal User Created',
835                    'shortdesc' => $noteShortDesc,
836                    'longdesc'  => '',
837                )
838            );
839
840        }
841    }
842}
843
844    #} creates customer client portal user (if setting)
845function zeroBSCRM_IA_NewCustomerClientPortal( $obj = array() ) {
846
847    # if setting
848    $autoFireThis = zeroBSCRM_getSetting( 'portalusers' );
849
850    if ( $autoFireThis > 0 ) {
851
852        #} Retrieve necessary info:
853        $userID = -1;
854        if ( is_array( $obj ) && isset( $obj['id'] ) && $obj['id'] > 0 ) {
855            $userID = (int) $obj['id'];
856        }
857
858        // yup
859        $okayToFire = true;
860
861        // Specific status mode ==================================
862
863        #} If using "specific statuses only"
864        $statusList = zeroBSCRM_getSetting( 'portalusers_status' );
865        if ( ! is_array( $statusList ) && ( empty( $statusList ) || $statusList == 'all' ) ) {
866
867            // nothing to do
868
869        } elseif ( is_array( $statusList ) ) {
870
871                // generate a list of "Okay" statuses that this'll check later on...
872                $zbsStatusStr = zeroBSCRM_getCustomerStatuses();
873                $zbsStatuses  = explode( ',', $zbsStatusStr );
874                $okayStatuses = array();
875
876                // cycle through settings + copy "full str" rather than "full_str" that it'll be saved as
877            foreach ( $zbsStatuses as $statusStr ) {
878
879                    // permify
880                    $statusKey = strtolower( str_replace( ' ', '_', str_replace( ':', '_', $statusStr ) ) );
881
882                    // present?
883                if ( in_array( $statusKey, $statusList ) ) {
884                    $okayStatuses[] = $statusStr;
885                }
886            }
887
888                // is user's status in one of these?
889                $customerStatus = '';
890            if ( isset( $obj['customerMeta'] ) && is_array( $obj['customerMeta'] ) && isset( $obj['customerMeta']['status'] ) ) {
891                $customerStatus = $obj['customerMeta']['status'];
892            }
893
894                    // if no status, try fill from (whatever was added) to db
895            if ( empty( $customerStatus ) ) {
896
897                $cMeta = zeroBS_getCustomerMeta( $userID );
898
899                if ( is_array( $cMeta ) && isset( $cMeta['status'] ) ) {
900                    $customerStatus = $cMeta['status'];
901                }
902            }
903
904                    // check status
905            if ( ! empty( $customerStatus ) && in_array( $customerStatus, $okayStatuses ) ) {
906                $okayToFire = true;
907            } else {
908                $okayToFire = false; // customer status empty or not in approved list :)
909            }
910        } else {
911
912            // non-standard val for status list, override it to all
913            global $zbs;
914            $zbs->settings->update( 'portalusers_status', 'all' );
915
916            // and let it fire..
917
918        }
919
920        // / Specific status mode ==================================
921
922        if ( $okayToFire ) {
923
924            // this'll check itself if already exists, so no harm in letting it (potentially) multifire
925            if ( $userID > 0 ) {
926                zeroBSCRM_createClientPortalUserFromRecord( $userID );
927            }
928        }
929    }
930}
931
932    #} Compiles any segments which this contact fits in
933    // works for new contacts + contact edits
934function zeroBSCRM_IA_ContactSegmentCompiler( $obj = array() ) {
935
936    # if setting
937    $autoCompileSegments = 1;
938
939    if ( $autoCompileSegments > 0 ) {
940
941        #} Retrieve necessary info:
942        $zbsNoteAgainstPostID = -1;
943        if ( is_array( $obj ) && isset( $obj['id'] ) ) {
944            $zbsNoteAgainstPostID = (int) $obj['id'];
945        }
946        $contactWasInSegments = array();
947        if ( is_array( $obj ) && isset( $obj['prevSegments'] ) && is_array( $obj['prevSegments'] ) ) {
948            $contactWasInSegments = $obj['prevSegments'];
949        }
950
951        if ( isset( $zbsNoteAgainstPostID ) && ! empty( $zbsNoteAgainstPostID ) ) {
952
953            global $zbs;
954                $zbs->DAL->segments->compileSegmentsAffectedByContact( $zbsNoteAgainstPostID, $contactWasInSegments );
955        }
956    }
957}
958
959    /*
960    * Recompiles any segments affected by quote change
961    *
962    * Fires on:
963    * quote.new
964    * quote.update
965    *
966    */
967function zeroBSCRM_IA_quoteSegmentCompiler( $obj = array() ) {
968
969    global $zbs;
970
971    // if quote passed:
972    if ( is_array( $obj ) && isset( $obj['data'] ) ) {
973        $zbs->DAL->segments->compileSegmentsAffectedByQuote( $obj['data'] ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
974    }
975}
976
977    /*
978    * Recompiles any segments affected by invoice change
979    *
980    * Fires on:
981    * invoice.new
982    * invoice.update
983    *
984    */
985function zeroBSCRM_IA_invoiceSegmentCompiler( $obj = array() ) {
986
987    global $zbs;
988
989    // if quote passed:
990    if ( is_array( $obj ) && isset( $obj['data'] ) ) {
991        $zbs->DAL->segments->compileSegmentsAffectedByInvoice( $obj['data'] ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
992    }
993}
994
995    /*
996    * Recompiles any segments affected by transaction change
997    *
998    * Fires on:
999    * transaction.new
1000    * transaction.update
1001    *
1002    */
1003function zeroBSCRM_IA_transactionSegmentCompiler( $obj = array() ) {
1004
1005    global $zbs;
1006
1007    // if quote passed:
1008    if ( is_array( $obj ) && isset( $obj['data'] ) ) {
1009        $zbs->DAL->segments->compileSegmentsAffectedByTransaction( $obj['data'] ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
1010    }
1011}
1012
1013    #} when customer status changes, portal access can be revoked/added based on status (if setting)
1014function zeroBSCRM_IA_CustomerStatusChangePortalAndLog( $obj = array() ) {
1015
1016    # if setting
1017    $autoFireThis = zeroBSCRM_getSetting( 'portalusers' );
1018
1019    if ( $autoFireThis > 0 ) {
1020
1021        #} Retrieve necessary info:
1022        $userID = -1;
1023        if ( is_array( $obj ) && isset( $obj['id'] ) && $obj['id'] > 0 ) {
1024            $userID = (int) $obj['id'];
1025        }
1026
1027        #} If using "specific statuses only"
1028        $statusList = zeroBSCRM_getSetting( 'portalusers_status' );
1029        if ( ! is_array( $statusList ) && ( empty( $statusList ) || $statusList == 'all' ) ) {
1030
1031            // nothing to do - all statuses allowed
1032
1033        } elseif ( is_array( $statusList ) ) {
1034
1035                // generate a list of "Okay" statuses that this'll check later on...
1036                $zbsStatusStr = zeroBSCRM_getCustomerStatuses();
1037                $zbsStatuses  = explode( ',', $zbsStatusStr );
1038                $okayStatuses = array();
1039
1040                // cycle through settings + copy "full str" rather than "full_str" that it'll be saved as
1041            foreach ( $zbsStatuses as $statusStr ) {
1042
1043                    // permify
1044                    $statusKey = strtolower( str_replace( ' ', '_', str_replace( ':', '_', $statusStr ) ) );
1045
1046                    // present?
1047                if ( in_array( $statusKey, $statusList ) ) {
1048                    $okayStatuses[] = $statusStr;
1049                }
1050            }
1051
1052                // is user's status in one of these?
1053                $customerStatus = '';
1054            if ( isset( $obj['customerMeta'] ) && is_array( $obj['customerMeta'] ) && isset( $obj['customerMeta']['status'] ) ) {
1055                $customerStatus = $obj['customerMeta']['status'];
1056            }
1057
1058                    // if no status, try fill from (whatever was added) to db
1059            if ( empty( $customerStatus ) ) {
1060
1061                $cMeta = zeroBS_getCustomerMeta( $userID );
1062
1063                if ( is_array( $cMeta ) && isset( $cMeta['status'] ) ) {
1064                    $customerStatus = $cMeta['status'];
1065                }
1066            }
1067
1068                    // check status
1069            if ( ! empty( $customerStatus ) && in_array( $customerStatus, $okayStatuses ) ) {
1070
1071                // NEEDS account access
1072
1073                    // already got?
1074                    $portalID = zeroBSCRM_getClientPortalUserID( $userID );
1075
1076                if ( ! empty( $portalID ) && $portalID > 0 ) {
1077
1078                    $isDisabled = zeroBSCRM_isCustomerPortalDisabled( $userID );
1079
1080                    // if disabled
1081                    if ( $isDisabled ) {
1082
1083                        // already got acc, make sure enabled
1084                        zeroBSCRM_customerPortalDisableEnable( $userID, 'enable' );
1085
1086                            $noteShortDesc = __( 'Access enabled (by change of status to', 'zero-bs-crm' ) . ' "' . $customerStatus . '"';
1087
1088                            #} Add log
1089                            $newLogID = zeroBS_addUpdateContactLog(
1090                                $userID,
1091                                -1,
1092                                -1,
1093                                array(
1094                                    'type'      => 'Client Portal Access Changed',
1095                                    'shortdesc' => $noteShortDesc,
1096                                    'longdesc'  => '',
1097                                )
1098                            );
1099
1100                    }
1101                } else {
1102
1103                        // make acc
1104                    if ( $userID > 0 ) {
1105
1106                        zeroBSCRM_createClientPortalUserFromRecord( $userID );
1107
1108                        $noteShortDesc = __( 'Access created (by change of status to', 'zero-bs-crm' ) . ' "' . $customerStatus . '"';
1109
1110                        #} Add log
1111                        $newLogID = zeroBS_addUpdateContactLog(
1112                            $userID,
1113                            -1,
1114                            -1,
1115                            array(
1116                                'type'      => 'Client Portal Access Changed',
1117                                'shortdesc' => $noteShortDesc,
1118                                'longdesc'  => '',
1119                            )
1120                        );
1121
1122                    }
1123                }
1124            } else {
1125
1126                    // SHOULD Not have account
1127
1128                        // already got?
1129                        $portalID = zeroBSCRM_getClientPortalUserID( $userID );
1130
1131                if ( ! empty( $portalID ) && $portalID > 0 ) {
1132
1133                    $isDisabled = zeroBSCRM_isCustomerPortalDisabled( $userID );
1134
1135                    // if not already disabled
1136                    if ( ! $isDisabled ) {
1137
1138                        // disable if found
1139                        zeroBSCRM_customerPortalDisableEnable( $userID, 'disable' );
1140
1141                        $noteShortDesc = __( 'Access disabled (by change of status to', 'zero-bs-crm' ) . ' "' . $customerStatus . '"';
1142
1143                        #} Add log
1144                        $newLogID = zeroBS_addUpdateContactLog(
1145                            $userID,
1146                            -1,
1147                            -1,
1148                            array(
1149                                'type'      => 'Client Portal Access Changed',
1150                                'shortdesc' => $noteShortDesc,
1151                                'longdesc'  => '',
1152                            )
1153                        );
1154
1155                    }
1156                }
1157            }
1158        } else {
1159
1160            // non-standard val for status list, override it to all
1161            global $zbs;
1162            $zbs->settings->update( 'portalusers_status', 'all' );
1163
1164            // and... nothing to do - all statuses allowed
1165
1166        }
1167
1168        // / Specific status mode ==================================
1169
1170    } // / if autofire on :)
1171}
1172
1173    #} Adds a "changed" log when customer status change (if setting)
1174function zeroBSCRM_IA_CustomerStatusChangeAutoLog( $obj = array() ) {
1175
1176    # if setting
1177    $autoLogThis = zeroBSCRM_getSetting( 'autolog_customer_statuschange' );
1178
1179    if ( $autoLogThis > 0 ) {
1180
1181        #} Retrieve necessary info:
1182        $zbsNoteAgainstPostID = -1;
1183        if ( is_array( $obj ) && isset( $obj['againstid'] ) && $obj['againstid'] > 0 ) {
1184            $zbsNoteAgainstPostID = (int) $obj['againstid'];
1185        }
1186        #TRANSITIONTOMETANO
1187
1188        // I nicely pass these...
1189        $from = '';
1190        if ( is_array( $obj ) && isset( $obj['from'] ) && ! empty( $obj['from'] ) ) {
1191            $from = $obj['from'];
1192        }
1193        $to = '';
1194        if ( is_array( $obj ) && isset( $obj['to'] ) && ! empty( $obj['to'] ) ) {
1195            $to = $obj['to'];
1196        }
1197
1198        if ( isset( $zbsNoteAgainstPostID ) && ! empty( $zbsNoteAgainstPostID ) && isset( $to ) && ! empty( $to ) ) {
1199
1200            $shortDesc = '';
1201            if ( ! empty( $from ) ) {
1202                $shortDesc = __( 'From', 'zero-bs-crm' ) . ' "' . $from . '" ' . __( 'to', 'zero-bs-crm' ) . ' "' . $to . '"';
1203            } else {
1204                $shortDesc = __( 'To', 'zero-bs-crm' ) . ' "' . $to . '"';
1205            }
1206
1207            #} Add log
1208            $newLogID = zeroBS_addUpdateContactLog(
1209                $zbsNoteAgainstPostID,
1210                -1,
1211                -1,
1212                array(
1213                    'type'      => 'Status Change',
1214                    'shortdesc' => $shortDesc,
1215                    'longdesc'  => '',
1216                )
1217            );
1218
1219        }
1220    }
1221}
1222
1223/*
1224======================================================
1225    / Internal Automator Recipe Functions
1226    ====================================================== */
1227
1228/*
1229======================================================
1230    Internal Automator Recipe Functions - WP HOOK tieins... just middlemen here really
1231    ====================================================== */
1232
1233    #} Fires the hook & passes in the obj, for those who still want to use wp_hook's rather than IA Automator
1234
1235    #} Fires on 'customer.new' IA
1236function zeroBSCRM_IA_NewCustomerWPHook( $obj = array() ) {
1237
1238    if ( is_array( $obj ) && isset( $obj['id'] ) && ! empty( $obj['id'] ) ) {
1239
1240        do_action( 'jpcrm_after_contact_insert', $obj['id'] );
1241
1242        // legacy, use `jpcrm_after_contact_insert` from 5.3+
1243        do_action( 'zbs_new_customer', $obj['id'] );
1244
1245    }
1246}
1247
1248/**
1249 * Fires on 'contact.update', 'contact.email.update', and 'contact.status.update IA.
1250 *
1251 * @param array $obj An array holding contact object data.
1252 */
1253function zeroBSCRM_IA_EditCustomerWPHook( $obj = array() ) {
1254
1255    if ( is_array( $obj ) && isset( $obj['id'] ) && ! empty( $obj['id'] ) ) {
1256
1257        do_action( 'jpcrm_after_contact_update', $obj['id'] );
1258
1259        // legacy, use `jpcrm_after_contact_update` from 5.3+
1260        do_action( 'zbs_edit_customer', $obj['id'] );
1261
1262    }
1263}
1264
1265    #} Fires on 'customer.vitals.edit' IA.
1266function zeroBSCRM_IA_EditCustomerVitalsWPHook( $obj = array() ) {
1267
1268    if ( is_array( $obj ) && isset( $obj['id'] ) && ! empty( $obj['id'] ) ) {
1269        do_action( 'zbs_edit_customer_vitals', $obj['id'] );
1270    }
1271}
1272
1273    /**
1274     * Fires on 'contact.email.update' IA. Now legacy, redirecting to zeroBSCRM_IA_EditCustomerWPHook
1275     *
1276     * @param array $obj An array holding contact object data.
1277     */
1278function zeroBSCRM_IA_EditCustomerEmailWPHook( $obj = array() ) {
1279
1280    if ( is_array( $obj ) && isset( $obj['id'] ) && ! empty( $obj['id'] ) ) {
1281        do_action( 'zbs_edit_customer_email', $obj['id'] );
1282    }
1283}
1284
1285/**
1286 * Fires on 'contact.delete' IA.
1287 *
1288 * @param array $obj An array holding contact object data.
1289 */
1290function zeroBSCRM_IA_DeleteCustomerWPHook( $obj = array() ) {
1291
1292    if ( is_array( $obj ) && isset( $obj['id'] ) && ! empty( $obj['id'] ) ) {
1293        // Legacy:
1294        do_action( 'zbs_delete_customer', $obj['id'] );
1295    }
1296}
1297
1298    /**
1299     * Fires on 'contact.before.delete' IA.
1300     *
1301     * @param array $obj An array holding contact object data.
1302     */
1303function zeroBSCRM_IA_BeforeDeleteCustomerWPHook( $obj = array() ) {
1304    if ( is_array( $obj ) && isset( $obj['id'] ) && ! empty( $obj['id'] ) ) {
1305        do_action( 'jpcrm_before_delete_contact', $obj );
1306    }
1307}
1308
1309/**
1310 * Fires on 'company.new' IA.
1311 *
1312 * @param array $obj An array holding company object data.
1313 */
1314function zeroBSCRM_IA_NewCompanyWPHook( $obj = array() ) {
1315
1316    if ( is_array( $obj ) && isset( $obj['id'] ) && ! empty( $obj['id'] ) ) {
1317
1318        // Legacy:
1319        do_action( 'zbs_new_company', $obj['id'] );
1320
1321    }
1322}
1323
1324/**
1325 * Fires on 'company.delete' IA.
1326 *
1327 * @param array $obj An array holding company object data.
1328 */
1329function zeroBSCRM_IA_DeleteCompanyWPHook( $obj = array() ) {
1330
1331    if ( is_array( $obj ) && isset( $obj['id'] ) && ! empty( $obj['id'] ) ) {
1332
1333        // Legacy:
1334        do_action( 'zbs_delete_company', $obj['id'] );
1335    }
1336}
1337    #} Fires on 'quote.new' IA
1338function zeroBSCRM_IA_NewQuoteWPHook( $obj = array() ) {
1339
1340    if ( is_array( $obj ) && isset( $obj['id'] ) && ! empty( $obj['id'] ) ) {
1341        do_action( 'zbs_new_quote', $obj['id'] );
1342    }
1343}
1344    #} Fires on 'quote.accepted' IA
1345function zeroBSCRM_IA_AcceptedQuoteWPHook( $obj = array() ) {
1346
1347    if ( is_array( $obj ) && isset( $obj['id'] ) && ! empty( $obj['id'] ) ) {
1348        do_action( 'jpcrm_quote_accepted', $obj['id'] );
1349    }
1350}
1351    #} Fires on 'quote.delete' IA
1352function zeroBSCRM_IA_DeleteQuoteWPHook( $obj = array() ) {
1353
1354    if ( is_array( $obj ) && isset( $obj['id'] ) && ! empty( $obj['id'] ) ) {
1355        do_action( 'zbs_delete_quote', $obj['id'] );
1356    }
1357}
1358
1359/**
1360 * Fires on 'invoice.new' IA.
1361 *
1362 * @param array $obj An array holding invoice object data.
1363 */
1364function zeroBSCRM_IA_NewInvoiceWPHook( $obj = array() ) {
1365
1366    if ( is_array( $obj ) && isset( $obj['id'] ) && ! empty( $obj['id'] ) ) {
1367        do_action( 'zbs_new_invoice', $obj['id'] );
1368    }
1369}
1370
1371/**
1372 * Fires on 'invoice.update' and 'invoice.status.update' IA.
1373 *
1374 * @param array $obj An array holding invoice object data.
1375 */
1376function zeroBSCRM_IA_EditInvoiceWPHook( $obj = array() ) {
1377    if ( is_array( $obj ) && isset( $obj['id'] ) && ! empty( $obj['id'] ) ) {
1378        do_action( 'zbs_new_invoice', $obj['id'] );
1379    }
1380}
1381
1382/**
1383 * Fires on 'invoice.delete' IA.
1384 *
1385 * @param array $obj An array holding invoice object data.
1386 */
1387function zeroBSCRM_IA_DeleteInvoiceWPHook( $obj = array() ) {
1388
1389    if ( is_array( $obj ) && isset( $obj['id'] ) && ! empty( $obj['id'] ) ) {
1390        do_action( 'zbs_delete_invoice', $obj['id'] );
1391    }
1392}
1393
1394    #} Fires on 'transaction.new' IA
1395function zeroBSCRM_IA_NewTransactionWPHook( $obj = array() ) {
1396
1397    if ( is_array( $obj ) && isset( $obj['id'] ) && ! empty( $obj['id'] ) ) {
1398        do_action( 'zbs_new_transaction', $obj['id'] );
1399    }
1400}
1401    #} Fires on 'transaction.delete' IA
1402function zeroBSCRM_IA_DeleteTransactionWPHook( $obj = array() ) {
1403
1404    if ( is_array( $obj ) && isset( $obj['id'] ) && ! empty( $obj['id'] ) ) {
1405        do_action( 'zbs_delete_transaction', $obj['id'] );
1406    }
1407}
1408    #} Fires on 'event.new' IA
1409function zeroBSCRM_IA_NewEventWPHook( $obj = array() ) {
1410
1411    if ( is_array( $obj ) && isset( $obj['id'] ) && ! empty( $obj['id'] ) ) {
1412        do_action( 'zbs_new_event', $obj['id'] );
1413    }
1414}
1415    #} Fires on 'event.update' IA
1416function zeroBSCRM_IA_UpdateEventWPHook( $obj = array() ) {
1417
1418    if ( is_array( $obj ) && isset( $obj['id'] ) && ! empty( $obj['id'] ) ) {
1419        do_action( 'zbs_update_event', $obj['id'] );
1420    }
1421}
1422    #} Fires on 'event.delete' IA
1423function zeroBSCRM_IA_DeleteEventWPHook( $obj = array() ) {
1424
1425    if ( is_array( $obj ) && isset( $obj['id'] ) && ! empty( $obj['id'] ) ) {
1426        do_action( 'zbs_delete_event', $obj['id'] );
1427    }
1428}
1429    #} Fires on 'clientwpuser.new' IA
1430function zeroBSCRM_IA_NewClientPortalUserHook( $obj = array() ) {
1431
1432    if ( is_array( $obj ) && isset( $obj['id'] ) && ! empty( $obj['id'] ) ) {
1433        do_action( 'zbs_new_client_portal_user', $obj['id'] );
1434    }
1435}
1436    #} Fires on 'form.delete' IA
1437function zeroBSCRM_IA_DeleteFormWPHook( $obj = array() ) {
1438
1439    if ( is_array( $obj ) && isset( $obj['id'] ) && ! empty( $obj['id'] ) ) {
1440        do_action( 'zbs_delete_form', $obj['id'] );
1441    }
1442}
1443    #} Fires on 'segment.delete' IA
1444function zeroBSCRM_IA_DeleteSegmentWPHook( $obj = array() ) {
1445
1446    if ( is_array( $obj ) && isset( $obj['id'] ) && ! empty( $obj['id'] ) ) {
1447        do_action( 'zbs_delete_segment', $obj['id'] );
1448    }
1449}
1450
1451/*
1452======================================================
1453    / Internal Automator Recipe Functions - WP HOOK tieins
1454    ====================================================== */