viewData['pageTitle'] = lang('PapelImpresion.moduleTitle'); $this->viewData['usingSweetAlert'] = true; // Se indica que este controlador trabaja con soft_delete $this->soft_delete = true; // Se indica el flag para los ficheros borrados $this->delete_flag = 1; $this->tpModel = new PapelImpresionTipologiaModel(); // Breadcrumbs $this->viewData['breadcrumb'] = [ ['title' => lang("App.menu_configuration"), 'route' => "javascript:void(0);", 'active' => false], ['title' => lang("App.menu_papelimpresion"), 'route' => site_url('configuracion/papelesimpresion'), 'active' => true] ]; parent::initController($request, $response, $logger); } public function index() { $viewData = [ 'currentModule' => static::$controllerSlug, 'pageSubTitle' => lang('Basic.global.ManageAllRecords', [lang('PapelImpresion.papelImpresion')]), 'papelImpresion' => new PapelImpresion(), 'usingServerSideDataTable' => true, ]; $viewData = array_merge($this->viewData, $viewData); // merge any possible values from the parent controller class return view(static::$viewPath . 'viewPapelImpresionList', $viewData); } public function add() { $requestMethod = $this->request->getMethod(); if ($requestMethod === 'post') : $nullIfEmpty = true; // !(phpversion() >= '8.1'); $postData = $this->request->getPost(); $sanitizedData = $this->sanitized($postData, $nullIfEmpty); $noException = true; if ($successfulResult = $this->canValidate()) : // if ($successfulResult = $this->validate($this->formValidationRules) ) : if ($this->canValidate()) : try { $successfulResult = $this->model->skipValidation(true)->save($sanitizedData); } catch (\Exception $e) { $noException = false; $this->dealWithException($e); } else : $this->viewData['errorMessage'] = lang('Basic.global.formErr1', [lang('Basic.global.record')]); $this->session->setFlashdata('formErrors', $this->model->errors()); endif; $thenRedirect = true; // Change this to false if you want your user to stay on the form after submission endif; if ($noException && $successfulResult) : $id = $this->model->db->insertID(); $message = lang('Basic.global.saveSuccess', [lang('Basic.global.record')]) . '.'; if ($thenRedirect) : if (!empty($this->indexRoute)) : return redirect()->to(route_to($this->indexRoute))->with('sweet-success', $message); else : return $this->redirect2listView('sweet-success', $message); endif; else : $this->session->setFlashData('sweet-success', $message); endif; endif; // $noException && $successfulResult endif; // ($requestMethod === 'post') $this->viewData['papelImpresion'] = isset($sanitizedData) ? new PapelImpresion($sanitizedData) : new PapelImpresion(); $this->viewData['papelGenericoList'] = $this->getPapelGenericoListItems($papelImpresion->papel_generico_id ?? null); $this->viewData['formAction'] = route_to('createPapelImpresion'); $this->viewData['boxTitle'] = lang('Basic.global.addNew') . ' ' . lang('PapelImpresion.moduleTitle') . ' ' . lang('Basic.global.addNewSuffix'); return $this->displayForm(__METHOD__); } // end function add() public function edit($requestedId = null) { // JJO $session = session(); if ($requestedId == null) : return $this->redirect2listView(); endif; $id = filter_var($requestedId, FILTER_SANITIZE_URL); $papelImpresion = $this->model->find($id); if ($papelImpresion == false) : $message = lang('Basic.global.notFoundWithIdErr', [mb_strtolower(lang('PapelImpresion.papelImpresion')), $id]); return $this->redirect2listView('sweet-error', $message); endif; $requestMethod = $this->request->getMethod(); if ($requestMethod === 'post') : $nullIfEmpty = true; // !(phpversion() >= '8.1'); $postData = $this->request->getPost(); $sanitizedData = $this->sanitized($postData, $nullIfEmpty); // JJO $sanitizedData['user_updated_id'] = $session->id_user; if ($this->request->getPost('defecto') == null) { $sanitizedData['defecto'] = false; } if ($this->request->getPost('bn') == null) { $sanitizedData['bn'] = false; } if ($this->request->getPost('color') == null) { $sanitizedData['color'] = false; } if ($this->request->getPost('portada') == null) { $sanitizedData['portada'] = false; } if ($this->request->getPost('cubierta') == null) { $sanitizedData['cubierta'] = false; } if ($this->request->getPost('rotativa') == null) { $sanitizedData['rotativa'] = false; } $noException = true; if ($successfulResult = $this->canValidate()) : // if ($successfulResult = $this->validate($this->formValidationRules) ) : if ($this->canValidate()) : try { $successfulResult = $this->model->skipValidation(true)->update($id, $sanitizedData); } catch (\Exception $e) { $noException = false; $this->dealWithException($e); } else : $this->viewData['warningMessage'] = lang('Basic.global.formErr1', [mb_strtolower(lang('PapelImpresion.papelImpresion'))]); $this->session->setFlashdata('formErrors', $this->model->errors()); endif; $papelImpresion->fill($sanitizedData); $thenRedirect = false; endif; if ($noException && $successfulResult) : $id = $papelImpresion->id ?? $id; $message = lang('Basic.global.updateSuccess', [lang('Basic.global.record')]).'.'; if ($thenRedirect) : if (!empty($this->indexRoute)) : return redirect()->to(route_to($this->indexRoute))->with('sweet-success', $message); else : return $this->redirect2listView('sweet-success', $message); endif; else : $this->session->setFlashData('sweet-success', $message); endif; endif; // $noException && $successfulResult endif; // ($requestMethod === 'post') $this->viewData['papelImpresion'] = $papelImpresion; $this->viewData['papelGenericoList'] = $this->getPapelGenericoListItems($papelImpresion->papel_generico_id ?? null); $this->viewData['formAction'] = route_to('updatePapelImpresion', $id); $this->viewData['boxTitle'] = lang('Basic.global.edit2') . ' ' . lang('PapelImpresion.moduleTitle') . ' ' . lang('Basic.global.edit3'); $this->viewData['usingServerSideDataTable'] = true; //JJO return $this->displayForm(__METHOD__, $id); } // end function edit(...) public function datatable() { if ($this->request->isAJAX()) { $reqData = $this->request->getPost(); if (!isset($reqData['draw']) || !isset($reqData['columns'])) { $errstr = 'No data available in response to this specific request.'; $response = $this->respond(Collection::datatable([], 0, 0, $errstr), 400, $errstr); return $response; } if(isset($reqData['id_PI'])){ $id_PI = $reqData['id_PI'] ?? -1; if($id_PI>=0){ $data = $this->tpModel->findTipologiasForPapelImpresion($id_PI); $resourceData = $data->get()->getResultObject(); return $this->respond(Collection::datatable( $resourceData, $this->tpModel->findTipologiasForPapelImpresion($id_PI)->countAllResults(), $this->tpModel->findTipologiasForPapelImpresion($id_PI)->countAllResults() )); } } else{ $id_PG = $reqData['id_PG'] ?? -1; $start = $reqData['start'] ?? 0; $length = $reqData['length'] ?? 5; $search = $reqData['search']['value']; $requestedOrder = $reqData['order']['0']['column'] ?? 1; $order = PapelImpresionModel::SORTABLE[$requestedOrder >= 0 ? $requestedOrder : 1]; $dir = $reqData['order']['0']['dir'] ?? 'asc'; if($id_PG<0){ $resourceData = $this->model->getResource($search)->orderBy($order, $dir)->limit($length, $start)->get()->getResultObject(); return $this->respond(Collection::datatable( $resourceData, $this->model->getResource()->countAllResults(), $this->model->getResource($search)->countAllResults() )); }else{ $resourceData = $this->model->getResource($search, $id_PG)->orderBy($order, $dir)->limit($length, $start)->get()->getResultObject(); return $this->respond(Collection::datatable( $resourceData, $this->model->getResource("", $id_PG)->countAllResults(), $this->model->getResource($search, $id_PG)->countAllResults() )); } } } else { return $this->failUnauthorized('Invalid request', 403); } } public function datatable_editor(){ /*if ($this->request->isAJAX()) { $reqData = $this->request->getPost(); $rows_id = array_keys($reqData['data']); echo '
' ;
var_dump($reqData['data'][$keys[0]]) ;
echo '';
}*/
if ($this->request->isAJAX()) {
$sql_details = array(
"type" => "Mysql", // Database type: "Mysql", "Postgres", "Sqlserver", "Sqlite" or "Oracle"
"user" => "sk_jjo", // Database user name
"pass" => "61tv&G1Zf^XY", // Database password
"host" => "localhost", // Database host
"port" => "", // Database connection port (can be left empty for default)
"db" => "sk_jjo", // Database name
"dsn" => "", // PHP DSN extra information. Set as `charset=utf8mb4` if you are using MySQL
"pdoAttr" => array() // PHP PDO attributes array. See the PHP documentation for all options
);
$db = new Database( array(
"type" => "Mysql",
"pdo" => $sql_details
) );
// Build our Editor instance and process the data coming from _POST
Editor::inst( $db, 'lg_papel_impresion_tipologias' )
->fields(
Field::inst( 'tipo' ),
//->validator( Validate::notEmpty( ValidateOptions::inst()) ),//->validator( Validate::values( array('negro', 'color', 'bicolor') ) ),
Field::inst( 'negro' ),
//->validator( Validate::notEmpty( ValidateOptions::inst()) ),
Field::inst( 'cyan' ),
//->validator( Validate::notEmpty( ValidateOptions::inst()) ),
Field::inst( 'magenta' ),
//->validator( Validate::notEmpty( ValidateOptions::inst()) ),
Field::inst( 'amarillo' ),
//->validator( Validate::notEmpty( ValidateOptions::inst()) ),
Field::inst( 'gota_negro' ),
//->validator( Validate::notEmpty( ValidateOptions::inst()) ),
Field::inst( 'got_color' ),
//->validator( Validate::notEmpty( ValidateOptions::inst()) ),
)
->process( $_POST )
->json();
}
}
public function allItemsSelect()
{
if ($this->request->isAJAX()) {
$onlyActiveOnes = true;
$reqVal = $this->request->getPost('val') ?? 'id';
$menu = $this->model->getAllForMenu($reqVal . ', nombre', 'nombre', $onlyActiveOnes, false);
$nonItem = new \stdClass;
$nonItem->id = '';
$nonItem->nombre = '- ' . lang('Basic.global.None') . ' -';
array_unshift($menu, $nonItem);
$newTokenHash = csrf_hash();
$csrfTokenName = csrf_token();
$data = [
'menu' => $menu,
$csrfTokenName => $newTokenHash
];
return $this->respond($data);
} else {
return $this->failUnauthorized('Invalid request', 403);
}
}
public function menuItems()
{
if ($this->request->isAJAX()) {
$searchStr = goSanitize($this->request->getPost('searchTerm'))[0];
$reqId = goSanitize($this->request->getPost('id'))[0];
$reqText = goSanitize($this->request->getPost('text'))[0];
$onlyActiveOnes = false;
$columns2select = [$reqId ?? 'id', $reqText ?? 'nombre'];
$onlyActiveOnes = false;
$menu = $this->model->getSelect2MenuItems($columns2select, $columns2select[1], $onlyActiveOnes, $searchStr);
$nonItem = new \stdClass;
$nonItem->id = '';
$nonItem->text = '- ' . lang('Basic.global.None') . ' -';
array_unshift($menu, $nonItem);
$newTokenHash = csrf_hash();
$csrfTokenName = csrf_token();
$data = [
'menu' => $menu,
$csrfTokenName => $newTokenHash
];
return $this->respond($data);
} else {
return $this->failUnauthorized('Invalid request', 403);
}
}
function fetch_single_data()
{
if ($this->request->isAJAX()) {
$reqData = $this->request->getPost();
if(isset($reqData['id']))
{
$newTokenHash = csrf_hash();
$csrfTokenName = csrf_token();
$data = [
'data' => $this->tpModel->getResource($reqData['id'])->get()->getResultObject()[0],
$csrfTokenName => $newTokenHash
];
echo json_encode($data);
}
}
}
protected function getPapelGenericoListItems($selId = null)
{
$data = ['' => lang('Basic.global.pleaseSelectA', [mb_strtolower(lang('PapelGenerico.papelGenerico'))])];
if (!empty($selId)) :
$papelGenericoModel = model('App\Models\Configuracion\PapelGenericoModel');
$selOption = $papelGenericoModel->where('id', $selId)->findColumn('nombre');
if (!empty($selOption)) :
$data[$selId] = $selOption[0];
endif;
endif;
return $data;
}
}