Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 110
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 2
zeroBSCRM_TagManagerMetaboxSetup
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
90
zeroBS__Metabox_TagList
0.00% covered (danger)
0.00%
0 / 81
0.00% covered (danger)
0.00%
0 / 2
72
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
 html
0.00% covered (danger)
0.00%
0 / 70
0.00% covered (danger)
0.00%
0 / 1
56
zeroBS__Metabox_TagAdd
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 9
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 */
11
12defined( 'ZEROBSCRM_PATH' ) || exit( 0 );
13
14/*
15======================================================
16    Init Func
17    ====================================================== */
18
19function zeroBSCRM_TagManagerMetaboxSetup() {
20
21    // lazy page switch
22    $typeInt = -1;
23    if ( zeroBSCRM_is_customertags_page() ) {
24        $typeInt = ZBS_TYPE_CONTACT;
25    }
26    if ( zeroBSCRM_is_companytags_page() ) {
27        $typeInt = ZBS_TYPE_COMPANY;
28    }
29    if ( zeroBSCRM_is_quotetags_page() ) {
30        $typeInt = ZBS_TYPE_QUOTE;
31    }
32    if ( zeroBSCRM_is_invoicetags_page() ) {
33        $typeInt = ZBS_TYPE_INVOICE;
34    }
35    if ( zeroBSCRM_is_transactiontags_page() ) {
36        $typeInt = ZBS_TYPE_TRANSACTION;
37    }
38    if ( zeroBSCRM_is_formtags_page() ) {
39        $typeInt = ZBS_TYPE_FORM;
40    }
41    if ( zeroBSCRM_is_tasktags_page() ) {
42        $typeInt = ZBS_TYPE_TASK;
43    }
44
45    if ( $typeInt > 0 ) {
46
47            // Tag List
48            $zeroBS__Metabox_TagList = new zeroBS__Metabox_TagList( __FILE__, $typeInt );
49
50            // Add Tags
51            $zeroBS__Metabox_TagAdd = new zeroBS__Metabox_TagAdd( __FILE__, $typeInt );
52
53    }
54}
55
56    add_action( 'admin_init', 'zeroBSCRM_TagManagerMetaboxSetup' );
57
58/*
59======================================================
60    / Init Func
61    ====================================================== */
62
63/*
64======================================================
65    Declare Globals
66    ====================================================== */
67
68    #} Used throughout
69    // Don't know who added this, but GLOBALS are out of scope here
70    // global $zbsCustomerFields,$zbsCustomerQuoteFields,$zbsCustomerInvoiceFields;
71
72/*
73======================================================
74    / Declare Globals
75    ====================================================== */
76
77    // PerfTest: zeroBSCRM_performanceTest_startTimer('custmetabox');
78
79/*
80======================================================
81    Tag List Metabox
82    ====================================================== */
83
84class zeroBS__Metabox_TagList extends zeroBS__Metabox {
85
86    /**
87     * The legacy object name (e.g. 'zerobs_customer')
88     *
89     * @var string
90     */
91    private $postType; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.PropertyNotSnakeCase
92
93    public function __construct( $plugin_file, $typeInt = ZBS_TYPE_CONTACT ) {
94
95        global $zbs;
96
97        // set these
98        $this->typeInt         = $typeInt;
99        $this->postType        = $zbs->DAL->typeCPT( $typeInt );
100        $this->metaboxID       = 'zerobs-' . $zbs->DAL->objTypeKey( $typeInt ) . '-tags-edit';
101        $this->metaboxTitle    = __( $zbs->DAL->typeStr( $typeInt ) . ' Tags', 'zero-bs-crm' );
102        $this->metaboxScreen   = 'zerobs_edit_tags'; // we can use anything here as is now using our func
103        $this->metaboxArea     = 'normal';
104        $this->metaboxLocation = 'high';
105        $this->saveOrder       = 1;
106        // headless!
107        $this->headless = true;
108
109        // call this
110        $this->initMetabox();
111    }
112
113    public function html( $contact, $metabox ) {
114
115        global $zbs;
116
117        // Get all tags
118        $tags = $zbs->DAL->getTagsForObjType(
119            array(
120
121                'objtypeid'    => $this->typeInt,
122                'excludeEmpty' => false,
123                'withCount'    => true,
124                'ignoreowner'  => true,
125                // sort
126                'sortByField'  => 'tagcount',
127                'sortOrder'    => 'DESC',
128
129            )
130        );
131
132        // pre-inject some potential js errors :)
133        ?><div id="zbs-error-prep">
134            <?php
135
136            echo zeroBSCRM_UI2_messageHTML( 'info hidden', __( 'Tag Exists', 'zero-bs-crm' ), __( 'Cannot add a tag. A tag already exists with this text', 'zero-bs-crm' ), '', 'zbsTagAlreadyExists' );
137            echo zeroBSCRM_UI2_messageHTML( 'info hidden', __( 'Tag Text Empty', 'zero-bs-crm' ), __( 'You cannot add empty tags', 'zero-bs-crm' ), '', 'zbsTagEmpty' );
138
139            ?>
140            </div>
141            <?php
142
143            // long term perhaps we need a list metabox type, for now, hardcoded:
144            ?>
145                <table class="ui celled table" id="zbs-tag-manager" style="margin-top:0;">
146                    <thead>
147                        <tr>
148                        <th><?php esc_html_e( 'Name', 'zero-bs-crm' ); ?></th>
149                        <th><?php esc_html_e( 'Slug', 'zero-bs-crm' ); ?></th>
150                    <?php /* this shows 1 date as DAL2 migration...<th><?php _e('First Used',"zero-bs-crm"); ?></th> */ ?>
151                        <th class="center aligned"><?php esc_html_e( 'Count', 'zero-bs-crm' ); ?></th>
152                        <th class="center aligned"><?php esc_html_e( 'Action', 'zero-bs-crm' ); ?></th>
153                        </tr>
154                    </thead>
155                    <tbody>
156                    <?php
157                    if ( count( $tags ) > 0 ) {
158                        foreach ( $tags as $tag ) {
159
160                            $link = jpcrm_esc_link( 'listtagged', -1, $this->postType, -1, $tag['id'] );
161                            ?>
162                            <tr>
163                                <td>
164                                    <?php
165                                    if ( isset( $tag['name'] ) ) {
166                                        echo '<a href="' . esc_url( $link ) . '" class="ui large label">' . esc_html( $tag['name'] ) . '</a>';
167                                    }
168                                    ?>
169                                </td>
170                                <td>
171                                    <?php
172                                    if ( isset( $tag['slug'] ) ) {
173                                        echo esc_html( $tag['slug'] );
174                                    }
175                                    ?>
176                                </td>
177                                <?php /* this shows 1 date as DAL2 migration... <td><?php if (isset($tag['created']) && !empty($tag['created']) && $tag['created'] !== -1) echo zeroBSCRM_locale_utsToDate($tag['created']); ?></td> */ ?>
178                                <td class="center aligned">
179                                    <?php
180                                    if ( isset( $tag['count'] ) ) {
181                                        echo '<a href="' . esc_url( $link ) . '">' . esc_html( zeroBSCRM_prettifyLongInts( $tag['count'] ) ) . '</a>';}
182                                    ?>
183                                </td>
184                                <td class="center aligned">
185                                    <button type="button" class="ui mini button black zbs-delete-tag" data-tagid="<?php echo esc_attr( $tag['id'] ); ?>">
186                                        <i class="trash alternate icon"></i>
187                                        <?php esc_html_e( 'Delete', 'zero-bs-crm' ); ?>
188                                    </button>
189                                </td>
190                            </tr>
191                            <?php
192                        }
193                    }
194                    ?>
195                    </tbody>
196                </table>
197                <?php
198
199                if ( count( $tags ) == 0 ) {
200                    echo zeroBSCRM_UI2_messageHTML( 'info', __( 'No Tags Found', 'zero-bs-crm' ), __( 'There are no Tags here, create one using the box to the right.', 'zero-bs-crm' ), 'disabled warning sign', 'zbsNoTagResults' );
201                }
202
203                ?>
204                <?php
205                $jpcrm_tag_list_lang = array(
206                    'delete'            => __( 'Delete', 'zero-bs-crm' ),
207                    'deleteswaltitle'   => __( 'Are you sure?', 'zero-bs-crm' ),
208                    /* translators: %s: object type (e.g. "contact", "company") */
209                    'deleteswaltext'    => sprintf( __( 'This will delete the tag and remove it from any tagged %s. This is irreversable.', 'zero-bs-crm' ), $zbs->DAL->typeStr( $this->typeInt ) ),
210                    'deleteswalconfirm' => __( 'Yes, delete the tag!', 'zero-bs-crm' ),
211                    'tagdeleted'        => __( 'Tag Deleted!', 'zero-bs-crm' ),
212                    'tagremoved'        => __( 'Your tag has been removed.', 'zero-bs-crm' ),
213                    'tagnotdeleted'     => __( 'Tag Not Deleted!', 'zero-bs-crm' ),
214                    'tagnotremoved'     => __( 'Your tag was not removed, please try again.', 'zero-bs-crm' ),
215                );
216                ?>
217                <script type="text/javascript">
218                var zbscrmjs_secToken = <?php echo wp_json_encode( wp_create_nonce( 'zbscrmjs-ajax-nonce' ), JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ); ?>;
219
220                var zbsTagListLang = <?php echo wp_json_encode( $jpcrm_tag_list_lang, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ); ?>;
221
222
223                </script>
224                <?php
225    }
226}
227
228/*
229======================================================
230    / Tag List Metabox
231    ====================================================== */
232
233/*
234======================================================
235    Create Tags Box
236    ====================================================== */
237
238class zeroBS__Metabox_TagAdd extends zeroBS__Metabox_Tags {
239
240    /**
241     * The legacy object name (e.g. 'zerobs_customer')
242     *
243     * @var string
244     */
245    private $postType; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.PropertyNotSnakeCase
246
247    public function __construct( $plugin_file, $typeInt = ZBS_TYPE_CONTACT ) {
248
249        global $zbs;
250
251        $this->typeInt         = $typeInt; // until db2 ZBS_TYPE_CONTACT;
252        $this->postType        = $zbs->DAL->typeCPT( $typeInt );
253        $this->metaboxID       = 'zerobs-' . $zbs->DAL->objTypeKey( $typeInt ) . '-tags';
254        $this->metaboxTitle    = __( 'Add ' . $zbs->DAL->typeStr( $typeInt ) . ' Tags', 'zero-bs-crm' );
255        $this->metaboxScreen   = 'zerobs_edit_tags'; // we can use anything here as is now using our func
256        $this->metaboxArea     = 'side';
257        $this->metaboxLocation = 'high';
258
259        // call this
260        $this->initMetabox();
261    }
262
263    // html + save dealt with by parent class :)
264}
265
266/*
267======================================================
268    / Create Tags Box
269    ====================================================== */