PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/x2engine/protected/components/ExportAccountsReportAction.php

https://gitlab.com/e0/X2CRM
PHP | 207 lines | 159 code | 8 blank | 40 comment | 27 complexity | 4ad384c7ef0c2effeb28e87e55dcd340 MD5 | raw file
  1. <?php
  2. /***********************************************************************************
  3. * X2CRM is a customer relationship management program developed by
  4. * X2Engine, Inc. Copyright (C) 2011-2016 X2Engine Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it under
  7. * the terms of the GNU Affero General Public License version 3 as published by the
  8. * Free Software Foundation with the addition of the following permission added
  9. * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
  10. * IN WHICH THE COPYRIGHT IS OWNED BY X2ENGINE, X2ENGINE DISCLAIMS THE WARRANTY
  11. * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
  12. *
  13. * This program is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  15. * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License along with
  19. * this program; if not, see http://www.gnu.org/licenses or write to the Free
  20. * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  21. * 02110-1301 USA.
  22. *
  23. * You can contact X2Engine, Inc. P.O. Box 66752, Scotts Valley,
  24. * California 95067, USA. on our website at www.x2crm.com, or at our
  25. * email address: contact@x2engine.com.
  26. *
  27. * The interactive user interfaces in modified source and object code versions
  28. * of this program must display Appropriate Legal Notices, as required under
  29. * Section 5 of the GNU Affero General Public License version 3.
  30. *
  31. * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
  32. * these Appropriate Legal Notices must retain the display of the "Powered by
  33. * X2Engine" logo. If the display of the logo is not reasonably feasible for
  34. * technical reasons, the Appropriate Legal Notices must display the words
  35. * "Powered by X2Engine".
  36. **********************************************************************************/
  37. /**
  38. * This class is functionally identical to {@link ExportServiceReportAction} and
  39. * full documentation can be found there.
  40. */
  41. class ExportAccountsReportAction extends CAction {
  42. public function run(){
  43. $this->attachBehavior('ImportExportBehavior', array('class'=>'application.components.behaviors.ImportExportBehavior'));
  44. $page = $_GET['page'];
  45. $gridviewSettings = json_decode(Yii::app()->params->profile->gridviewSettings, true);
  46. // remove x2gridview columns which don't correspond to account attributes
  47. $accountColumns = array_keys($gridviewSettings['accounts']);
  48. $accountAttrs = Services::model ()->attributeNames ();
  49. $accountColumns = array_filter (
  50. $accountColumns, function ($a) use ($accountAttrs) {
  51. return (in_array ($a, $accountAttrs));
  52. }
  53. );
  54. $file = $_SESSION['accountsReport']['accountsReportFile'];
  55. $filePath = $this->safePath($file);
  56. $fields = X2Model::model('Accounts')->getFields();
  57. if($page == 0){
  58. $fp = fopen($filePath, 'w+');
  59. fputcsv($fp, $accountColumns);
  60. }else{
  61. $fp = fopen($filePath, 'a+');
  62. }
  63. $fieldNames = Yii::app()->db->createCommand()
  64. ->select('fieldName')
  65. ->from('x2_fields')
  66. ->where('modelName="Accounts"')
  67. ->queryColumn();
  68. $_GET = json_decode($_SESSION['accountsReport']['GET'], true);
  69. $dateRange = X2DateUtil::getDateRange();
  70. if(isset($_GET['dateField'], $_GET['start'], $_GET['end'], $_GET['range'])){
  71. if(isset($_GET['sort'])){
  72. $_SESSION['accountsReportSort'] = str_replace('.', ' ', $_GET['sort']);
  73. }
  74. $dateField = $_GET['dateField'];
  75. $startDate = $dateRange['start'];
  76. $endDate = $dateRange['end'];
  77. $attributeConditions = "($dateField BETWEEN $startDate AND $endDate)";
  78. $criteria = new CDbCriteria;
  79. if(isset($_GET['Accounts'], $_GET['Accounts']['attribute'], $_GET['Accounts']['comparison'], $_GET['Accounts']['value'])){
  80. $filters = $_GET['Accounts'];
  81. for($i = 0; $i < count($filters['attribute']); $i++){
  82. $attribute = $filters['attribute'][$i];
  83. $comparison = $filters['comparison'][$i];
  84. $value = $filters['value'][$i];
  85. foreach(X2Model::model('Accounts')->fields as $field){
  86. if($field->fieldName == $attribute){
  87. switch($field->type){
  88. case 'date':
  89. case 'dateTime':
  90. if(ctype_digit((string) $value) || (substr($value, 0, 1) == '-' && ctype_digit((string) substr($value, 1))))
  91. $value = (int) $value;
  92. else
  93. $value = strtotime($value);
  94. break;
  95. case 'link':
  96. if(!ctype_digit((string) $value))
  97. $value = Fields::getLinkId($field->linkType, $value); break;
  98. case 'boolean':
  99. case 'visibility':
  100. $value = in_array(strtolower($value), array('1', 'yes', 'y', 't', 'true')) ? 1 : 0;
  101. break;
  102. }
  103. break;
  104. }
  105. }
  106. switch($comparison){
  107. case '=':
  108. $criteria->compare($attribute, $value, false);
  109. break;
  110. case '>':
  111. $criteria->compare($attribute, '>='.$value, true);
  112. break;
  113. case '<':
  114. $criteria->compare($attribute, '<='.$value, true);
  115. break;
  116. case '<>': // must test for != OR is null, because both mysql and yii are stupid
  117. $criteria->addCondition('('.$attribute.' IS NULL OR '.$attribute.'!='.CDbCriteria::PARAM_PREFIX.CDbCriteria::$paramCount.')');
  118. $criteria->params[CDbCriteria::PARAM_PREFIX.CDbCriteria::$paramCount++] = $value;
  119. break;
  120. case 'notEmpty':
  121. $criteria->addCondition($attribute.' IS NOT NULL AND '.$attribute.'!=""');
  122. break;
  123. case 'empty':
  124. $criteria->addCondition('('.$attribute.'="" OR '.$attribute.' IS NULL)');
  125. break;
  126. case 'list':
  127. $criteria->addInCondition($attribute, explode(',', $value));
  128. break;
  129. case 'notList':
  130. $criteria->addNotInCondition($attribute, explode(',', $value));
  131. break;
  132. case 'noContains':
  133. $criteria->compare($attribute, '<>'.$value, true);
  134. break;
  135. case 'contains':
  136. default:
  137. $criteria->compare($attribute, $value, true);
  138. }
  139. }
  140. $attributeConditions.=" AND ".$criteria->condition;
  141. }
  142. $accountColumnStr = implode(', ', $accountColumns);
  143. $sql = 'SELECT '.$accountColumnStr.' FROM x2_accounts WHERE '.$attributeConditions;
  144. $count = Yii::app()->db->createCommand()
  145. ->select('COUNT(*)')
  146. ->from('x2_accounts')
  147. ->where($attributeConditions, $criteria->params)
  148. ->queryScalar();
  149. $dataProvider = new CSqlDataProvider($sql, array(
  150. 'totalItemCount' => $count,
  151. 'params' => $criteria->params,
  152. 'sort' => array(
  153. 'attributes' => $fieldNames,
  154. 'defaultOrder' => isset($_SESSION['accountsReportSort']) ? $_SESSION['accountsReportSort'] : "$dateField ASC"
  155. ),
  156. 'pagination' => array(
  157. 'pageSize' => 100,
  158. ),
  159. ));
  160. $pg = $dataProvider->getPagination();
  161. $pg->setCurrentPage($page);
  162. $dataProvider->setPagination($pg);
  163. $records = $dataProvider->getData();
  164. $pageCount = $dataProvider->getPagination()->getPageCount();
  165. foreach($records as $record){
  166. foreach($fields as $field){
  167. if(in_array($field->fieldName, $accountColumns)){
  168. $fieldName = $field->fieldName;
  169. if($field->type == 'date' || $field->type == 'dateTime'){
  170. if(is_numeric($record[$fieldName])){
  171. if($field->type == 'date'){
  172. $record[$fieldName] = Formatter::formatDate($record[$fieldName]);
  173. }else{
  174. $record[$fieldName] = Formatter::formatDateTime($record[$fieldName]);
  175. }
  176. }
  177. }elseif($field->type == 'link'){
  178. try{
  179. $linkModel = X2Model::model($field->linkType)->findByPk($record[$fieldName]);
  180. if(isset($linkModel) && $linkModel->hasAttribute('name')){
  181. $record[$fieldName] = $linkModel->name;
  182. }
  183. }catch(Exception $e){
  184. }
  185. }elseif($fieldName == 'visibility'){
  186. $record[$fieldName] = $record[$fieldName] == 1 ? 'Public' : 'Private';
  187. }
  188. }
  189. }
  190. fputcsv($fp, $record);
  191. }
  192. unset($dataProvider);
  193. fclose($fp);
  194. if($page + 1 < $pageCount){
  195. echo $page + 1;
  196. }
  197. }
  198. }
  199. }