public function actionExport() { $model = new $this->modelName(); $photosExport = array(); if (isset($_POST['ajax']) && $_POST['ajax'] === 'export-form') { echo CActiveForm::validate($model); Yii::app()->end(); } if (isset($_POST[$this->modelName])) { $model->attributes = $_POST[$this->modelName]; $arrSel = ''; if (isset($_POST[$this->modelName]['itemsSelectedExport']) && $_POST[$this->modelName]['itemsSelectedExport']) { $arrSel = explode(',', $_POST[$this->modelName]['itemsSelectedExport']); $arrSel = array_map("intval", $arrSel); } if (!$arrSel || !count($arrSel)) { Yii::app()->user->setFlash('error', tt('Please select ads for export.')); $this->redirect('viewExport'); } else { $backUrl = $this->createUrl('viewExport', array($this->modelName . '_sel' => $arrSel)); $isZip = isset($_POST[$this->modelName]['isZip']) ? $_POST[$this->modelName]['isZip'] : 0; $fileName = date('Y_m_d_s') . rand(0, 9) . '_export.csv'; $filePath = $model->csvPath . DIRECTORY_SEPARATOR . $fileName; $fileLine = array(); $fileLine[0] = $this->mask; $i = 1; foreach ($arrSel as $v) { // get needed info $apartment = Apartment::model()->cache(param('cachingTime', 1209600), Apartment::getFullDependency($v))->with('windowTo', 'images')->findByPk($v); // set values $type = $apartment->type; $priceType = $apartment->price_type; $objType = $apartment->obj_type_id; $countryName = $regionName = $cityName = ''; if (issetModule('location')) { // country if (isFree()) { if ($apartment->loc_country) { $countryInfo = Country::model()->findByPk($apartment->loc_country); if ($countryInfo && isset($countryInfo->name)) { $countryName = $countryInfo->name; } } } else { if ($apartment->loc_country) { Yii::app()->setLanguage($this->defLang); $countryInfo = Country::model()->findByPk($apartment->loc_country); if ($countryInfo && isset($countryInfo->name)) { $countryName = $countryInfo->name; } Yii::app()->setLanguage($this->currLang); } } // region if (isFree()) { if ($apartment->loc_region) { $regionInfo = Region::model()->findByPk($apartment->loc_region); if ($regionInfo && isset($regionInfo->name)) { $regionName = $regionInfo->name; } } } else { if ($apartment->loc_region) { Yii::app()->setLanguage($this->defLang); $regionInfo = Region::model()->findByPk($apartment->loc_region); if ($regionInfo && isset($regionInfo->name)) { $regionName = $regionInfo->name; } Yii::app()->setLanguage($this->currLang); } } // city if (isFree()) { if ($apartment->loc_city) { $cityInfo = City::model()->findByPk($apartment->loc_city); if ($cityInfo && isset($cityInfo->name)) { $cityName = $cityInfo->name; } } } else { if ($apartment->loc_city) { Yii::app()->setLanguage($this->defLang); $cityInfo = City::model()->findByPk($apartment->loc_city); if ($cityInfo && isset($cityInfo->name)) { $cityName = $cityInfo->name; } Yii::app()->setLanguage($this->currLang); } } } else { if (isFree()) { if ($apartment->city_id) { Yii::import('application.modules.apartmentCity.models.ApartmentCity'); $cityInfo = ApartmentCity::model()->findByPk($apartment->city_id); if ($cityInfo && isset($cityInfo->name)) { $cityName = $cityInfo->name; } } } else { if ($apartment->city_id) { Yii::app()->setLanguage($this->defLang); Yii::import('application.modules.apartmentCity.models.ApartmentCity'); $cityInfo = ApartmentCity::model()->findByPk($apartment->city_id); if ($cityInfo && isset($cityInfo->name)) { $cityName = $cityInfo->name; } Yii::app()->setLanguage($this->currLang); } } } $isPricePoa = $apartment->is_price_poa; $price = $apartment->price; $priceTo = $apartment->price_to; $numberRooms = $apartment->num_of_rooms; $floor = $apartment->floor; $floorTotal = $apartment->floor_total; $square = $apartment->square; $landSquare = $apartment->land_square; $sleeps = $apartment->berths; // get title, description, near, address, exchange $title = $description = $near = $address = $exchangeTo = ''; if (isFree()) { $titleField = 'title_' . Yii::app()->language; $title = $this->clearHtml($apartment->{$titleField}); $descrField = 'description_' . Yii::app()->language; $description = $this->clearHtml($apartment->{$descrField}); $descrNearField = 'description_near_' . Yii::app()->language; $near = $this->clearHtml($apartment->{$descrNearField}); $addressField = 'address_' . Yii::app()->language; $address = $this->clearHtml($apartment->{$addressField}); $exchangeToField = 'exchange_to_' . Yii::app()->language; $exchangeTo = $this->clearHtml($apartment->{$exchangeToField}); } else { if ($this->allLangs) { $title = $description = $near = $address = $exchangeTo = array(); foreach ($this->allLangs as $lang) { $titleField = 'title_' . $lang->name_iso; $descrField = 'description_' . $lang->name_iso; $descrNearField = 'description_near_' . $lang->name_iso; $addressField = 'address_' . $lang->name_iso; $exchangeToField = 'exchange_to_' . $lang->name_iso; $title[$lang->name_iso] = $this->clearHtml($apartment->{$titleField}); $description[$lang->name_iso] = $this->clearHtml($apartment->{$descrField}); $near[$lang->name_iso] = $this->clearHtml($apartment->{$descrNearField}); $address[$lang->name_iso] = $this->clearHtml($apartment->{$addressField}); $exchangeTo[$lang->name_iso] = $this->clearHtml($apartment->{$exchangeToField}); } } } // get coords $lat = $apartment->lat; $lng = $apartment->lng; // get photos $photos = $apartment->images(); if ($photos) { foreach ($photos as $key => $value) { if ($isZip) { $photos[$key] = $value->file_name; $photosExport["{$apartment->id}_{$key}"] = $model->csvPath . DIRECTORY_SEPARATOR . $value->file_name; copy(Yii::getPathOfAlias('webroot.uploads.objects.' . $apartment->id . '.original') . DIRECTORY_SEPARATOR . $value->file_name, $model->csvPath . DIRECTORY_SEPARATOR . $value->file_name); } else { $photos[$key] = Yii::app()->getBaseUrl(true) . '/uploads/objects/' . $apartment->id . '/original/' . $value->file_name; } } } // get reference info $refInfo = $this->getReferenceInfo($v); $comfort = $refInfo['comfort']; $bathroom = $refInfo['bathroom']; $kitchen = $refInfo['kitchen']; $employment = $refInfo['employment']; $safety = $refInfo['safety']; $entertainment = $refInfo['entertainment']; $terms = $refInfo['terms']; $services = $refInfo['services']; // insert $fileLine[$i] = $type . $this->separator . $priceType . $this->separator . $objType . $this->separator; $fileLine[$i] .= $countryName . $this->separator . $regionName . $this->separator . $cityName . $this->separator . $isPricePoa . $this->separator . $price . $this->separator; $fileLine[$i] .= $priceTo . $this->separator . $numberRooms . $this->separator . $floor . $this->separator; $fileLine[$i] .= $floorTotal . $this->separator . $square . $this->separator . $landSquare . $this->separator . $this->deleteChars($sleeps) . $this->separator; if (isFree()) { $fileLine[$i] .= $this->deleteChars($title) . $this->separator . $this->deleteChars($description) . $this->separator; $fileLine[$i] .= $this->deleteChars($near) . $this->separator . $this->deleteChars($address) . $this->separator; $fileLine[$i] .= $this->deleteChars($exchangeTo) . $this->separator; } else { array_walk($title, array($this, 'deleteChars')); $fileLine[$i] .= implode($this->separator, $title) . $this->separator; array_walk($description, array($this, 'deleteChars')); $fileLine[$i] .= implode($this->separator, $description) . $this->separator; array_walk($near, array($this, 'deleteChars')); $fileLine[$i] .= implode($this->separator, $near) . $this->separator; array_walk($address, array($this, 'deleteChars')); $fileLine[$i] .= implode($this->separator, $address) . $this->separator; array_walk($exchangeTo, array($this, 'deleteChars')); $fileLine[$i] .= implode($this->separator, $exchangeTo) . $this->separator; } $fileLine[$i] .= $bathroom . $this->separator . $safety . $this->separator; $fileLine[$i] .= $comfort . $this->separator . $kitchen . $this->separator; $fileLine[$i] .= $employment . $this->separator . $entertainment . $this->separator; $fileLine[$i] .= $services . $this->separator . $terms . $this->separator; $fileLine[$i] .= is_array($photos) ? implode($this->separatorElem, $photos) . $this->separator : '' . $this->separator; $fileLine[$i] .= $lat . $this->separator . $lng; $i++; } // write in file $handle = fopen($filePath, "w+"); foreach ($fileLine as $item) { fputs($handle, $item); fputs($handle, "\r\n"); } fclose($handle); if ($isZip) { include_once $model->libraryPath . DIRECTORY_SEPARATOR . 'pclzip.lib.php'; $arrFile = $photosExport; $arrFile[] = $model->csvPath . DIRECTORY_SEPARATOR . $fileName; $archive = new PclZip($model->csvPath . DIRECTORY_SEPARATOR . str_replace(".csv", "", $fileName) . ".zip"); $list = $archive->create($arrFile, PCLZIP_OPT_REMOVE_ALL_PATH); if ($list == 0) { Yii::app()->user->setFlash('error', $archive->errorInfo(true)); //$this->redirect('viewExport'); $this->redirect($backUrl); } // unlink all files foreach ($arrFile as $item) { @unlink($item); } $fileName = str_replace('.csv', '.zip', $fileName); $filePath = $model->csvPath . DIRECTORY_SEPARATOR . $fileName; header("Content-Type: application/zip"); } else { header("Content-Type: application/csv"); } header("Content-Disposition: attachment; filename={$fileName}"); header("Content-Transfer-Encoding: binary"); header("Pragma: no-cache"); header("Expires: 0"); readfile($filePath); Yii::app()->end(); } } }