Map.php 7.5 KB
Newer Older
totten's avatar
totten committed
1 2 3
<?php
/*
 +--------------------------------------------------------------------+
totten's avatar
totten committed
4
 | CiviCRM version 5                                                  |
totten's avatar
totten committed
5
 +--------------------------------------------------------------------+
Seamus Lee's avatar
Seamus Lee committed
6
 | Copyright CiviCRM LLC (c) 2004-2019                                |
totten's avatar
totten committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 +--------------------------------------------------------------------+
 | This file is a part of CiviCRM.                                    |
 |                                                                    |
 | CiviCRM is free software; you can copy, modify, and distribute it  |
 | under the terms of the GNU Affero General Public License           |
 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
 |                                                                    |
 | CiviCRM is distributed in the hope that it will be useful, but     |
 | WITHOUT ANY WARRANTY; without even the implied warranty of         |
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
 | See the GNU Affero General Public License for more details.        |
 |                                                                    |
 | You should have received a copy of the GNU Affero General Public   |
 | License and the CiviCRM Licensing Exception along                  |
 | with this program; if not, contact CiviCRM LLC                     |
 | at info[AT]civicrm[DOT]org. If you have questions about the        |
 | GNU Affero General Public License or the licensing of CiviCRM,     |
 | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
 +--------------------------------------------------------------------+
26
 */
totten's avatar
totten committed
27 28 29 30

/**
 *
 * @package CRM
Seamus Lee's avatar
Seamus Lee committed
31
 * @copyright CiviCRM LLC (c) 2004-2019
totten's avatar
totten committed
32 33 34 35 36 37 38 39 40 41
 * $Id$
 *
 */

/**
 * This class gets the name of the file to upload
 */
class CRM_Export_Form_Map extends CRM_Core_Form {

  /**
colemanw's avatar
colemanw committed
42
   * Mapper fields
totten's avatar
totten committed
43 44 45 46 47 48
   *
   * @var array
   */
  protected $_mapperFields;

  /**
colemanw's avatar
colemanw committed
49
   * Number of columns in import file
totten's avatar
totten committed
50 51 52 53 54 55
   *
   * @var int
   */
  protected $_exportColumnCount;

  /**
colemanw's avatar
colemanw committed
56
   * Loaded mapping ID
totten's avatar
totten committed
57 58 59 60 61 62
   *
   * @var int
   */
  protected $_mappingId;

  /**
63
   * Build the form object.
totten's avatar
totten committed
64
   *
colemanw's avatar
colemanw committed
65
   * @return void
totten's avatar
totten committed
66 67 68
   */
  public function preProcess() {
    $this->_exportColumnCount = $this->get('exportColumnCount');
69 70
    $this->_mappingId = $this->get('mappingId');

totten's avatar
totten committed
71
    if (!$this->_exportColumnCount) {
72 73 74 75 76 77 78 79 80
      // Set default from saved mapping
      if ($this->_mappingId) {
        $mapping = new CRM_Core_DAO_MappingField();
        $mapping->mapping_id = $this->_mappingId;
        $this->_exportColumnCount = $mapping->count();
      }
      else {
        $this->_exportColumnCount = 10;
      }
totten's avatar
totten committed
81 82
    }
    else {
83
      $this->_exportColumnCount += 10;
totten's avatar
totten committed
84 85 86 87 88 89 90 91
    }
  }

  public function buildQuickForm() {
    CRM_Core_BAO_Mapping::buildMappingForm($this,
      'Export',
      $this->_mappingId,
      $this->_exportColumnCount,
92
      2,
totten's avatar
totten committed
93 94 95
      $this->get('exportMode')
    );

96
    $this->addButtons([
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
      [
        'type' => 'back',
        'name' => ts('Previous'),
      ],
      [
        'type' => 'next',
        'name' => ts('Export'),
        'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
      ],
      [
        'type' => 'done',
        'icon' => 'fa-times',
        'name' => ts('Done'),
      ],
    ]);
totten's avatar
totten committed
112 113 114
  }

