File "GSModel-20260225063017.php"
Full Path: /home/ichhrkpd/public_html/application/controllers/GSModel-20260225063017.php
File size: 22.21 KB
MIME-type: text/x-php
Charset: utf-8
<?php
class GSModel extends CI_Model {
private static $db2 = null;
private static $ccount = 1;
private static $db1 = null;
private $selectDB = null;
private $updateDB = null;
protected $strPrpertyForLog = "";
protected $setProperties = array ();
protected $setOption = array ();
protected $validations = array ();
private $isValidationRule = false;
protected $tableName = null;
protected $primaryKey = null;
private $isSelected = false;
protected $UpdateRestrictedField=array();
protected $updateWhereExtraField=array();
//public $topic_type = array("S"=>"---Select---","T"=>"Theoritical","P"=>"Practical","B"=>"Both");
static function GetTotalQueries($isInSqlFile=false) {
if(!IsDebugMode){
return "";
}
$str = "";
if (isset ( GSModel::$db1->queries )) {
if($isInSqlFile){
$str .= "<hr/>/*Select Queries*/";
}else{
$str .= "<hr/>Select Queries";
}
foreach ( GSModel::$db1->queries as $qur ) {
$str .= "<hr/>" . $qur;
}
}
if (isset ( GSModel::$db2->queries )) {
if($isInSqlFile){
$str . "<hr/>/*Insert/Update Queries*/";
}else{
$str . "<hr/>Insert/Update Queries";
}
foreach ( GSModel::$db2->queries as $qur ) {
$str .= "<hr/>" . $qur;
}
}
return $str;
}
function SetWhereUpdate($property,$value){
$this->updateWhereExtraField[$property]=$value;
}
function UnsetUpdateRetrictedField(){
foreach ($this->UpdateRestrictedField as $key=>$value){
if(isset($this->setProperties[$key])&& property_exists($this, $key)){
$this->SetWhereUpdate($key, $this->$key);
}
unset($this->setProperties[$key]);
unset($this->setOption[$key]);
}
}
function GetPostValue($name){
$default="";
if(!empty($this->$name)){
$default= $this->$name;
}
return PostValue($name,$default);
}
function GetLog() {
$parm = func_get_args ();
$parm [0] = $this->lang->line ( strtolower ( $parm [0] ) );
if (isset ( $parm [1] ) && substr_count ( $parm [1], "~" ) > 0) {
$NewParam = array_merge ( array ($parm [0] ), explode ( "~", $parm [1] ) );
} else {
$NewParam = $parm;
}
if ((count ( $NewParam ) - 1) >= substr_count ( $parm [0], "%s" )) {
return call_user_func_array ( "sprintf", $NewParam );
}
return "Parametter missing";
}
function AddLog($msg = "", $parm, $value = "", $type = "V") {
if ($this->Mlog->AddLog ( $msg, $parm, $value, $type )) {
return true;
} else {
return false;
}
}
function IsPropertySet($property) {
if (! empty ( $this->setProperties [$property] )) {
return true;
} else {
return false;
}
}
function __construct() {
parent::__construct ();
$this->load->library ( 'form_validation' );
}
function LastInsertedId() {
return $this->GetUpdateDB ()->insert_id ();
}
function InsertBatch($DataArray) {
return $this->GetUpdateDB ()->insert_batch ( $this->tableName, $DataArray );
}
function AddDebugMsg($str, $status = StatusOk) {
if (IsDebugMode) {
Mlog::AddDebugMsg ( $str, $status, $this->session, true );
}
}
function GetDebugMsg() {
return Mlog::GetDebugMsg ( $this->session );
}
function AddSystemLog($str, $isSession = false) {
Mlog::AddSystemLog ( $str, $this->session, $isSession );
}
function AddError($error, $isSession = false,$isPTagAdded=false) {
Mlog::AddError ( $error, $this->session, $isSession ,$isPTagAdded);
}
function AddInfo($info, $isSession = false) {
Mlog::AddInfo ( $info, $this->session, $isSession );
}
function GetError() {
Mlog::GetError ( $this->session );
}
function GetInfo() {
Mlog::GetInfo ( $this->session );
}
function GetSysLog() {
Mlog::GetSystemLog ( $this->session );
}
/*function __destruct() {
$data = "----------------------------------------\n";
if (isset ( $this->selectDB->queries )) {
$length = count ( $this->selectDB->queries );
for($i = 0; $i < $length; $i ++) {
$data .= $this->selectDB->queries [$i] . " \n(takes time =" . $this->selectDB->query_times [$i] . ")\n";
$data .= "\n\n";
}
}
if (isset ( $this->updateDB->queries )) {
$length = count ( $this->updateDB->queries );
for($i = 0; $i < $length; $i ++) {
$data .= $this->updateDB->queries [$i] . " (takes time" . $this->updateDB->query_times [$i] . ") at " . date ( "D M j G:i:s T Y" ) . "\n";
$data .= "\n\n";
}
}
$myFile = BASEPATH . "log/query.log";
$fh = fopen ( $myFile, 'w+' );
fwrite ( $fh, $data );
fclose ( $fh );
}*/
function __call($func, $args) {
if (isset ( $args [0] )) {
$value = $this->$func;
//echo $func."=>".$value."==".$args[0];
if ($value != $args [0] || ($args [0] == '' && $value == null)) {
$this->$func = trim($args [0]);
if ($func != $this->primaryKey) {
$this->setProperties [$func] = $args [0];
}
if (isset ( $args [1] )) {
$this->setOption [$func] = $args [1];
}
}
} else {
//echo $func;
}
}
public function GetUpdateDB() {
if ($this->updateDB == null) {
if (GSModel::$db2 == null) {
GSModel::$db2 = $this->load->database ( "db2", TRUE );
}
$this->updateDB = GSModel::$db2;
}
return $this->updateDB;
}
public function GetSelectDB() {
if ($this->selectDB == null) {
if (GSModel::$db1 == null) {
GSModel::$db1 = $this->load->database ( "default", TRUE );
//mysql_query("SET CHARACTER SET utf8");
//mysql_query("SET SESSION collation_connection='utf8_general_ci'");
}
$this->selectDB = GSModel::$db1;
}
return $this->selectDB;
}
public function Test() {
return GSModel::$ccount ++;
}
function GetLastId() {
return $this->GetUpdateDB ()->insert_id ();
}
function GetFromArray($Data) {
//echo "call from ".$this->tableName;
$this->isValidationRule = false;
foreach ( $Data as $key => $value ) {
if (property_exists ( $this, $key )) {
$NewValue = $Data [$key];
if (isset ( $Data ["old_" . $key] )) {
$oldValue = $Data ["old_" . $key];
if ($oldValue != null) {
// echo $oldValue." ".$NewValue;
if ($oldValue == $NewValue)
continue;
}
}
$this->$key ( $Data [$key] );
//$this->SetValidationRule ( $key );
}
}
return true; //$this->ValidData ();
}
function UnSetProperty($property) {
$this->$property = null;
if (isset ( $this->setProperties [$property] )) {
unset ( $this->setProperties [$property] );
}
}
function GetFromPostData() {
//echo "call from ".$this->tableName;
$this->isValidationRule = false;
foreach ( $_POST as $key => $value ) {
if (property_exists ( $this, $key )) {
$NewValue = $this->input->post ( $key, true );
$oldValue = $this->input->post ( "old_" . $key, true );
if ($oldValue != null) {
if ($oldValue == $NewValue) {
$this->$key = $NewValue;
}else {
$this->$key ( $NewValue );
$this->SetValidationRule ( $key );
}
}else {
if ($NewValue !== $oldValue){
$this->$key ( $NewValue );
}else{
$this->$key = $NewValue;
}
$this->SetValidationRule ( $key );
}
}
}
return $this->ValidData ();
}
function CleanForSession() {
$this->selectDB = null;
$this->updateDB = null;
}
function IsPropertySetted() {
return count ( $this->setProperties ) > 0 ? true : false;
}
function GetFromUriData() {
//echo "call from ".$this->tableName;
$uriData = $this->uri->uri_to_assoc ( 3 );
$this->isValidationRule = false;
foreach ( $uriData as $key => $value ) {
//echo $value;
if (property_exists ( $this, $key )) {
$this->$key ( $value );
//$this->SetValidationRule ( $key );
}
}
//GPrint($this);
$this->AddDebugMsg ( "GetFromPostData of " . get_class ( $this ) . " is successed" );
return true;
//return $this->ValidData();
}
private function SetValidationRule($key = '') {
if ($key != '') {
if (isset ( $this->validations [$key] ) && $this->validations [$key] ['Rule'] != "") {
$this->isValidationRule = true;
$this->form_validation->set_rules ( $key, $this->validations [$key] ['Text'], $this->validations [$key] ['Rule'] );
} else {
$this->form_validation->set_rules ( $key, '', '' );
}
} else {
if (count ( $this->setProperties ) > 0) {
foreach ( $this->setProperties as $key ) {
if (isset ( $this->validations [$key] ) && $this->validations [$key] ['Rule'] != "") {
$this->isValidationRule = true;
$this->form_validation->set_rules ( $key, $this->validations [$key] ['Text'], $this->validations [$key] ['Rule'] );
} else {
$this->form_validation->set_rules ( $key, '', '' );
}
}
}
}
}
function SetValueFromPostData($postKey, $proprtyName) {
$new = $this->input->post ( $postKey );
$old = $this->input->post ( "old_" . $postKey );
if ($new != $old) {
$this->$proprtyName ( $new );
}
}
function Save($isLogEntry = true) {
//echo " called from M" . $this->tableName;
if ($this->tableName == null || $this->primaryKey == null) {
$this->AddSystemLog ( $this->msg->GetMsg ( 's002' ) );
return false;
}
if (! $this->isSelected) {
$primaryKey = $this->primaryKey;
if ($this->$primaryKey != null && $this->$primaryKey != '') {
$this->setProperties [$primaryKey] = $this->$primaryKey;
}
if (count ( $this->setProperties ) > 0) {
foreach ( $this->setProperties as $key => $value ) {
if ($key == 'password') {
$this->GetUpdateDB ()->set ( 'password', "MD5('" . $this->$key . "')", FALSE );
} else {
$this->strPrpertyForLog .= "$key=" . $this->$key . ",";
$this->GetUpdateDB ()->set ( $key, $this->$key );
}
}
if ($this->GetUpdateDB ()->insert ( $this->tableName )) {
$this->setProperties = array ();
$this->AddDebugMsg ( "Insert/Save of " . get_class ( $this ) . " is successed" );
if ($isLogEntry)
/*$this->AddLog ($this->strPrpertyForLog, "A" );*/
return true;
} else {
$this->AddDebugMsg ( "Insert/Save of" . get_class ( $this ) . " is Failed", StatusError );
return false;
}
} else {
$this->AddSystemLog ( $this->msg->GetMsg ( 's003' ) ); //"Value is not set for insert" );
$this->AddDebugMsg ( "Insert/Save of " . get_class ( $this ) . " is Failed for setProperties is empty", StatusError );
return false;
}
} else {
return $this->Update ();
}
}
function Select($QueryString = '', $id = '') {
if ($this->tableName == null || $this->primaryKey == null) {
$this->AddSystemLog ( $this->msg->GetMsg ( 's002' ) );
$this->AddDebugMsg ( "Select of " . get_class ( $this ) . " is Failed for table name is not found", StatusError );
return false;
}
if ($QueryString != '')
$this->GetSelectDB ()->Select ( $QueryString );
if ($id != '')
$this->GetSelectDB ()->where ( $this->primaryKey, $id );
else {
foreach ( $this->setProperties as $key => $value ) {
$this->GetSelectDB ()->where ( $key, $this->$key );
}
}
$primaryKey = $this->primaryKey;
$pData = $this->$primaryKey;
if ($pData && $pData != '') {
$this->GetSelectDB ()->where ( $primaryKey, $pData );
//echo "Setted $primaryKey = $pData<br/>";
}
$data = $this->GetSelectDB ()->get ( $this->tableName )->first_row ();
if (!empty($data)) {
foreach ( $data as $key => $value ) {
$this->$key = $value;
}
$this->isSelected = true;
$this->setProperties = array ();
$this->AddDebugMsg ( "Select of " . get_class ( $this ) . " is successed" );
return true;
} else {
$this->AddDebugMsg ( "Select of " . get_class ( $this ) . " return false for no data found", StatusError );
return false;
}
}
function SelectAllWithLike($QueryString = "", $orderBy = '', $order = 'asc', $limit = 0, $limitStart = 0) {
if ($this->tableName == null || $this->primaryKey == null) {
$this->AddSystemLog ( $this->msg->GetMsg ( 's002' ) );
return false;
}
if ($QueryString != "")
$this->GetSelectDB ()->Select ( $QueryString );
foreach ( $this->setProperties as $key => $value ) {
if (isset ( $this->setOption [$key] )) {
$this->GetSelectDB ()->where ( $this->$key, '', $this->setOption [$key] );
} else {
$this->GetSelectDB ()->like ( $key, $this->$key, 'after' );
}
//$this->GetSelectDB ()->where ( $key, $this->$key );
}
if ($orderBy != '' && property_exists ( $this, $orderBy )) {
if ($order == '')
$order = 'asc';
$this->GetSelectDB ()->order_by ( $orderBy, $order );
}
if ($limit > 0) {
$this->GetSelectDB ()->limit ( $limit, $limitStart );
}
$data = $this->GetSelectDB ()->get ( $this->tableName )->result ();
if (count ( $data ) > 0) {
$allData = null;
$i = 0;
foreach ( $data as $tm ) {
$allData [$i ++] = $this->BindObject ( $tm, true );
}
$this->AddDebugMsg ( "SelectAllWithLike of " . get_class ( $this ) . " is successed" );
return $allData;
} else {
$this->AddDebugMsg ( "SelectAllWithLike of " . get_class ( $this ) . " return null for no data found", StatusError );
return null;
}
}
function SelectAllWithDate($QueryString = "", $orderBy = '', $order = 'asc', $limit = 0, $limitStart = 0, $sDate = "", $eDate = "", $dateName = "") {
if ($this->tableName == null || $this->primaryKey == null) {
$this->AddSystemLog ( $this->msg->GetMsg ( 's002' ) );
return false;
}
if ($QueryString != "")
$this->GetSelectDB ()->Select ( $QueryString );
foreach ( $this->setProperties as $key => $value ) {
if (isset ( $this->setOption [$key] )) {
$this->GetSelectDB ()->where ( $this->$key, '', $this->setOption [$key] );
} else {
$this->GetSelectDB ()->like ( $key, $this->$key, 'after' );
}
//$this->GetSelectDB ()->where ( $key, $this->$key );
}
if ($sDate != "" && $eDate != "" && $dateName != "") {
$whereDate = $dateName . " BETWEEN '" . $sDate . "' AND '" . $eDate . "'";
$this->GetSelectDB ()->where ( $whereDate );
}
if ($orderBy != '' && property_exists ( $this, $orderBy )) {
if ($order == '')
$order = 'asc';
$this->GetSelectDB ()->order_by ( $orderBy, $order );
}
if ($limit > 0) {
$this->GetSelectDB ()->limit ( $limit, $limitStart );
}
$data = $this->GetSelectDB ()->get ( $this->tableName )->result ();
if (count ( $data ) > 0) {
$allData = null;
$i = 0;
foreach ( $data as $tm ) {
$allData [$i ++] = $this->BindObject ( $tm, true );
}
$this->AddDebugMsg ( "SelectAllWithDate of " . get_class ( $this ) . " is successed" );
return $allData;
} else {
$this->AddDebugMsg ( "SelectAllWithDate of " . get_class ( $this ) . " return null for no data found", StatusError );
return null;
}
}
function SelectAll($QueryString = "", $orderBy = '', $order = 'asc', $limit = 0, $limitStart = 0) {
if ($this->tableName == null) {
$this->AddSystemLog ( $this->msg->GetMsg ( 's002' ) );
return false;
}
if ($QueryString != "")
$this->GetSelectDB ()->Select ( $QueryString );
foreach ( $this->setProperties as $key => $value ) {
if (isset ( $this->setOption [$key] )) {
$this->GetSelectDB ()->where ( $this->$key, '', $this->setOption [$key] );
} else {
$this->GetSelectDB ()->where ( $key, $this->$key );
}
}
$pkey=$this->primaryKey;
if(isset($this->setOption[$pkey])){
$this->GetSelectDB ()->where ( $this->$pkey, '', $this->setOption [$pkey] );
}
if ($orderBy != '' && property_exists ( $this, $orderBy )) {
if ($order == '')
$order = 'asc';
$this->GetSelectDB ()->order_by ( $orderBy, $order );
}
if ($limit > 0) {
$this->GetSelectDB ()->limit ( $limit, $limitStart );
}
$data = $this->GetSelectDB ()->get ( $this->tableName )->result ();
if (count ( $data ) > 0) {
$allData = null;
$i = 0;
foreach ( $data as $tm ) {
$allData [$i ++] = $this->BindObject ( $tm, true );
}
$this->AddDebugMsg ( "SelectAll of " . get_class ( $this ) . " is found data" );
return $allData;
} else {
$this->AddDebugMsg ( "SelectAll of " . get_class ( $this ) . " return null for no data found", StatusError );
return null;
}
}
function SelectAllForCSV($QueryString = "", $orderBy = '', $order = 'asc', $limit = 0, $limitStart = 0) {
if ($this->tableName == null || $this->primaryKey == null) {
$this->AddSystemLog ( $this->msg->GetMsg ( 's002' ) );
return false;
}
if ($QueryString != "")
$this->GetSelectDB ()->Select ( $QueryString );
foreach ( $this->setProperties as $key => $value ) {
$this->GetSelectDB ()->like ( $key, $this->$key, 'after' );
}
if ($orderBy != '' && property_exists ( $this, $orderBy )) {
if ($order == '')
$order = 'asc';
$this->GetSelectDB ()->order_by ( $orderBy, $order );
}
if ($limit > 0) {
$this->GetSelectDB ()->limit ( $limit, $limitStart );
}
$data = $this->GetSelectDB ()->get ( $this->tableName );
//if (count ( $data ) > 0) {
return $data;
//} else {
// $this->AddDebugMsg ( "SelectAll for CSV of " . get_class ( $this ) . " return null for no data found", StatusError );
// return null;
// }
}
function Update($isForce=false) {
if(!$isForce){
$this->UnsetUpdateRetrictedField();
}
$this->strPrpertyForLog = "";
if ($this->tableName == null || $this->primaryKey == null) {
$this->AddSystemLog ( $this->msg->GetMsg ( 's002' ) );
return false;
}
// echo count($this->setProperties);
if (count ( $this->setProperties ) == 0) {
$this->AddError ( $this->msg->GetMsg ( 's003' ) ); //"No change for update.");
return false;
}
$primaryKey = $this->primaryKey;
foreach ( $this->setProperties as $key => $value ) {
if (isset ( $this->setOption [$key] )) {
$this->GetUpdateDB ()->Set ( $key, $this->$key, $this->setOption [$key] );
} else {
if ($key == 'password') {
$this->GetUpdateDB ()->set ( 'password', "MD5('" . $this->$key . "')", FALSE );
} else {
$this->GetUpdateDB ()->set ( $key, $this->$key );
}
}
$this->strPrpertyForLog .= "$key=" . $this->$key . ",";
//$this->GetUpdateDB ()->Set ( $key, $this->$key );
}
$isPKeySet = false;
foreach ($this->updateWhereExtraField as $key=>$value){
$isWhereSet=true;
$this->GetUpdateDB ()->where ( $key, $value);
if ($key == $primaryKey){
$isPKeySet = true;
}
}
if (!$isPKeySet) {
$this->GetUpdateDB ()->where ( $primaryKey, $this->$primaryKey );
}
$this->GetUpdateDB ()->limit(1);
if ($this->GetUpdateDB ()->Update ( $this->tableName )) {
if($this->GetUpdateDB()->affected_rows()>0){
$this->setProperties = array ();
$this->AddDebugMsg ( "update of " . get_class ( $this ) . " is successed" );
return true;
}else{
return false;
}
/*$this->AddLog ($this->strPrpertyForLog, "U" );*/
} else {
$this->AddDebugMsg ( "update of " . get_class ( $this ) . " is failed", StatusError );
return false;
}
}
protected function BindObject($obj, $isNew = false) {
if ($this->tableName == null ) {
$this->AddSystemLog ( $this->msg->GetMsg ( 's002' ) );
return false;
}
if ($isNew) {
$className = "M" . $this->tableName;
$newobj = new $className ();
foreach ( $obj as $key => $value ) {
$newobj->$key = $value;
}
return $newobj;
} else {
foreach ( $obj as $key => $value ) {
$this->$key = $value;
}
}
}
function Delete($stopLimit=false) {
$primaryKey = $this->primaryKey;
$isPKEYSet=false;
if (! empty ( $this->primaryKey )) {
$primaryKey = $this->primaryKey;
$pData = $this->$primaryKey;
if ($pData && $pData != '') {
$isPKEYSet=true;
$this->GetUpdateDB ()->where ( $primaryKey, $pData );
}
}
if(count($this->setProperties)>0 && !$isPKEYSet){
return false;
}
$this->strPrpertyForLog = "";
foreach ( $this->setProperties as $key => $value ) {
$this->strPrpertyForLog .= "$key=" . $this->$key . ",";
$this->GetUpdateDB ()->where ( $key, $this->$key );
}
if(!$stopLimit){$this->GetUpdateDB ()->limit(1);}
$data = ($this->GetUpdateDB ()->delete ( $this->tableName ));
if ($data) {
$this->isSelected = false;
$this->setProperties = array ();
$this->AddDebugMsg ( "Delete of" . get_class ( $this ) . " is successed" );
/*$this->AddLog ($this->strPrpertyForLog, "D" );*/
return true;
}
$this->AddDebugMsg ( "Delete of " . get_class ( $this ) . " is failed", StatusError );
return false;
}
private function ValidData($isURI = false) {
if (count ( $this->validations ) > 0 && $this->isValidationRule) {
if ($this->form_validation->run () == FALSE) {
$this->AddError ( validation_errors (),false,true);
if (! $isURI)
$data = $_POST;
else {
$data = $this->uri->uri_to_assoc ( 3 );
}
if (count ( $data ) > 0) {
foreach ( $data as $key => $value ) {
if (isset ( $this->setProperties [$key] )) {
if (property_exists ( $this, $key )) {
$this->$key = null;
unset ( $this->setProperties [$key] );
}
}
}
$this->AddDebugMsg ( "ValidData of " . get_class ( $this ) . " is failed", StatusError );
return false;
} else {
$this->AddDebugMsg ( "ValidData of " . get_class ( $this ) . " is failed", StatusError );
return false;
}
} else {
$this->AddDebugMsg ( "ValidData of " . get_class ( $this ) . " is successed" );
return true;
}
} else {
return true;
}
}
function CountAll($isLike = false, $sDate = "", $eDate = "", $dateName = "") {
if ($this->tableName == null || $this->primaryKey == null) {
$this->AddSystemLog ( $this->msg->GetMsg ( 's002' ) );
return 0;
}
foreach ( $this->setProperties as $key => $value ) {
if ($isLike) {
$this->GetSelectDB ()->like ( $key, $this->$key, 'after' );
} else {
$this->GetSelectDB ()->where ( $key, $this->$key );
}
}
if ($sDate != "" && $eDate != "" && $dateName != "") {
$whereDate = $dateName . " BETWEEN '" . $sDate . "' AND '" . $eDate . "'";
$this->GetSelectDB ()->where ( $whereDate );
}
return $this->GetSelectDB ()->count_all_results ( $this->tableName );
}
}