*/
class AgileStoreLocator_Admin {
/**
* The ID of this plugin.
*
* @since 1.0.0
* @access private
* @var string $AgileStoreLocator The ID of this plugin.
*/
private $AgileStoreLocator;
/**
* The version of this plugin.
*
* @since 1.0.0
* @access private
* @var string $version The current version of this plugin.
*/
private $version;
/**
* Initialize the class and set its properties.
*
* @since 1.0.0
* @param string $AgileStoreLocator The name of this plugin.
* @param string $version The version of this plugin.
*/
public function __construct( $AgileStoreLocator, $version ) {
$this->AgileStoreLocator = $AgileStoreLocator;
$this->version = $version;
}
/**
* Register the stylesheets for the admin area.
*
* @since 1.0.0
*/
public function enqueue_styles() {
/**
* This function is provided for demonstration purposes only.
*
* An instance of this class should be passed to the run() function
* defined in AgileStoreLocator_Loader as all of the hooks are defined
* in that particular class.
*
* The AgileStoreLocator_Loader will then create the relationship
* between the defined hooks and the functions defined in this
* class.
*/
wp_enqueue_style( $this->AgileStoreLocator, AGILESTORELOCATOR_URL_PATH . 'public/css/bootstrap.min.css', array(), $this->version, 'all' );
wp_enqueue_style( 'asl_chosen_plugin', AGILESTORELOCATOR_URL_PATH . 'public/css/chosen.min.css', array(), $this->version, 'all' );
wp_enqueue_style( 'asl_admin', AGILESTORELOCATOR_URL_PATH . 'admin/css/style.css', array(), $this->version, 'all' );
wp_enqueue_style( 'asl_datatable1', AGILESTORELOCATOR_URL_PATH . 'public/datatable/media/css/demo_page.css', array(), $this->version, 'all' );
wp_enqueue_style( 'asl_datatable2', AGILESTORELOCATOR_URL_PATH . 'public/datatable/media/css/jquery.dataTables.css', array(), $this->version, 'all' );
}
/**
* Register the JavaScript for the admin area.
*
* @since 1.0.0
*/
public function enqueue_scripts() {
/**
* This function is provided for demonstration purposes only.
*
* An instance of this class should be passed to the run() function
* defined in AgileStoreLocator_Loader as all of the hooks are defined
* in that particular class.
*
* The AgileStoreLocator_Loader will then create the relationship
* between the defined hooks and the functions defined in this
* class.
*/
//wp_enqueue_script( $this->AgileStoreLocator, AGILESTORELOCATOR_URL_PATH . 'public/js/jquery-1.11.3.min.js', array( 'jquery' ), $this->version, false );
wp_enqueue_script( $this->AgileStoreLocator.'-lib', AGILESTORELOCATOR_URL_PATH . 'public/js/libs.min.js', array('jquery'), $this->version, false );
wp_enqueue_script( $this->AgileStoreLocator.'-choosen', AGILESTORELOCATOR_URL_PATH . 'public/js/chosen.proto.min.js', array('jquery'), $this->version, false );
wp_enqueue_script( $this->AgileStoreLocator.'-datatable', AGILESTORELOCATOR_URL_PATH . 'public/datatable/media/js/jquery.dataTables.min.js', array('jquery'), $this->version, false );
wp_enqueue_script( 'bootstrap', AGILESTORELOCATOR_URL_PATH . 'public/js/bootstrap.min.js', array('jquery'), $this->version, false );
wp_enqueue_script( $this->AgileStoreLocator.'-upload', AGILESTORELOCATOR_URL_PATH . 'admin/js/jquery.fileupload.min.js', array('jquery'), $this->version, false );
wp_enqueue_script( $this->AgileStoreLocator.'-jscript', AGILESTORELOCATOR_URL_PATH . 'admin/js/jscript.js', array('jquery'), $this->version, false );
wp_localize_script( $this->AgileStoreLocator.'-jscript', 'ASL_REMOTE', array( 'URL' => admin_url( 'admin-ajax.php' ),'1.1', true ));
}
public function upload_logo() {
$response = new \stdclass();
$response->success = false;
$uniqid = uniqid();
$target_dir = AGILESTORELOCATOR_PLUGIN_PATH."public/Logo/";
$target_file = $uniqid.'_'. strtolower($_FILES["files"]["name"]);
$target_name = isset($_POST['data']['category_name'])?$_POST['data']['category_name']:('Logo '.time());
$imageFileType = explode('.', $_FILES["files"]["name"]);
$imageFileType = $imageFileType[count($imageFileType) - 1];
//if file not found
/*
if (file_exists($target_name)) {
$response->message = "Sorry, file already exists.";
}
*/
//to big size
if ($_FILES["files"]["size"] > 5000000) {
$response->message = "Sorry, your file is too large.";
}
// not a valid format
else if(!in_array($imageFileType, array('jpg','png','jpeg','gif','JPG'))) {
$response->message = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
}
// upload
else if(move_uploaded_file($_FILES["files"]["tmp_name"], $target_dir.$target_file)) {
global $wpdb;
$wpdb->insert(AGILESTORELOCATOR_PREFIX.'storelogos',
array('path'=>$target_file,'name'=>$target_name),
array('%s','%s'));
$response->list = $wpdb->get_results( "SELECT * FROM ".AGILESTORELOCATOR_PREFIX."storelogos ORDER BY id DESC");
$response->message = "The file has been uploaded.";
$response->success = true;
}
//error
else {
$response->message = 'Some Error Occured';
}
echo json_encode($response);
die;
}
/*POST METHODS*/
public function add_new_store() {
global $wpdb;
$response = new \stdclass();
$response->success = false;
$form_data = $_REQUEST['data'];
//insert into stores table
if($wpdb->insert( AGILESTORELOCATOR_PREFIX.'stores', $form_data))
{
$response->success = true;
$store_id = $wpdb->insert_id;
/*Save Categories*/
if(is_array($_REQUEST['category']))
foreach ($_REQUEST['category'] as $category) {
$wpdb->insert(AGILESTORELOCATOR_PREFIX.'stores_categories',
array('store_id'=>$store_id,'category_id'=>$category),
array('%s','%s'));
}
$response->msg = 'Store added successfully.';
}
else
{
$response->error = 'Error occurred while saving Store';//$form_data
$response->msg = $wpdb->last_error;
}
echo json_encode($response);die;
}
//update Store
public function update_store() {
global $wpdb;
$response = new \stdclass();
$response->success = false;
$form_data = $_REQUEST['data'];
$update_id = $_REQUEST['updateid'];
//update into stores table
$wpdb->update(AGILESTORELOCATOR_PREFIX."stores",
array(
'title' => $form_data['title'],
'description' => $form_data['description'],
'phone' => $form_data['phone'],
'fax' => $form_data['fax'],
'email' => $form_data['email'],
'street' => $form_data['street'],
'postal_code' => $form_data['postal_code'],
'city' => $form_data['city'],
'state' => $form_data['state'],
'lat' => $form_data['lat'],
'lng' => $form_data['lng'],
'website' => $form_data['url'],
'country' => $form_data['country'],
'is_disabled' => $form_data['is_disabled'],
'description_2' => $form_data['description_2'],
'logo_id' => $form_data['logo_id'],
'start_time' => $form_data['start_time'],
'end_time' => $form_data['end_time'],
'days' => $form_data['days'],
'marker_id' => 1,
'logo_id' => $form_data['logo_id'],
'updated_on' => date('Y-m-d H:i:s')
),
array('id' => $update_id)
);
$sql = "DELETE FROM ".AGILESTORELOCATOR_PREFIX."stores_categories WHERE store_id = ".$update_id;
$wpdb->query($sql);
if(is_array($_REQUEST['category']))
foreach ($_REQUEST['category'] as $category) {
$wpdb->insert(AGILESTORELOCATOR_PREFIX.'stores_categories',
array('store_id'=>$update_id,'category_id'=>$category),
array('%s','%s'));
}
//ADD THE TIMINGS
$timing_result = $wpdb->get_results("SELECT count(*) as c FROM ".AGILESTORELOCATOR_PREFIX."stores_timing WHERE store_id = $update_id");
//INSERT OR UPDATE
if($timing_result[0]->c == 0) {
$datatime = $_REQUEST['datatime'];
$datatime['store_id'] = $update_id;
$wpdb->insert( AGILESTORELOCATOR_PREFIX.'stores_timing', $datatime);
}
else {
$datatime = $_REQUEST['datatime'];
$wpdb->update( AGILESTORELOCATOR_PREFIX.'stores_timing', $datatime,array('store_id' => $update_id));
}
$response->success = true;
$response->msg = 'Store update successfully.';
echo json_encode($response);die;
}
// to delete the store
public function delete_store() {
global $wpdb;
$response = new \stdclass();
$response->success = false;
$store_id = $_REQUEST['store_id'];
$sql = "DELETE FROM ".AGILESTORELOCATOR_PREFIX."stores WHERE id = ".$store_id;
if($wpdb->query($sql))
{
$response->success = true;
$response->msg = 'Store deleted successfully.';
}
else
{
$response->error = 'Error occurred while saving record';//$form_data
$response->msg = $wpdb->show_errors();
}
echo json_encode($response);die;
}
/////////////////////////////////ALL Category Methods
/*Categories methods*/
public function add_category() {
global $wpdb;
$response = new \stdclass();
$response->success = false;
$target_dir = AGILESTORELOCATOR_PLUGIN_PATH."public/icon/";
$namefile = substr(strtolower($_FILES["files"]["name"]), 0, strpos(strtolower($_FILES["files"]["name"]), '.'));
$imageFileType = pathinfo($_FILES["files"]["name"],PATHINFO_EXTENSION);
$target_name = uniqid();
//add extension
$target_name .= '.'.$imageFileType;
///CREATE DIRECTORY IF NOT EXISTS
if(!file_exists($target_dir)) {
mkdir( $target_dir, 0775, true );
}
/*//if file not found
if (file_exists($target_name)) {
$response->message = "Sorry, file already exists.";
}
//to big size
else */
if ($_FILES["files"]["size"] > 5000000) {
$response->message = "Sorry, your file is too large.";
}
// not a valid format
else if(!in_array($imageFileType, array('jpg','png','jpeg','JPG','gif'))) {
$response->message = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
}
// upload
else if(move_uploaded_file($_FILES["files"]["tmp_name"], $target_dir.$target_name)) {
$form_data = $_REQUEST['data'];
if($wpdb->insert(AGILESTORELOCATOR_PREFIX.'categories',
array( 'category_name' => $form_data['category_name'],
'is_active' => 1,
'icon' => $target_name
),
array('%s','%d','%s'))
)
{
$response->message = "Category Add successfully";
$response->success = true;
}
else
{
$response->message = 'Error occurred while saving record';//$form_data
}
}
//error
else {
$response->message = 'Some error occured';
}
echo json_encode($response);
die;
}
//delete category
public function delete_category() {
global $wpdb;
$response = new \stdclass();
$response->success = false;
$store_id = $_REQUEST['category_id'];
$sql = "DELETE FROM ".AGILESTORELOCATOR_PREFIX."categories WHERE id = ".$store_id;
$categoriesesult = $wpdb->get_results("SELECT * FROM ".AGILESTORELOCATOR_PREFIX."categories WHERE id = ".$store_id );
if(count($categoriesesult) != 0){
if($wpdb->query($sql))
{
$inputFileName = AGILESTORELOCATOR_PLUGIN_PATH.'public/icon/'.$categoriesesult[0]->icon;
if(file_exists($inputFileName)) {
unlink($inputFileName);
$response->success = true;
$response->msg = 'Category deleted successfully.';
}
}
else
{
$response->error = 'Error occurred while deleting record';//$form_data
$response->msg = $wpdb->show_errors();
}
}
else
{
$response->error = 'Error occurred while deleting record';
}
echo json_encode($response);die;
}
//update category with icon
public function update_category() {
global $wpdb;
$response = new \stdclass();
$response->success = false;
$data = $_REQUEST['data'];
// with icon
if($data['action'] == "notsame") {
$target_dir = AGILESTORELOCATOR_PLUGIN_PATH."public/icon/";
$namefile = substr(strtolower($_FILES["files"]["name"]), 0, strpos(strtolower($_FILES["files"]["name"]), '.'));
$imageFileType = pathinfo($_FILES["files"]["name"],PATHINFO_EXTENSION);
$target_name = uniqid();
//add extension
$target_name .= '.'.$imageFileType;
$res = $wpdb->get_results( "SELECT * FROM ".AGILESTORELOCATOR_PREFIX."categories WHERE id = ".$data['category_id']);
if ($_FILES["files"]["size"] > 5000000) {
$response->msg = "Sorry, your file is too large.";
}
// not a valid format
else if(!in_array($imageFileType, array('jpg','png','gif','jpeg','JPG'))) {
$response->msg = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
}
// upload
else if(move_uploaded_file($_FILES["files"]["tmp_name"], $target_dir.$target_name)) {
//delete previous file
$wpdb->update(AGILESTORELOCATOR_PREFIX."categories",array( 'category_name'=> $data['category_name'], 'icon'=> $target_name),array('id' => $data['category_id']),array( '%s' ), array( '%d' ));
$response->msg = 'Update successfully.';
$response->success = true;
if (file_exists($target_dir.$res[0]->icon)) {
unlink($target_dir.$res[0]->icon);
}
}
//error
else {
$response->msg = 'Some error occured';
}
}
//without icon
else {
$wpdb->update(AGILESTORELOCATOR_PREFIX."categories",array( 'category_name'=> $data['category_name']),array('id' => $data['category_id']),array( '%s' ), array( '%d' ));
$response->msg = 'Update successfully.';
$response->success = true;
}
echo json_encode($response);die;
}
//get category by id
public function get_category_by_id() {
global $wpdb;
$response = new \stdclass();
$response->success = false;
$store_id = $_REQUEST['category_id'];
$response->list = $wpdb->get_results( "SELECT * FROM ".AGILESTORELOCATOR_PREFIX."categories WHERE id = ".$store_id);
if(count($response->list)!=0){
$response->success = true;
}
else{
$response->error = 'Error occurred while geting record';//$form_data
}
echo json_encode($response);die;
}
/*GET the Categories*/
public function get_categories() {
global $wpdb;
$start = isset( $_REQUEST['iDisplayStart'])?$_REQUEST['iDisplayStart']:0;
$params = isset($_REQUEST)?$_REQUEST:null;
$acolumns = array(
'id','category_name','is_active','icon','created_on'
);
$columnsFull = $acolumns;
$clause = array();
if(isset($_REQUEST['filter'])) {
foreach($_REQUEST['filter'] as $key => $value) {
if($value != '') {
if($key == 'is_active')
{
$value = ($value == 'yes')?1:0;
}
$clause[] = "$key like%'{$value}'%";
}
}
}
//iDisplayStart::Limit per page
$sLimit = "";
if ( isset( $_REQUEST['iDisplayStart'] ) && $_REQUEST['iDisplayLength'] != '-1' )
{
$sLimit = "LIMIT ".intval( $_REQUEST['iDisplayStart'] ).", ".
intval( $_REQUEST['iDisplayLength'] );
}
/*
* Ordering
*/
$sOrder = "";
if ( isset( $_REQUEST['iSortCol_0'] ) )
{
$sOrder = "ORDER BY ";
for ( $i=0 ; $i < intval( $_REQUEST['iSortingCols'] ) ; $i++ )
{
if (isset($_REQUEST['iSortCol_'.$i]))
{
$sOrder .= "`".$acolumns[ intval( $_REQUEST['iSortCol_'.$i] ) ]."` ".$_REQUEST['sSortDir_0'];
break;
}
}
//$sOrder = substr_replace( $sOrder, "", -2 );
if ( $sOrder == "ORDER BY" )
{
$sOrder = "";
}
}
$sWhere = implode(' AND ',$clause);
if($sWhere != '')$sWhere = ' WHERE '.$sWhere;
$fields = implode(',', $columnsFull);
###get the fields###
$sql = "SELECT $fields FROM ".AGILESTORELOCATOR_PREFIX."categories";
$sqlCount = "SELECT count(*) 'count' FROM ".AGILESTORELOCATOR_PREFIX."categories";
/*
* SQL queries
* Get data to display
*/
$sQuery = "{$sql} {$sWhere} {$sOrder} {$sLimit}";
$data_output = $wpdb->get_results($sQuery);
/* Data set length after filtering */
$sQuery = "{$sqlCount} {$sWhere}";
$r = $wpdb->get_results($sQuery);
$iFilteredTotal = $r[0]->count;
$iTotal = $iFilteredTotal;
/*
* Output
*/
$sEcho = isset($_REQUEST['sEcho'])?intval($_REQUEST['sEcho']):1;
$output = array(
"sEcho" => intval($_REQUEST['sEcho']),
//"test" => $test,
"iTotalRecords" => $iTotal,
"iTotalDisplayRecords" => $iFilteredTotal,
"aaData" => array()
);
foreach($data_output as $aRow)
{
$row = $aRow;
if($row->is_active == 1) {
$row->is_active = 'Yes';
}
else {
$row->is_active = 'No';
}
$row->icon = "";
$row->action = '