<?php

/* Employee  Model */

class ShapModel {
	

	
	
	
	public static function shapList() {
		
		$requestData= $_REQUEST;
		
		$columns = array( 
		0 =>'name'
		
		
		
	);
	
	$sql  = "SELECT `id`";
    $sql .= " FROM `sf_shaps`";
	
	 $database = DatabaseFactory::getFactory()->getConnection();
	 $query = $database->prepare($sql);
	 $query->execute();
	 $totalData = $query->rowCount();
     $totalFiltered = $totalData;
	 
	 
	 
	 
	 $sql = "SELECT *";
$sql.=" FROM `sf_shaps` WHERE 1=1";

if( !empty($requestData['search']['value']) ) {   // if there is a search parameter, $requestData['search']['value'] contains search parameter
	$sql.=" AND ( id LIKE '".$requestData['search']['value']."%' ";    
	
	
	$sql.=" OR name='".$requestData['search']['value']."' )";
}
$query = $database->prepare($sql);
 $query->execute();
$totalFiltered = $query->rowCount(); // when there is a search parameter then we have to modify total number filtered rows as per search result. 
$sql .= " ORDER BY ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']."  LIMIT ".$requestData['start']." ,".$requestData['length']." ";
/* $requestData['order'][0]['column'] contains colmun index, $requestData['order'][0]['dir'] contains order such as asc/desc  */	
//$query = $con->rowCount($sql);
	$query=$database->prepare($sql);
	$query->execute();

$data = array();

$row = $query->fetchAll();
foreach($row as $rr) {
	
	
	
	$nestedData=array(); 
    $nestedData[] = $rr->name;
    
	
	
	
	$nestedData[] = '
	
	<a class="btn btn-block btn-outline-info btn-flat" href="javascript:void()" title="Edit Shap" onclick="edit_shap('."'".$rr->id."'".')"><i class="mdi mdi-pencil-outline"></i></a>
	<a class="btn btn-block btn-outline-danger btn-flat" href="javascript:void()" title="Delete" onclick="delete_shap('."'".$rr->id."'".')"><i class="mdi mdi-delete-outline"></i></a>';
	
	
	
	$data[] = $nestedData;
	
	
}

$json_data = array(
			"draw"            => intval( $requestData['draw'] ),   // for every request/draw by clientside , they send a number as a parameter, when they recieve a response/data they first check the draw number, so we are sending same number in draw. 
			"recordsTotal"    => intval( $totalData ),  // total number of records
			"recordsFiltered" => intval( $totalFiltered ), // total number of records after searching, if there is no searching then totalFiltered = totalData
			"data"            => $data   // total data array
			);

echo json_encode($json_data);  // send data as json format
		
		
		
	}
	
public static function deleteShap($id) {
	if (!$id) {
            return false;
        }

        $database = DatabaseFactory::getFactory()->getConnection();

        $sql = "DELETE FROM sf_shaps WHERE id = :id LIMIT 1";
        $query = $database->prepare($sql);
        $query->execute(array(':id' => $id));

        if ($query->rowCount() == 1) {
			
			echo json_encode(array("status" => TRUE));
            return true;
        }

        // default return
        Session::add('feedback_negative', 'Delete Failed.');
        return false;
	
}

public static function shapAdd() {
    
   
	
	$name = strip_tags(Request::post('name'));

	
	$database = DatabaseFactory::getFactory()->getConnection();
	$sql = "INSERT INTO sf_shaps (name) 
			 VALUES (:name)";
	$query = $database->prepare($sql);
	$query->execute(array(':name' => $name
							 ));
	$count =  $query->rowCount();
	if($count==1) {
		echo json_encode(array("status" => TRUE));
        return true;
		
	}
	return false;
	
}


public static function getShapDrop() {
		$database = DatabaseFactory::getFactory()->getConnection();
		$sql = "SELECT * FROM sf_shaps ORDER BY name ASC";
        $query = $database->prepare($sql);
        $query->execute();
		$st = $query->fetchAll();
		foreach($st as $ss) {
			echo '<option value="'.$ss->id.'">'.$ss->name.'</option>';
		}
	}
	
	
	public static function getShapSelect() {
		$database = DatabaseFactory::getFactory()->getConnection();
		$sql = "SELECT * FROM sf_shaps ORDER BY name ASC";
        $query = $database->prepare($sql);
        $query->execute();
		return $query->fetchAll();
		
	}




	public static function getShap($id)
    {
        $database = DatabaseFactory::getFactory()->getConnection();

        $sql = "SELECT * FROM sf_shaps WHERE id = :id LIMIT 1";
        $query = $database->prepare($sql);
        $query->execute(array(':id' => $id));

      
        return $query->fetch();
    }
    
    
    public static function editShap($id) {
	if (!$id) {
    return false;
        }
        $database = DatabaseFactory::getFactory()->getConnection();
        $sql = "SELECT * FROM sf_shaps WHERE id = :id LIMIT 1";
        $query = $database->prepare($sql);
        $query->execute(array(':id' => $id));

        if ($query->rowCount() == 1) {
			
			echo json_encode($query->fetch());
            return true;
        }

        return false;
	
}


public static function shapUpdate() {
	

	
	$name = strip_tags(Request::post('name'));
    $id = strip_tags(Request::post('id'));
	

	$database = DatabaseFactory::getFactory()->getConnection();
	$sql = "UPDATE sf_shaps SET name=:name  WHERE id=:id";
	$query = $database->prepare($sql);
	$query->execute(array(':name' => $name, ':id' => $id  ));
	$count =  $query->rowCount();
	if($count==1) {
		echo json_encode(array("status" => TRUE));
        return true;
		
	}
	return false;
	
}
    
    
    

	
}

