Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 103
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Segment_Condition_Woo_Order_Count
0.00% covered (danger)
0.00%
0 / 102
0.00% covered (danger)
0.00%
0 / 2
90
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 conditionArg
0.00% covered (danger)
0.00%
0 / 99
0.00% covered (danger)
0.00%
0 / 1
72
1<?php
2/*
3 * Jetpack CRM
4 * https://jetpackcrm.com
5 *
6 * WooSync: Segment Condition: Order Count
7 *
8 */
9
10// block direct access
11defined( 'ZEROBSCRM_PATH' ) || exit( 0 );
12
13/**
14 * WooSync: Segment Condition: Order Count class
15 */
16class Segment_Condition_Woo_Order_Count extends zeroBSCRM_segmentCondition {
17
18    public $key       = 'woo_order_count';
19    public $condition = array(
20        'category'  => 'WooSync',
21        'position'  => 2,
22        'operators' => array( 'equal', 'notequal', 'larger', 'less', 'intrange', 'largerequal', 'lessequal' ),
23        'fieldname' => 'woo_order_count',
24        'inputmask' => 'int',
25    );
26
27    /**
28     * init, here just used to set translated attributes.
29     */
30    public function __construct( $constructionArgs = array() ) {
31
32        // set translations
33        $this->condition['name']        = __( 'WooCommerce Order Count', 'zero-bs-crm' );
34        $this->condition['description'] = __( 'Select contacts who match WooCommerce customers with specific order counts', 'zero-bs-crm' );
35
36        // fire main class init
37        $this->init( $constructionArgs );
38    }
39
40    public function conditionArg( $startingArg = false, $condition = false, $conditionKeySuffix = false ) {
41
42        global $zbs, $ZBSCRM_t;
43
44            // here we just count objlinks, which has the vulnerability that if there are orphan links created it'll show wrong count, but for now is 'enough'
45            // Note this also ignores ownership :O
46            $order_count_query = 'SELECT COUNT(DISTINCT(obj_links.zbsol_objid_from)) FROM ' . $ZBSCRM_t['objlinks'] . ' obj_links'
47            . ' INNER JOIN ' . $ZBSCRM_t['externalsources'] . ' ext_sources'
48            . ' ON obj_links.zbsol_objid_from = ext_sources.zbss_objid'
49            . ' WHERE obj_links.zbsol_objtype_from = ' . ZBS_TYPE_TRANSACTION
50            . ' AND obj_links.zbsol_objtype_to = ' . ZBS_TYPE_CONTACT
51            . ' AND obj_links.zbsol_objid_to = contact.ID'
52            . ' AND ext_sources.zbss_objtype = ' . ZBS_TYPE_TRANSACTION;
53
54            /*
55            example:
56                SELECT * FROM
57                wp_zbs_object_links obj_links
58                INNER JOIN wp_zbs_externalsources ext_sources
59                ON obj_links.zbsol_objid_from = ext_sources.zbss_objid
60                WHERE obj_links.zbsol_objtype_from = 5
61                AND obj_links.zbsol_objtype_to = 1
62                AND obj_links.zbsol_objid_to = {CONTACTID}
63                AND ext_sources.zbss_objtype = 5
64            */
65
66        if ( $condition['operator'] == 'equal' ) {
67            return array(
68                'additionalWhereArr' =>
69                            array(
70                                'woo_order_count_equal' . $conditionKeySuffix => array(
71                                    '(' . $order_count_query . ')',
72                                    '=',
73                                    '%d',
74                                    $condition['value'],
75                                ),
76                            ),
77            );
78        } elseif ( $condition['operator'] == 'notequal' ) {
79            return array(
80                'additionalWhereArr' =>
81                            array(
82                                'woo_order_count_notequal' . $conditionKeySuffix => array(
83                                    '(' . $order_count_query . ')',
84                                    '<>',
85                                    '%d',
86                                    $condition['value'],
87                                ),
88                            ),
89            );
90        } elseif ( $condition['operator'] == 'larger' ) {
91            return array(
92                'additionalWhereArr' =>
93                            array(
94                                'woo_order_count_larger' . $conditionKeySuffix => array(
95                                    '(' . $order_count_query . ')',
96                                    '>',
97                                    '%d',
98                                    $condition['value'],
99                                ),
100                            ),
101            );
102        } elseif ( $condition['operator'] == 'largerequal' ) {
103            return array(
104                'additionalWhereArr' =>
105                        array(
106                            'woo_order_count_larger_equal' . $conditionKeySuffix => array(
107                                '(' . $order_count_query . ')',
108                                '>=',
109                                '%d',
110                                $condition['value'],
111                            ),
112                        ),
113            );
114        } elseif ( $condition['operator'] == 'less' ) {
115            return array(
116                'additionalWhereArr' =>
117                            array(
118                                'woo_order_count_smaller' . $conditionKeySuffix => array(
119                                    '(' . $order_count_query . ')',
120                                    '<',
121                                    '%d',
122                                    $condition['value'],
123                                ),
124                            ),
125            );
126        } elseif ( $condition['operator'] == 'lessequal' ) {
127            return array(
128                'additionalWhereArr' =>
129                            array(
130                                'woo_order_count_smaller_equal' . $conditionKeySuffix => array(
131                                    '(' . $order_count_query . ')',
132                                    '<=',
133                                    '%d',
134                                    $condition['value'],
135                                ),
136                            ),
137            );
138        } elseif ( $condition['operator'] == 'intrange' ) {
139            return array(
140                'additionalWhereArr' =>
141                            array(
142                                'woo_order_count_larger' . $conditionKeySuffix => array(
143                                    '(' . $order_count_query . ')',
144                                    '>=',
145                                    '%d',
146                                    $condition['value'],
147                                ),
148                                'woo_order_count_smaller' . $conditionKeySuffix => array(
149                                    '(' . $order_count_query . ')',
150                                    '<=',
151                                    '%d',
152                                    $condition['value2'],
153                                ),
154                            ),
155            );
156        }
157
158            return $startingArg;
159    }
160}