Skip to content

Commit 69e0a39

Browse files
ЛюбимыйЛюбимый
Любимый
authored and
Любимый
committed
First release
0 parents  commit 69e0a39

34 files changed

+1692
-0
lines changed

.htaccess

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
RewriteEngine On
2+
RewriteBase /
3+
4+
#Add trailing slash
5+
RewriteCond %{REQUEST_URI} /+[^\.]+$
6+
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
7+
8+
9+
RewriteCond %{REQUEST_FILENAME} !-f
10+
RewriteCond %{REQUEST_FILENAME} !-d
11+
RewriteRule ^(.*)/$ index.php?q=$1 [L,QSA]

core/app/Application.php

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
namespace Core\App;
3+
4+
use Core\App\Database;
5+
use Core\App\Routing;
6+
7+
class Application{
8+
private $routing;
9+
private $data;
10+
public $pdo;
11+
public $pagetitle='';
12+
13+
public function run(){
14+
15+
$this->pdo=new Database();
16+
$this->pdo->run();
17+
18+
$this->routing=new Routing();
19+
$this->routing->run();
20+
21+
$class = 'Core\Controller\\' . $this->routing->controller;
22+
$controller=new $class();
23+
$controller->app=$this;
24+
$controller->{$this->routing->action}();
25+
26+
}
27+
28+
public function render($template='',$data=Array()){
29+
$wrapper=$this->renderPartial($template,$data);
30+
31+
require(dirname(__FILE__).'/../theme/layout.php');
32+
}
33+
34+
public function renderPartial($template='',$data=Array()){
35+
$this->data=$data;
36+
$filename=dirname(__FILE__).'/../theme/'.$template.'.php';
37+
if (!file_exists($filename)){
38+
echo 'Not found template: '.$template;
39+
exit;
40+
}
41+
ob_start();
42+
require($filename);
43+
$wrapper = ob_get_contents();
44+
ob_end_clean();
45+
return $wrapper;
46+
}
47+
48+
public function isErrorField($fieldname){
49+
if (isset($this->data['error'][$fieldname])){
50+
return $this->data['error'][$fieldname];
51+
}
52+
return false;
53+
}
54+
55+
public function formPost($name,$default_value=''){
56+
if (!isset($_POST[$name])){
57+
return $default_value;
58+
}
59+
return $_POST[$name];
60+
}
61+
62+
public function formGet($name){
63+
if (!isset($_GET[$name])){
64+
return ;
65+
}
66+
return $_GET[$name];
67+
}
68+
69+
public function formAscDesc($field){
70+
if (($this->formGet('field')==$field) && ($this->formGet('sort')=='asc')){
71+
return 'desc';
72+
}else{
73+
return 'asc';
74+
}
75+
}
76+
77+
public function redirect($page){
78+
if ($this->getIsAjaxRequest()){
79+
$response['redirect']="http://{$_SERVER['HTTP_HOST']}/$page";
80+
echo json_encode($response);
81+
exit();
82+
}
83+
header("Location: http://{$_SERVER['HTTP_HOST']}/$page");
84+
85+
}
86+
87+
public function getIsAjaxRequest()
88+
{
89+
return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH']==='XMLHttpRequest';
90+
}
91+
}

core/app/Database.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
namespace Core\App;
3+
4+
class Database{
5+
public $db;
6+
public $prefix;
7+
8+
public function run(){
9+
$this->connect();
10+
}
11+
12+
private function connect(){
13+
include_once dirname(__FILE__).'/../config/config.php';
14+
try {
15+
$this->db = new \PDO('mysql:host='.$db['host'].';dbname='.$db['dbname'], $db['user'], $db['password']);
16+
$this->db->exec("SET NAMES utf8");
17+
$this->prefix=$db['prefix'];
18+
} catch (\PDOException $e) {
19+
echo "Error db!: " . $e->getMessage();
20+
exit();
21+
}
22+
unset($db);
23+
}
24+
}

core/app/Routing.php

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
namespace Core\App;
3+
4+
class Routing{
5+
public $controller;
6+
public $action;
7+
public $suffix='/';
8+
9+
private $request;
10+
11+
public function run(){
12+
$this->init();
13+
}
14+
15+
private function init(){
16+
include_once dirname(__FILE__).'/../config/routing.php';
17+
if (!isset($routing)){
18+
echo 'No "routing" in routing.php!';
19+
exit;
20+
}
21+
if (!isset($default)){
22+
echo 'No "default" routing in routing.php!';
23+
exit;
24+
}
25+
$this->request=$default;
26+
27+
if (isset($_GET['q']) && $_GET['q'] && isset($routing[$_GET['q']])){
28+
$this->request=$_GET['q'];
29+
}elseif (isset($_GET['q']) && ($_GET['q'])){
30+
header("HTTP/1.1 301 Moved Permanently");
31+
header("Location: /");
32+
}
33+
$this->controller=$routing[$this->request]['controller'];
34+
$this->action=$routing[$this->request]['action'];
35+
}
36+
}

