This commit is contained in:
Krivchikov Dmitry 2014-04-02 15:20:51 +04:00
commit 85c6618681
716 changed files with 540 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
ground/config.php

148
ground/app.php Normal file
View File

@ -0,0 +1,148 @@
<?php
class App
{
private static $url = array();
private static $controller = 'IndexController';
private static $action = 'actionIndex';
private static $basedir = false;
private static $config = array();
private static $urlParams = array();
public static $DB = null;
/**
* Разбивает урл на контроллер, метод и выбирает параметры.
*/
private static function parseUrl()
{
self::$url = explode('/', $_SERVER['REQUEST_URI']);
self::$controller = isset(self::$url[1]) && self::$url[1] ? ucfirst(self::$url[1]) . 'Controller' : self::$controller;
self::$action = isset(self::$url[2]) && self::$url[2] ? 'action' . ucfirst(self::$url[2]) : self::$action;
// Разбираем параметры из урла
if ((count(self::$url) > 3) && (self::$url[3]))
{
for ($index = 3; $index < count(self::$url); $index = $index + 2)
{
$paramName = self::$url[$index];
$paramValue = isset(self::$url[$index + 1]) && self::$url[$index + 1] ? self::$url[$index + 1] : NULL;
self::$urlParams[$paramName] = $paramValue;
}
}
}
/**
* Загружает класс контроллера.
* @param string $controllerName
*/
private static function loadController($controllerName)
{
include_once 'controllers/' . strtolower($controllerName) . '.php';
}
/**
* Основной метод для запуска приложения.
* @param array $config
*/
public static function run($config)
{
ob_start();
self::$config = is_array($config) ? $config : array();
self::parseUrl();
//var_dump(self::$controller, self::$action);die;
if (self::getConfig('DB'))
{
try
{
$confDB = self::getConfig('DB');
self::$DB = new PDO('mysql:host=' . $confDB['host'] . ';dbname=' . $confDB['dbname'].';charset=utf8;', $confDB['user'], $confDB['password']);
foreach (glob(App::getBasedir() . "models/*.php") as $filename)
{
include_once $filename;
}
} catch (PDOException $e)
{
echo $e->getMessage();
}
}
$controller = self::$controller;
$action = self::$action;
if (method_exists($controller, $action))
{
$controller::$action();
} else
{
self::error404();
}
}
public static function error404()
{
header("HTTP/1.0 404 Not Found");
die('404');
}
public static function redirect($url)
{
header("Location: " . $url);
}
/**
* Возвращает директорию фреймворка /full/path/ground/
* @return type
*/
public static function getBasedir()
{
if (!self::$basedir)
self::$basedir = dirname(__FILE__) . '/';
return self::$basedir;
}
/**
* Возвращает значение параметра из конфига
* @param string $paramName
* @return multi
*/
public static function getConfig($paramName)
{
return isset(self::$config[$paramName]) ? self::$config[$paramName] : NULL;
}
/**
* Устанавливает значение в конфиге
* @param string $paramName
* @param multi $paramValue
* @return true
*/
public static function setConfig($paramName, $paramValue)
{
self::$config[$paramName] = $paramValue;
return true;
}
/**
* Возвращает значение параметра, переданного в URL
* @param string $paramName
* @return string
*/
public static function getParam($paramName)
{
return isset(self::$urlParams[$paramName]) ? self::$urlParams[$paramName] : NULL;
}
/**
* Возвращает массив параметров, переданных в URL
* @return array
*/
public static function getParams()
{
return self::$urlParams;
}
}

View File

@ -0,0 +1,45 @@
<?php
abstract class Controller
{
static $layout = 'layout.php';
static function renderPartial($template, $data, $return = false)
{
if (!file_exists(App::getBasedir() . 'views/' . $template))
return 'Template not found';
if ($return)
$oldContentFormBuffer_temp = ob_get_clean();
extract($data);
include App::getBasedir() . 'views/' . $template;
if ($return)
{
$newContentFormBuffer_temp = ob_get_clean();
echo $oldContentFormBuffer_temp;
return $newContentFormBuffer_temp;
} else
{
echo $newContentFormBuffer_temp;
}
}
static function render($template, $data, $return = false)
{
$content = self::renderPartial($template, $data, true);
$result = self::renderPartial(self::$layout, array('content' => $content), true);
if ($return)
{
return $result;
} else
{
echo $result;
}
}
}

63
ground/classes/model.php Normal file
View File

