Esempio n. 1
0
<?php

use yii\helpers\Url;
use common\vendor\FileManager\Manager\FileFactory;
use common\models\Announcement;
$options = Yii::$app->params;
$fileManager = FileFactory::factory((object) $options['fileManager']);
?>

<?php 
foreach ($dataProvider->getModels() as $row) {
    ?>

    <?php 
    // FILE MANAGER
    $fileManager->setAsset('announcement', ['id' => $row['id']]);
    $filepath = '/' . $fileManager->getPath($row['image'], 'preview');
    // HREF
    $href = Url::to(['catalog/view', 'id' => $row['id']]);
    // MILEAGE
    Yii::$app->formatter->thousandSeparator = ' ';
    $mileage = Yii::$app->formatter->asInteger($row['mileage'] * 1000);
    $mileage .= ' ' . Yii::t('ads', 'km');
    // FUEL SYSTEM
    $fuelSystem = '';
    $fuelSystemForList = '';
    if ($row['fuel_system']) {
        $fuelSystem = Yii::t('ads', $row['fuel_system']);
        if ($row['engine_capacity']) {
            $fuelSystemForList = Yii::t('ads', $row['fuel_system']) . ' ' . $row['engine_capacity'] . ', ';
        } else {
Esempio n. 2
0
 public function updateImages()
 {
     // EXISTS IMAGES
     $rows = User2ImageModel::find()->select(['id'])->where(['user_id' => $this->user_id])->asArray()->all();
     if (!count($rows)) {
         return;
     }
     $oldIds = [];
     foreach ($rows as $row) {
         $oldIds[] = $row['id'];
     }
     $insert = $this->images;
     $delete = $oldIds;
     $update = array();
     // IF NO IMAGES - DELETE FROM DB
     if (!count($insert)) {
         foreach ($delete as $id) {
             $announcement2image = User2ImageModel::findOne($id);
             $announcement2image->delete();
         }
         return;
     }
     foreach ($oldIds as $id) {
         $update[] = array('filename' => array_shift($insert), 'id' => $id);
         array_shift($delete);
         if (!count($insert)) {
             break;
         }
     }
     $filenames = [];
     // UPDATE
     if (count($update)) {
         foreach ($update as $row) {
             $id = $row['id'];
             $filename = $row['filename'];
             $isMain = 0;
             if ($filename == $this->main_image) {
                 $isMain = 1;
             }
             $announcement2image = User2ImageModel::findOne($id);
             $announcement2image->filename = $filename;
             $announcement2image->is_main = $isMain;
             $announcement2image->save();
             $filenames[] = $filename;
         }
     }
     // INSERT
     if (count($insert)) {
         foreach ($insert as $filename) {
             $isMain = 0;
             if ($filename == $this->main_image) {
                 $isMain = 1;
             }
             $announcement2image = new User2ImageModel();
             $announcement2image->user_id = $this->user_id;
             $announcement2image->filename = $filename;
             $announcement2image->is_main = $isMain;
             $announcement2image->save();
             $filenames[] = $filename;
         }
     }
     // DELETE
     if (count($delete)) {
         foreach ($delete as $id) {
             $announcement2image = User2ImageModel::findOne($id);
             $announcement2image->delete();
         }
     }
     // MOVE NEW IMAGES TO PERMANENT DIRECTORY
     if (!count($filenames)) {
         return;
     }
     $options = Yii::$app->params;
     $fileManager = FileFactory::factory((object) $options['fileManager']);
     foreach ($filenames as $filename) {
         $fileManager->setAsset('temp', []);
         $target = $fileManager->getDestination();
         $fileManager->setAsset('user', ['id' => $this->user_id]);
         try {
             if (file_exists($target . '/' . $filename)) {
                 $fileManager->persist($target, $filename);
             }
         } catch (Exception $e) {
         }
     }
 }