core/config/config.php

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
$db['host']='localhost';
4+
$db['dbname']='workers';
5+
$db['user']='workers';
6+
$db['prefix']='w_';
7+
$db['password']='dKb4fP9KJxmt2BKy';

core/config/routing.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
$default='post';
3+
$routing=array(
4+
'post'=>array('controller'=>'Post','action'=>'actionList'),
5+
'post/add'=>array('controller'=>'Post','action'=>'actionAdd'),
6+
'post/edit'=>array('controller'=>'Post','action'=>'actionEdit'),
7+
'staff'=>array('controller'=>'Staff','action'=>'actionList'),
8+
'staff/add'=>array('controller'=>'Staff','action'=>'actionAdd'),
9+
'staff/edit'=>array('controller'=>'Staff','action'=>'actionEdit'),
10+
);

core/controller/Post.php

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
namespace Core\Controller;
3+
4+
class Post{
5+
public $app;
6+
7+
/**
8+
* List posts
9+
*/
10+
public function actionList(){
11+
$this->app->pagetitle='Должности';
12+
$error=array();
13+
14+
if ($this->app->formGet('delete') && $this->app->formGet('id')){
15+
$sth = $this->app->pdo->db->prepare("SELECT COUNT(*) FROM {$this->app->pdo->prefix}employee WHERE post_id=:id");
16+
$sth->bindParam(':id', $this->app->formGet('id'), \PDO::PARAM_INT);
17+
if (!$sth->execute()){
18+
echo 'Select db error!';
19+
exit;
20+
}
21+
if ($sth->fetchColumn()){
22+
$error['message']='Нельзя удалить, у работника стоит должность!';
23+
}else{
24+
$sth = $this->app->pdo->db->prepare("DELETE FROM {$this->app->pdo->prefix}post WHERE id=:id");
25+
$sth->bindParam(':id', $this->app->formGet('id'), \PDO::PARAM_INT);
26+
if (!$sth->execute()){
27+
echo 'delete db error!';
28+
exit;
29+
}
30+
$this->app->redirect('post/');
31+
}
32+
}
33+
34+
$sth = $this->app->pdo->db->prepare("SELECT * FROM {$this->app->pdo->prefix}post");
35+
if (!$sth->execute()){
36+
echo 'Select db error!';
37+
exit;
38+
}
39+
$result=$sth->fetchAll();
40+
$this->app->render('post',array('result'=>$result,'error'=>$error));
41+
}
42+
43+
/**
44+
* Add Post
45+
*/
46+
public function actionAdd(){
47+
$this->app->pagetitle='Добавить должности';
48+
$error=Array();
49+
50+
if (isset($_POST['post_form'])){
51+
$name=$this->app->formPost('name');
52+
$description=$this->app->formPost('description');
53+
if (!$name){
54+
$error['name']='Поле пустое!';
55+
}
56+
57+
if (!count($error))
58+
{
59+
$sth = $this->app->pdo->db->prepare("INSERT INTO {$this->app->pdo->prefix}post (name,description) VALUES (:name,:description)");
60+
$sth->bindParam(':name', $name, \PDO::PARAM_STR, 64);
61+
$sth->bindParam(':description', $description, \PDO::PARAM_STR, 255);
62+
if (!$sth->execute()){
63+
echo 'Insert db error!';
64+
exit;
65+
}
66+
$this->app->redirect('post/');
67+
}
68+
}
69+
70+
$this->app->render('post_form',array('error'=>$error));
71+
}
72+
73+
/**
74+
* Edit Post
75+
*/
76+
public function actionEdit(){
77+
$this->app->pagetitle='Изменить должность';
78+
$error=Array();
79+
80+
if (isset($_POST['post_form'])){
81+
if ($id=$this->app->formPost('id')){
82+
$name=$this->app->formPost('name');
83+
$description=$this->app->formPost('description');
84+
if (!$name){
85+
$error['name']='Поле пустое!';
86+
}
87+
88+
if (!count($error))
89+
{
90+
$sth = $this->app->pdo->db->prepare("UPDATE {$this->app->pdo->prefix}post SET name=:name,description=:description WHERE id=:id");
91+
$sth->bindParam(':name', $name, \PDO::PARAM_STR, 64);
92+
$sth->bindParam(':description', $description, \PDO::PARAM_STR, 255);
93+
$sth->bindParam(':id', $id, \PDO::PARAM_INT);
94+
if (!$sth->execute()){
95+
echo 'Update db error!';
96+
exit;
97+
}
98+
$this->app->redirect('post/');
99+
}
100+
}
101+
}
102+
103+
if (!$id=$this->app->formGet('id')){
104+
$this->app->redirect('post/');
105+
}
106+
107+
//Check
108+
$sth = $this->app->pdo->db->prepare("SELECT * FROM {$this->app->pdo->prefix}post WHERE id=:id");
109+
$sth->bindParam(':id', $id, \PDO::PARAM_INT);
110+
if (!($sth->execute()) or (!$result=$sth->fetch())){
111+
$this->app->redirect('post/');
112+
}
113+
114+
$this->app->render('post_form',array('result'=>$result,'edit'=>1,'error'=>$error));
115+
}
116+
}

0 commit comments

Comments
 (0)