@ -0,0 +1,63 @@
<?php
class Model
{
private $_primaryKey_;
protected $_tableName_;
private $_id_;
private $_vals_ = array();
function __construct()
{
$this->_primaryKey_ = $this->getPk();
}
function __set($name, $value)
{
$this->_vals_[$name] = $value;
}
function __get($name)
{
return isset($this->_vals_[$name]) ? $this->_vals_[$name] : null;
}
function getPk()
{
$res = App::$DB->query("SHOW KEYS FROM " . $this->_tableName_ . " WHERE Key_name = 'PRIMARY'")->fetch(PDO::FETCH_ASSOC);
return $res["Column_name"];
}
function getByPK($id)
{
return App::$DB->query('SELECT * FROM ' . $this->_tableName_ . ' WHERE ' . $this->_primaryKey_ . ' = ' . $id)->fetch(PDO::FETCH_ASSOC);
}
function getAll()
{
$res = App::$DB->query('SELECT * FROM ' . $this->_tableName_);
$ready = array();
while ($row = $res->fetch(PDO::FETCH_ASSOC))
{
$ready[$row[$this->_primaryKey_]] = $row;
}
return $ready;
}
function delete($id)
{
return App::$DB->query('DELETE FROM ' . $this->_tableName_ . ' WHERE ' . $this->_primaryKey_ . ' = ' . $id);
}
function add($values)
{
$request = App::$DB->prepare('INSERT INTO ' . $this->_tableName_ . ' (' . implode(',', array_keys($values)) . ') values (:' . implode(', :', array_keys($values)) . ')');
return $request->execute((array) $values);
}
}

View File

@ -0,0 +1,35 @@
<?php
// Базовый синглтон
abstract class Singleton
{
protected static $_instance;
private function __construct(){
/**
* При этом в функцию можно вписать
* свой код инициализации. Также можно
* использовать деструктор класса.
* Эти функции работают по прежднему,
* только не доступны вне класса
*/
}
private function __clone(){
}
/**
* Статическая функция, которая возвращает
* экземпляр класса или создает новый при
* необходимости
*
* @return SingletonTest
*/
public static function getInstance() {
if (null === self::$_instance) {
self::$_instance = new self();
}
return self::$_instance;
}
}

View File

@ -0,0 +1,12 @@
<?php
$config = array(
'title' => 'Интересные выходные вместе с wikipoints.ru',
'DB' => array(
'host' => 'localhost',
'user' => 'root',
'password' => 'root',
'dbname' => 'poi',
),
);

View File

@ -0,0 +1,64 @@
<?php
class IndexController extends Controller
{
/**
* Действие по умолчанию - вывоим каталог товаров.
*/
static function actionIndex()
{
$category = new Category();
self::render('indexTemplate.php', array(
'category' => $category->getAll(),
));
}
/**
* Проверка передачи переменных в шаблон
*/
static function actionTest()
{
self::render('testTemplate.php', array(
'name' => 'Dmitry',
'fname' => 'Krivchikov',
'phone' => '+7-904-610-6141',
));
}
static function actionPoints()
{
$points = new Point();
$points = $points->getAll();
$categories = new Category();
$categories = $categories->getAll();
foreach ($points as &$point) {
$point['categoryIcon'] = $categories[$point['categoryId']]['icon'];
$point['categoryName'] = $categories[$point['categoryId']]['name'];
}
self::renderPartial('json.php', array(
'data' => $points,
));
}
/**
* Вывод элемента каталога. ID товара берётся из параметра /index/catalog/id/1
*/
static function actionCatalog()
{
$id = (int) App::getParam('id');
if ($id)
{
$goods = new Goods();
self::render('goods.php', $goods->getByPK($id));
} else
{
App::error404();
}
}
}

View File

@ -0,0 +1,21 @@
<?php
class YandexController extends Controller
{
static function actionIndex()
{
$goods = new Goods();
$category = new Category();
$categorySet = $category->getAll();
$goodsSet = $goods->getAll();
self::renderPartial('yandexMarket.php', array(
'categorySet' => $categorySet,
'goodsSet' => $goodsSet,
));
}
}

View File

@ -0,0 +1,13 @@
<?php
class Category extends Model
{
function __construct()
{
$this->_tableName_ = 'categories';
parent::__construct();
}
}

13
ground/models/point.php Normal file
View File

@ -0,0 +1,13 @@
<?php
class Point extends Model
{
function __construct()
{
$this->_tableName_ = 'points';
parent::__construct();
}
}

10
ground/views/goods.php Normal file
View File

@ -0,0 +1,10 @@
Товар:<br/>
<br/>
Артикул: <?=$id?><br/>
Название: <b><?=$name?></b><br/>
Цена: <b><?=$price?></b><br/>
Номер категории: <?=$categoryId?><br/>
Производитель: <?=$vendorName?><br/>
Остаток на складе: <?=$count?><br/>
Описание: <?=$description?><br/>

View File

@ -0,0 +1,6 @@
test:
<ol>
<?php foreach ($category as $val): ?>
<li><a href="/index/catalog/id/<?=$val['id']?>"><img src="<?=$val['icon']?>" /> <?=$val['name']?></a></li>
<?php endforeach; ?>
</ol>

5
ground/views/json.php Normal file
View File

@ -0,0 +1,5 @@
<?php
header('Content-Type: application/json');
echo json_encode($data);

15
ground/views/layout.php Normal file
View File

@ -0,0 +1,15 @@
<html>
<head>
<title>Тестовое задание для Timeweb</title>
</head>
<body>
<h1>Магазин радиотоваров</h1>
<hr/>
<a href="/">Главная</a> |
<a href="/yandex/" target="_blank">Выгрузка в Яндекс.Маркет</a> |
<a href="/index/yandex/" target="_blank">Yandex</a> |
<a href="/index/test/">Контакты</a>
<hr/>
<?= isset($content) && !empty($content) ? $content : 'No content =(' ?>
</body>
</html>

