CrestRoad/laravel/app/Http/Controllers/ArticlebankController.php

107 lines
3.6 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\LambdaRequest;
use App\Http\Requests\FormAdditional;
use DB;
use Log;
use Storage;
use Maatwebsite\Excel\Facades\Excel;
use App\Exports\InvoicesExport;
use Auth;
use App\Mail\ReportExcel;
use Mail;
use Illuminate\Support\Facades\Cache;
use Aws\Ses\SesClient;
use Aws\Credentials\CredentialProvider;
class ArticlebankController extends Controller
{
public function __construct()
{
$this->_service = 'lms';
$this->_module = 'articles_bank';
$this->_viewname = 'articlesbank';
$this->_title = 'Article Bank';
$this->_base_url = '/articlebank';
}
protected function get_list_fields()
{
return [
'actions' => ['title' => 'Actions', 'buttons' => ['edit', 'delete']],
'title' => ['title' => 'Title', 'type' => 'text'],
'description' => ['title' => 'Description', 'type' => 'text'],
'status' => ['title' => 'Status', 'type' => 'select']
];
}
protected function get_form_fields($type, $values)
{
return [
'title' => ['title' => 'Title', 'type' => 'text', 'required' => true],
'description' => ['title' => 'Description', 'type' => 'text', 'required' => true],
'status' => ['title' => 'Status', 'type' => 'select', 'required' => true, 'values' => ['active' => 'Active', 'disabled' => 'Disabled']],
'contents' => ['title' => 'Content', 'type' => 'mapping', 'fullwidth' => true],
'icon' => ['title' => 'Icon', 'type' => 'file', 'required' => true]
];
}
protected function _beforesave($type, $request)
{
$data = $request->except('_token');
if ($request->file('icon')) {
$data['icon'] = $this->_uploadMedia($request->file('icon'), 'icon', 'lms_cources');
}
$contents = [];
foreach($data['sortorder'] as $i => $sort) {
if(isset($data['content'][$i]) && is_object($data['content'][$i]) && get_class($data['content'][$i]) == 'Illuminate\Http\UploadedFile') {
$data['content'][$i] = $this->_uploadMedia($data['content'][$i], 'image', 'lms_cources');
}
$contents[] = ['order' => $sort, 'content' => isset($data['content'][$i]) ? (string)$data['content'][$i] : (isset($data['old-content'][$i]) ? (string)$data['old-content'][$i] : ''), 'type' => $data['type'][$i]];
}
$data['contents'] = $contents;
unset($data['sortorder']);
unset($data['type']);
unset($data['content']);
unset($data['old-content']);
// var_dump($data);
//echo '<pre>'; print_r($data); exit;
// foreach ($data['contents']['tempcontent'] as $tempcontent) {
// if (!is_file($tempcontent)) {
// $data['contents']['content'][] = $tempcontent;
// }
// }
// $files = $request->file('contents.tempcontent');
// if ($files) {
// foreach ($files as $file) {
// $path = $this->_uploadMedia($file, 'lms_articlebank');
// $data['contents']['content'][] = $path;
// }
// }
// unset($data['contents']['tempcontent']);
return $data;
}
protected function _uploadMedia($file, $name = 'image', $folder = 'image')
{
$path = '';
try {
if ($file) {
$path = $file->store($folder, 's3');
$path = env('AWS_URL') . '/' . $path;
}
} catch (AwsException $e) {
Log::debug($e->getMessage());
}
return $path;
}
}