Merge pull request #12483 from colemanw/DAODefault
[civicrm-core.git] / CRM / Export / BAO / ExportProcessor.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2018
32 */
33
34 /**
35 * Class CRM_Export_BAO_ExportProcessor
36 *
37 * Class to handle logic of export.
38 */
39 class CRM_Export_BAO_ExportProcessor {
40
41 /**
42 * @var int
43 */
44 protected $queryMode;
45
46 /**
47 * @var int
48 */
49 protected $exportMode;
50
51 /**
52 * Array of fields in the main query.
53 *
54 * @var array
55 */
56 protected $queryFields = [];
57
58 /**
59 * Either AND or OR.
60 *
61 * @var string
62 */
63 protected $queryOperator;
64
65 /**
66 * CRM_Export_BAO_ExportProcessor constructor.
67 *
68 * @param int $exportMode
69 * @param string $queryOperator
70 */
71 public function __construct($exportMode, $queryOperator) {
72 $this->setExportMode($exportMode);
73 $this->setQueryMode();
74 $this->setQueryOperator($queryOperator);
75 }
76
77 /**
78 * @return string
79 */
80 public function getQueryOperator() {
81 return $this->queryOperator;
82 }
83
84 /**
85 * @param string $queryOperator
86 */
87 public function setQueryOperator($queryOperator) {
88 $this->queryOperator = $queryOperator;
89 }
90
91 /**
92 * @return array
93 */
94 public function getQueryFields() {
95 return $this->queryFields;
96 }
97
98 /**
99 * @param array $queryFields
100 */
101 public function setQueryFields($queryFields) {
102 $this->queryFields = $queryFields;
103 }
104
105 /**
106 * @return int
107 */
108 public function getQueryMode() {
109 return $this->queryMode;
110 }
111
112 /**
113 * Set the query mode based on the export mode.
114 */
115 public function setQueryMode() {
116
117 switch ($this->getExportMode()) {
118 case CRM_Export_Form_Select::CONTRIBUTE_EXPORT:
119 $this->queryMode = CRM_Contact_BAO_Query::MODE_CONTRIBUTE;
120 break;
121
122 case CRM_Export_Form_Select::EVENT_EXPORT:
123 $this->queryMode = CRM_Contact_BAO_Query::MODE_EVENT;
124 break;
125
126 case CRM_Export_Form_Select::MEMBER_EXPORT:
127 $this->queryMode = CRM_Contact_BAO_Query::MODE_MEMBER;
128 break;
129
130 case CRM_Export_Form_Select::PLEDGE_EXPORT:
131 $this->queryMode = CRM_Contact_BAO_Query::MODE_PLEDGE;
132 break;
133
134 case CRM_Export_Form_Select::CASE_EXPORT:
135 $this->queryMode = CRM_Contact_BAO_Query::MODE_CASE;
136 break;
137
138 case CRM_Export_Form_Select::GRANT_EXPORT:
139 $this->queryMode = CRM_Contact_BAO_Query::MODE_GRANT;
140 break;
141
142 case CRM_Export_Form_Select::ACTIVITY_EXPORT:
143 $this->queryMode = CRM_Contact_BAO_Query::MODE_ACTIVITY;
144 break;
145
146 default:
147 $this->queryMode = CRM_Contact_BAO_Query::MODE_CONTACTS;
148 }
149 }
150
151 /**
152 * @return int
153 */
154 public function getExportMode() {
155 return $this->exportMode;
156 }
157
158 /**
159 * @param int $exportMode
160 */
161 public function setExportMode($exportMode) {
162 $this->exportMode = $exportMode;
163 }
164
165 /**
166 * @param $params
167 * @param $order
168 * @param $returnProperties
169 * @return array
170 */
171 public function runQuery($params, $order, $returnProperties) {
172 $query = new CRM_Contact_BAO_Query($params, $returnProperties, NULL,
173 FALSE, FALSE, $this->getQueryMode(),
174 FALSE, TRUE, TRUE, NULL, $this->getQueryOperator()
175 );
176
177 //sort by state
178 //CRM-15301
179 $query->_sort = $order;
180 list($select, $from, $where, $having) = $query->query();
181 $this->setQueryFields($query->_fields);
182 return array($query, $select, $from, $where, $having);
183 }
184
185 }