  /**
115
   * Global validation rules for the form.
totten's avatar
totten committed
116
   *
117 118
   * @param array $fields
   *   Posted values of the form.
totten's avatar
totten committed
119
   *
Eileen McNaughton's avatar
Eileen McNaughton committed
120
   * @param $values
colemanw's avatar
colemanw committed
121
   * @param int $mappingTypeId
Eileen McNaughton's avatar
Eileen McNaughton committed
122
   *
123 124
   * @return array
   *   list of errors to be posted back to the form
totten's avatar
totten committed
125
   */
126
  public static function formRule($fields, $values, $mappingTypeId) {
127
    $errors = [];
totten's avatar
totten committed
128

129
    if (!empty($fields['saveMapping']) && !empty($fields['_qf_Map_next'])) {
totten's avatar
totten committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
      $nameField = CRM_Utils_Array::value('saveMappingName', $fields);
      if (empty($nameField)) {
        $errors['saveMappingName'] = ts('Name is required to save Export Mapping');
      }
      else {
        //check for Duplicate mappingName
        if (CRM_Core_BAO_Mapping::checkMapping($nameField, $mappingTypeId)) {
          $errors['saveMappingName'] = ts('Duplicate Export Mapping Name');
        }
      }
    }

    if (!empty($errors)) {
      $_flag = 1;
      $assignError = new CRM_Core_Page();
      $assignError->assign('mappingDetailsError', $_flag);
      return $errors;
    }
    else {
      return TRUE;
    }
  }

  /**
154
   * Process the uploaded file.
totten's avatar
totten committed
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 203 204 205 206 207 208 209 210 211 212 213 214 215 216
   *
   * @return void
   */
  public function postProcess() {
    $params = $this->controller->exportValues($this->_name);
    $exportParams = $this->controller->exportValues('Select');

    $greetingOptions = CRM_Export_Form_Select::getGreetingOptions();

    if (!empty($greetingOptions)) {
      foreach ($greetingOptions as $key => $value) {
        if ($option = CRM_Utils_Array::value($key, $exportParams)) {
          if ($greetingOptions[$key][$option] == ts('Other')) {
            $exportParams[$key] = $exportParams["{$key}_other"];
          }
          elseif ($greetingOptions[$key][$option] == ts('List of names')) {
            $exportParams[$key] = '';
          }
          else {
            $exportParams[$key] = $greetingOptions[$key][$option];
          }
        }
      }
    }

    $currentPath = CRM_Utils_System::currentPath();

    $urlParams = NULL;
    $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $this);
    if (CRM_Utils_Rule::qfKey($qfKey)) {
      $urlParams = "&qfKey=$qfKey";
    }

    //get the button name
    $buttonName = $this->controller->getButtonName('done');
    $buttonName1 = $this->controller->getButtonName('next');
    if ($buttonName == '_qf_Map_done') {
      $this->set('exportColumnCount', NULL);
      $this->controller->resetPage($this->_name);
      return CRM_Utils_System::redirect(CRM_Utils_System::url($currentPath, 'force=1' . $urlParams));
    }

    if ($this->controller->exportValue($this->_name, 'addMore')) {
      $this->set('exportColumnCount', $this->_exportColumnCount);
      return;
    }

    $mapperKeys = $params['mapper'][1];

    $checkEmpty = 0;
    foreach ($mapperKeys as $value) {
      if ($value[0]) {
        $checkEmpty++;
      }
    }

    if (!$checkEmpty) {
      $this->set('mappingId', NULL);
      CRM_Utils_System::redirect(CRM_Utils_System::url($currentPath, '_qf_Map_display=true' . $urlParams));
    }

    if ($buttonName1 == '_qf_Map_next') {
217
      if (!empty($params['updateMapping'])) {
totten's avatar
totten committed
218 219 220 221
        //save mapping fields
        CRM_Core_BAO_Mapping::saveMappingFields($params, $params['mappingId']);
      }

222
      if (!empty($params['saveMapping'])) {
223
        $mappingParams = [
totten's avatar
totten committed
224 225 226
          'name' => $params['saveMappingName'],
          'description' => $params['saveMappingDesc'],
          'mapping_type_id' => $this->get('mappingTypeId'),
227
        ];
totten's avatar
totten committed
228 229 230 231 232 233 234 235 236 237 238

        $saveMapping = CRM_Core_BAO_Mapping::add($mappingParams);

        //save mapping fields
        CRM_Core_BAO_Mapping::saveMappingFields($params, $saveMapping->id);
      }
    }

    //get the csv file
    CRM_Export_BAO_Export::exportComponents($this->get('selectAll'),
      $this->get('componentIds'),
colemanw's avatar
colemanw committed
239
      (array) $this->get('queryParams'),
totten's avatar
totten committed
240 241 242 243 244 245 246 247
      $this->get(CRM_Utils_Sort::SORT_ORDER),
      $mapperKeys,
      $this->get('returnProperties'),
      $this->get('exportMode'),
      $this->get('componentClause'),
      $this->get('componentTable'),
      $this->get('mergeSameAddress'),
      $this->get('mergeSameHousehold'),
248 249
      $exportParams,
      $this->get('queryOperator')
totten's avatar
totten committed
250 251 252 253 254 255 256 257 258 259 260
    );
  }

  /**
   * Return a descriptive name for the page, used in wizard header
   *
   * @return string
   */
  public function getTitle() {
    return ts('Select Fields to Export');
  }
261

totten's avatar
totten committed
262
}