File: /home/barbeatleanalyti/public_html/beatleanalytics.com/application/controllers/Admin.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Admin extends CI_Controller {
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* @see https://codeigniter.com/user_guide/general/urls.html
*/
public function __construct()
{
parent::__construct();
// load library form validation
$this->load->library('form_validation');
$this->load->helper('url');
$this->load->model('AdminModel');
// $this->load->library('auth');
$this->load->library('user_agent');
$loginid = $this->session->userdata('userid');
// print_r($this->session->userdata('')); die;
}
// Login function
public function index()
{
//echo md5('admin@123'); die;
if(!empty($this->session->userdata('userid'))){redirect(ADMIN_URL.'dashboard');}
if (isset($_POST) && !empty($_POST)){
$username = $this->input->post('username');
$userpass = $this->input->post('userpass');
$data = $this->AdminModel->loginadminuser($username,$userpass);
$loginid = $this->session->userdata('userid');
if(!empty($loginid)){
$this->session->set_flashdata('message', 'Welcome to the daashboard');
redirect(ADMIN_URL.'dashboard');
}
}
$this->load->view('site_admin/index');
}
// logout function
public function logout()
{
$user_data = $this->session->all_userdata();
foreach ($user_data as $key => $value) {
if ($key != 'session_id' && $key != 'ip_address' && $key != 'user_agent' && $key != 'last_activity') {
$this->session->unset_userdata($key);
}
}
$this->session->sess_destroy();
redirect(ADMIN_URL.'index');
}
//Index function start here ---
public function dashboard()
{
if($this->session->userdata('userid') == ''){redirect(ADMIN_URL.'index');}
$userdata = $this->session->userdata();
$allnews = $this->AdminModel->getallNews();
$allblog = $this->AdminModel->getallblog();
$newsholdstatus = $this->AdminModel->newsholdstatus();
$newspendingstatus = $this->AdminModel->newspendingstatus();
$newsactivestatus = $this->AdminModel->newsactivestatus();
$blogholdstatus = $this->AdminModel->blogholdstatus();
$blogpendingstatus = $this->AdminModel->blogpendingstatus();
$blogactivestatus = $this->AdminModel->blogactivestatus();
$data = array
(
'allnews'=>json_decode($allnews),
'allblog'=>json_decode($allblog),
'newsholdstatus' => json_decode($newsholdstatus),
'newspendingstatus' => json_decode($newspendingstatus),
'newsactivestatus' => json_decode($newsactivestatus),
'blogholdstatus' => json_decode($blogholdstatus),
'blogpendingstatus' => json_decode($blogpendingstatus),
'blogactivestatus' => json_decode($blogactivestatus),
'userdata' => $userdata,
);
// echo '<pre>'; print_r($data); die;
// die;
$this->load->view('site_admin/home',$data);
}
// Get all News data from here
public function news()
{
if($this->session->userdata('userid') == ''){redirect(ADMIN_URL.'index');}
$userdata = $this->session->userdata();
$getallnews = $this->AdminModel->getallNews();
$data = array
(
'getallnews' => json_decode($getallnews),
'userdata' => $userdata
);
$this->load->view('site_admin/allnews',$data);
}
// Add news news from here
public function addnews()
{
if($this->session->userdata('userid') == ''){redirect(ADMIN_URL.'index');}
// sumit news details from here
$userdata = $this->session->userdata();
$maindata = array('userdata'=>$userdata);
$userid = $userdata['userid'];
if (!empty($_POST))
{
//news_short_description
$news_title = $this->input->post('news_title');
$news_description = $this->input->post('news_description');
$news_short = $this->input->post('news_short');
$news_video = $this->input->post('news_video');
$news_startdate = date('Y-m-d H:i:s',strtotime($this->input->post('news_startdate')));
$date = date('Y-m-d H:i:s');
$targetDir = FILE_UPLOAD_PATH.'news_image/';
$allowTypes = array('jpg','png','jpeg');
//echo '<pre>'; print_r($fileNames);
$fileName = time().'_newsImage.jpg';
$targetFilePath = $targetDir . $fileName;
// Check whether file type is valid
$fileType = pathinfo($targetFilePath, PATHINFO_EXTENSION);
// echo $_FILES["new_image"]["size"]; die;
if(!empty($_FILES["new_image"]["tmp_name"]))
{
if(in_array($fileType, $allowTypes)){
// Upload file to server
if($_FILES["new_image"]["size"] <= 2097152)
{
if(move_uploaded_file($_FILES["new_image"]["tmp_name"], $targetFilePath))
{
$news_image = $fileName;//"('".$fileName."', NOW()),";
$insertdata = array(
'news_title' => $news_title,
'created_by' => $userid,
'news_image' => $news_image,
'news_description' => $news_description,
'news_short_description' => $news_short,
'news_video_link' => $news_video,
'news_start_time' => $news_startdate,
'created_date' => $date
);
//echo '<pre>'; print_r($insertdata); die;
$data = $this->db->insert("beatle_news",$insertdata);
if ($data){
$this->session->set_flashdata('message', 'News Added Successfully');
redirect(ADMIN_URL.'news');
}
}
}
else {
$this->session->set_flashdata('error', 'Image Size should be less then 2MB');
}
}
else
{
$this->session->set_flashdata('error', 'only jpeg , jpg , png accepted');
}
}
else {
$this->session->set_flashdata('error', 'Please insert the image');
}
}
$this->load->view('site_admin/addnews',$maindata);
}
// news acc. to the newsID
public function editnews()
{
if($this->session->userdata('userid') == ''){redirect(ADMIN_URL.'index');}
$userdata = $this->session->userdata();
$userid = $userdata['userid'];
$newsid = $this->uri->segment('3');
$newsdata = $this->AdminModel->getNewsAccId($newsid);
if (!empty($_POST))
{
//
$editid = $this->input->post('news_id');
$news_title = $this->input->post('news_title');
$news_description = $this->input->post('news_description');
$news_short = $this->input->post('news_short');
$news_video = $this->input->post('news_video');
$news_startdate = date('Y-m-d H:i:s',strtotime($this->input->post('news_startdate')));
$date = date('Y-m-d H:i:s');
$targetDir = FILE_UPLOAD_PATH.'beatle_analytics/news_image/';
$allowTypes = array('jpg','png','jpeg');
//echo '<pre>'; print_r($fileNames);
$fileName = time().'_newsImage.jpg';
$targetFilePath = $targetDir . $fileName;
// Check whether file type is valid
$fileType = pathinfo($targetFilePath, PATHINFO_EXTENSION);
//echo'<pre>'; print_r($_FILES); die;
if(!empty($_FILES["new_image"]["tmp_name"])){
if(in_array($fileType, $allowTypes)){
// Upload file to server
if(move_uploaded_file($_FILES["new_image"]["tmp_name"], $targetFilePath)){
$news_image = $fileName;//"('".$fileName."', NOW()),";
}
}
}
else {
$news_image = $this->input->post('old_new_image');
}
$insertdata = array(
'news_title' => $news_title,
'news_image' => $news_image,
'news_description' => $news_description,
'news_short_description' => $news_short,
'news_video_link' => $news_video,
'news_start_time' => $news_startdate,
'created_date' => $date
);
//echo '<pre>'; print_r($insertdata); die;
$data = $this->db->where("id",$editid)->update("beatle_news",$insertdata);
if($data){
redirect(ADMIN_URL.'news');
}
}
$data = array('newsdata'=>json_decode($newsdata),'userdata'=>$userdata);
$this->load->view('site_admin/editnews',$data);
}
// change status of blog and news section
public function changestatus()
{
if($this->session->userdata('userid') == ''){redirect(ADMIN_URL.'index');}
$status = $_POST['stautstype'];
$pagetype = $_POST['pagetype'];
$mainid = $_POST['mainid'];
$data = $this->AdminModel->changestatus($status,$pagetype,$mainid);
//echo '<pre>'; print_r($data);
}
//details function for news
public function newsdetails()
{
if($this->session->userdata('userid') == ''){redirect(ADMIN_URL.'index');}
$userdata = $this->session->userdata();
$userid = $userdata['userid'];
$newsid = $this->uri->segment('3');
$newsdata = $this->AdminModel->getNewsAccId($newsid);
$data = array('newsdata'=> json_decode($newsdata),'userdata'=>$userdata);
$this->load->view('site_admin/newsdetails',$data);
}
// Get All Blog Data from here
public function blog()
{
if($this->session->userdata('userid') == ''){redirect(ADMIN_URL.'index');}
$userdata = $this->session->userdata();
$userid = $userdata['userid'];
$getallblog = $this->AdminModel->getallblog();
$data = array
(
'getallblog' => json_decode($getallblog),
'userdata' => $userdata
);
$this->load->view('site_admin/allblog',$data);
}
// Add Blog from here
public function addblog()
{
if($this->session->userdata('userid') == ''){redirect(ADMIN_URL.'index');}
$userdata = $this->session->userdata();
$getalltag = $this->AdminModel->getalltagactive();
$maindata = array('userdata'=>$userdata,'getalltag'=>$getalltag);
$userid = $userdata['userid'];
// sumit news details from here
if (!empty($_POST))
{
//
$news_title = $this->input->post('blog_title');
$news_description = $this->input->post('blog_description');
$blog_short = $this->input->post('blog_short');
$news_video = $this->input->post('blog_video');
$news_startdate = date('Y-m-d H:i:s',strtotime($this->input->post('blog_startdate')));
$date = date('Y-m-d H:i:s');
$targetDir = FILE_UPLOAD_PATH.'blog_image/';
$allowTypes = array('jpg','png','jpeg');
$blog_tag = implode('|', $this->input->post('blog_tag'));
$fileName = time().'_blogImage.jpg';
$targetFilePath = $targetDir . $fileName;
// Check whether file type is valid
$fileType = pathinfo($targetFilePath, PATHINFO_EXTENSION);
if(!empty($_FILES["blog_image"]["tmp_name"])){
if(in_array($fileType, $allowTypes)){
if($_FILES["blog_image"]["size"] <= 2097152)
{
if(move_uploaded_file($_FILES["blog_image"]["tmp_name"], $targetFilePath)){
$news_image = $fileName;//"('".$fileName."', NOW()),";
$insertdata = array(
'blog_title' => $news_title,
'created_by' => $userid,
'blog_short_description' => $blog_short,
'blog_image' => $news_image,
'blog_description' => $news_description,
'blog_video_link' => $news_video,
'blog_tag' => $blog_tag,
'blog_start_date' => $news_startdate,
'created_date' => $date
);
//echo '<pre>'; print_r($insertdata); die;
$data = $this->db->insert("beatle_blog",$insertdata);
if($data){
$this->session->set_flashdata('message','Blog Inserted Successfully');
}
else {
$this->session->set_flashdata('Error','Blog Not Inserted.Please check');
}
}
}
else {
$this->session->set_flashdata('error','Image Size Should be less then 2MB');
}
}
else {
$this->session->set_flashdata('error','Image shuold have jpeg,jpg,png format');
}
}
else {
$this->session->set_flashdata('error','Pease selete image');
}
}
$this->load->view('site_admin/addblog',$maindata);
}
// Edit Blog acc. to the newsID
public function editblog()
{
if($this->session->userdata('userid') == ''){redirect(ADMIN_URL.'index');}
$blogid = $this->uri->segment('3');
$blogdata = $this->AdminModel->getBlogAccId($blogid);
$userdata = $this->session->userdata();
$getalltag = $this->AdminModel->getalltagactive();
//echo '<pre>'; print_r($getalltag); die;
$maindata = array('userdata'=>$userdata);
$userid = $userdata['userid'];
if (!empty($_POST))
{
//
$userdata = $this->session->userdata();
$userid = $userdata['userid'];
$editid = $this->input->post('blog_id');
$blog_title = $this->input->post('blog_title');
$blog_description = $this->input->post('blog_description');
$blog_short = $this->input->post('blog_short');
$blog_video = $this->input->post('blog_video');
$blog_startdate = date('Y-m-d H:i:s',strtotime($this->input->post('blog_startdate')));
$date = date('Y-m-d H:i:s');
$targetDir = FILE_UPLOAD_PATH.'blog_image/';
$allowTypes = array('jpg','png','jpeg');
if(!empty($this->input->post('blog_tag'))){
$blog_tag = implode('|', $this->input->post('blog_tag'));
}else{
$blog_tag = $this->input->post('pre_tag');
}
//echo '<pre>'; print_r($fileNames);
$fileName = time().'_blogImage.jpg';
$targetFilePath = $targetDir . $fileName;
// Check whether file type is valid
$fileType = pathinfo($targetFilePath, PATHINFO_EXTENSION);
//echo'<pre>'; print_r($_FILES); die;
if(!empty($_FILES["blog_image"]["tmp_name"])){
if(in_array($fileType, $allowTypes)){
// Upload file to server
if(move_uploaded_file($_FILES["blog_image"]["tmp_name"], $targetFilePath)){
$blog_image = $fileName;//"('".$fileName."', NOW()),";
}
}
}
else {
$blog_image = $this->input->post('old_blog_image');
}
$insertdata = array(
'blog_title' => $blog_title,
'created_by' => $userid,
'blog_image' => $blog_image,
'blog_description' => $blog_description,
'blog_short_description' => $blog_short,
'blog_video_link' => $blog_video,
'blog_tag' => $blog_tag,
'blog_start_date' => $blog_startdate,
'created_date' => $date
);
//echo '<pre>'; print_r($insertdata); die;
$data = $this->db->where("id",$editid)->update("beatle_blog",$insertdata);
if($data){
redirect(ADMIN_URL.'blog');
}
}
$btag = json_decode($blogdata);
$preblog = explode('|',$btag[0]->blog_tag);
$tagdata = $this->AdminModel->gettagAccidwherein($preblog);
$data = array('blogdata'=>json_decode($blogdata),'tagdata'=>$tagdata,'userdata'=>$userdata,'getalltag'=>$getalltag);
$this->load->view('site_admin/editblog',$data);
}
//details function for blog
public function blogdetails()
{
if($this->session->userdata('userid') == ''){redirect(ADMIN_URL.'index');}
$userdata = $this->session->userdata();
$blogid = $this->uri->segment('3');
$blogdata = $this->AdminModel->getBlogAccId($blogid);
$data = array('blogdata'=> json_decode($blogdata),'userdata'=>$userdata);
$this->load->view('site_admin/blogdetails',$data);
}
public function delete($id)
{
$this->db->delete('beatle_blog', array('id' => $id));
echo 'Deleted successfully.';
}
// Profile Edit Start here
public function profile()
{
if($this->session->userdata('userid') == ''){redirect(ADMIN_URL.'index');}
$userdata = $this->session->userdata();
$userid = $this->session->userdata('userid');
$getadmin = $this->AdminModel->getadmindetail($userid);
if (!empty($_POST) && !empty($_POST['username'])){
$username = $this->input->post('username');
$first_name = $this->input->post('user_fname');
$last_name = $this->input->post('user_lname');
$user_email = $this->input->post('user_email');
$user_description = $this->input->post('user_description');
$user_password = $this->input->post('user_pass');
if(!empty($user_password)){$pasword = md5($user_password);}else{$pasword = $getadmin[0]->user_password;}
//echo $password; die;
$targetDir = $_SERVER['DOCUMENT_ROOT'] . '/beatleanalytics/beatle_analytics/user_image/';
$allowTypes = array('jpg','png','jpeg');
//echo '<pre>'; print_r($fileNames);
$fileName = time().'_userimage.jpg';
$targetFilePath = $targetDir . $fileName;
// Check whether file type is valid
$fileType = pathinfo($targetFilePath, PATHINFO_EXTENSION);
if (!empty($_FILES["user_image"]["tmp_name"]))
{
if(!empty($_FILES["user_image"]["tmp_name"])){
if(in_array($fileType, $allowTypes)){
if($_FILES["user_image"]["size"] <= 2097152)
{
if(move_uploaded_file($_FILES["user_image"]["tmp_name"], $targetFilePath)){ $news_image = $fileName;//"('".$fileName."', NOW()),";
}
}
}
}
}
else
{
$news_image = $this->input->post("old_user_email");
}
$updatarray = array(
'username' => $username,
'first_name' => $first_name,
'last_name' => $last_name,
'user_email' => $user_email,
'user_image' => $news_image,
'user_description' => $user_description,
'user_password' => $pasword
);
// echo '<pre>'; print_r($updatarray); die;
$insertarray = $this->db->where("user_id",$userid)->update("beatle_users",$updatarray);
if($insertarray){
$this->session->set_flashdata('message', 'Profile Data Updated Successfully');
redirect(ADMIN_URL.'profile');
}
}
$data = array('getadmin'=>$getadmin,'userdata'=>$userdata);
$this->load->view('site_admin/profile',$data);
}
// get all tag start here
public function tag()
{
if($this->session->userdata('userid') == ''){redirect(ADMIN_URL.'index');}
$userdata = $this->session->userdata();
$getalltag = $this->AdminModel->getalltag();
$data = array('userdata'=>$userdata,'getalltag'=>$getalltag);
$this->load->view('site_admin/tag',$data);
}
// end here
// Add Tag Start Here
public function addtag()
{
if($this->session->userdata('userid') == ''){redirect(ADMIN_URL.'index');}
$userdata = $this->session->userdata();
if(!empty($_POST) && !empty($_POST['tag_name'])){
$insertarrr = array(
'tag_name' => $this->input->post('tag_name'),
'created_by' => $this->session->userdata('userid'),
'created_date' => date('Y-m-d H:i:s'),
);
$insert = $this->db->insert("beatle_tag",$insertarrr);
if($insert){
$this->session->set_flashdata("message","Tag hgs been added successfully");
}
}
$data = array('userdata'=>$userdata);
$this->load->view('site_admin/addtag',$data);
}
// Edit Tag start here
public function edittag()
{
if($this->session->userdata('userid') == ''){redirect(ADMIN_URL.'index');}
$userdata = $this->session->userdata();
$tagid = $this->uri->segment(3);
$gettag = $this->AdminModel->gettagAccid($tagid);
if(!empty($_POST) && !empty($_POST['tag_name'])){
$tid = $this->input->post('tid');
$insertarrr = array(
'tag_name' => $this->input->post('tag_name'),
'created_by' => $this->session->userdata('userid'),
'updated_date' => date('Y-m-d H:i:s'),
);
$insert = $this->db->where("id",$tid)->update("beatle_tag",$insertarrr);
//echo $this->db->last_query(); die;
if($insert){
$this->session->set_flashdata("message","Tag hgs been updated successfully");
redirect(ADMIN_URL.'tag');
}
}
$data = array('userdata'=>$userdata,'gettag'=>$gettag);
$this->load->view('site_admin/edittag',$data);
}
public function deletenews($id) {
$this->load->model("AdminModel");
$this->AdminModel->delete_news($id);
redirect(ADMIN_URL.'news');
}
public function blogdelete($id) {
$this->load->model("AdminModel");
$this->AdminModel->delete_blog($id);
redirect(ADMIN_URL.'blog');
}
public function tagdelete($id) {
$this->load->model("AdminModel");
$this->AdminModel->delete_tag($id);
redirect(ADMIN_URL.'tag');
}
}