/** * An AJAX called function which exports data to a CSV via pagination. * This is a generalized version of the Contacts export. * @param int $page The page of the data provider to export */ public function actionExportModelRecords($page, $model) { X2Model::$autoPopulateFields = false; $file = $this->safePath($_SESSION['modelExportFile']); $staticModel = X2Model::model(str_replace(' ', '', $model)); $fields = $staticModel->getFields(); $fp = fopen($file, 'a+'); // Load data provider based on export criteria $excludeHidden = !isset($_GET['includeHidden']) || $_GET['includeHidden'] === 'false'; if ($page == 0 && $excludeHidden && isset($_SESSION['exportModelCriteria']) && $_SESSION['exportModelCriteria'] instanceof CDbCriteria) { // Save hidden condition in criteria $hiddenConditions = $staticModel->getHiddenCondition(); $_SESSION['exportModelCriteria']->addCondition($hiddenConditions); } $dp = new CActiveDataProvider($model, array('criteria' => isset($_SESSION['exportModelCriteria']) ? $_SESSION['exportModelCriteria'] : array(), 'pagination' => array('pageSize' => 100))); // Flip through to the right page. $pg = $dp->getPagination(); $pg->setCurrentPage($page); $dp->setPagination($pg); $records = $dp->getData(); $pageCount = $dp->getPagination()->getPageCount(); // Retrieve specified export delimeter and enclosure $delimeter = isset($_GET['delimeter']) ? $_GET['delimeter'] : ','; $enclosure = isset($_GET['enclosure']) ? $_GET['enclosure'] : '"'; // We need to set our data to be human friendly, so loop through all the // records and format any date / link / visibility fields. foreach ($records as $record) { foreach ($fields as $field) { $fieldName = $field->fieldName; if ($field->type == 'date' || $field->type == 'dateTime') { if (is_numeric($record->{$fieldName})) { $record->{$fieldName} = Formatter::formatLongDateTime($record->{$fieldName}); } } elseif ($field->type == 'link') { $name = $record->{$fieldName}; if (!empty($field->linkType)) { list($name, $id) = Fields::nameAndId($name); } if (!empty($name)) { $record->{$fieldName} = $name; } } elseif ($fieldName == 'visibility') { switch ($record->{$fieldName}) { case 0: $record->{$fieldName} = 'Private'; break; case 1: $record->{$fieldName} = 'Public'; break; case 2: $record->{$fieldName} = 'User\'s Groups'; break; default: $record->{$fieldName} = 'Private'; } } } // Enforce metadata to ensure accuracy of column order, then export. $combinedMeta = array_combine($_SESSION['modelExportMeta'], $_SESSION['modelExportMeta']); $recordAttributes = $record->attributes; if ($model === 'Actions') { // Export descriptions with Actions $actionText = $record->actionText; if ($actionText) { $actionDescription = $actionText->text; $recordAttributes = array_merge($recordAttributes, array('actionDescription' => $actionDescription)); } } if ($_SESSION['includeTags']) { $tags = $record->getTags(); $recordAttributes = array_merge($recordAttributes, array('tags' => implode(',', $tags))); } $tempAttributes = array_intersect_key($recordAttributes, $combinedMeta); $tempAttributes = array_merge($combinedMeta, $tempAttributes); fputcsv($fp, $tempAttributes, $delimeter, $enclosure); } unset($dp); fclose($fp); if ($page + 1 < $pageCount) { echo $page + 1; } }
public function __construct($owner = null) { X2Model::$autoPopulateFields = false; parent::__construct($owner); }