File: /home/barbeatleanalyti/www/baris.beatleanalytics.com/baris/application/models/Crud_model.php
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Crud_model extends CI_Model
{
/*
* Date : 19 July, 2020
*/
function __construct()
{
parent::__construct();
}
function clear_cache()
{
$this->output->set_header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
$this->output->set_header('Pragma: no-cache');
}
/////////GET NAME BY TABLE NAME AND ID/////////////
function get_type_name_by_id($type, $type_id = '', $field = 'name')
{
if ($type_id != '') {
$l = $this->db->get_where($type, array(
$type . '_id' => $type_id
));
$n = $l->num_rows();
if ($n > 0) {
return $l->row()->$field;
}
}
}
/////////GET ID BY TABLE NAME AND NAME/////////////
function get_type_id_by_name($type, $fieldIn, $fieldOut, $data)
{
if ($fieldIn != '') {
return $this->db->get_where($type, array($fieldIn.' like' => '%'.$data.'%'))->row()->$fieldOut;
}
}
function get_settings_value($type, $type_name = '', $field = 'value')
{
if ($type_name != '') {
return $this->db->get_where($type, array('type' => $type_name))->row()->$field;
}
}
function fetchReportData($orgID,$questionId,$subquestionId,$station,$auditor,$FromDate,$ToDate)
{
$SQL = "SELECT tokenid FROM
baris_survey WHERE is_submit = 'Y' ";
$SQL .= "
AND created_date BETWEEN '".date("Y-m-d",strtotime($FromDate))." 00:00:00' AND '".date("Y-m-d",strtotime($ToDate))." 23:59:59' ";
$SQL .= " AND OrgID = ".$orgID." ";
if($station != '0')
$SQL .= " db_surveyStationId = ".$station." ";
if($auditor != '0')
$SQL .= " AND db_surveyUserid = ".$auditor." " ;
if($questionId != '0')
$SQL .= " AND db_surveyQuestionId = ".$questionId ;
if($subquestionId != '0')
$SQL .= " AND db_surveySubQuestionId = ".$subquestionId ;
$SQL .= " GROUP BY tokenid ORDER BY created_date DESC";
$query = $this->db->query($SQL);
$result = $query->result();
$surveyArr = array();
for($i=0; $i<count($result); $i++){
$SQL = "SELECT
baris_survey.db_surveyStationId,
baris_survey.db_surveyUserid,
baris_survey.db_surveyQuestionId,
baris_survey.db_surveySubQuestionId,
baris_survey.db_coachNo,
baris_survey.db_coachName,
baris_survey.db_surveyPageId,
baris_survey.db_surveyValue,
baris_survey.OrgID,
baris_survey.DivisionId,
baris_survey.created_date,
baris_survey.tokenid,
baris_survey.db_trainno,
baris_survey.db_depart_time,
baris_survey.db_totalCoachCount,
baris_userlogin.db_username,
baris_userlogin.userId,
baris_organization.db_Orgname,
baris_station.stationName,
baris_division.DivisionName
FROM
baris_survey
INNER JOIN baris_userlogin ON baris_survey.db_surveyUserid = baris_userlogin.userId
INNER JOIN baris_organization ON baris_survey.OrgID = baris_organization.OrgID
INNER JOIN baris_station ON baris_survey.db_surveyStationId = baris_station.stationId
INNER JOIN baris_division ON baris_survey.DivisionId = baris_division.DivisionId
WHERE baris_survey.is_submit = 'Y'
AND LOWER(baris_userlogin.db_usertype) = 'auditor' ";
$SQL .= " AND baris_survey.OrgID = ".$orgID." ";
if($station != '0')
$SQL .= "
AND baris_survey.db_surveyStationId = ".$station." ";
if($auditor != '0')
$SQL .= "
AND baris_survey.db_surveyUserid = ".$auditor." " ;
if($questionId != '0')
$SQL .= " AND baris_survey.db_surveyQuestionId = ".$questionId ;
if($subquestionId != '0')
$SQL .= " AND baris_survey.db_surveySubQuestionId = ".$subquestionId ;
$SQL .= "
AND baris_survey.db_surveyPageId != '15' " ;
$SQL .= "
AND baris_survey.tokenid = '".$result[$i]->tokenid."' " ;
$SQL .= " GROUP BY baris_survey.db_surveyPageId ORDER BY baris_survey.db_surveyPageId ASC, baris_survey.created_date DESC";
$query = $this->db->query($SQL);
$surveyArr[] = $query->result();
}
return $surveyArr;
}
function select_html($from, $name, $field, $type, $class, $e_match = '', $condition = '', $c_match = '', $onchange = '',$condition_type='single',$default_text='Choose one', $is_val = '')
{
$return = '';
$other = '';
$multi = 'no';
$phrase = 'Choose a ' . $name;
if ($class == 'demo-cs-multiselect') {
$other = 'multiple';
$name = $name . '[]';
if ($type == 'edit') {
$e_match = json_decode($e_match);
if ($e_match == NULL) {
$e_match = array();
}
$multi = 'yes';
}
}
if ($onchange == '0') {
$return = '<select name="' . $name . '" class="' . $class . '" ' . $other . ' data-placeholder="' . $phrase . '" tabindex="2" data-hide-disabled="true" >';
} else {
$return = '<select name="' . $name . '" onChange="' . $onchange . '(this.value,this)" class="' . $class . '" ' . $other . ' data-placeholder="' . $phrase . '" tabindex="2" data-hide-disabled="true" >';
}
if (!is_array($from)) {
if ($condition == '') {
$all = $this->db->get($from)->result_array();
} else if ($condition !== '') {
if($condition_type=='single'){
$all = $this->db->get_where($from, array(
$condition => $c_match
))->result_array();
}else if($condition_type=='multi'){
$this->db->where_in($condition,$c_match);
$all = $this->db->get($from)->result_array();
}
}
$return .= '<option value="'.$is_val.'">'.$default_text.'</option>';
foreach ($all as $row):
if ($type == 'add') {
$return .= '<option value="' . $row[$from . '_id'] . '">' . $row[$field] . '</option>';
} else if ($type == 'edit') {
$return .= '<option value="' . $row[$from . '_id'] . '" ';
if ($multi == 'no') {
if ($row[$from . '_id'] == $e_match) {
$return .= 'selected=."selected"';
}
} else if ($multi == 'yes') {
if (in_array($row[$from . '_id'], $e_match)) {
$return .= 'selected=."selected"';
}
}
$return .= '>' . $row[$field] . '</option>';
}
endforeach;
} else {
$all = $from;
$return .= '<option value="'.$is_val.'">'.$default_text.'</option>';
foreach ($all as $row):
if ($type == 'add') {
$return .= '<option value="' . $row . '">';
if ($condition == '') {
$return .= ucfirst(str_replace('_', ' ', $row));
} else {
$return .= $this->crud_model->get_type_name_by_id($condition, $row, $c_match);
}
$return .= '</option>';
} else if ($type == 'edit') {
$return .= '<option value="' . $row . '" ';
if ($row == $e_match) {
$return .= 'selected=."selected"';
}
$return .= '>';
if ($condition == '') {
$return .= ucfirst(str_replace('_', ' ', $row));
} else {
$return .= $this->crud_model->get_type_name_by_id($condition, $row, $c_match);
}
$return .= '</option>';
}
endforeach;
}
$return .= '</select>';
return $return;
}
//CHECK IF PRODUCT EXISTS IN TABLE
function exists_in_table($table, $field, $val)
{
$ret = '';
$res = $this->db->get($table)->result_array();
foreach ($res as $row) {
if ($row[$field] == $val) {
$ret = $row[$table . '_id'];
}
}
if ($ret == '') {
return false;
} else {
return $ret;
}
}
//DAYS START-END TIMESTAMP
function date_timestamp($date, $type)
{
$date = explode('-', $date);
$d = $date[2];
$m = $date[1];
$y = $date[0];
if ($type == 'start') {
return mktime(0, 0, 0, $m, $d, $y);
}
if ($type == 'end') {
return mktime(0, 0, 0, $m, $d + 1, $y);
}
}
//GETTING LIMITING CHARECTER
function limit_chars($string, $char_limit)
{
$length = 0;
$return = array();
$words = explode(" ", $string);
foreach ($words as $row) {
$length += strlen($row);
$length += 1;
if ($length < $char_limit) {
array_push($return, $row);
} else {
array_push($return, '...');
break;
}
}
return implode(" ", $return);
}
function get_search_place($search_keyword='',$search_category='',$search_location='',$search_tag='',$search_destination='',$short_order='ASC',$latlongs)
{
//echo $search_category;
//echo $latlongs;
//echo 'short_order' .$short_order;
//echo 'latlongs' .$latlongs;
//die;
//echo "vishal";die;
if($short_order=='nearby' || $latlongs!=''){
$latlong = explode(',', $latlongs);
$sql="SELECT pl.place_id,pl.name as placename,pl.tagline as placetagline,pl.description placedescription,pl.logo as placelogo,pl.cover_image as placecoverimage,pl.gallery as placegallery,pl.video_link as placevideo,pl.email as placeemail,pl.phone_number as placephoneno,pl.destination as placedestination,pl.category as placecategory,pl.first_cat_icon as firstcategoryicon,pl.tag as placetag,pl.is_verify_tag,pl.author as placeauthor,pl.add_by as placeaddby,pl.status as placestatus,pl.is_deleted as placeis_deleted,pl.address,pl.created as placecreated,pl.latitude as placelatitude,pl.longitude as placelongitude,ds.destination_id,ds.name as destinationname,ds.tagline as destinationtagline,ds.description as destinationdescription,ds.profile_image as destinationprofileimage,ds.cover_image as destinationcoverimage,ds.category as destinationcategory,cp.category_place_id,cp.category_name,cp.category_icon,tp.tag_place_id,tp.tag_name,tp.tag_icon, ( 3959 * acos( cos( radians(".$latlong[0].") ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(".$latlong[1].") ) + sin(radians(".$latlong[0].") ) * sin( radians( latitude ) ) ) ) AS distance
from place as pl
LEFT JOIN destination as ds ON pl.destination = ds.destination_id
LEFT JOIN category_place as cp ON pl.category = cp.category_place_id
LEFT JOIN tag_place as tp ON pl.tag = tp.tag_place_id where ";
}else{
$sql = "SELECT pl.place_id,pl.name as placename,pl.tagline as placetagline,pl.description placedescription,pl.logo as placelogo,pl.cover_image as placecoverimage,pl.gallery as placegallery,pl.video_link as placevideo,pl.email as placeemail,pl.phone_number as placephoneno,pl.destination as placedestination,pl.category as placecategory,pl.first_cat_icon as firstcategoryicon,pl.tag as placetag,pl.is_verify_tag,pl.author as placeauthor,pl.add_by as placeaddby,pl.status as placestatus,pl.is_deleted as placeis_deleted,pl.address,pl.created as placecreated,pl.latitude as placelatitude,pl.longitude as placelongitude,ds.destination_id,ds.name as destinationname,ds.tagline as destinationtagline,ds.description as destinationdescription,ds.profile_image as destinationprofileimage,ds.cover_image as destinationcoverimage,ds.category as destinationcategory,cp.category_place_id,cp.category_name,cp.category_icon,tp.tag_place_id,tp.tag_name,tp.tag_icon
from place as pl
LEFT JOIN destination as ds ON pl.destination = ds.destination_id
LEFT JOIN category_place as cp ON pl.category = cp.category_place_id
LEFT JOIN tag_place as tp ON pl.tag = tp.tag_place_id where ";
}
//$search_keyword = 'parks';
if (!empty($search_keyword)) {
//$search_keyword="'%".$search_keyword."%'";
$sql .= "(pl.name LIKE '%".$search_keyword."%' OR pl.description LIKE '%".$search_keyword."%') OR ";
//get_type_id_by_name
$sql .= "pl.category LIKE '%".$this->crud_model->get_type_id_by_name('category_place', 'category_name', 'category_place_id', 'parks')."%' AND ";
}
//if (!empty($search_category))
//$sql .= "pl.category LIKE '$search_category' AND ";
$search_category = array($search_category);
if (!empty($search_category)){
foreach ($search_category as $cat) {
$query_parts_cat[] = "pl.category like '%$cat%'";
}
$dtype_cat = implode(" OR ",$query_parts_cat);
$sql .= "(".$dtype_cat.") AND ";
}
if (!empty($search_destination))
$sql .= "pl.destination LIKE '$search_destination' AND ";
if (!empty($search_location))
$sql .= "pl.address LIKE '%$search_location%' AND ";
if (!empty($search_tag)){
foreach ($search_tag as $val) {
$query_parts[] = "pl.tag like '%$val%'";
}
$dtype = implode(" OR ",$query_parts);
$sql .= "(".$dtype.") AND ";
}
if($short_order=='latest'){
$short_='DESC';
$sql .= "pl.status='Approve' AND pl.is_deleted='no' ORDER BY pl.place_id ".$short_."";
}else if($short_order=='nearby' || $latlongs!=''){
$sql .= "pl.status='Approve' AND pl.is_deleted='no' HAVING distance < 125 ORDER BY distance";
}else{
$sql .= "pl.status='Approve' AND pl.is_deleted='no' ORDER BY rand()";
}
//echo $sql;
$query = $this->db->query($sql);
return $result = $query->result();
}
function getTagsPlaceFromIDS($ids){
$ids_array = json_decode($ids);
$data="";
$index=1;
//echo "<pre>";
foreach ($ids_array as $value) {
//echo $value;
//$result = $this->db->get_where('category_place',array('category_place_id' => $value));
$query = $this->db->query("SELECT tag_name from tag_place where tag_place_id = ".$value);
$result = $query->result();
$data .= $result[0]->tag_name;
if(count($ids_array) > $index){
$data .= ",";
}
$index++;
}
return $data;
}
/***custom email sender****/
function do_email($from = '', $from_name = '', $to = '', $sub ='', $msg ='')
{
$this->load->library('email');
$this->email->set_newline("\r\n");
$this->email->from($from, $from_name);
$this->email->to($to);
$this->email->subject($sub);
$this->email->message($msg);
if($this->email->send()){
return true;
}else{
//echo $this->email->print_debugger();
return false;
}
//echo $this->email->print_debugger();
}
function reset_password_email_templare($name,$url)
{
return "<!DOCTYPE html><html><head><title></title></head><body><div style='margin:0;padding:0' dir='ltr' bgcolor='#ffffff'><table border='0' cellspacing='0' cellpadding='0' align='center' style='border-collapse:collapse'><tbody><tr><td style='font-family:Helvetica Neue,Helvetica,Lucida Grande,tahoma,verdana,arial,sans-serif;background:#ffffff'><table border='0' width='100%' cellspacing='0' cellpadding='0' style='border-collapse:collapse'><tbody><tr><td height='20' style='line-height:20px' colspan='3'> </td></tr><tr><td height='1' colspan='3' style='line-height:1px'></td></tr><tr><td><table border='0' width='100%' cellspacing='0' cellpadding='0' style='border-collapse:collapse;border:solid 1px white;margin:0 auto 5px auto;padding:3px 0;text-align:center;width:430px'><tbody><tr><td width='15px' style='width:15px'></td><td style='line-height:0px;width:400px;padding:0 0 15px 0'><table border='0' cellspacing='0' cellpadding='0' style='border-collapse:collapse'><tbody><tr><td style='width:100%;text-align:left;height:33px'> <img height='33' width='120' src='' alt='Logo' title='Logo' style='border:0;display:block;'></td></tr></tbody></table></td><td width='15px' style='width:15px'></td></tr><tr><td width='15px' style='width:15px'></td><td style='border-top:solid 1px #dbdbdb'></td><td width='15px' style='width:15px'></td></tr></tbody></table></td></tr><tr><td><table border='0' width='430' cellspacing='0' cellpadding='0' style='border-collapse:collapse;margin:0 auto 0 auto'><tbody><tr><td><table border='0' width='430px' cellspacing='0' cellpadding='0' style='border-collapse:collapse;margin:0 auto 0 auto;width:430px'><tbody><tr><td width='15' style='display:block;width:15px'> </td></tr><tr><td><table border='0' width='100%' cellspacing='0' cellpadding='0' style='border-collapse:collapse'><tbody><tr><td><table border='0' cellspacing='0' cellpadding='0' style='border-collapse:collapse'><tbody><tr><td width='20' style='display:block;width:20px'> </td><td><table border='0' cellspacing='0' cellpadding='0' style='border-collapse:collapse'><tbody><tr><td></td></tr><tr><td><p style='margin:10px 0 10px 0;color:#565a5c;font-size:18px'>Hi ".$name.",</p><p style='margin:10px 0 10px 0;color:#565a5c;font-size:18px'>We got a request to reset your Bubba Billing password.</p></td></tr><tr></tr><tr><td height='10' style='line-height:10px' colspan='1'> </td></tr><tr><td> <a href='".$url."' style='color:#3b5998;text-decoration:none;display:block;width:370px' target='_blank'><table border='0' width='390' cellspacing='0' cellpadding='0' style='border-collapse:collapse'><tbody><tr><td style='border-collapse:collapse;border-radius:3px;text-align:center;display:block;border:solid 1px #009fdf;padding:10px 16px 14px 16px;margin:0 2px 0 auto;min-width:80px;background-color:#47a2ea'> <a href='".$url."' style='color:#3b5998;text-decoration:none;display:block' target='_blank'><center><font size='3'><span style='font-family:Helvetica Neue,Helvetica,Roboto,Arial,sans-serif;white-space:nowrap;font-weight:bold;vertical-align:middle;color:#fdfdfd;font-size:16px;line-height:16px'>Reset Password</span></font></center></a></td></tr></tbody></table></a></td></tr><tr><td height='10' style='line-height:10px' colspan='1'> </td></tr><tr><td><p style='margin:10px 0 10px 0;color:#565a5c;font-size:18px'>If you ignore this message, your password will not be changed.</p></td></tr></tbody></table></td><td width='20' style='display:block;width:20px'> </td></tr></tbody></table></td></tr></tbody></table></td></tr></tbody></table></td></tr></tbody></table></td></tr><tr><td><table border='0' width='430px' cellspacing='0' cellpadding='0' style='border-collapse:collapse;margin:0 auto 0 auto;width:430px'><tbody><tr><td height='5' style='line-height:5px' colspan='3'> </td></tr><tr><td width='20' style='display:block;width:20px'> </td><td><div style='padding-top:10px'><div style='color:#abadae;font-size:11px;margin:0 auto 5px auto'>From<br></div><div style='color:#000000;font-size:13px;margin:0 auto 5px auto;font-weight: 700;'>Bubba Billing<br></div> <br></div><div style='color:#abadae;font-size:11px;margin:0 auto 5px auto'>© 2020 Bubba Billing, Bubba Billing - 5968 Atlanta Hwy. Flowery Branch, Ga. 30542<br></div></td><td width='20' style='display:block;width:20px'> </td></tr></tbody></table></td></tr><tr><td height='20' style='line-height:20px' colspan='3'> </td></tr></tbody></table></td></tr></tbody></table></div></body></html>";
}
}