/
Blog.php
151 lines (122 loc) · 4.83 KB
/
Blog.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
<?php
namespace DDD\Service;
use DDD\Service\ServiceBase;
use Library\Constants\Objects;
use Library\Constants\DbTables;
use Library\Upload\Images;
use Library\Utility\Helper;
use FileManager\Constant\DirectoryStructure;
class Blog extends ServiceBase
{
protected $_blogDao = FALSE;
public function getBlogResult($filterParams = array()) {
$dao = $this->getBlogDao();
$blogs = $dao->fetchAll();
return $blogs;
}
public function getBlogById($id){
$dao = $this->getBlogDao();
$blogs = $dao->fetchOne(['id'=>$id]);
return $blogs;
}
public function saveToTemp(Array $file)
{
$image = new Images($file);
if($image->errors){
$result['status'] = 'error';
$result['msg'] = $image->errors;
return $result;
} else {
$result['status'] = 'success';
$result['msg'] = 'Successfully uploaded';
$result['src'] = $image->saveImage(DirectoryStructure::FS_GINOSI_ROOT
. DirectoryStructure::FS_IMAGES_ROOT
. DirectoryStructure::FS_IMAGES_TEMP_PATH);
return $result;
}
}
public function blogSave($data, $id){
$blogDao = $this->getBlogDao();
$data = (array)$data;
$saveData = array(
'content'=> $data['body'],
'en_status'=> 3,
'title'=> $data['title'],
'date'=> ($data['date'] ? date('Y-m-d', strtotime($data['date'])) : ''),
'en_title_status'=>3,
'slug' => str_replace(' ', '-', strtolower(preg_replace('/[^a-zA-Z0-9 -]/','',$data['title'])))
);
$insert_id = $id;
$location_id = 0;
if($id > 0){
$blogDao->save($saveData, array('id'=>(int)$id));
} else {
if(!$data['date'])
$saveData['date'] = date('Y-m-d');
$insert_id = $blogDao->save($saveData);
}
if($data['img_post'] !== '' AND file_exists($data['img_post'])){
$filePathArray = explode('/', $data['img_post']);
$fileInfo[0]['name'] = end($filePathArray);
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$fileInfo[0]['type'] = finfo_file($finfo, $data['img_post']);
finfo_close($finfo);
$fileInfo[0]['tmp_name'] = $data['img_post'];
$fileInfo[0]['error'] = 0;
$fileInfo[0]['size'] = filesize($data['img_post']);
$image = new Images($fileInfo);
$img_size = 300;
$image->resizeToWidth(array($img_size));
$blogImagesPath = DirectoryStructure::FS_GINOSI_ROOT
. DirectoryStructure::FS_IMAGES_ROOT
. DirectoryStructure::FS_IMAGES_BLOG_PATH;
$newImg = str_replace(array('/ginosi/images', '_original', 'jpeg', 'jpg', 'gif'), array('','_300', 'png', 'png', 'png'), $image->saveImage($blogImagesPath.$insert_id.'/'));
if($newImg){
$row = $blogDao->fetchOne(['id'=>$insert_id]);
if($row->getImg() != ''){
if (strstr($row->getImg(), '_300')) {
$imagePathParts = explode('_300', $row->getImg());
} elseif (strstr($row->getImg(), '_orig')) {
$imagePathParts = explode('_orig', $row->getImg());
}
$imagePathMask = '/ginosi/images' . $imagePathParts[0] . '*';
array_map('unlink', glob($imagePathMask));
}
$blogDao->save(array('img'=>$newImg), array('id' => $insert_id));
}
}
return array($insert_id, $location_id);
}
public function removeImage($id)
{
$blogDao = $this->getBlogDao();
$row = $blogDao->fetchOne(['id'=>$id]);
if($row->getImg() != '') {
$currentImg = explode('_', explode('/', $row->getImg())[3])[0];
$removeMask = DirectoryStructure::FS_GINOSI_ROOT
. DirectoryStructure::FS_IMAGES_ROOT
. DirectoryStructure::FS_IMAGES_BLOG_PATH
. $id . '/' . $currentImg . '*';
$removedByMask = array_map( "unlink", glob($removeMask));
if (empty($removedByMask)) {
return FALSE;
}
$blogDao->save(array('img'=>''), array('id' => $id));
}
}
function deleteBlog($id){
Helper::deleteDirectory(DirectoryStructure::FS_GINOSI_ROOT
. DirectoryStructure::FS_IMAGES_ROOT
. DirectoryStructure::FS_IMAGES_BLOG_PATH
. $id);
$blogDao = $this->getBlogDao();
$blogDao->deleteWhere(array('id'=>$id));
return true;
}
private function getBlogDao()
{
if (!$this->_blogDao)
$this->_blogDao = $this->getServiceLocator()->get('dao_blog_blog');
return $this->_blogDao;
}
}