Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
39 changes: 39 additions & 0 deletions data/all_in_one.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
require 'functions.php';
function _return($arr){
echo json_encode($arr);
exit();
}
$act = ((!empty($_POST['act'])) ? (int)$_POST['act'] : 0);
if($act <= 0) _return([false, 'act is not supplied!']);

// var_dump($_POST);

$arr_sbor = array();
if(isset($_POST['add_opt_v2'])){
if(!isset($_POST['add_opt_v2']['opt'])){
_return(['false', 'Неверное переданы параметры!']);
}
$arr_sbor = $_POST['add_opt_v2']['opt'];
}

switch($act):
case 1:
$pl = ((!empty($arr_sbor['pl'])) ? $arr_sbor['pl'] : '' );
if($pl == 'site'){
require 'classes/site.class.php';
$site = new site();
$arr_answer = $site -> sub_func([
// 'pl' => ((!empty($arr_sbor['pl'])) ? $arr_sbor['pl'] : '' ),
'target' => ((!empty($arr_sbor['action'])) ? $arr_sbor['action'] : '' ),
'arr_sbor' => $arr_sbor
]);
}
else $arr_answer = [false, 'unknown pl'];

break;
default: $arr_answer = [false, 'What U want?'];
endswitch;

echo json_encode($arr_answer);
?>
9 changes: 9 additions & 0 deletions data/classes/_test_abstract.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
// namespace test;
abstract class _test_abstract {
abstract protected function getValue();

function printValue(){
print $this -> getValue() . "\n";
}
}
148 changes: 148 additions & 0 deletions data/classes/db.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?php
// Объявление класса
class db{
protected static $dbh = null;
function __construct(){}
private function connect(){
if(!self::$dbh){
$user = 'root';
$pwd = '123';
try{
self::$dbh = new PDO('mysql:dbname=php-course;host=localhost', $user, $pwd);
// self::$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e){
return [
false, $e -> getMessage()
];
}
}
return [
true, 'gut'
];
}

// private function getObj(){
// if(self::$connect === null){
// self::$connect = new self;
// }
// return self::$connect;
// }

// $db = new db();

//$__r = \db::exec_sql([
// 'query' => " select * from products "
//]);
//
//var_dump($__r);

static function get_query($arr_opt){
$get = ((!empty($arr_opt['get'])) ? $arr_opt['get'] : '' );

if($get == 'products'){
$query = " select * from products where 1 ";
}
elseif($get == 'products'){
$query = " select * from prod__pics where 1 ";
}
// else return [false, 'unknown get'];
else $query = '';

return $query;

// return [
// true,
// 'query' => $query
// ];
}

function exec_sql__test(){
$user = 'root';
$pwd = '123';
$dbh = new PDO('mysql:dbname=php-course;host=localhost', $user, $pwd);

$sth = $dbh -> query(" select * from products where 1 ", PDO::FETCH_ASSOC);
$arr_sql = $sth->fetchAll();
var_dump($arr_sql);
}

function exec_sql($arr_opt){
$query = ((!empty($arr_opt['query_get'])) ? $arr_opt['query_get'] : '' );
if(empty($query)) return [false, 'query is empty!'];

$__r = $this -> connect();
if(!$__r[0]) return $__r;

// var_dump($__r);

// $sql =
// <<<SQL
// SELECT ALL
// c1, -- For result indexing
// c1, c2
// FROM (
// VALUES
// ROW('ID-1', 'Value 1'),
// ROW('ID-2', 'Value 2a'),
// ROW('ID-2', 'Value 2b'),
// ROW('ID-3', 'Value 3')
// ) AS t (c1, c2);
// SQL;

// $sth = self::$dbh -> query($query, PDO::FETCH_ASSOC);
$sth = self::$dbh -> query($query, PDO::FETCH_ASSOC);
// $sth = self::$dbh -> query($query);

if(!$sth){
return [false, self::$dbh->errorCode() . " - " . implode(', ', self::$dbh->errorInfo())];
}

// var_dump($sth);

// print_r($sth->fetchAll(PDO::FETCH_UNIQUE));


$arr_sql = $sth->fetchAll();
// var_dump($arr_sql);

// foreach($arr_sql as $row) {
// var_dump($row);
// }


// if(empty($rows)){
// $arr_sql = [];
// }
// else{
//
//// foreach($rows as ){
////
//// }
//
// var_dump($rows);
//
//// while ($row = $sth->fetchObject()) {
//// $data[] = $row;
//// }
//
//
// $arr_sql = [
// [
// 'id' => 1,
// 'name' => 'prod1',
// 'price' => 1500
// ],
// ];
// }


return [
true,
'arr' => $arr_sql
];
}

}

