-
Notifications
You must be signed in to change notification settings - Fork 4
/
Food.php
162 lines (125 loc) · 6.11 KB
/
Food.php
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
<?php
namespace IdnoPlugins\Food {
use Idno\Core\Autosave;
class Food extends \Idno\Common\Entity
{
public $mapping = array(
'ate' => 'fa-utensils',
'drank' => 'fa-glass-martini',
'drank-coffee' => 'fa-coffee',
'drank-beer' => 'fa-beer'
);
function getTitle()
{
if (empty($this->title)) return 'Untitled';
return $this->title;
}
function getCategoryIcon()
{
return $this->mapping[$this->category];
}
function getDescription()
{
if (!empty($this->body)) return $this->body;
return '';
}
function getCategory()
{
if (!empty($this->category)) return $this->category;
return '';
}
function getURL()
{
// If we have a URL override, use it
if (!empty($this->url)) {
return $this->url;
}
if (!empty($this->canonical)) {
return $this->canonical;
}
if (!$this->getSlug() && ($this->getID())) {
return \Idno\Core\site()->config()->url . 'food/' . $this->getID() . '/' . $this->getPrettyURLTitle();
} else {
return parent::getURL();
}
}
/**
* Food objects have type 'food'
* @return 'food'
*/
function getActivityStreamsObjectType()
{
return 'food';
}
function saveDataFromInput()
{
if (empty($this->_id)) {
$new = true;
} else {
$new = false;
}
$body = \Idno\Core\site()->currentPage()->getInput('body');
$this->body = $body;
$this->title = \Idno\Core\site()->currentPage()->getInput('title');
$this->category = \Idno\Core\site()->currentPage()->getInput('category');
$access = \Idno\Core\site()->currentPage()->getInput('access');
$this->setAccess($access);
if (empty($this->category)) {
$this->category = 'ate';
}
if ($time = \Idno\Core\site()->currentPage()->getInput('created')) {
if ($time = strtotime($time)) {
$this->created = $time;
}
}
if ($new) {
if (!empty($_FILES['photo']['tmp_name'])) {
if (\Idno\Entities\File::isImage($_FILES['photo']['tmp_name'])) {
// Extract exif data so we can rotate
if (is_callable('exif_read_data') && $_FILES['photo']['type'] == 'image/jpeg') {
try {
if (function_exists('exif_read_data')) {
if ($exif = exif_read_data($_FILES['photo']['tmp_name'])) {
$this->exif = base64_encode(serialize($exif)); // Yes, this is rough, but exif contains binary data that can not be saved in mongo
}
}
} catch (\Exception $e) {
$exif = false;
}
} else {
$exif = false;
}
if ($photo = \Idno\Entities\File::createFromFile($_FILES['photo']['tmp_name'], $_FILES['photo']['name'], $_FILES['photo']['type'], true, true)) {
$this->attachFile($photo);
// Now get some smaller thumbnails, with the option to override sizes
$sizes = \Idno\Core\site()->events()->dispatch('photo/thumbnail/getsizes', new \Idno\Core\Event(array('sizes' => array('large' => 800, 'medium' => 400, 'small' => 200))));
$eventdata = $sizes->data();
foreach ($eventdata['sizes'] as $label => $size) {
$filename = $_FILES['photo']['name'];
if ($thumbnail = \Idno\Entities\File::createThumbnailFromFile($_FILES['photo']['tmp_name'], "{$filename}_{$label}", $size, false)) {
$varname = "thumbnail_{$label}";
$this->$varname = \Idno\Core\site()->config()->url . 'file/' . $thumbnail;
$varname = "thumbnail_{$label}_id";
$this->$varname = substr($thumbnail, 0, strpos($thumbnail, '/'));
}
}
}
} else {
\Idno\Core\site()->session()->addErrorMessage('This doesn\'t seem to be an image ..');
}
}
}
if ($this->save($new)) {
$autosave = new Autosave();
$autosave->clearContext('food');
\Idno\Core\Webmention::pingMentions($this->getURL(), \Idno\Core\site()->template()->parseURLs($this->getTitle() . ' ' . $this->getDescription()));
return true;
}
return false;
}
function deleteData()
{
\Idno\Core\Webmention::pingMentions($this->getURL(), \Idno\Core\site()->template()->parseURLs($this->getTitle() . ' ' . $this->getDescription()));
}
}
}