118 lines
4.9 KiB
PHP
118 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
use Illuminate\Http\Request;
|
|
use App\Http\Requests\LambdaRequest;
|
|
use App\Models\Library;
|
|
use App\Models\Session;
|
|
use URL;
|
|
class LibraryController extends LocalController
|
|
{
|
|
public function __construct()
|
|
{
|
|
$this->middleware('auth');
|
|
$this->_service = 'mag';
|
|
$this->_module = 'magazine';
|
|
$this->_viewname = 'library';
|
|
$this->_title = 'Library Collection';
|
|
$this->_base_url = '/library';
|
|
$this->_basemodel = new Library;
|
|
}
|
|
|
|
public function list(Request $request) {
|
|
$values = $this->get_list_data($request);
|
|
$sessions = Session::orderby('id', 'desc')->select("id", "number", "type", "start_date", "venue")->get();
|
|
$sessions_dd = [];
|
|
foreach($sessions as $sess){
|
|
$sessions_dd[$sess->id] = $sess->number . ' ('.$sess->type.') on ' . date("d M Y", strtotime($sess->start_date) ) . ' at ' . $sess->venue ;
|
|
}
|
|
//echo '<pre>'; print_r($sessions_dd); print_r($values); exit;
|
|
foreach($values as &$value) {
|
|
$value->link = URL::to('/') . '/contentlibrary/'.$value->code;
|
|
$value->session = '';
|
|
if($value->session_id > 0) {
|
|
$value->session = $sessions_dd[$value->session_id];
|
|
}
|
|
}
|
|
$fields = $this->get_list_fields();
|
|
return $this->view('list')->with([
|
|
'title' => $this->_title,
|
|
'fields' => $fields,
|
|
'base_url' => $this->_base_url,
|
|
'items' => $values,
|
|
'buttons' => $this->get_list_buttons()
|
|
]);
|
|
}
|
|
|
|
protected function get_list_buttons() {
|
|
return [ ['title' => 'Add New', 'link' => $this->_base_url.'/addnew' ] ];
|
|
}
|
|
|
|
protected function get_list_fields(){
|
|
return [
|
|
'actions' => ['title' => 'Actions', 'buttons' => [ 'edit', 'delete', 'visit' => ['icon' => 'eye', 'querystring' => 'id={ID}', 'target' => '_blank'] ] ],
|
|
'title' => ['title' => 'Collection Name', 'type' => 'text'],
|
|
'session' => ['title' => 'Session Name', 'type' => 'text'],
|
|
'link' => ['title' => 'Collection URL', 'type' => 'text'],
|
|
'status' => ['title' => 'Status', 'type' => 'select'],
|
|
];
|
|
}
|
|
|
|
protected function get_form_fields($type, $values){
|
|
$magzines = $this->getlambda('get', $this->_service, $this->_module, 'dropdown');
|
|
//echo '<pre>'; print_r($magzines); exit;
|
|
$magzines_dd = [];
|
|
foreach($magzines->data->items as $magzine){
|
|
$magzines_dd[$magzine->id] = $magzine->name;
|
|
}
|
|
$sessions = Session::orderby('id', 'desc')->select("id", "number", "type", "start_date", "venue")->get();
|
|
$sessions_dd = [];
|
|
foreach($sessions as $sess){
|
|
$sessions_dd[$sess->id] = $sess->number . ' ('.$sess->type.') on ' . date("d M Y", strtotime($sess->start_date) ) . ' at ' . $sess->venue ;
|
|
}
|
|
$fields = [
|
|
'title' => ['title' => 'Library Name', 'type' => 'text', 'required' => true],
|
|
'status' => ['title' => 'Status', 'type' => 'select', 'required' => true, 'values' => ['active' => 'Active', 'disabled' => 'Disabled'] ],
|
|
'description' => ['title' => 'Library Description', 'type' => 'textarea', 'required' => true,],
|
|
'type' => ['title' => 'Type', 'type' => 'select', 'required' => true, 'values' => ['pre' => 'Pre Course', 'post' => 'Post Course'] ],
|
|
'library_ids' => ['title' => 'Libraries', 'type' => 'checkbox', 'required' => true, 'values' => $magzines_dd ],
|
|
'session_id' => ['title' => 'Session', 'type' => 'select', 'required' => true, 'values' => $sessions_dd ],
|
|
];
|
|
return $fields;
|
|
}
|
|
|
|
protected function _beforesave($type, $request) {
|
|
$data = $request->except('_token');
|
|
if ($type == 'new') {
|
|
$data['code'] = \Str::random(20);
|
|
}
|
|
//echo '<pre>'; print_r($data);
|
|
$data['library_values'] = [];
|
|
$magzines = $this->getlambda('get', $this->_service, $this->_module, 'dropdown');
|
|
foreach($magzines->data->items as $magzine){
|
|
if(in_array($magzine->id, $data['library_ids'])){
|
|
$data['library_values'][] = $magzine;
|
|
}
|
|
}
|
|
$data['library_values'] = json_encode($data['library_values']);
|
|
$data['library_ids'] = implode(',', $data['library_ids']);
|
|
// echo '<pre>';
|
|
// print_r($magzines->data->items);
|
|
// print_r($data);
|
|
// exit;
|
|
return $data;
|
|
}
|
|
protected function _beforeform($type, $values) {
|
|
if ($type == 'edit') {
|
|
$values->library_ids = explode(',', $values->library_ids);
|
|
}
|
|
return $values;
|
|
}
|
|
|
|
public function visit(Request $request) {
|
|
$id = $request->input('id');
|
|
$values = $this->_basemodel->find($id)->toArray();
|
|
return redirect('/contentlibrary/'.$values['code']);
|
|
}
|
|
}
|