View File

@ -0,0 +1,5 @@
Проверка кирилицы и вывода значений переданных переменных:<br/>
<?=$name?><br/>
<?=$fname?><br/>
<?=$phone?><br/>
<?=App::getConfig('hello')?>

View File

@ -0,0 +1,47 @@
<!DOCTYPE yml_catalog SYSTEM "shops.dtd">
<yml_catalog date="<?= date('Y-m-d H-i') ?>">
<shop>
<name>Test for Timeweb</name>
<company>Dmitry Krivchikov</company>
<url>http://all4dk.blogspot.com/</url>
<currencies>
<currency id="RUR" rate="1" plus="0"/>
</currencies>
<categories>
<?php foreach ($categorySet as $val): ?>
<category id="<?= $val['id'] ?>"><?= $val['name'] ?></category>
<?php endforeach; ?>
</categories>
<local_delivery_cost><?= App::getConfig('localDeliveryPrice') ?></local_delivery_cost>
<offers>
<?php foreach ($goodsSet as $val): ?>
<offer id="<?= $val['id'] ?>" type="<?= $val['vendorName'] ?>" available="<?= (int) $val['count'] > 0 ? 'true' : 'false' ?>">
<url>http://yam.loc/index/catalog/id/<?= $val['id'] ?></url>
<price><?= $val['price'] ?></price>
<currencyId>RUR</currencyId>
<categoryId type="Own"><?= $val['categoryId'] ?></categoryId>
<delivery>true</delivery>
<local_delivery_cost><?= App::getConfig('localDeliveryPrice') ?></local_delivery_cost>
<typePrefix><?= $categorySet[$val['categoryId']]['name'] ?></typePrefix>
<vendor><?= $val['vendorName'] ?></vendor>
<vendorCode>---</vendorCode>
<model><?= $val['name'] ?></model>
<description><?= $val['description'] ?></description>
</offer>
<?php endforeach; ?>
</offers>
</shop>
</yml_catalog>

9
public/.htaccess Normal file
View File

@ -0,0 +1,9 @@
AddDefaultCharset utf-8
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L,QSA]

BIN
public/favicon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.8 KiB

BIN
public/ico/2hand.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

BIN
public/ico/360degrees.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 724 B

BIN
public/ico/abduction.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

BIN
public/ico/aboriginal.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 805 B

BIN
public/ico/accesdenied.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

BIN
public/ico/acupuncture.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

BIN
public/ico/aed-2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 786 B

BIN
public/ico/agritourism.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

BIN
public/ico/air_fixwing.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

BIN
public/ico/airport.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 862 B

BIN
public/ico/airshow-2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 745 B

BIN
public/ico/algae.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

BIN
public/ico/alien.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

BIN
public/ico/alligator.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 677 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

BIN
public/ico/amphitheater.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

BIN
public/ico/anchorpier.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

BIN
public/ico/anniversary.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 976 B

BIN
public/ico/ant-export.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

BIN
public/ico/anthropo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 583 B

BIN
public/ico/apartment-3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 839 B

BIN
public/ico/apple.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

BIN
public/ico/aquarium.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

BIN
public/ico/arch.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 803 B

BIN
public/ico/archery.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

BIN
public/ico/army.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

BIN
public/ico/art-museum-2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 720 B

BIN
public/ico/artgallery.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 837 B

BIN
public/ico/atm-2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 718 B

BIN
public/ico/atv.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

BIN
public/ico/audio.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

BIN
public/ico/avalanche1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

BIN
public/ico/award.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

BIN
public/ico/badminton-2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

BIN
public/ico/bags.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

BIN
public/ico/bank.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

BIN
public/ico/bar.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 908 B

BIN
public/ico/bar_coktail.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

BIN
public/ico/bar_juice.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

BIN
public/ico/barbecue.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1023 B

BIN
public/ico/barber.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

BIN
public/ico/barrier.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 677 B

BIN
public/ico/baseball.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

BIN
public/ico/basketball.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 979 B

BIN
public/ico/bats.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

BIN
public/ico/battlefield.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

BIN
public/ico/battleship-3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 931 B

BIN
public/ico/beach.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

BIN
public/ico/beautysalon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

BIN
public/ico/beergarden.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

BIN
public/ico/bicycle_shop.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

BIN
public/ico/bigcity.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 958 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

BIN
public/ico/bike_rising.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

BIN
public/ico/billiard-2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

BIN
public/ico/binoculars.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 742 B

BIN
public/ico/birds-2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

BIN
public/ico/blast.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

BIN
public/ico/boardercross.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

BIN
public/ico/boat.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

BIN
public/ico/boatcrane.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 840 B

BIN
public/ico/bobsleigh.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

BIN
public/ico/bollie.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

BIN
public/ico/bomb.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

BIN
public/ico/bomber-2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

BIN
public/ico/bouddha.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

BIN
public/ico/bowling.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

BIN
public/ico/boxing.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

BIN
public/ico/bread.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Some files were not shown because too many files have changed in this diff Show More