/** * Delete multiple existing Gallery model. * For ajax request will return json object * and for non-ajax request if deletion is successful, the browser will be redirected to the 'index' page. * @return mixed */ public function actionPhotosDelete() { $request = Yii::$app->request; $photoIds = $request->post('ids'); // Array or selected records primary keys $photoModels = GalleryPhoto::findAll($photoIds); if (empty($photoModels)) { return null; } $galleryModel = $this->findModel($photoModels[0]->gallery_id); $dir = Yii::getAlias('@app/web/img/gallery/' . Translator::rus2translit($galleryModel->name)); foreach ($photoModels as $photo) { try { unlink($dir . '/' . $photo->name); unlink($dir . '/thumb/' . $photo->name); } catch (\Exception $e) { echo 'Не удалось удалить файл ' . $photo->name . ' - ' . $e->getMessage(); } } GalleryPhoto::deleteAll(['photo_id' => $photoIds]); if ($request->isAjax) { Yii::$app->response->format = Response::FORMAT_JSON; return true; } else { return $this->redirect(['index']); } }
<div class="gallery-item"> <div class="image"> <?php echo Html::beginTag('div', ['class' => 'change-btns']); echo Html::a('<i class="glyphicon glyphicon-trash"></i>', Url::toRoute(["delete", 'id' => $model->gallery_id]), ['title' => 'Delete', 'class' => 'update-btn', 'role' => 'modal-toggle', 'data-modal-title' => 'Are you sure?', 'data-modal-body' => 'This will permanently delete all the pictures are in the gallery.']); echo Html::a('<i class="glyphicon glyphicon-pencil"></i>', Url::toRoute(["update", 'id' => $model->gallery_id]), ['title' => 'Update', 'method' => 'get', 'class' => "update-btn", 'role' => "modal-toggle", 'data-modal-title' => 'Update']); echo Html::endTag('div'); ?> <a class="image-wrap" href="<?php echo Url::toRoute(["view", 'id' => $model->gallery_id]); ?> "> <?php foreach ($model->galleryPhotos as $prevPhoto) { echo \yii\helpers\Html::img('/img/gallery/' . Translator::rus2translit($model->name) . '/thumb/' . $prevPhoto->name); } ?> </a> </div> <div class="name"> <span><?php echo $model->name; ?> </span> <span class="date-gallery"><?php echo ' (' . $date->format('d.m.Y') . ')'; ?> </span> </div> </div>
$this->registerJs(<<<JS \$('#preloader').show(); \$('body').css('overflow', 'hidden'); window.onload = function() { \t\$('body').css('overflow', 'auto'); \$('#preloader').hide(); }; \$("[data-toggle='tooltip']").tooltip(); JS ); echo Html::beginTag('div', ['class' => 'gallery-view']); echo \yii\bootstrap\Collapse::widget(['items' => [['label' => $model->name . ' (' . count((array) $photos) . ' photos)', 'content' => !empty($model->descr) ? $model->descr : '']], 'options' => ['class' => 'header-collapse']]); $galleryName = $model->name; if (!empty($photos)) { foreach ($photos as $photo) { $items[] = ['original' => '/img/gallery/' . Translator::rus2translit($galleryName) . '/' . $photo->name, 'thumbnail' => '/img/gallery/' . Translator::rus2translit($galleryName) . '/thumb/' . $photo->name, 'options' => ['title' => $galleryName, 'data-id' => $photo->photo_id]]; } } else { echo 'There is no photos yet...'; } ?> <div class="row"> <div class="col-md-1"></div> <div class="col-md-10"> <?php if (!empty($items)) { echo Gallery::widget(['id' => 'gallery-links', 'items' => $items, 'pluginOptions' => ['slideshowInterval' => 2000, 'transitionSpeed' => 200]]); } ?> </div> <div class="col-md-1"></div>