      1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202:

    <?php

    namespace WPGMZA;

    class ProAdminMarkerDataTable extends AdminMarkerDataTable
    {
        public function __construct($ajax_parameters=null)
        {
            AdminMarkerDataTable::__construct($ajax_parameters);
        }

        protected function getActionButtons()
        {
            global $wpgmza_ugm_version;

            $string = AdminMarkerDataTable::getActionButtons();

            if(!empty($wpgmza_ugm_version))
            {
                if(!preg_match('/REPLACE\((.+\')/msi', $string, $m, PREG_OFFSET_CAPTURE))
                    return $string;

                $inside = $m[1][0];
                $inside_original_length = strlen($inside);
                $inside_position = $m[1][1];

                if(!preg_match('/<a.+?class="wpgmza_del_btn/', $inside, $m, PREG_OFFSET_CAPTURE))
                    return $string;

                $button_insert_position = $m[0][1];
                $button_html = '<a href="javascript: ;"
                    title="' . esc_attr( __('Approve this marker', 'wp-google-maps') ) . '"
                    class="wpgmza_approve_btn button"
                    id="' . AdminMarkerDataTable::ID_PLACEHOLDER . '">
                        <i class="fa fa-check"></i>
                    </a>';

                $before = substr($inside, 0, $button_insert_position);
                $after = substr($inside, $button_insert_position);

                $replacement = "CONCAT($before',

                    CASE WHEN approved = '0' THEN '$button_html'
                    ELSE '' END,

                '$after)";

                $result = substr_replace($string, $replacement, $inside_position, $inside_original_length);

                return $result;
            }

            return $string;
        }

        protected function filterColumns(&$columns, $input_params)
        {
            global $WPGMZA_TABLE_NAME_MARKERS;
            global $WPGMZA_TABLE_NAME_CATEGORIES;
            global $WPGMZA_TABLE_NAME_MARKERS_HAS_CATEGORIES;

            AdminMarkerDataTable::filterColumns($columns, $input_params);

            // Temporary workaround for map ID not passed through datatables endpoint
            if(isset($_REQUEST['wpgmzaDataTableRequestData']))
                $map_id = (int)$_REQUEST['wpgmzaDataTableRequestData']['map_id'];

            foreach($columns as $key => $value)
            {
                $name = $this->getColumnNameByIndex($key);

                switch($name)
                {
                    case 'icon':

                        // TODO: Update to use ProMarker::getIconSQL. Need to transmit map_id first
                        $columns[$key] = ProMarker::getIconSQL($map_id, true);

                        break;

                    case 'category':

                        $columns[$key] = "(
                            SELECT GROUP_CONCAT(category_name SEPARATOR ', ')
                            FROM $WPGMZA_TABLE_NAME_CATEGORIES
                            WHERE $WPGMZA_TABLE_NAME_CATEGORIES.id IN (
                                SELECT category_id
                                FROM $WPGMZA_TABLE_NAME_MARKERS_HAS_CATEGORIES
                                WHERE marker_id = $WPGMZA_TABLE_NAME_MARKERS.id
                            )
                        ) AS category";

                        break;

                    case 'description':

                        $columns[$key] = 'description';

                        break;

                    case 'pic':

                        $columns[$key] = "(
                            CASE WHEN LENGTH(pic)=0 THEN
                                ''
                            ELSE
                                CONCAT(
                                    '<img src=\"',
                                    pic,
                                    '\" width=\"40\"/>'
                                )
                            END
                        ) AS pic";

                        break;

                    case 'link':

                        $columns[$key] = "(

                            CASE WHEN LENGTH(link)=0 THEN
                                ''
                            ELSE
                                CONCAT(
                                    '<a href=\"',
                                    link,
                                    '\" target=\"_blank\">&gt;&gt;</a>'
                                )
                            END

                        ) AS link";

                        break;
                }
            }

            return $columns;
        }

        public static function appendCategoryAndCustomFieldSearchClauses($sql, $input_params, &$query_params)
        {
            global $wpdb;
            global $WPGMZA_TABLE_NAME_MARKERS;
            global $WPGMZA_TABLE_NAME_CATEGORIES;
            global $WPGMZA_TABLE_NAME_MARKERS_HAS_CATEGORIES;
            global $WPGMZA_TABLE_NAME_MARKERS_HAS_CUSTOM_FIELDS;

            $clauses = explode(' OR ', trim($sql, '()'));
            $term = $input_params['search']['value'];

            // Categories
            $categories = "(

                SELECT GROUP_CONCAT(category_name SEPARATOR ', ')
                FROM $WPGMZA_TABLE_NAME_CATEGORIES
                WHERE $WPGMZA_TABLE_NAME_CATEGORIES.id IN (
                    SELECT category_id
                    FROM $WPGMZA_TABLE_NAME_MARKERS_HAS_CATEGORIES
                    WHERE marker_id=$WPGMZA_TABLE_NAME_MARKERS.id
                )

            ) LIKE %s";

            $clauses[] = $categories;
            $query_params[] = "%%" . $wpdb->esc_like($term) . "%%";

            // Custom fields
            $custom_fields = "(

                SELECT GROUP_CONCAT(value SEPARATOR ', ')
                FROM $WPGMZA_TABLE_NAME_MARKERS_HAS_CUSTOM_FIELDS
                WHERE $WPGMZA_TABLE_NAME_MARKERS_HAS_CUSTOM_FIELDS.object_id = $WPGMZA_TABLE_NAME_MARKERS.id

            ) LIKE %s";

            $clauses[] = $custom_fields;
            $query_params[] = "%%" . $wpdb->esc_like($term) . "%%";

            // Rebuild clause
            $sql = "(" . implode(' OR ', $clauses) . ")";

            return $sql;
        }

        // TODO: Implement this as a trait when we drop support for PHP 5.3
        public function getSearchClause($input_params, &$query_params, $exclude_columns=null)
        {
            $sql = AdminMarkerDataTable::getSearchClause($input_params, $query_params, $exclude_columns);

            if(empty($input_params['search']['value']))
                return $sql;

            return ProAdminMarkerDataTable::appendCategoryAndCustomFieldSearchClauses($sql, $input_params, $query_params);
        }
    }

    add_filter('wpgmza_create_WPGMZA\\AdminMarkerDataTable', function($ajax_parameters=null) {

        return new ProAdminMarkerDataTable($ajax_parameters);

    });