1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package Redcore |
4
|
|
|
* @subpackage Api |
5
|
|
|
* |
6
|
|
|
* @copyright Copyright (C) 2008 - 2021 redWEB.dk. All rights reserved. |
7
|
|
|
* @license GNU General Public License version 2 or later, see LICENSE. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
defined('JPATH_BASE') or die; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Api Helper class for overriding default methods |
14
|
|
|
* |
15
|
|
|
* @package Redcore |
16
|
|
|
* @subpackage Api Helper |
17
|
|
|
* @since 1.8 |
18
|
|
|
*/ |
19
|
|
|
class RApiHalHelperSiteContent |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Service for creating content. |
23
|
|
|
* |
24
|
|
|
* @param string $data content |
25
|
|
|
* |
26
|
|
|
* @return boolean True on success. False otherwise. |
27
|
|
|
* @throws Exception |
28
|
|
|
*/ |
29
|
|
|
public function save($data): string |
30
|
|
|
{ |
31
|
|
|
if (version_compare(JVERSION, '3.0', 'lt')) { |
32
|
|
|
JTable::addIncludePath(JPATH_PLATFORM . 'joomla/database/table'); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$data = (object) $data; |
36
|
|
|
|
37
|
|
|
if (isset($data->job_type) && $data->job_type == 'create') |
38
|
|
|
{ |
39
|
|
|
return $this->createContent($data); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if (isset($data->job_type) && $data->job_type == 'edit') |
43
|
|
|
{ |
44
|
|
|
return $this->editContent($data); |
|
|
|
|
45
|
|
|
} |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @throws Exception |
50
|
|
|
*/ |
51
|
|
|
private function createContent($data): string |
52
|
|
|
{ |
53
|
|
|
$article = JTable::getInstance('content'); |
54
|
|
|
$article->title = $data->title; |
|
|
|
|
55
|
|
|
$article->alias = JFilterOutput::stringURLSafe($data->title); |
56
|
|
|
$article->introtext = '<p>'.$data->description.'</p>'; |
57
|
|
|
$article->created = JFactory::getDate()->toSQL();; |
58
|
|
|
$article->created_by_alias = $data->user; |
|
|
|
|
59
|
|
|
$article->state = 1; |
60
|
|
|
$article->access = 1; |
|
|
|
|
61
|
|
|
$article->metadata = '{"page_title":"'.$data->title.'","author":"'.$data->user.'","robots":""}'; |
62
|
|
|
$article->language = '*'; |
|
|
|
|
63
|
|
|
|
64
|
|
|
if (!$article->check()) { |
65
|
|
|
throw new Exception($article->getError()); |
|
|
|
|
66
|
|
|
return false; |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if ($article->store(TRUE)) { |
70
|
|
|
|
71
|
|
|
if (isset($data->image) && $data->image) |
72
|
|
|
{ |
73
|
|
|
$articleId = $article->get('id'); |
74
|
|
|
$imgSrc = $this->checkImages($data->image, $articleId); |
75
|
|
|
$article->set('introtext', '<p>'.$data->description.'</p>' . $imgSrc); |
76
|
|
|
$article->store(); |
77
|
|
|
} |
78
|
|
|
return json_encode($article->getProperties()); |
79
|
|
|
|
80
|
|
|
}else { |
81
|
|
|
throw new Exception($article->getError()); |
|
|
|
|
82
|
|
|
return false; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @throws Exception |
88
|
|
|
*/ |
89
|
|
|
private function editContent($data): string |
90
|
|
|
{ |
91
|
|
|
if (empty($data->article_id)) |
92
|
|
|
{ |
93
|
|
|
return false; |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$article = JTable::getInstance('content'); |
97
|
|
|
$id = $data->article_id; |
98
|
|
|
$imgSrc = ''; |
99
|
|
|
$article->load($id); |
100
|
|
|
$article->set('title', $data->title); |
101
|
|
|
|
102
|
|
|
if (isset($data->image) && $data->image) |
103
|
|
|
{ |
104
|
|
|
$imgSrc = $this->checkImages($data->image, $id); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$article->set('introtext', '<p>'.$data->description.'</p>' . $imgSrc); |
108
|
|
|
|
109
|
|
|
if (!$article->store()) { |
110
|
|
|
throw new Exception($article->getError()); |
|
|
|
|
111
|
|
|
return FALSE; |
|
|
|
|
112
|
|
|
} |
113
|
|
|
; |
114
|
|
|
return json_encode($article->getProperties()); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
|
118
|
|
|
private function checkImages($image, $articleId) |
119
|
|
|
{ |
120
|
|
|
$baseImgs = $image; |
121
|
|
|
$baseImgs = json_decode($baseImgs); |
122
|
|
|
$imgSrc = ''; |
123
|
|
|
|
124
|
|
|
foreach ($baseImgs as $baseImg) |
125
|
|
|
{ |
126
|
|
|
$imgSrc .= $this->processImages($baseImg, $articleId); |
127
|
|
|
} |
128
|
|
|
return $imgSrc; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
private function processImages($imgBase64, $articleId) |
132
|
|
|
{ |
133
|
|
|
$imageInfo = explode(";base64,", $imgBase64); |
134
|
|
|
$imgExt = str_replace('data:image/', '', $imageInfo[0]); |
135
|
|
|
$image = str_replace(' ', '+', $imageInfo[1]); |
136
|
|
|
$md5Name = md5($imgBase64); |
137
|
|
|
$imageName = $md5Name.".".$imgExt; |
138
|
|
|
$path = JPATH_BASE.'/images/aesir/'.$articleId.'/'; |
139
|
|
|
|
140
|
|
|
if (!is_dir($path)) { |
141
|
|
|
mkdir($path,0777,true); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
if (file_put_contents($path . $imageName, base64_decode($image))) |
145
|
|
|
{ |
146
|
|
|
return '<p><img src="images/aesir/'.$articleId.'/'.$imageName.'" /></p>'; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
|