-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunit.php
More file actions
169 lines (154 loc) · 4.81 KB
/
unit.php
File metadata and controls
169 lines (154 loc) · 4.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
# вывод отдельной фотографии и комментариев
if (isset($_GET['uri']) and $page['uri'] = $_GET['uri']) {
require_once 'db.php';
require_once 'simpleMySQLi.class.php';
# создание объекта для работы с БД
$sql = new simpleMySQLi($db, pathinfo(__FILE__, PATHINFO_DIRNAME));
# поиск записи по URI
$sql->str = 'select * from apodUnits where link like ' . $sql->varchar('%' . $page['uri']);
$r = $sql->execute() ? $sql->assoc() : false;
$sql->free();
if (!$sql->rows) {
$page['err'] = 'запись не найдена';
goto foo;
}
$page = $r;
} else {
$page['err'] = 'URI не задан или неверен';
}
foo:
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title><?php echo isset($page['err']) ? 'apod test' : $page['title'] ?></title>
<link rel="stylesheet" href="spectre.min.css">
</head>
<body>
<?php
if (isset($page['err'])) {
?>
<div class="container" style="width: 128rem">
<ul class="breadcrumb">
<li class="breadcrumb-item">
<a href="/apod/">Home</a>
</li>
<li class="breadcrumb-item">
error
</li>
</ul>
<div class="columns">
<div class="column col-12">
<p class="toast toast-danger">Ошибка: <?php echo $page['err']; ?></p>
</div>
</div>
</div>
<?php
} else {
?>
<input id="unit" type="hidden" data-id="<?php echo $page['id'] ?>">
<div class="container" style="width: 128rem">
<div class="columns">
<ul class="breadcrumb">
<li class="breadcrumb-item">
<a href="/apod/">Home</a>
</li>
<li class="breadcrumb-item">
<?php echo $page['title'] ?>
</li>
</ul>
<div class="column col-12">
<h4 class="text-bold"><?php echo $page['title'] ?></h4>
<p><img src="<?php echo $page['url'] ?>" class="img-responsive"></p>
<p class="text-justify"><?php echo $page['description'] ?></p>
<p><a href="<?php echo $page['link'] ?>"><?php echo $page['link'] ?></a></p>
</div>
<div class="column col-12">
<div class="form-horizontal">
<div class="form-group">
<div class="col-12">
<input class="form-input" type="text" id="comment" placeholder="введите текст комментария и нажмите enter...">
</div>
</div>
</div>
</div>
<div class="column col-12" id="comments">
</div>
</div>
</div>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript" src="helper.js"></script>
<script>
var settings = { loading : false };
$(function() {
settings.id = $('#unit').data('id');
$('a[href^="http"]').on('click', function() { window.open(this.href); return false });
$('#comment').on('keypress', function(e) {
var code = (e.keyCode ? e.keyCode : e.which);
if (code == 13) actionHandler({'data': {'action': 'post'}})
});
$.ajaxSetup({
type : 'post',
dataType : 'json',
url : 'ajax.php',
cache : false,
timeout : 300000,
beforeSend : function() { settings.loading = true; },
complete : function() { settings.loading = false; },
error : function(xhr, status) { if (status == 'timeout') alert('Превышено время ожидания ответа'); }
});
// вывод комментариев
actionHandler({'data': {'action': 'comments'}});
});
function actionHandler(e) {
if (settings.loading || !settings.id) return;
var post = {};
$.each(e.data, function(key, val) { post[key] = val; });
try { console.log(post) } catch(err) {};
post.id = settings.id;
if (post.action == 'comments') {
var $out = $('#comments'); $out.empty();
$.ajax({
data : post,
success : function(data) {
if (data.ok) {
$.each(data.comments, function() {
$out.append('<p><b>' + this.dtime.toDate().phpDate('d.m.Y H:i:') + '</b> ' + this.data + '</p>');
});
} else {
$out.html('<p class="toast toast-danger">' + data.err + '</p>');
}
}
});
} else if (post.action == 'post') {
var $this = $('#comment');
post.data = $.trim($this.val());
if (post.data.length == 0) {
alert('напишите же что-нибудь!');
$this.focus();
return;
}
$.ajax({
data : post,
success : function(data) {
if (data.ok) {
settings.loading = false;
$this.val('');
actionHandler({'data': {'action': 'comments'}});
} else {
alert(data.err);
}
}
});
}
}
</script>
<?php
}
?>
</body>
</html>