236 lines
6.6 KiB
PHP

<?php
namespace Aiko;
use Aiko\Database\Connections;
use PDO;
use PHPExcel;
use Exception;
class Model
{
use Logdb {
insertLog as protected ;
}
protected $registry;
protected $query;
protected $param = array();
private $stmt;
private $db;
protected $preparedStatements;
public $enabledDebugMode = false;
public function __construct($registry)
{
$this->registry = $registry;
/* if($this->registry->config->dbMainConType!=='local')
{
$this->registry->db = Connections::getInstance($this->registry->config->dbMainConType);
}else {
$this->registry->db = Connections::getInstance(
$this->registry->config->dbMainConType,
$this->registry->config->host,
$this->registry->config->socket,
$this->registry->config->user,
$this->registry->config->password
);
} */
}
public function ConnectToOracle()
{
try {
$host = $this->registry->config->hostOracle;
$db = $this->registry->config->dbOracle;
$user = $this->registry->config->userOracle;
$pass = $this->registry->config->passwordOracle;
// die($user.$pass);
$this->registry->dbOracle = new PDO("oci:dbname=//$host:1521/$db;", "$user", "$pass", array(
PDO::ATTR_TIMEOUT => 10,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
));
$this->registry->dbOracle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->registry->dbOracle->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL);
} catch (\PDOException $e) {
die("Sorry, an error has occured. Please try your request \n");
}
}
public function connectToDBPMA()
{
$this->registry->dbpma = Connections::getInstancePMA();
}
protected function beginTransaction()
{
$this->registry->db->beginTransaction();
}
protected function commit()
{
$this->registry->db->commit();
}
protected function rollBack()
{
$this->registry->db->rollBack();
}
protected function sendResponse($moreInfo, $messages, $status)
{
return array(
"moreInfo" => $moreInfo,
"messages" => $messages,
"status" => $status,
);
}
public function connectToCarTal()
{
return Connections::getInstanceCartal($this->registry->config->dbCarTalType);
}
public function connectToScada()
{
return Connections::getInstanceSCADA($this->registry->config->dbScadaType);
}
protected function checkValidNikByEmpId($empId,$nik){
try{
$stmt=$this->registry->db->prepare('select nik from employement where nik=:nik and emp_profile_id=:emp_id');
$stmt->bindValue(':nik',$nik,PDO::PARAM_STR);
$stmt->bindValue(':emp_id',$empId,PDO::PARAM_INT);
$stmt->execute();
if($stmt->rowCount()>0){
return true;
}
return false;
}catch(\PDOException $e){
return false;
}catch(\ErrorException $e){
return false;
}
}
private function serializeColumn($arr = [], $isBinding = false)
{
$serialize = '';
for ($i = 0; $i < count($arr); $i++) {
if ($isBinding) {
$serialize .= ":$arr[$i],";
} else {
if (is_numeric($arr[$i])) {
$serialize .= "$arr[$i],";
} else {
$serialize .= "'$arr[$i]',";
}
}
}
return substr($serialize, 0, -1);
}
public function prepareQuery($query)
{
if (isset($this->preparedStatements[$query])) {
$stmt = $this->preparedStatements[$query];
} else {
// Call PDO::prepare.
$stmt = $this->registry->db->prepare($query);
$this->preparedStatements[$query] = $stmt;
}
return $stmt;
}
private function checkDebugMode($stmt)
{
if ($this->enabledDebugMode) {
$stmt->debugDumpParams();
die();
}
}
protected function error($e, $name="")
{
if(!empty($name)){
$this->registry->log->customError($name, 'Message: '. $e->getMessage() . ' | Line: '. $e->getLine(). ' | File: '. $e->getFile()) . ' | User: ' . \Helper::getSessionVar('username');
}else{
$this->registry->log->error('Message: '. $e->getMessage() . ' | Line: '. $e->getLine(). ' | File: '. $e->getFile() . ' | User: ' . \Helper::getSessionVar('username'));
}
}
protected function getColumnIndex($col)
{
$idx = \PHPExcel_Cell::columnIndexFromString($col);
return $idx - 1;
}
protected function getWorkSheetData($fileName, $sheetIndex)
{
$objPHPExcel = new PHPExcel();
$inputFileType = \PHPExcel_IOFactory::identify($fileName);
$objReader = \PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($fileName);
$objWorkSheet = $objPHPExcel->setActiveSheetIndex($sheetIndex);
return $objWorkSheet;
}
/**
* $type allowed
* - time untuk jam
* - date untuk tanggal
*/
protected function getColumnValue($objWorkSheet, $columnIndex, $row, $type='')
{
$result = $objWorkSheet->getCellByColumnAndRow($this->getColumnIndex($columnIndex), $row)->getValue();
if(!empty($type)){
$format = 'YYYY-MM-DD';
$defValue = "1970-01-01";
if($type=='time'){
$defValue = "00:00:00";
$format = 'hh:mm:ss';
}
if(empty($result)){
return $defValue;
}
return \PHPExcel_Style_NumberFormat::toFormattedString(trim($result), $format);
}
return trim($result);
}
protected function saveFileData($file, $path, $allowedMime = [])
{
$filename = $file->getClientOriginalName();
$mimeType = $file->getClientMimeType();
$mimeClientAlowed = array('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/vnd.ms-excel');
if(count($allowedMime)>0){
$mimeClientAlowed = $allowedMime;
}
if (!in_array($mimeType, $mimeClientAlowed)) {
throw new Exception('error file type');
}
$targetPath = $this->registry->config->base_storage. $path;
$targetFile = str_replace('//', '/', $targetPath);
$newfilename = $targetFile . '_' . time() . '_' . $filename;
// store data to storage
$file->move($targetFile, $newfilename);
return array('filename' => $filename, 'newfilename' => $newfilename);
}
protected function isEmpty($param)
{
if(is_null($param) || empty($param) || !$param){
return true;
}
return false;
}
}