Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 145
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
zeroBSCRM_TagManager
0.00% covered (danger)
0.00%
0 / 144
0.00% covered (danger)
0.00%
0 / 3
1980
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 1
462
 catchPost
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
12
 drawTagView
0.00% covered (danger)
0.00%
0 / 105
0.00% covered (danger)
0.00%
0 / 1
420
1<?php
2/*
3 * Jetpack CRM
4 * https://jetpackcrm.com
5 * V2.52+
6 *
7 * Copyright 2020 Automattic
8 *
9 * Date: 19/03/18
10 */
11
12defined( 'ZEROBSCRM_PATH' ) || exit( 0 );
13
14class zeroBSCRM_TagManager {
15
16    private $objTypeID = false;
17
18    // following all set by objTypeID pass, v3.0+
19    private $objType  = false;
20    private $singular = false;
21    private $plural   = false;
22    // v3.0 this was removed private $postType = false; // set in child class 'zerobs_customer' // ONLY USED IN save funcs etc. maybe, potentially just legacy now.
23    // renamed 'listViewSlug' v3.0+ private $postPage = false;
24    private $listViewSlug = false;
25
26    // except these:
27    private $langLabels = false;
28    private $extraBoxes = '';
29
30    function __construct( $args = array() ) {
31
32        #} =========== LOAD ARGS ==============
33        $defaultArgs = array(
34
35            'objTypeID'    => false,   // 5 (v3.0+)
36
37            'objType'      => false,   // transaction
38            'singular'     => false,  // Transaction
39            'plural'       => false,      // Transactions
40            // 'postType' => false,    //zerobs_transaction - removed v3.0 +
41            // renamed 'listViewSlug' v3.0+ 'postPage' => false,    //manage-transactions-tags
42            'listViewSlug' => false,
43            'langLabels'   => array(),
44            'extraBoxes'   => '', // html for extra boxes e.g. upsells :)
45
46        );
47        foreach ( $defaultArgs as $argK => $argV ) {
48            $$argK = $argV;
49            if ( is_array( $args ) && isset( $args[ $argK ] ) ) {
50                if ( is_array( $args[ $argK ] ) ) {
51                    $newData = $$argK;
52                    if ( ! is_array( $newData ) ) {
53                        $newData = array();
54                    } foreach ( $args[ $argK ] as $subK => $subV ) {
55                        $newData[ $subK ] = $subV;
56                    }$$argK = $newData;
57                } else {
58                    $$argK = $args[ $argK ]; }
59            }
60        }
61        #} =========== / LOAD ARGS =============
62        global $zbs;
63
64        // we load from DAL defaults, if objType passed (overriding anything passed, if empty/false)
65        if ( ! empty( $objTypeID ) ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
66
67            $objTypeID = (int) $objTypeID;
68            if ( $objTypeID > 0 ) {
69                // obj type (contact)
70                $objTypeStr = $zbs->DAL->objTypeKey( $objTypeID );
71                if ( ( ! isset( $this->objType ) || $this->objType == false ) && ! empty( $objTypeStr ) ) {
72                    $this->objType = $objTypeStr;
73                }
74
75                // singular
76                $objSingular = $zbs->DAL->typeStr( $objTypeID );
77                if ( ( ! isset( $this->singular ) || $this->singular == false ) && ! empty( $objSingular ) ) {
78                    $this->singular = $objSingular;
79                }
80
81                // plural
82                $objPlural = $zbs->DAL->typeStr( $objTypeID, true );
83                if ( ( ! isset( $this->plural ) || $this->plural == false ) && ! empty( $objPlural ) ) {
84                    $this->plural = $objPlural;
85                }
86
87                // listViewSlug
88                $objSlug = $zbs->DAL->listViewSlugFromObjID( $objTypeID );
89                if ( ( ! isset( $this->listViewSlug ) || $this->listViewSlug == false ) && ! empty( $objSlug ) ) {
90                    $this->listViewSlug = $objSlug;
91                }
92            }
93        }
94
95        // anything to save?
96        $this->catchPost();
97    }
98
99    public function catchPost() {
100
101        // If post, fire do_action
102        if ( isset( $_POST['zbs-tag-form-master'] ) && $_POST['zbs-tag-form-master'] == $this->objType ) {
103
104            // fire it
105            do_action( 'zerobs_save_' . $this->objType . '_tags', $this->objID, $this->obj );
106
107        }
108    }
109
110    public function drawTagView() {
111
112        if ( empty( $this->objType ) || empty( $this->listViewSlug ) || empty( $this->singular ) || empty( $this->plural ) ) {
113
114            return 'Error.';
115        }
116
117        global $zbs;
118
119        ?><div id="zbs-edit-master-wrap"><form method="post" id="zbs-edit-form" enctype="multipart/form-data"><input type="hidden" name="zbs-edit-form-master" value="<?php echo esc_attr( $this->objType ); ?>" />
120        <style>
121
122        </style>
123        <?php
124        // left in for later :)
125                $currentFields = array();
126        $allFields             = array();
127        ?>
128            <!-- field editor -->
129            <div id="zbs-edit-field-editor" class="ui segment secondary">
130            <?php if ( current_user_can( 'administrator' ) ) { ?>
131
132                <h4 class="ui horizontal divider header">
133                    <i class="list layout icon"></i>
134                    <?php echo esc_html( $this->singular ) . ' ' . esc_html__( 'Field Manager', 'zero-bs-crm' ); ?>
135                </h4>
136
137                <div id="zbs-edit-field-wrap" class="ui divided grid">
138
139                    <div class="ui active inverted dimmer hidden" id="zbs-field-manager-loading" style="display:none">
140                    <div class="ui text loader"><?php esc_html_e( 'Loading', 'zero-bs-crm' ); ?></div>
141                    </div>
142
143                    <div class="row">
144                        <div class="twelve wide column">
145
146                            <h4><?php esc_html_e( 'Current Fields', 'zero-bs-crm' ); ?></h4>
147
148
149                            <div id="zbs-column-manager-current-fields" class="ui segment zbs-column-manager-connected"> 
150                                <?php
151                                if ( is_array( $currentFields ) ) {
152                                    foreach ( $currentFields as $colKey => $col ) {
153
154                                        ?>
155                                    <div id="zbs-column-manager-field-<?php echo esc_attr( $colKey ); ?>" class="ui basic button zbs-column-manager-field" data-key="<?php echo esc_attr( $colKey ); ?>"><?php esc_html_e( $col[0], 'zero-bs-crm' ); ?></div>
156                                        <?php
157
158                                    }
159                                }
160                                ?>
161                            </div>
162
163                        </div>
164                        <div class="four wide column">
165
166                            <h4><?php esc_html_e( 'Available Fields', 'zero-bs-crm' ); ?></h4>
167
168                            <div id="zbs-column-manager-available-fields" class="ui segment zbs-column-manager-connected"> 
169                                <?php
170                                if ( is_array( $allFields ) ) {
171                                    foreach ( $allFields as $colKey => $col ) {
172
173                                        if ( ! array_key_exists( $colKey, $currentFields ) ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
174
175                                            ?>
176                                        <div id="zbs-column-manager-field-<?php echo esc_attr( $colKey ); ?>" class="ui basic button zbs-column-manager-field" data-key="<?php echo esc_attr( $colKey ); ?>"><?php esc_html_e( $col[0], 'zero-bs-crm' ); ?></div>
177                                            <?php
178
179                                        }
180                                    }
181                                }
182                                ?>
183                            </div>
184
185                        </div>
186                    </div>
187                </div>
188
189
190
191            <?php } // / can admin ?>
192
193            </div>
194            <!-- field manager -->
195
196            <div id="zbs-edit-warnings-wrap">
197                <?php
198                #} Pre-loaded msgs, because I wrote the helpers in php first... should move helpers to js and fly these
199
200                echo zeroBSCRM_UI2_messageHTML( 'warning hidden', 'Error Retrieving ' . $this->plural, 'There has been a problem retrieving your ' . $this->singular . ', if this issue persists, please contact support.', 'disabled warning sign', 'zbsCantLoadData' );
201
202                ?>
203            </div>
204            <!-- main view: list + sidebar -->
205            <div id="zbs-edit-wrap" class="ui divided grid">
206
207                <?php
208
209                if ( count( $zbs->pageMessages ) > 0 ) {
210
211                    #} Updated Msgs
212                    // was doing like this, but need control over styling
213                    // do_action( 'zerobs_updatemsg_contact');
214                    // so for now just using global :)
215                    echo '<div class="row" style="padding-bottom: 0 !important;"><div class="sixteen wide column" id="zbs-edit-notification-wrap">';
216
217                    foreach ( $zbs->pageMessages as $msg ) {
218
219                        // for now these can be any html :)
220                        echo $msg;
221
222                    }
223
224                        echo '</div></div>';
225
226                }
227
228                ?>
229
230                <div class="row">
231
232
233                    <!-- record list -->
234                    <div class="twelve wide column" id="zbs-edit-table-wrap">
235
236                        <?php
237                            #} Main Metaboxes
238                            zeroBSCRM_do_meta_boxes( 'zerobs_edit_tags', 'normal', $this->objType );
239                        ?>
240
241                    </div>
242                    <!-- side bar -->
243                    <div class="four wide column" id="zbs-edit-sidebar-wrap">
244                        <?php
245
246                            #} Sidebar metaboxes
247                            zeroBSCRM_do_meta_boxes( 'zerobs_edit_tags', 'side', $this->objType );
248
249                        ?>
250
251                        <?php ##WLREMOVE ?>
252                        <?php echo $this->extraBoxes; ?>
253                        <?php ##/WLREMOVE ?>
254                    </div>
255                </div>
256
257                <!-- could use this for mobile variant?) 
258                <div class="two column mobile only row" style="display:none"></div>
259                -->
260            </div> <!-- / mainlistview wrap -->
261        </form></div>
262
263        <script type="text/javascript">
264        <?php
265
266            // make simpler
267            $tags = $zbs->DAL->getTagsForObjType(
268                array(
269
270                    'objtypeid'    => $zbs->DAL->objTypeID( $this->objType ), // ZBS_TYPE_CONTACT in place of 'contact'=>1, 'transaction'=> etc.
271                    'excludeEmpty' => false,
272                    'withCount'    => false,
273                    'ignoreowner'  => true,
274
275                )
276            );
277            $tagsArr = array(); if ( is_array( $tags ) && count( $tags ) > 0 ) {
278            foreach ( $tags as $t ) {
279                $tagsArr[] = $t['name'];
280            }
281            }
282            $tags = $tagsArr;
283
284            ?>
285
286            // this forces firing of our custom init in admin.tags.metabox.js
287            var zbsCustomTagInitFunc = 'zbsJS_bindTagManagerInit';
288            var zbsDontDrawTags = true; // don't draw em in edit box
289            // and this OVERRIDES the tag metabox list:
290            var zbsCRMJS_currentTags = <?php echo json_encode( $tags ); ?>;
291
292            jQuery(function($){
293                console.log("======= TAG MANAGER VIEW UI =========");
294
295
296            });
297
298            // General options for listview
299            var zbsEditSettings = {
300
301                <?php /*objid: <?php echo $this->objID; ?>,*/ ?>
302                objdbname: '<?php echo esc_html( $this->objType ); ?>'
303
304            };
305            var zbsDrawEditViewBlocker = false;
306            var zbsDrawEditAJAXBlocker = false;
307            var zbsObjectViewLinkPrefixCustomer = '<?php echo jpcrm_esc_link( 'view', -1, 'zerobs_customer', true ); ?>';
308            var zbsObjectEditLinkPrefixCustomer = '<?php echo jpcrm_esc_link( 'edit', -1, 'zerobs_customer', true ); ?>';
309            var zbsObjectViewLinkPrefixCompany = '<?php echo jpcrm_esc_link( 'view', -1, 'zerobs_company', true ); ?>';
310            var zbsListViewLink = '<?php echo jpcrm_esc_link( $this->listViewSlug ); ?>';
311            var zbsClick2CallType = parseInt('<?php echo esc_html( zeroBSCRM_getSetting( 'clicktocalltype' ) ); ?>');
312            var zbsEditViewLangLabels = {
313
314                    'today': '<?php echo esc_html( zeroBSCRM_slashOut( __( 'Today', 'zero-bs-crm' ) ) ); ?>',
315
316                    <?php
317                    $labelCount = 0;
318                    if ( is_array( $this->langLabels ) && count( $this->langLabels ) > 0 ) {
319                        foreach ( $this->langLabels as $labelK => $labelV ) {
320
321                            if ( $labelCount > 0 ) {
322                                echo ',';
323                            }
324
325                            echo esc_html( $labelK ) . ":'" . esc_html( zeroBSCRM_slashOut( $labelV ) ) . "'";
326
327                            ++$labelCount;
328
329                        }
330                    }
331                    ?>
332
333            };
334            <?php
335            #} Nonce for AJAX
336                    echo "var zbscrmjs_secToken = '" . esc_js( wp_create_nonce( 'zbscrmjs-ajax-nonce' ) ) . "';";
337            ?>
338                    </script>
339                    <?php
340    } // /draw func
341} // class