?>
121 changes: 121 additions & 0 deletions data/classes/site.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php
spl_autoload_register(function ($class_name) {
include "$class_name.class.php";
});
class site{
private $dir_root = '';

// limit N,25
public
// $limit = 25, // Отсутп с начала сколько показывать - НАФИК НЕ НУЖЕН
$limit_count = 25; // Сколько показывать

function __construct(){
$this -> dir_root = \get_dir_root();
}
function get_prods($arr_opt = []){
$db = new DB();
$limit = ((!empty($arr_opt['limit'])) ? (int)$arr_opt['limit'] : 0 );
$limit_count = ((!empty($arr_opt['limit_count'])) ? (int)$arr_opt['limit_count'] : 0 );
if($limit > 0 && $limit_count > 0){
// Если есть и начало и конец
$wh = " limit $limit, $limit_count ";
}
elseif($limit > 0){
// начала нет, то значится-с так, начинаем с нуля-с сударь-с
$wh = " limit $limit ";
}
else $wh = '';

$query = " select * from products p where prod_state is null order by prod_name $wh ";

$__r = $db -> exec_sql([
'query_get' => $query
]);
if(!$__r[0]) return $__r;
$arr_prods = [];
// var_dump($__r['arr']);
if(!empty($__r['arr'])){
$arr_ids = [];
foreach($__r['arr'] as $item){
$arr_ids[] = $item['prod_id'];
$arr_prods['products'][$item['prod_id']]['data'] = $item;
if(!isset($arr_prods['products'][$item['prod_id']]['pics'])) $arr_prods['products'][$item['prod_id']]['pics'] = [];
}
$__r = $db -> exec_sql([
'query_get' => " select * from prod__pics pp where pp_prod_id in(" . implode(',', $arr_ids) . ") "
]);
if(!$__r[0]) return $__r;
foreach($__r['arr'] as $item){
$arr_prods['products'][$item['pp_prod_id']]['pics'][] = $item;
}
}
return [
true,
'arr' => $arr_prods,
// 'query' => $query
];
}

function sub_func($arr_opt){
$target = ((!empty($arr_opt['target'])) ? $arr_opt['target'] : '' );

$arr_sbor = ((!empty($arr_opt['arr_sbor'])) ? $arr_opt['arr_sbor'] : [] );
if(!is_array($arr_sbor)) return [false, 'arr_sbor must be array!'];

if($target == 'load_more'){
$limit = ((!empty($arr_sbor['limit'])) ? (int)$arr_sbor['limit'] : 0 );

// var_dump($arr_sbor);

$limit += $this -> limit_count;

$__r = $this -> get_prods([
'limit' => $limit,
'limit_count' => $this -> limit_count
]);

// require '../../vendor/autoload.php';
require $this -> dir_root . '/data/vendor/autoload.php';

// require_once '/path/to/vendor/autoload.php';

$loader = new \Twig\Loader\FilesystemLoader($this -> dir_root . '/site/www/templates');
$twig = new \Twig\Environment($loader, [
// 'cache' => '../../temp/twig_cache',
'debug' => true,
'autoescape' => false,
]);

// $assigns['document'] = [
//// 'title' => 'Список товаров',
//// 'h1' => 'Список товаров',
//// 'place' => 'products',
// 'arr_prods' => $__r['arr']
// ];

$assigns = [
'blush' => 1,
'arr_prods' => $__r['arr']
];

// $tpl = $twig->load('products.tpl');
$tpl = $twig->load('product_line_item.tpl');
// $assigns['document']['content'] = $tpl->render($assigns);

return [
true,
// 'query' => $__r['query'],
'prod_act' => [
'action' => $target,
'limit' => $limit,
'content' => $tpl->render($assigns),
]
];

return [false, 'Under construction'];
}
else return [false, 'unknown target: ' . $target];
}
}
?>
5 changes: 5 additions & 0 deletions data/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require": {
"twig/twig": "^3.0"
}
}
Loading