Files
simrs-jatim/core/Mastermain.php
2024-04-19 14:04:41 +07:00

492 lines
11 KiB
PHP

<?php
$path = realpath(__DIR__);
require_once $path.'/../core/main.php';
require_once ( $path.'/../core/ssp.class.php' );
// require_once (_DOCROOT_.'vendor/autoload.php');
require_once ($path.'/../vendor/autoload.php');
use Verot\Upload\Upload;
Class Mastermain {
public $title = '';
public $subtitle = '';
public $add_button = '';
public $edit_button = '';
public $delete_button = '';
public $table = '';
public $primaryKey = '';
public $select = '';
public $join = '';
public $where = '';
public $groupBy = '';
public $orderBy = '';
protected $render_html = '';
function __construct() {
global $db;
$args = func_get_args();
$this->add_button = '<button type="button" id="add_button" class="btn btn-sm btn-flat bg-info text-white btn-block"><i class="fa fa-plus"></i> Baru</button>';
$this->edit_button = '<button type="button" id="edit_button" class="btn btn-sm btn-flat bg-orange"><i class="fa fa-pencil"></i> Edit</button>';
$this->delete_button = '<button type="button" id="delete_button" class="btn btn-sm btn-flat btn-danger"><i class="fa fa-trash"></i> Hapus</button>';
}
public function view() {
$args = func_get_args();
if(array_key_exists(0, $args)) {
$view_file = $args[0];
}
else {
return false;
}
$this->render_html = $this->render($view_file, $args[1]);
if($args[2] == true) {
return $this->render_html;
}
else {
echo $this->render_html;
}
}
private function render()
{
$args = func_get_args();
if(count($args) > 0) {
$template = explode("/", $args[0]);
$file_name = end($template);
array_pop($template);
$folder = implode("/", $template);
$loader = new \Twig\Loader\FilesystemLoader(_DOCROOT_.$folder);
$twig = new \Twig\Environment($loader, [
'cache' => _DOCROOT_.'logs/cache',
]);
// $escaper = new \Twig\Extension\EscaperExtension('html');
// $twig->addExtension($escaper);
return $twig->render($file_name, $args[1]);
}
}
function add() {
if($this->table != '') {
$table = $this->table;
$args = func_get_args();
if(count($args) > 0) {
if(is_array($args[0])) {
$is_returning = (array_key_exists(1, $args)) ? $args[1] : false;
$returning_col = ($is_returning) ? $args[2] : false;
$query_returning = "";
if($returning_col != false) {
$query_returning = " RETURNING ".$returning_col;
}
$query = "INSERT INTO $table ".bind_sql($args[0]).$query_returning;
return execute($query);
}
else {
throw new Exception("Error Parameter Not Found", 1);
}
}
else {
throw new Exception("Error Parameter Not Found", 1);
}
}
else {
throw new Exception("Error Table Not Found", 1);
}
}
function edit() {
if($this->table != '') {
$table = $this->table;
$args = func_get_args();
if(count($args) > 0) {
if(is_array($args[0])) {
$update_condition = (array_key_exists(1, $args)) ? $args[1] : false;
if($update_condition != ''){
$query = "UPDATE $table ".bind_sql($args[0],1).' WHERE '.$update_condition;
return execute($query);
}
else {
throw new Exception("Error Where Condition Not Found", 1);
}
}
else {
throw new Exception("Error Parameter Not Found", 1);
}
}
else {
throw new Exception("Error Parameter Not Found", 1);
}
}
else {
throw new Exception("Error Table Not Found", 1);
}
}
function delete() {
if($this->table != '') {
$table = $this->table;
$args = func_get_args();
if(count($args) > 0) {
if(is_array($args[0])) {
$delete_condition = implode("\r\n",$args[0]);
if($delete_condition != ''){
$query = "DELETE FROM $table WHERE ".$delete_condition;
return execute($query);
}
else {
throw new Exception("Error Where Condition Not Found", 1);
}
}
else {
throw new Exception("Error Parameter Not Found", 1);
}
}
else {
throw new Exception("Error Parameter Not Found", 1);
}
}
else {
throw new Exception("Error Table Not Found", 1);
}
}
function check() {
global $db;
if($this->table != '') {
$table = $this->table;
$args = func_get_args();
if(count($args) > 0) {
if(is_array($args[0])) {
$check_condition = implode("\r\n",$args[0]);
if($check_condition != ''){
$query = "SELECT * FROM $table WHERE ".$check_condition;
$row_result = $db->query($query);
if($row_result->numRows() > 0)
{
return FALSE;
}
return TRUE;
}
else {
throw new Exception("Error Where Condition Not Found", 1);
}
}
else {
throw new Exception("Error Parameter Not Found", 1);
}
}
else {
throw new Exception("Error Parameter Not Found", 1);
}
}
else {
throw new Exception("Error Table Not Found", 1);
}
}
function soft_delete() {
if($this->table != '') {
$table = $this->table;
$args = func_get_args();
if(count($args) > 0) {
if(is_array($args[0])) {
$delete_condition = implode("\r\n",$args[0]);
if($delete_condition != ''){
$timestamp = date('Y-m-d H:i:s');
$query = "UPDATE $table SET deleted_at = '$timestamp' WHERE ".$delete_condition;
return execute($query);
}
else {
throw new Exception("Error Where Condition Not Found", 1);
}
}
else {
throw new Exception("Error Parameter Not Found", 1);
}
}
else {
throw new Exception("Error Parameter Not Found", 1);
}
}
else {
throw new Exception("Error Table Not Found", 1);
}
}
function datasource() {
global $db;
$args = func_get_args();
// get table
if($this->table != '') {
$table = $this->table;
}
else {
throw new Exception("Error Table Not Found", 1);
}
// get selected column
$columns = [];
if(is_array($this->select)) {
// $select = implode(", ",$this->select);
$column_inx = 0;
foreach($this->select as $field => $item) {
$arr_field = [];
if(is_array($item)) {
$arr_field = ['db'=>$field,'dt'=>$column_inx];
if(array_key_exists('format',$item)) {
$arr_field['formatter'] = $item['format'];
}
$columns[] = $arr_field;
}
else {
$columns[] = ['db'=>$field,'dt'=>$column_inx];
}
$column_inx++;
}
}
else {
// $select = $this->select;
// if($select == '') {
// $select = '*';
// }
}
// get join table
if(is_array($this->join)) {
$join = '';
foreach($this->join as $rel => $item) {
if(is_array($item)) {
$val = array_values($item);
$table_name = $rel;
if(count($val) > 1) {
$join_type = "\r\n".$val[1];
$join_condition = $val[0];
}
else {
$join_type = "\r\nJOIN ";
$join_condition = $val[0];
}
$join .= $join_type.' '.$table_name.' ON '.$join_condition;
}
else {
$join .= $item;
}
}
}
if($this->where != '') {
if(is_array($this->where)) {
$where = implode("\r\n", $this->where);
}
else {
$where = 'WHERE '.$this->where;
}
}
if($this->groupBy != '') {
if(is_array($this->groupBy)) {
$groupBy = 'GROUP BY '.implode("\r\n", $this->groupBy);
}
else {
$groupBy = 'GROUP BY '.$this->groupBy;
}
}
if($this->orderBy != '') {
if(is_array($this->orderBy)) {
$orderBy = 'ORDER BY '.implode("\r\n", $this->orderBy);
}
else {
$orderBy = 'ORDER BY '.$this->orderBy;
}
}
// Table's primary key
$primaryKey = $this->primaryKey;
// Array of database columns which should be read and sent back to DataTables.
// The `db` parameter represents the column name in the database, while the `dt`
// parameter represents the DataTables column identifier. In this case simple
// indexes
// SQL server connection information
$dbhost = $_ENV['POSTGRE_DB_HOST'];
$dbuser = $_ENV['POSTGRE_DB_USER'];
$dbpass = $_ENV['POSTGRE_DB_PASS'];
$dbname = $_ENV['POSTGRE_DB_NAME'];
$port = $_ENV['POSTGRE_DB_PORT'];
$sql_details = array(
'user' => $dbuser,
'pass' => $dbpass,
'db' => $dbname,
'host' => $dbhost,
'port' => $port
);
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* If you just want to use the basic configuration for DataTables with PHP
* server-side, there is no need to edit below this line.
*/
return json_encode(
SSP::complex( $args, $sql_details, $table, $join, $primaryKey, $columns , $where)
);
// $tmp_query = $db->query("SELECT $select FROM $table $join $where $groupBy $orderBy");
// $result['data'] = [];
// if($tmp_query->numRows() > 0) {
// foreach($tmp_query->fetchAll() as $row) {
// $result['data'][] = $row;
// }
// }
// return json_encode($result);
// exit;
}
private function strip_slash(&$value) {
if(is_array($value))
{
strip($value);
}
else {
$value = $value;
}
}
private function strip(&$request)
{
if(!is_array($request))
{
die("Inputan Bukan Array");
}
array_walk($request, "strip_slash");
return $request;
}
public function request() {
$wrap_request = [];
if(!empty($_REQUEST))
{
$getVar = $this->strip($_REQUEST);
if(!empty($getVar)) {
foreach($getVar as $k => $v) {
if(!array_key_exists($k, $wrap_request)) {
$wrap_request[$k] = $v;
}
}
}
}
if(!empty($_POST))
{
$getVar = $this->strip($_POST);
if(!empty($getVar)) {
foreach($getVar as $k => $v) {
if(!array_key_exists($k, $wrap_request)) {
$wrap_request[$k] = $v;
}
}
}
}
if(!empty($_GET))
{
$getVar = $this->strip($_GET);
if(!empty($getVar)) {
foreach($getVar as $k => $v) {
if(!array_key_exists($k, $wrap_request)) {
$wrap_request[$k] = $v;
}
}
}
}
return $wrap_request;
}
public function load_form(){
global $db;
}
public function file_save($file,$config){
global $path;
$base_upload_dir = './storage/';
// handle upload
$handle = new Upload($file);
$handle->file_max_size = (array_key_exists('file_max_size',$config)) ? $config['file_max_size'] : '8M';
$handle->allowed = (array_key_exists('allowed',$config)) ? $config['allowed'] : array('image/jpg','image/jpeg','image/png');
if($config['resize'])
{
$handle->image_resize = $config['resize'];
$handle->image_x = $config['image_x'];
$handle->image_y = $config['image_y'];
if(array_key_exists('image_ratio_y',$config) && $config['image_ratio_y'])
{
$handle->image_ratio_y = $config['image_ratio_y'];
}
if(array_key_exists('image_ratio_x',$config) && $config['image_ratio_x'])
{
$handle->image_ratio_x = $config['image_ratio_x'];
}
}
if ($handle->uploaded) {
$sub = (array_key_exists('storage_dir',$config)) ? $config['storage_dir'].'/' : '';
$sub_dir = makeDir($base_upload_dir.$sub,0777);
$handle->process($base_upload_dir.$sub);
if ($handle->processed) {
$handle->clean();
return $handle->file_dst_pathname;
} else {
log_message('error','Upload error '.$handle->error.". Dir ".$base_upload_dir.$sub);
throw new Exception("Error Upload : ".$handle->error.". Dir ".$base_upload_dir.$sub, 1);
return $handle->error;
}
}
else {
log_message('error','Upload error '.$handle->error.". Dir ".$base_upload_dir.$sub);
throw new Exception("Error Upload : ".$handle->error.". Dir ".$base_upload_dir.$sub, 1);
return false;
}
}
}