Initialized project.

This commit is contained in:
Alexis Lahouze
2013-01-07 18:42:02 +01:00
commit 30bf84480f
20 changed files with 20073 additions and 0 deletions

BIN
src/html/api/.entry.php.swp Normal file

Binary file not shown.

156
src/html/api/entry.php Normal file
View File

@ -0,0 +1,156 @@
<?php
require_once('rest.inc.php');
class EntryAPI extends RestAPI {
public function __contruct() {
parent::__construct();
}
protected function get_entries() {
$connection=$this->get_db_connection();
if(array_key_exists("month", $this->_request) && array_key_exists("year", $this->_request)) {
$day=$this->_request['year']."-".$this->_request['month']."-01";
} else {
$day=date("Y-m-01");
}
$account=$this->_request['account'];
$statement=$connection->prepare("select sum(value) as futuresold, sum(case when operation_date is not null then value else cast(0 as numeric) end) as pointedsold, sum(case when value_date <= now() then value else cast(0 as numeric) end) as currentsold from entry");
$return=$statement->execute();
if(!$return) {
$this->response($statement->errorInfo()[2], 500);
}
$pageInfos=$statement->fetch(PDO::FETCH_ASSOC);
$statement=$connection->prepare("select id, value_date, operation_date, label, value, account_id, sold, pointedSold from (select *, sum(value) over(order by value_date, operation_date, label desc, id desc) as sold, sum(value) over(partition by operation_date is not null order by value_date, operation_date, label desc, id desc) as pointedSold from entry where account_id=:account order by value_date desc, operation_date desc, label, id) as e where date_trunc('month', e.value_date) = :day ");
$statement->bindParam("day", $day);
$statement->bindParam("account", $account);
$return=$statement->execute();
$data=array(
"pointedSold"=>$pageInfos['pointedsold'],
"futureSold"=>$pageInfos['futuresold'],
"currentSold"=>$pageInfos['currentsold'],
"entries"=>$statement->fetchAll(PDO::FETCH_ASSOC));
if($return) {
$this->response($data);
} else {
$this->response($statement->errorInfo()[2], 500);
}
}
protected function save_entry() {
$entry=$this->_request['entry'];
$connection=$this->get_db_connection();
if($entry['id'] != null) {
$statement=$connection->prepare("update entry set value_date=:value_date, operation_date=:operation_date, label=:label, value=:value, account_id=:account where id=:id");
} else {
$statement=$connection->prepare("insert into entry (value_date, operation_date, label, value, account_id) values (:value_date, :operation_date, :label, :value, :account)");
}
$statement->bindParam("value_date", $entry['value_date']);
$statement->bindParam("operation_date", $entry['operation_date'] == '' ? null : $entry['operation_date']);
$statement->bindParam("label", $entry['label']);
$statement->bindParam("value", $entry['value']);
$statement->bindParam("account", $entry['account']);
$statement->bindParam("id", $entry['id']);
$return=$statement->execute();
if($return) {
$this->response(array("status"=>1, "message" => $entry['id'] . " saved."));
} else {
$this->response($statement->errorInfo()[2], 500);
}
}
protected function remove_entry() {
$entry=$this->_request['entry'];
$connection=$this->get_db_connection();
$statement=$connection->prepare("delete from entry where id=:id");
$statement->bindParam("id", $entry['id']);
$return=$statement->execute();
if($return) {
$this->response(array("status"=>1, "message"=>"Entry #" . $entry['id'] . " removed."));
} else {
$this->response($statement->errorInfo()[2], 500);
}
}
protected function get_accounts() {
$connection=$this->get_db_connection();
$statement=$connection->prepare("select id, name from account order by name");
$return=$statement->execute();
if($return) {
$result=$statement->fetchAll(PDO::FETCH_ASSOC);
$this->response($result);
} else {
$this->response($statement->errorInfo()[2], 500);
}
}
protected function get_years() {
$account=$this->_request['account'];
$connection=$this->get_db_connection();
$statement=$connection->prepare("select distinct extract(year from value_date) as year from entry where account_id = :account order by year desc");
$statement->bindParam("account", $account);
$return=$statement->execute();
if($return) {
$result=[];
foreach($statement->fetchAll(PDO::FETCH_NUM) as $value) {
array_push($result, $value[0]);
}
$this->response($result);
} else {
$this->response($statement->errorInfo()[2], 500);
}
}
protected function get_months() {
$year=$this->_request['year'];
$connection=$this->get_db_connection();
$statement=$connection->prepare("select distinct extract(month from value_date) as month from entry where value_date between date (:year || '-01-01') and date(:year || '-01-01') + interval '1 year' - interval '1 day' order by month desc");
$statement->bindParam("year", $year);
$return=$statement->execute();
if($return) {
$result=[];
foreach($statement->fetchAll(PDO::FETCH_NUM) as $value) {
array_push($result, $value[0]);
}
$this->response($result);
} else {
$this->response($statement->errorInfo()[2], 500);
}
}
}
$entryApi = new EntryAPI();
$entryApi->process_api();
?>

86
src/html/api/rest.inc.php Normal file
View File

@ -0,0 +1,86 @@
<?php
class RestAPI {
private $_content_type="application/json";
protected $_request = array();
private $_code = 200;
public function __construct() {
$this->init_request();
}
private function init_request() {
switch($_SERVER['REQUEST_METHOD']) {
case "POST":
$this->_request = $this->cleanup_request($_POST);
break;
case "GET":
case "DELETE":
$this->_request = $this->cleanup_request($_GET);
break;
case "PUT":
parse_str(file_get_contents("php://input"),$this->_request);
$this->_request = $this->cleanup_request($this->_request);
break;
default:
$this->response('',406);
break;
}
}
private function cleanup_request($data){
$clean_input = array();
if(is_array($data)) {
foreach($data as $k => $v) {
$clean_input[$k] = $this->cleanup_request($v);
}
} else {
if(get_magic_quotes_gpc()) {
$data = trim(stripslashes($data));
}
$data = strip_tags($data);
$clean_input = trim($data);
}
return $clean_input == 'null' ? null : $clean_input;
}
protected function response($data, $http_status=200) {
$this->send_headers($http_status);
if(http_response_code() == 200) {
if(is_array($data)) {
echo json_encode($data);
} else {
echo $data;
}
}
exit;
}
private function send_headers($http_status) {
http_response_code($http_status);
if($http_status == 200) {
header("Content-Type:" . "application/json");
}
}
public function process_api() {
$func = strtolower(trim(str_replace("/","",$_REQUEST['action'])));
if((int)method_exists($this,$func) > 0) {
$this->$func();
} else {
$this->response('',404);
}
}
protected function get_db_connection() {
return new PDO("pgsql:host=localhost;dbname=accountant", "accountant", "accountant");
}
}
?>