Revised database drivers, query builder, and file/inflector helpers for improved consistency and functionality. Updated copyrights, links, and standardized function names including deprecating `is_countable` in favor of `word_is_countable`. Added support for new SQL features and optimized query handling.
404 lines
9.4 KiB
PHP
404 lines
9.4 KiB
PHP
<?php if ( ! defined( 'BASEPATH' ) ) exit( 'No direct script access allowed' );
|
|
|
|
/**
|
|
* Description of MY_Model
|
|
* Algemene database functies
|
|
*
|
|
* @author Theo Weijmer
|
|
* @date 03-06-2012π§
|
|
*
|
|
* my_model MeQuaDos versie:1.0
|
|
*
|
|
*/
|
|
class MY_Model extends CI_Model {
|
|
|
|
protected $sql;
|
|
protected $filepictures;
|
|
|
|
function __construct()
|
|
{
|
|
parent::__construct();
|
|
|
|
$this->filepictures = array("", "images/word.png", "images/excel.png", "images/pdf.png", "images/other.png", "images/text.png");
|
|
}
|
|
|
|
function get($where = FALSE, $fieldname = FALSE, $row = FALSE)
|
|
{
|
|
if( $fieldname ) $row = TRUE;
|
|
|
|
if ( ! is_array( $where ) && $where )
|
|
{
|
|
$this->db->where( array('T1.id' => $where) );
|
|
$row = TRUE;
|
|
}
|
|
elseif ( $where )
|
|
{
|
|
$this->db->where( $where );
|
|
}
|
|
|
|
|
|
if ( $fieldname ) $this->db->select( $fieldname );
|
|
|
|
$query = $this->db->get( $this->tablename . ' AS T1' );
|
|
|
|
if ( $query->num_rows() == 0 ) return FALSE;
|
|
|
|
if ( $row )
|
|
{
|
|
$row = $query->row();
|
|
|
|
if ( $fieldname )
|
|
{
|
|
return $row->$fieldname;
|
|
}
|
|
else
|
|
{
|
|
return $row;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return $query->result();
|
|
}
|
|
}
|
|
|
|
function get_record_by_id($id, $fieldname = FALSE, $row = TRUE)
|
|
{
|
|
$query = $this->db->get_where( $this->tablename, array('id' => $id) );
|
|
if ( $query->num_rows() == 0 )
|
|
{
|
|
return FALSE;
|
|
}
|
|
elseif ( $row )
|
|
{
|
|
$row = $query->row();
|
|
if ( $fieldname )
|
|
{
|
|
return $row->$fieldname;
|
|
}
|
|
else
|
|
{
|
|
return $row;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return $query->result();
|
|
}
|
|
}
|
|
|
|
function get_fieldvalue($where, $fieldname)
|
|
{
|
|
$query = $this->db->get_where( $this->tablename, $where );
|
|
if ( $query->num_rows() == 0 )
|
|
{
|
|
return FALSE;
|
|
}
|
|
else
|
|
{
|
|
$row = $query->row();
|
|
|
|
return $row->$fieldname;
|
|
}
|
|
}
|
|
|
|
function get_records_where($where = FALSE, $row = FALSE, $order = FALSE, $sort = 'asc')
|
|
{
|
|
if ( $order ) $this->db->order_by( $order, $sort );
|
|
|
|
if ( $where ) $this->db->where( $where );
|
|
|
|
$query = $this->db->get( $this->tablename );
|
|
|
|
if ( $query->num_rows() == 0 )
|
|
{
|
|
return FALSE;
|
|
}
|
|
else
|
|
{
|
|
if ( $row )
|
|
{
|
|
return $query->row();
|
|
}
|
|
else
|
|
{
|
|
return $query->result();
|
|
}
|
|
}
|
|
}
|
|
|
|
function check_for_record($where = FALSE)
|
|
{
|
|
if ( $where ) $this->db->where( $where );
|
|
$query = $this->db->get( $this->tablename, $where );
|
|
if ( $query->num_rows() == 0 )
|
|
{
|
|
return FALSE;
|
|
}
|
|
else
|
|
{
|
|
return TRUE;
|
|
}
|
|
}
|
|
|
|
function get_query_result()
|
|
{
|
|
$query = $this->db->query( $this->sql );
|
|
if ( $query->num_rows() == 0 )
|
|
{
|
|
return FALSE;
|
|
}
|
|
else
|
|
{
|
|
return $query->result();
|
|
}
|
|
}
|
|
|
|
function get_query_row($fieldname = FALSE)
|
|
{
|
|
$query = $this->db->query( $this->sql );
|
|
if ( $query->num_rows() == 0 )
|
|
{
|
|
return FALSE;
|
|
}
|
|
else
|
|
{
|
|
if ( $fieldname )
|
|
{
|
|
$row = $query->row();
|
|
|
|
return $row->$fieldname;
|
|
}
|
|
else
|
|
{
|
|
return $query->row();
|
|
}
|
|
}
|
|
}
|
|
|
|
function check_query_record()
|
|
{
|
|
$query = $this->db->query( $this->sql );
|
|
if ( $query->num_rows() == 0 )
|
|
{
|
|
return FALSE;
|
|
}
|
|
else
|
|
{
|
|
return TRUE;
|
|
}
|
|
}
|
|
|
|
function get_query($where = FALSE, $row = FALSE)
|
|
{
|
|
if ( $where ) $this->db->where( $where );
|
|
|
|
$query = $this->db->get( $this->tablename );
|
|
if ( $query->num_rows() == 0 )
|
|
{
|
|
return FALSE;
|
|
}
|
|
elseif ( $row )
|
|
{
|
|
return $query->row();
|
|
}
|
|
else
|
|
{
|
|
return $query->result();
|
|
}
|
|
}
|
|
|
|
function get_minmax_field($where, $field, $max)
|
|
{
|
|
if ( $max )
|
|
{
|
|
$this->db->select_max( $field, 'minimaxi' );
|
|
}
|
|
else
|
|
{
|
|
$this->db->select_min( $field, 'minimaxi' );
|
|
}
|
|
if ( $where )
|
|
{
|
|
$query = $this->db->get_where( $this->tablename, $where );
|
|
}
|
|
else
|
|
{
|
|
$query = $this->db->get( $this->tablename );
|
|
}
|
|
$row = $query->row();
|
|
if ( is_null( $row->minimaxi ) )
|
|
{
|
|
return FALSE;
|
|
}
|
|
else
|
|
{
|
|
return $row->minimaxi;
|
|
}
|
|
}
|
|
|
|
function get_result_count($where = FALSE)
|
|
{
|
|
if ( $where ) $this->db->where( $where );
|
|
$this->db->from( $this->tablename . ' AS T1' );
|
|
|
|
return $this->db->count_all_results();
|
|
}
|
|
|
|
function get_query_count()
|
|
{
|
|
$query = $this->db->query( $this->sql );
|
|
|
|
return $query->num_rows();
|
|
}
|
|
|
|
function insert_record($data)
|
|
{
|
|
$data['creation_date'] = date( 'Y-m-d G:i:s' );
|
|
$data['created_by'] = $this->username;
|
|
$data['last_update_date'] = date( 'Y-m-d G:i:s' );
|
|
$data['last_updated_by'] = $this->username;
|
|
$this->db->insert( $this->tablename, $data );
|
|
|
|
$newID = $this->db->insert_id();
|
|
|
|
$this->createActivity( $newID, 'INSERT' );
|
|
|
|
return $newID;
|
|
}
|
|
|
|
/**
|
|
* Called from CLI. no activity recorded here
|
|
* @param array $data
|
|
* @return integer The inserted id
|
|
*/
|
|
function insert_record_cli($data)
|
|
{
|
|
$data['creation_date'] = date( 'Y-m-d G:i:s' );
|
|
$data['created_by'] = 'SYNCER';
|
|
$data['last_update_date'] = date( 'Y-m-d G:i:s' );
|
|
$data['last_updated_by'] = 'SYNCER';
|
|
$this->db->insert( $this->tablename, $data );
|
|
|
|
$newID = $this->db->insert_id();
|
|
|
|
return $newID;
|
|
}
|
|
|
|
function update_record($data, $id)
|
|
{
|
|
$this->createActivity( $id, 'UPDATE' );
|
|
|
|
$a = $this->externe;
|
|
|
|
$data['last_update_date'] = date( 'Y-m-d G:i:s' );
|
|
$data['last_updated_by'] = $this->username;
|
|
$this->db->update( $this->tablename, $data, 'id = ' . $id );
|
|
}
|
|
|
|
function update_record_where($data, $where)
|
|
{
|
|
$records = $this->get($where);
|
|
|
|
if( ! $records ) return;
|
|
|
|
foreach( $records as $record)
|
|
{
|
|
$this->update_record($data, $record->id);
|
|
}
|
|
// $data['last_update_date'] = date( 'Y-m-d G:i:s' );
|
|
// $data['last_updated_by'] = $this->username;
|
|
//
|
|
// $this->db->update( $this->tablename, $data, $where );
|
|
}
|
|
|
|
/**
|
|
* Used when script is called from CLI, so no user
|
|
* No logging to activitylog here
|
|
* @param array $data
|
|
* @param integer $id
|
|
*/
|
|
function update_record_cli($data, $id)
|
|
{
|
|
$data['last_update_date'] = date( 'Y-m-d G:i:s' );
|
|
$data['last_updated_by'] = 'SYNCER';
|
|
$this->db->update( $this->tablename, $data, 'id = ' . $id );
|
|
}
|
|
|
|
/**
|
|
* save_record insert or update a record
|
|
*
|
|
* @param array $data cpntains the data that needs to be inserted or udated
|
|
* @param int $id id of the update record, if FALSE the record is being inserted
|
|
*
|
|
* @return int if an insert returns the newly created id of the record, else returns the id of the updated record
|
|
*/
|
|
function save($data, $id)
|
|
{
|
|
if ( ! $id )
|
|
{
|
|
$id = $this->insert_record( $data );
|
|
}
|
|
else
|
|
{
|
|
$this->update_record( $data, $id );
|
|
}
|
|
|
|
return $id;
|
|
}
|
|
|
|
function delete_record($id)
|
|
{
|
|
$this->createActivity( $id, 'DELETE' );
|
|
|
|
$this->db->delete( $this->tablename, array('id' => $id) );
|
|
}
|
|
|
|
function delete_record_where($where)
|
|
{
|
|
$records = $this->get($where);
|
|
|
|
if( ! $records ) return;
|
|
|
|
foreach( $records as $record)
|
|
{
|
|
$this->delete_record($record->id);
|
|
}
|
|
//$this->db->delete( $this->tablename, $where );
|
|
}
|
|
|
|
function sanitise_postinput($post, $notinclude)
|
|
{
|
|
foreach ( $post as $key => $value )
|
|
{
|
|
if ( ! in_array( $key, $notinclude ) ) $data[] = array($key => $value);
|
|
}
|
|
}
|
|
|
|
function createActivity($id, $activityType)
|
|
{
|
|
if( ! isset( $this->logActivity ) || ! $this->logActivity ) return;
|
|
$oldData = $this->get( $id );
|
|
|
|
if ( $oldData ) $oldData = serialize( $oldData );
|
|
|
|
$data = array(
|
|
'table' => $this->tablename,
|
|
'table_id' => $id,
|
|
'username' => $this->username,
|
|
'olddata' => $oldData,
|
|
'activitytype' => $activityType,
|
|
'activitytime' => date( 'Y-m-d H:i:s' ),
|
|
'activityip' => $this->input->ip_address()
|
|
);
|
|
|
|
$this->db->insert( 'activitylogs', $data );
|
|
}
|
|
|
|
function clean_input($post){
|
|
return str_replace('null', '', $this->db->escape_str($this->input->post($post) ?? '', TRUE));
|
|
}
|
|
}
|
|
|
|
|