How to create Custom Helper class and function in codeigniter?
How to create Custom helper class
Create a new file called imagetodata_helper.php inside application/helper folder.
Define function here.
Load that helper in controller or even you can load in view as well.
Now you call their functions.
Controller File: image_controller.php
<?php
class image_controller extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->helper("form");
$this->load->library('form_validation');
$this->load->helper("imagetodata");
}
function index(){
$this->load->view('image_view');
}
function image_to_data() {
$URL = $this->input->post('img_name');
// Validation for image url.
$this->form_validation->set_rules('img_name', '', 'callback_checkimgurl');
if ($this->form_validation->run() == FALSE) {
echo "<script type='text/javascript'>
alert('Please Enter Image URL');
</script>";
$this->load->view('image_view');
} else {
$data['data_image'] = convertToBase64($URL);
$this->load->view('image_view', $data);
}
}
// Check image format, if input image is valid return TRUE else returned FALSE.
function checkimgurl($img) {
if (preg_match_all('!http://.+\.(?:jpe?g|png|gif)!Ui', $img)) {
return true;
} else {
return false;
}
}
}
?>
View File : image_view.php
<html>
<head>
<title>CodeIgniter Create New Helper</title>
<link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>css/style.css">
<link href='http://fonts.googleapis.com/css?family=Source+Sans+Pro|Open+Sans+Condensed:300|Raleway' rel='stylesheet' type='text/css'>
</head>
<body>
<div class="main">
<div id="content">
<h3 id="form_head">CodeIgniter Create New Helper</h3>
<div id="form_input">
<?php
//create form open tag
echo form_open('image_controller/image_to_data');
//create label
echo form_label('Enter image URL');
//create data input field
$data = array(
'name' => 'img_name',
'class' => 'input_box',
'placeholder' => "Please Enter Image URL",
'required' => 'required'
);
echo form_input($data);
?>
</div>
<div id="form_button">
<?php echo form_submit('submit', 'Submit', "class='submit'"); ?>
</div>
<?php
//Form close.
echo form_close(); ?>
</div>
<!-- Result div display -->
<?php if(isset($data_image)) { ?>
<div class="encode_img">
<div class="result_head"><h3>Image</h3></div>
<div class="data">
<img src="<?php echo $data_image; ?>">
</div>
</div>
<div class="real_image">
<div class="result_head"><h3>Image URL in base 64 encode form</h3></div>
<div class="data">
<?php echo $data_image; ?>
</div>
</div>
<?php } ?>
</div>
</body>
</html>
Helper File : imagetodata_helper.php
Helper function convertToBase64 it will take image URL and encode to base64 and return the data.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if (!function_exists('convertToBase64'))
{
function convertToBase64($path)
{
// $path = FCPATH.$path;
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
return $base64;
}
}
CSS File : style.css
body {
font-family: 'Raleway', sans-serif;
}
.main{
width: 1015px;
position: absolute;
top: 10%;
left: 20%;
}
#form_head{
text-align: center;
background-color: #FEFFED;
border-bottom: 1px solid #9A9A9A;
height: 35px;
margin: 0 0 -29px 0;
padding-top: 23px;
padding-bottom: 13px;
border-radius: 8px 8px 0 0;
color: rgb(97, 94, 94);
}
#content {
position: absolute;
width: 900px;
height: 187px;
border: 2px solid gray;
border-radius: 10px;
margin-left: -90px;
}
#form_input{
margin-left: 50px;
margin-top: 65px;
}
label{
margin-right: 6px;
font-weight: bold;
}
#form_button{
margin-left: 700px;
margin-top: -75px;
}
.input_box{
height:40px;
width:480px;
margin-left: 20px;
padding:10px;
border-radius:3px;
background-color: #FEFFED;
font-family: 'Raleway', sans-serif;
}
.result_head{
text-align: center;
background-color: #FEFFED;
border-bottom: 1px solid #9A9A9A;
height: 35px;
margin: 0 0 -29px 0;
padding-top: 8px;
padding-bottom: 22px;
border-radius: 8px 8px 0 0;
color: rgb(97, 94, 94);
}
.data{
margin-top: 50;
}
.encode_img{
position: absolute;
height: 400px;
width: 445px;
margin-left: 370px;
margin-top: 220px;
border: 2px solid gray;
border-radius: 10px;
overflow: scroll;
}
.real_image{
margin-top: 220px;
border: 2px solid gray;
border-radius: 10px;
position: absolute;
height: 400px;
width: 445px;
margin-left: -93px;
border: 2px solid gray;
border-radius: 10px;
overflow: scroll;
}
.submit{
font-size: 16px;
background: linear-gradient(#ffbc00 5%, #ffdd7f 100%);
border: 1px solid #e5a900;
color: #4E4D4B;
font-weight: bold;
cursor: pointer;
width: 140px;
border-radius: 5px;
padding: 10px 0;
outline: none;
margin-top: 20px;
margin-left: 15%;
}
.submit:hover{
background: linear-gradient(#ffdd7f 5%, #ffbc00 100%);
}
How to create custom helper class in codeigniter
In application/helpers folder create a new php file demo_helper.php
NOTE: In helper functions, you can not use the $this keyword to access the CI’s object, so you have to call get_instance() function, this method will give the access to the CI’s object. Using this object you load other helper, libraries ..etc.
<?php
if (! defined('BASEPATH')) exit('No direct script access allowed');
if (! function_exists('demo')) {
function demo()
{
// get main CodeIgniter object
$ci = get_instance();
// Write your logic as per requirement
echo "hello helper";
}
}
How to use custom helper
You can load custom helper in two ways, globally or within the controller or within the controller method.
How to load custom helper globally:
Open your application/config/autoload.php file and search for the helper array and add your custom helper name to the array.
$autoload['helper'] = array('demo');
How to load custom helper within the controller:
common_helper.php
//load custom helper
$this->load->helper('common');
How to use custom helper
you can use the helper function in your controller and views.
// just call the function name
demo();
public function myfun()
{
demo();
}
common_helper.php
<?php
function get_user_image($user_id)
{
$result= array();
$ci=& get_instance();
$ci->load->database();
$ci->db->select('*');
$ci->db->from('user_register');
$ci->db->where('user_id',$user_id);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result[0]->profile_photo;
}
else{
return '';
}
}
function get_username($user_id)
{
$result= array();
$ci=& get_instance();
$ci->load->database();
$ci->db->select('*');
$ci->db->from('user_register');
$ci->db->where('user_id',$user_id);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result[0]->name;
}
else{
return '';
}
}
// How to get dropdown value from another Database.
function get_location($location)
{
$result= array();
$ci=& get_instance();
//$ci->load->database();
$ci->mis_db = $ci->load->database('anotherdb', TRUE);
$ci->mis_db->select('branch_code');
$ci->mis_db->from('branch');
$ci->mis_db->where('id',$location);
$query = $ci->mis_db->get();
$result = $query->result();
if($result)
{
return $result[0]->branch_code;
}
else{
return '';
}
}
function get_account_ledgername($id)
{
$result= array();
$ci=& get_instance();
$ci->load->database();
$ci->db->select('account_ledger_name');
$ci->db->from('ledger_details');
$ci->db->where('id',$id);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result[0]->account_ledger_name;
}
else{
return '';
}
}
function get_total_member($user_id)
{
$result= array();
$ci=& get_instance();
$ci->load->database();
$ci->db->select('*');
$ci->db->from('user_register');
$ci->db->where('user_type',2);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return count($result);
}
else{
return '0';
}
}
function user_postion_exist($user_pid,$pos)
{
$result= array();
$ci=& get_instance();
$ci->load->database();
$ci->db->select('*');
$ci->db->from('auto_pool_user');
$ci->db->where('parent_id',$user_pid);
$ci->db->where('user_position',$pos);
$query = $ci->db->get();
//echo $ci->db->last_query();
//exit;
$result = $query->result();
if($result)
{
return $result;
}
else{
return $result;
}
}
function get_child_nodes($pid)
{
$result= array();
$ci=& get_instance();
$ci->load->database();
$ci->db->select('user_id');
$ci->db->from('auto_pool_user');
$ci->db->where('parent_id',$pid);
$query = $ci->db->get();
//echo $ci->db->last_query();
//exit;
$result = $query->result();
if($result)
{
return $result;
}
else{
return $result;
}
}
function checkAllNodesLevelOne($id)
{
$result= array();
$ci=& get_instance();
$ci->load->database();
$ci->db->select('id');
$ci->db->from('auto_pool_level');
$ci->db->where('user_id',$id);
$ci->db->where('user_level',1);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result;
}
else{
return $result;
}
}
function get_total_deposit($id)
{
$result= array();
$ci=& get_instance();
$ci->load->database();
$ci->db->select('sum(amount) as sum_amount');
$ci->db->from('wallet_money');
$ci->db->where('user_id',$id);
$ci->db->where('type','cr');
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result[0]->sum_amount;
}
else{
return '0';
}
}
function get_total_withdrawal($id)
{
$result= array();
$ci=& get_instance();
$ci->load->database();
$ci->db->select('sum(amount) as sum_amount');
$ci->db->from('wallet_money');
$ci->db->where('user_id',$id);
$ci->db->where('type','dr');
//$ci->db->where('status',1);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result[0]->sum_amount;
}
else{
return '0';
}
}
function get_wallet_trans($id)
{
$result= array();
$ci=& get_instance();
$ci->load->database();
$ci->db->select('*');
$ci->db->from('wallet_details');
$ci->db->where('uid',$id);
$ci->db->order_by('id','DESC');
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result;
}
else{
return $result;
}
}
function total_active_user($id='')
{
$result= array();
$ci=& get_instance();
$ci->load->database();
$ci->db->select('*');
$ci->db->from('user_register');
$ci->db->where('status',1);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return count($result);
}
else{
return '0';
}
}
function active_user($id)
{
$result= array();
$ci=& get_instance();
$ci->load->database();
$ci->db->select('status');
$ci->db->from('user_register');
$ci->db->where('user_id',$id);
$ci->db->where('status',1);
$query = $ci->db->get();
$result = $query->result();
$result = $query->result();
if($result)
{
return $result[0]->status;
}
else{
return '0';
}
}
/* ################# Startup helper ###################### */
function startup_get_user_image($user_id)
{
$result= array();
$ci=& get_instance();
$ci->load->database();
$ci->db->select('*');
$ci->db->from('user_register');
$ci->db->where('user_id',$user_id);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result[0]->profile_photo;
}
else{
return '';
}
}
function startup_get_username($user_id)
{
$result= array();
$ci=& get_instance();
$ci->load->database();
$ci->db->select('*');
$ci->db->from('user_register');
$ci->db->where('user_id',$user_id);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result[0]->name;
}
else{
return '';
}
}
function startup_get_total_member($user_id)
{
$result= array();
$ci=& get_instance();
$ci->load->database();
$ci->db->select('*');
$ci->db->from('user_register');
$ci->db->where('user_type',2);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return count($result);
}
else{
return '0';
}
}
function startup_user_postion_exist($user_pid,$pos)
{
$result= array();
$ci=& get_instance();
$ci->load->database();
$ci->db->select('*');
$ci->db->from('startup_auto_pool_user');
$ci->db->where('parent_id',$user_pid);
$ci->db->where('user_position',$pos);
$query = $ci->db->get();
//echo $ci->db->last_query();
$result = $query->result();
if($result)
{
return $result;
}
else{
return false;
}
}
function startup_get_child_nodes($pid)
{
$result= array();
$ci=& get_instance();
$ci->load->database();
$ci->db->select('user_id');
$ci->db->from('startup_auto_pool_user');
$ci->db->where('parent_id',$pid);
$query = $ci->db->get();
//echo $ci->db->last_query();
//exit;
$result = $query->result();
if($result)
{
return $result;
}
else{
return $result;
}
}
function startup_checkAllNodesLevelOne($id)
{
$result= array();
$ci=& get_instance();
$ci->load->database();
$ci->db->select('id');
$ci->db->from('startup_auto_pool_level');
$ci->db->where('user_id',$id);
$ci->db->where('user_level',1);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result;
}
else{
return $result;
}
}
function startup_get_total_deposit($id)
{
$result= array();
$ci=& get_instance();
$ci->load->database();
$ci->db->select('sum(amount) as sum_amount');
$ci->db->from('wallet_money');
$ci->db->where('user_id',$id);
$ci->db->where('type','cr');
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result[0]->sum_amount;
}
else{
return '0';
}
}
function startup_get_total_withdrawal($id)
{
$result= array();
$ci=& get_instance();
$ci->load->database();
$ci->db->select('sum(amount) as sum_amount');
$ci->db->from('wallet_money');
$ci->db->where('user_id',$id);
$ci->db->where('type','dr');
//$ci->db->where('status',1);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result[0]->sum_amount;
}
else{
return '0';
}
}
function startup_get_wallet_trans($id)
{
$result= array();
$ci=& get_instance();
$ci->load->database();
$ci->db->select('*');
$ci->db->from('wallet_details');
$ci->db->where('uid',$id);
$ci->db->order_by('id','DESC');
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result;
}
else{
return $result;
}
}
function startup_total_active_user($id='')
{
$result= array();
$ci=& get_instance();
$ci->load->database();
$ci->db->select('*');
$ci->db->from('user_register');
$ci->db->where('status',1);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return count($result);
}
else{
return '0';
}
}
function startup_active_user($id)
{
$result= array();
$ci=& get_instance();
$ci->load->database();
$ci->db->select('status');
$ci->db->from('user_register');
$ci->db->where('user_id',$id);
$ci->db->where('status',1);
$query = $ci->db->get();
$result = $query->result();
$result = $query->result();
if($result)
{
return $result[0]->status;
}
else{
return '0';
}
}
/* #################### End Startup helper ################### */
/* ################# Basic helper ###################### */
function basic_get_child_nodes($pid)
{
$result= array();
$ci=& get_instance();
$ci->load->database();
$ci->db->select('user_id');
$ci->db->from('basic_auto_pool_user');
$ci->db->where('parent_id',$pid);
$query = $ci->db->get();
//echo $ci->db->last_query();
//exit;
$result = $query->result();
if($result)
{
return $result;
}
else{
return $result;
}
}
function basic_checkAllNodesLevelOne($id)
{
$result= array();
$ci=& get_instance();
$ci->load->database();
$ci->db->select('id');
$ci->db->from('basic_auto_pool_level');
$ci->db->where('user_id',$id);
$ci->db->where('user_level',1);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result;
}
else{
return $result;
}
}
function basic_user_postion_exist($user_pid,$pos)
{
$result= array();
$ci=& get_instance();
$ci->load->database();
$ci->db->select('*');
$ci->db->from('basic_auto_pool_user');
$ci->db->where('parent_id',$user_pid);
$ci->db->where('user_position',$pos);
$query = $ci->db->get();
//echo $ci->db->last_query();
//exit;
$result = $query->result();
if($result)
{
return $result;
}
else{
return $result;
}
}
function username($userid)
{
$result= array();
$ci=& get_instance();
$ci->load->database();
$ci->db->select('name');
$ci->db->from('user_register');
$ci->db->where('user_id',$userid);
$ci->db->where('status',1);
$query = $ci->db->get();
$result = $query->result();
$result = $query->result();
if($result)
{
return $result[0]->name;
}
else{
return '0';
}
}
/* #################### End Basic helper ################### */
/*
function get_total_count()
{
$result= array();
$ci=& get_instance();
$ci->load->database();
$ci->db->select('*');
$ci->db->from('register_user');
$ci->db->where('User_type',2);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return count($result);
}
else{
return '0';
}
}
function get_total_deposit($id)
{
$result= array();
$ci=& get_instance();
$ci->load->database();
$ci->db->select('sum(amount) as sum_amount');
$ci->db->from('wallet_money');
$ci->db->where('user_id',$id);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result[0]->sum_amount;
}
else{
return '0';
}
}
function get_total_withdrawal($id)
{
$result= array();
$ci=& get_instance();
$ci->load->database();
$ci->db->select('sum(amount) as sum_amount');
$ci->db->from('withdrawl_money');
$ci->db->where('UserID',$id);
$ci->db->where('status',1);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result[0]->sum_amount;
}
else{
return '0';
}
}
function get_total_direct($id)
{
$result= array();
$ci=& get_instance();
$ci->load->database();
$ci->db->select('*');
$ci->db->from('user_register');
$array = array('sponser_code' => $id, 'user_position' => 'direct');
$ci->db->where($array);
$query = $ci->db->get();
$query->result();
$direct_user = $query->num_rows();
return $direct_user;
}
function get_user_levelinfo($id)
{
$result= array();
$ci=& get_instance();
$ci->load->database();
$ci->db->select('*');
$ci->db->from('user_register');
$ci->db->where('UserID',$id);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result[0]->user_level;
}
else{
return '0';
}
}
function get_level_count($level)
{
$result= array();
$ci=& get_instance();
$ci->load->database();
$ci->db->select('*');
$ci->db->from('register_user');
$ci->db->where('level',$level);
$ci->db->where('User_type',2);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return count($result);
}
else{
return '0';
}
}
function get_ref_sponser($sponser_code)
{
$result= array();
$ci=& get_instance();
$ci->load->database();
$ci->db->select('UserID,Name,PhoneNumber,sponser_code, bank_name, branch_name, account_number, ifsc_code');
$ci->db->from('register_user');
$ci->db->where('sponser_code',$sponser_code);
$ci->db->limit(1);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result;
}
else{
return $result;
}
}
function get_sponser_code($user_id)
{
$ci=& get_instance();
$ci->load->database();
$ci->db->select('sponser_code');
$ci->db->from('register_user');
$ci->db->where('UserID',$user_id);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result[0]->sponser_code;
}
else{
return '';
}
}
function get_name_by_id($user_id)
{
$ci=& get_instance();
$ci->load->database();
$ci->db->select('Name');
$ci->db->from('register_user');
$ci->db->where('UserID',$user_id);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result[0]->Name;
}
else{
return '';
}
}
function get_50_receipt_id($user_id)
{
$ci=& get_instance();
$ci->load->database();
$ci->db->select('slip_id_50');
$ci->db->from('register_user');
$ci->db->where('UserID',$user_id);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result[0]->slip_id_50;
}
else{
return '';
}
}
function get_150_receipt_id($user_id)
{
$ci=& get_instance();
$ci->load->database();
$ci->db->select('slip_id_150');
$ci->db->from('register_user');
$ci->db->where('UserID',$user_id);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result[0]->slip_id_150;
}
else{
return '';
}
}
function get_level($user_id)
{
$ci=& get_instance();
$ci->load->database();
$ci->db->select('level');
$ci->db->from('register_user');
$ci->db->where('UserID',$user_id);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result[0]->level;
}
else{
return '';
}
}
function check_sponser_receipt_status($receipt_id)
{
$ci=& get_instance();
$ci->load->database();
$ci->db->select('status');
$ci->db->from('pay_slip');
$ci->db->where('id',$receipt_id);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result[0]->status;
}
else{
return '';
}
}
function get_total_slip_recived($id)
{
$result = array();
$ci=& get_instance();
$ci->load->database();
$ci->db->select('*');
$ci->db->from('pay_slip');
$ci->db->where('pay_to',$id);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result;
}
else{
return $result;
}
}
function get_pending_receipt_to_approvel($id)
{
$result = array();
$ci=& get_instance();
$ci->load->database();
$ci->db->select('*');
$ci->db->from('pay_slip');
$ci->db->where('pay_by',$id);
$ci->db->where('status',0);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result;
}
else{
return $result;
}
}
function update_slip($data,$id)
{
$ci=& get_instance();
$ci->load->database();
$ci-> db -> where('id',$id );
$res = $ci->db->update('pay_slip',$data);
if($res)
{
return 'update';
}
else
{
return '';
}
}
function update_total_slip($data,$id)
{
$ci=& get_instance();
$ci->load->database();
$ci-> db -> where('UserID',$id );
$res = $ci->db->update('register_user',$data);
if($res)
{
return 'update';
}
else
{
return '';
}
}
function update_receiptSend($data,$id)
{
$ci=& get_instance();
$ci->load->database();
$ci-> db -> where('UserID',$id );
$res = $ci->db->update('register_user',$data);
if($res)
{
return 'update';
}
else
{
return '';
}
}
function update_level($data,$user_id)
{
$ci=& get_instance();
$ci->load->database();
$ci-> db -> where('UserID',$user_id );
$res = $ci->db->update('register_user',$data);
if($res)
{
return 'update';
}
else
{
return '';
}
}
function activate_account($data,$user_id)
{
$ci=& get_instance();
$ci->load->database();
$ci-> db -> where('UserID',$user_id );
$res = $ci->db->update('register_user',$data);
if($res)
{
return 'update';
}
else
{
return '';
}
}
function get_user_status($user_id)
{
$result = array();
$ci=& get_instance();
$ci->load->database();
$ci->db->select('*');
$ci->db->from('register_user');
$ci->db->where('UserID',$user_id);
$ci->db->where('acc_active',1);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result;
}
else{
return $result;
}
}
function get_user_detail($user_id)
{
$result = array();
$ci=& get_instance();
$ci->load->database();
$ci->db->select('*');
$ci->db->from('register_user');
$ci->db->where('UserID',$user_id);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result;
}
else{
return $result;
}
}
function get_active_user()
{
$result= array();
$ci=& get_instance();
$ci->load->database();
$ci->db->select('*');
$ci->db->from('register_user');
$ci->db->where('acc_active',1);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result;
}
else{
return $result;
}
}
function get_levelwise_peneding_recipt($level,$recipt)
{
$block_user = array();
if($level==1)
{
$user_list = get_active_user();
if(!empty($user_list)){
foreach($user_list as $userList)
{
if($userList->total_recipt==2 && $userList->receiptSend==0 )
{
array_push($block_user,$userList->UserID);
}
}
}
}
if($level==2)
{
$user_list = get_active_user();
if(!empty($user_list)){
foreach($user_list as $userList)
{
if($userList->total_recipt==5 && $userList->receiptSend < 3 )
{
array_push($block_user,$userList->UserID);
}
}
}
}
if($level==3)
{
$user_list = get_active_user();
if(!empty($user_list)){
foreach($user_list as $userList)
{
if($userList->total_recipt==11 && $userList->receiptSend < 7 )
{
array_push($block_user,$userList->UserID);
}
}
}
}
if($level==4)
{
$user_list = get_active_user();
if(!empty($user_list)){
foreach($user_list as $userList)
{
if($userList->total_recipt==23 && $userList->receiptSend < 15 )
{
array_push($block_user,$userList->UserID);
}
}
}
}
$result= array();
$ci=& get_instance();
$ci->load->database();
$ci->db->select('UserID,Name, sponser_code,PhoneNumber,bank_name, branch_name, account_number, ifsc_code');
$ci->db->from('register_user');
if(!empty($block_user))
{
$ci->db->where_not_in('UserID',$block_user);
}
$ci->db->where('level',$level);
$ci->db->where('acc_active',1);
$ci->db->order_by('rand()');
//$ci->db->where("total_recipt <= $recipt");
$ci->db->limit(1);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result;
}
else{
return $result;
}
}
function get_user_level($userID)
{
$result= array();
$ci=& get_instance();
$ci->load->database();
$ci->db->select('level');
$ci->db->from('register_user');
$ci->db->where('UserID',$userID);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result[0]->level;
}
else{
return '';
}
}
function get_total_slip($userID)
{
$result= array();
$ci=& get_instance();
$ci->load->database();
$ci->db->select('total_recipt');
$ci->db->from('register_user');
$ci->db->where('UserID',$userID);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result[0]->total_recipt;
}
else{
return '';
}
}
function get_total_receiptSend($userID)
{
$result= array();
$ci=& get_instance();
$ci->load->database();
$ci->db->select('receiptSend');
$ci->db->from('register_user');
$ci->db->where('UserID',$userID);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result[0]->receiptSend;
}
else{
return '';
}
}
function get_parent_name($parent_id)
{
$ci=& get_instance();
$ci->load->database();
$ci->db->select('name');
$ci->db->from('menu');
$ci->db->where('id',$parent_id);
$ci->db->limit(1);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result[0]->name;
}
}
function get_parent_id($id)
{
$ci=& get_instance();
$ci->load->database();
$ci->db->select('parent_id');
$ci->db->from('menu');
$ci->db->where('id',$id);
$ci->db->where('status',1);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result[0]->parent_id;
}
}
function get_child_menu($id)
{
$result = array();
$ci=& get_instance();
$ci->load->database();
$ci->db->select('*');
$ci->db->from('menu');
$ci->db->where('parent_id', $id);
$ci->db->where('status',1);
$ci->db->order_by('order','DESC');
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result;
}
}
function get_parent_menu()
{
$ci=& get_instance();
$ci->load->database();
$ci->db->select('*');
$ci->db->from('menu');
$ci->db->where('parent_id', 0);
$ci->db->where('status',1);
$ci->db->order_by('order','asc');
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result;
}
}
function check_is_child($pid)
{
$ci=& get_instance();
$ci->load->database();
$ci->db->select('*');
$ci->db->from('menu');
$ci->db->where('parent_id',$pid);
$ci->db->where('status',1);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return 'yes';
}
else
{
return 'no';
}
}
function get_child($pid)
{
$result = array();
$ci=& get_instance();
$ci->load->database();
$ci->db->select('*');
$ci->db->from('menu');
$ci->db->where('parent_id',$pid);
$ci->db->order_by('order','asc');
$ci->db->where('status',1);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result;
}
}
function get_image($pid,$type)
{
$ci=& get_instance();
$ci->load->database();
$ci->db->select('image');
$ci->db->from('tbl_images');
$ci->db->where('pid',$pid);
$ci->db->where('type_id',$type);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result[0]->image;
}
}
function get_images($pid,$type)
{
$ci=& get_instance();
$ci->load->database();
$ci->db->select('*');
$ci->db->from('tbl_images');
$ci->db->where('pid',$pid);
$ci->db->where('type_id',$type);
$query = $ci->db->get();
$result = $query->result();
return $result;
}
function croped_images($imge_physical_path,$imge_physical_path_crop)
{
$config['image_library'] = 'gd2';
$config['source_image'] = $imge_physical_path;
$config['new_image'] = $imge_physical_path_crop;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 626;
$config['height'] = 464;
$CI =& get_instance();
$CI->load->library('image_lib'); // load library
$CI->image_lib->initialize($config);
if($CI->image_lib->resize()){
return $CI->image_lib->resize();
}
}
function croped_images_detail($imge_physical_path,$imge_physical_path_crop)
{
$config['image_library'] = 'gd2';
$config['source_image'] = $imge_physical_path;
$config['new_image'] = $imge_physical_path_crop;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 724;
$config['height'] = 448;
$CI =& get_instance();
$CI->load->library('image_lib'); // load library
$CI->image_lib->initialize($config);
if($CI->image_lib->resize()){
return $CI->image_lib->resize();
}
}
function get_product()
{
$ci=& get_instance();
$ci->load->database();
$ci->db->select('*');
$ci->db->from('tbl_product');
$ci->db->where('status',1);
$query = $ci->db->get();
//echo $ci->db->last_query();
//exit;
$result = $query->result();
if($result)
{
return $result;
}
}
function get_id_seourl($seourl)
{
$ci=& get_instance();
$ci->load->database();
$ci->db->select('id');
$ci->db->from('tbl_package');
$ci->db->where('seo_url',$seourl);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result[0]->id;
}
}
function get_hotel_id_seourl($seourl)
{
$ci=& get_instance();
$ci->load->database();
$ci->db->select('id');
$ci->db->from('tbl_hotel');
$ci->db->where('seo_url',$seourl);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result[0]->id;
}
}
function get_day_itinaary($package_id)
{
$ci=& get_instance();
$ci->load->database();
$ci->db->select('*');
$ci->db->from('tbl_day_detail');
$ci->db->where('package_id',$package_id);
$ci->db->order_by('id','asc');
$query = $ci->db->get();
$result = $query->result();
return $result;
}
function croped_banner_images($imge_physical_path,$imge_physical_path_crop)
{
$config['image_library'] = 'gd2';
$config['source_image'] = $imge_physical_path;
$config['new_image'] = $imge_physical_path_crop;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 2400;
$config['height'] = 800;
$CI =& get_instance();
$CI->load->library('image_lib'); // load library
$CI->image_lib->initialize($config);
if($CI->image_lib->resize()){
return $CI->image_lib->resize();
}
}
function get_email_setting()
{
$ci=& get_instance();
$ci->load->database();
$ci->db->select('to_email,cc_mail');
$ci->db->from('email_setting');
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result;
}
}
function get_hotel_category()
{
$ci=& get_instance();
$ci->load->database();
$ci->db->select('*');
$ci->db->from('tbl_category');
$ci->db->where('status',1);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result;
}
}
function get_hotel_category2()
{
$ci=& get_instance();
$ci->load->database();
$ci->db->select('*');
$ci->db->from('tbl_hotel_category');
$ci->db->where('status',1);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result;
}
}
function get_package_category()
{
$ci=& get_instance();
$ci->load->database();
$ci->db->select('*');
$ci->db->from('tbl_category');
$ci->db->where('status',1);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result;
}
}
function get_hotel_city()
{
$ci=& get_instance();
$ci->load->database();
$ci->db->select('*');
$ci->db->from('tbl_city');
$ci->db->where('status',1);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result;
}
}
function get_aminities()
{
$ci=& get_instance();
$ci->load->database();
$ci->db->select('*');
$ci->db->from('tbl_aminities');
$ci->db->where('status',1);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result;
}
}
function get_by_id_aminities($id_arr)
{
$ci=& get_instance();
$ci->load->database();
$ci->db->select('*');
$ci->db->from('tbl_aminities');
$ci->db->where('status',1);
$ci->db->where_in('id',$id_arr);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result;
}
}
function get_by_id_activity($id_arr)
{
$ci=& get_instance();
$ci->load->database();
$ci->db->select('*');
$ci->db->from('tbl_activity');
$ci->db->where('status',1);
$ci->db->where_in('id',$id_arr);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result;
}
}
function get_activity()
{
$ci=& get_instance();
$ci->load->database();
$ci->db->select('*');
$ci->db->from('tbl_activity');
$ci->db->where('status',1);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result;
}
}
function get_hotel_image($id)
{
$ci=& get_instance();
$ci->load->database();
$ci->db->select('image');
$ci->db->from('tbl_hotel_image');
$ci->db->where('hotel_id',$id);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result[0]->image;
}
}
function get_pack_images($id)
{
$ci=& get_instance();
$ci->load->database();
$ci->db->select('image,id');
$ci->db->from('tbl_hotel_image');
$ci->db->where('package_id',$id);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result;
}
}
function get_hotel_category_name_id($id)
{
$ci=& get_instance();
$ci->load->database();
$ci->db->select('category_name');
$ci->db->from('tbl_hotel_category');
$ci->db->where('id',$id);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result[0]->category_name;
}
}
function get_hotel_category_id($id)
{
$ci=& get_instance();
$ci->load->database();
$ci->db->select('category_name');
$ci->db->from('tbl_category');
$ci->db->where('id',$id);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result[0]->category_name;
}
}
function get_category_id_by_name($catname)
{
$ci=& get_instance();
$ci->load->database();
$ci->db->select('id');
$ci->db->from('tbl_category');
$ci->db->where('category_name',$catname);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result[0]->id;
}
}
//total city
function get_total_city()
{
$ci=& get_instance();
$ci->load->database();
$ci->db->select('*');
$ci->db->from('tbl_city');
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return count($result);
}
else
{
return 0;
}
}
function get_cityId_byName($name)
{
$ci=& get_instance();
$ci->load->database();
$ci->db->select('id');
$ci->db->from('tbl_city');
$ci->db->where('city_name',$name);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result[0]->id;
}
else
{
return 0;
}
}
//
function get_total_hotel()
{
$ci=& get_instance();
$ci->load->database();
$ci->db->select('*');
$ci->db->from('tbl_hotel');
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return count($result);
}
else
{
return 0;
}
}
function get_total_package()
{
$ci=& get_instance();
$ci->load->database();
$ci->db->select('*');
$ci->db->from('tbl_package');
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return count($result);
}
else
{
return 0;
}
}
function get_recent_package()
{
$ci=& get_instance();
$ci->load->database();
$ci->db->select('seo_url,package_name');
$ci->db->order_by('id','desc');
$ci->db->limit(10);
$ci->db->from('tbl_package');
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result;
}
else
{
return 0;
}
}
function get_package_by_category_id($category_id)
{
$ci=& get_instance();
$ci->load->database();
$ci->db->select('*');
$ci->db->from('tbl_package');
$ci->db->where('package_category',$category_id);
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result;
}
else
{
return 0;
}
}
function get_popluar_package()
{
$ci=& get_instance();
$ci->load->database();
$ci->db->select('seo_url,package_name');
$ci->db->order_by('id','desc');
$ci->db->limit(3);
$ci->db->from('tbl_package');
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result;
}
else
{
return 0;
}
}
function get_info($id)
{
$ci=& get_instance();
$ci->load->database();
$ci->db->select('Email,Address,PhoneNumber');
$ci->db->from('register_user');
$query = $ci->db->get();
$result = $query->result();
if($result)
{
return $result;
}
}
function seoUrl($string)
{
//Lower case everything
$string = strtolower($string);
//Make alphanumeric (removes all other characters)
$string = preg_replace("/[^a-z0-9_\s-]/", "", $string);
//Clean up multiple dashes or whitespaces
$string = preg_replace("/[\s-]+/", " ", $string);
//Convert whitespaces and underscore to dash
$string = preg_replace("/[\s_]/", "-", $string);
return $string;
}
*/
?>
Security helper function
File Name : security_helper.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
if (! function_exists('add_hyphen')){
function add_hyphen($string){
$string = preg_replace('/[^A-Za-z0-9\-\(\) ]/', ' ', $string);
$string = trim(str_replace(' ', '-', $string));
$string=preg_replace('/-{2,}/','-', $string);
$string=trim($string,'-');
return strtolower($string);
}
}
//add a underscore in a string
if ( ! function_exists('add_underscore')){
function add_underscore($string){
$string = preg_replace('/[^A-Za-z0-9\-\(\) ]/', ' ', $string);
$string = trim(str_replace(' ', '_', $string));
$string=preg_replace('/-{2,}/','-', $string);
$string=trim($string,'_');
return strtolower($string);
}
}
if ( ! function_exists('xss_clean')){
function xss_clean($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
$data = htmlentities($data);
$data = trim($data);
$data = stripslashes($data);
$data = str_replace('\'','',$data);
//$data = str_replace("'", "''", $data);
return $data;
}
}
if ( ! function_exists('remove_hyphen')){
//to remove underscore(-) from string
function remove_hyphen($string) {
$string = preg_replace('/[^A-Za-z0-9\-\(\) ]/', ' ', $string);
$string = trim(str_replace('-', ' ', $string));
$string = preg_replace('/-{2,}/',' ', $string);
return trim($string);
}
}
if ( ! function_exists('real_output')){
//to get real string output
function real_output($string) {
$string = strip_tags($string);
$string = html_entity_decode($string);
$string = stripslashes($string);
$string = trim(str_replace("&rsquo:","'", $string));
$string = str_replace("‘","",$string);
$string = str_replace("\'","'",$string);
//$string = trim(str_replace("\r\n","", $string));
//$string = strtolower($string);
return $string;
}
}
if ( ! function_exists('current_server_date')){
//to get real string output
function current_server_date($dataformate) {
$current_date_time =date("Y-m-d h:i:s", strtotime('+3 hour +44 minutes'));
//$current_date_hai_ye = date($dataformate,$current_date_time);
return $current_date_time;
}
}
if (! function_exists('indian_format')){
function indian_format($number){
setlocale(LC_MONETARY, 'en_IN');
$number=money_format('%!i', $number);
return $number;
}
}
if (! function_exists('encryptor')){
function encryptor($action,$string) {
$output = false;
$encrypt_method = "AES-256-CBC";
$secret_key = 'sana07';
$secret_iv = 'sana07';
$key = hash('sha256', $secret_key);
$iv = substr(hash('sha256', $secret_iv), 0, 16);
if( $action == 'encrypt' ) {
$output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
$output = base64_encode($output);
}
else if( $action == 'decrypt' ){
$output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
}
return $output;
}
}
if (! function_exists('file_uploading')){
function file_uploading($upload_path,$files) {
$CI = get_instance();
$CI->load->library('upload');
$date = date('Y-m-d');
$upload_path_directory = $upload_path;
if (!is_dir($upload_path_directory)) {
$oldmask = umask(0);
mkdir($upload_path_directory,0777,TRUE);
umask($oldmask);
}
if(is_array($files['name'])){
$file_type = 1;
}else{
$file_type = 2;
}
if($file_type==2){
$info = pathinfo($files['name']);
$uploadfile_names = $info['filename'];
$uploadfile_name = str_replace(' ', '_', $uploadfile_names);
$extension = $info['extension'];
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$random_strng = substr(str_shuffle($characters), 0, 4);
$filename = $uploadfile_name.'_'.$random_strng.'_'.$date.'.' . $extension;
$_FILES['file']['name'] = $filename;
$_FILES['file']['type'] = $files['type'];
$_FILES['file']['tmp_name'] = $files['tmp_name'];
$_FILES['file']['size'] = $files['size'];
$config['upload_path'] = $upload_path;
$config['allowed_types'] = 'jpg|jpeg|png|bmp|pdf';
$config['file_name'] = $filename;
$config['max_size'] = '5000';
$CI->load->library('upload', $config);
$CI->upload->initialize($config);
if(!$CI->upload->do_upload('file',$filename)){
return false;
}else{
return $allfiles = $filename;
}
}
else if($file_type==1){
$filesCount = count($files['name']);
for($i = 0; $i < $filesCount; $i++){
$info = pathinfo($files['name'][$i]);
$uploadfile_names = $info['filename'];
$uploadfile_name = str_replace(' ', '_', $uploadfile_names);
$extension = $info['extension'];
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$random_strng = substr(str_shuffle($characters), 0, 4);
$filename = $uploadfile_name .'_'.$random_strng.'_'.$date.'.' . $extension;
$_FILES['file']['name'] = $filename;
$_FILES['file']['type'] = $files['type'][$i];
$_FILES['file']['tmp_name'] = $files['tmp_name'][$i];
$_FILES['file']['size'] = $files['size'][$i];
$config['upload_path'] = $upload_path_directory;
$config['allowed_types'] = 'jpg|jpeg|png|bmp|pdf';
$config['file_name'] = $filename;
$config['max_size'] = '5000';
$CI->load->library('upload', $config);
$CI->upload->initialize($config);
if(!$CI->upload->do_upload('file',$filename)){
return false;
}else{
$allfiles[] = $filename;
}
}
$total_upload_file = count($allfiles);
if($total_upload_file>1){
return $allfiles;
}else{
$files = $allfiles;
return $files;
}
}
}
}
?>
Common Helper
File Name : Common_helper.php
<?php if(!defined('BASEPATH')) exit('No direct script access allowed');
/**
* This function is used to print the content of any data
*/
function pre($data)
{
echo "<pre>";
print_r($data);
echo "</pre>";
}
function p($data)
{
echo "<pre>";
print_r($data);
echo "</pre>";
}
function dd($data)
{
echo "<pre>";
print_r($data);
echo "</pre>"; exit;
}
/**
* This function used to get the CI instance
*/
if(!function_exists('get_instance'))
{
function get_instance()
{
$CI = &get_instance();
}
}
/**
* This function used to generate the hashed password
* @param {string} $plainPassword : This is plain text password
*/
if(!function_exists('getHashedPassword'))
{
function getHashedPassword($plainPassword)
{
return password_hash($plainPassword, PASSWORD_DEFAULT);
}
}
/**
* This function used to generate the hashed password
* @param {string} $plainPassword : This is plain text password
* @param {string} $hashedPassword : This is hashed password
*/
if(!function_exists('verifyHashedPassword'))
{
function verifyHashedPassword($plainPassword, $hashedPassword)
{
return password_verify($plainPassword, $hashedPassword) ? true : false;
}
}
/**
* This method used to get current browser agent
*/
if(!function_exists('getBrowserAgent'))
{
function getBrowserAgent()
{
$CI = get_instance();
$CI->load->library('user_agent');
$agent = '';
if ($CI->agent->is_browser())
{
$agent = $CI->agent->browser().' '.$CI->agent->version();
}
else if ($CI->agent->is_robot())
{
$agent = $CI->agent->robot();
}
else if ($CI->agent->is_mobile())
{
$agent = $CI->agent->mobile();
}
else
{
$agent = 'Unidentified User Agent';
}
return $agent;
}
}
if(!function_exists('setProtocol'))
{
function setProtocol()
{
$CI = &get_instance();
$CI->load->library('email');
$config['protocol'] = PROTOCOL;
$config['mailpath'] = MAIL_PATH;
$config['smtp_host'] = SMTP_HOST;
$config['smtp_port'] = SMTP_PORT;
$config['smtp_user'] = SMTP_USER;
$config['smtp_pass'] = SMTP_PASS;
$config['charset'] = "utf-8";
$config['mailtype'] = "html";
$config['newline'] = "\r\n";
$CI->email->initialize($config);
return $CI;
}
}
if(!function_exists('emailConfig'))
{
function emailConfig()
{
$CI->load->library('email');
$config['protocol'] = PROTOCOL;
$config['smtp_host'] = SMTP_HOST;
$config['smtp_port'] = SMTP_PORT;
$config['mailpath'] = MAIL_PATH;
$config['charset'] = 'UTF-8';
$config['mailtype'] = "html";
$config['newline'] = "\r\n";
$config['wordwrap'] = TRUE;
}
}
if(!function_exists('resetPasswordEmail'))
{
function resetPasswordEmail($detail)
{
$data["data"] = $detail;
// pre($detail);
// die;
$CI = setProtocol();
$CI->email->from(EMAIL_FROM, FROM_NAME);
$CI->email->subject("Reset Password");
$CI->email->message($CI->load->view('email/resetPassword', $data, TRUE));
$CI->email->to($detail["email"]);
$status = $CI->email->send();
return $status;
}
}
if(!function_exists('setFlashData'))
{
function setFlashData($status, $flashMsg)
{
$CI = get_instance();
$CI->session->set_flashdata($status, $flashMsg);
}
}
?>
Ckeditor Helper
file Name : Ckeditor_helper.php
<?php
if(!defined('BASEPATH')) exit('No direct script access allowed');
/*
* CKEditor helper for CodeIgniter
*
* @package CodeIgniter
* @license http://creativecommons.org/licenses/by-nc-sa/3.0/us/
* @tutorial http://itechtuto.com/developpement-php/codeigniter/ckeditor-helper-for-codeigniter/
* @see http://codeigniter.com/forums/viewthread/127374/
* @version 2010-08-28
*
*/
/**
* This function adds once the CKEditor's config vars
* @author itechtuto
* @access private
* @param array $data (default: array())
* @return string
*/
function cke_initialize($data = array()) {
$return = '';
if(!defined('CI_CKEDITOR_HELPER_LOADED')) {
define('CI_CKEDITOR_HELPER_LOADED', TRUE);
$return = '<script type="text/javascript" src="'.base_url(). $data['path'] . '/ckeditor.js"></script>';
$return .= "<script type=\"text/javascript\">CKEDITOR_BASEPATH = '" . base_url() . $data['path'] . "/';</script>";
}
return $return;
}
/**
* This function create JavaScript instances of CKEditor
* @author Samuel Sanchez
* @access private
* @param array $data (default: array())
* @return string
*/
function cke_create_instance($data = array()) {
$return = "<script type=\"text/javascript\">
CKEDITOR.replace('" . $data['id'] . "', {";
//Adding config values
if(isset($data['config'])) {
foreach($data['config'] as $k=>$v) {
// Support for extra config parameters
if (is_array($v)) {
$return .= $k . " : [";
$return .= config_data($v);
$return .= "]";
}
else {
$return .= $k . " : '" . $v . "'";
}
if($k !== end(array_keys($data['config']))) {
$return .= ",";
}
}
}
$return .= '});</script>';
return $return;
}
/**
* This function displays an instance of CKEditor inside a view
* @author Samuel Sanchez
* @access public
* @param array $data (default: array())
* @return string
*/
function display_ckeditor($data = array())
{
// Initialization
$return = cke_initialize($data);
// Creating a Ckeditor instance
$return .= cke_create_instance($data);
// Adding styles values
if(isset($data['styles'])) {
$return .= "<script type=\"text/javascript\">CKEDITOR.addStylesSet( 'my_styles_" . $data['id'] . "', [";
foreach($data['styles'] as $k=>$v) {
$return .= "{ name : '" . $k . "', element : '" . $v['element'] . "', styles : { ";
if(isset($v['styles'])) {
foreach($v['styles'] as $k2=>$v2) {
$return .= "'" . $k2 . "' : '" . $v2 . "'";
if($k2 !== end(array_keys($v['styles']))) {
$return .= ",";
}
}
}
$return .= '} }';
if($k !== end(array_keys($data['styles']))) {
$return .= ',';
}
}
$return .= ']);';
$return .= "CKEDITOR.instances['" . $data['id'] . "'].config.stylesCombo_stylesSet = 'my_styles_" . $data['id'] . "';
</script>";
}
return $return;
}
/**
* config_data function.
* This function look for extra config data
*
* @author ronan
* @link http://kromack.com/developpement-php/codeigniter/ckeditor-helper-for-codeigniter/comment-page-5/#comment-545
* @access public
* @param array $data. (default: array())
* @return String
*/
function config_data($data = array())
{
$return = '';
foreach ($data as $key)
{
if (is_array($key)) {
$return .= "[";
foreach ($key as $string) {
$return .= "'" . $string . "'";
if ($string != end(array_values($key))) $return .= ",";
}
$return .= "]";
}
else {
$return .= "'".$key."'";
}
if ($key != end(array_values($data))) $return .= ",";
}
return $return;
}
Ckeditor Controller
file Name : Ckeditor.php
<?php
class Ckeditor extends CI_Controller {
// extends CI_Controller for CI 2.x users
public $data = array();
public function __construct() {
//parent::Controller();
parent::__construct(); for CI 2.x users
$this->load->helper('ckeditor');
//Ckeditor's configuration
$this->data['ckeditor'] = array(
//ID of the textarea that will be replaced
'id' => 'content',
'path' => 'js/ckeditor',
//Optionnal values
'config' => array(
'toolbar' => "Full", //Using the Full toolbar
'width' => "550px", //Setting a custom width
'height' => '100px', //Setting a custom height
),
//Replacing styles from the "Styles tool"
'styles' => array(
//Creating a new style named "style 1"
'style 1' => array (
'name' => 'Blue Title',
'element' => 'h2',
'styles' => array(
'color' => 'Blue',
'font-weight' => 'bold'
)
),
//Creating a new style named "style 2"
'style 2' => array (
'name' => 'Red Title',
'element' => 'h2',
'styles' => array(
'color' => 'Red',
'font-weight' => 'bold',
'text-decoration' => 'underline'
)
)
)
);
$this->data['ckeditor_2'] = array(
//ID of the textarea that will be replaced
'id' => 'content_2',
'path' => 'js/ckeditor',
//Optionnal values
'config' => array(
'width' => "550px", //Setting a custom width
'height' => '100px', //Setting a custom height
'toolbar' => array( //Setting a custom toolbar
array('Bold', 'Italic'),
array('Underline', 'Strike', 'FontSize'),
array('Smiley'),
'/'
)
),
//Replacing styles from the "Styles tool"
'styles' => array(
//Creating a new style named "style 1"
'style 3' => array (
'name' => 'Green Title',
'element' => 'h3',
'styles' => array(
'color' => 'Green',
'font-weight' => 'bold'
)
)
)
);
}
public function index() {
$this->load->view('ckeditor', $this->data);
}
}
Previous
Next