Skip to content

Commit e3a91cf

Browse files
author
Thuong
committed
#8: joomla CMS update edit feature
1 parent f890bc1 commit e3a91cf

File tree

2 files changed

+100
-13
lines changed

2 files changed

+100
-13
lines changed

extensions/webservices/joomla/site.content.1.0.0.php

Lines changed: 98 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,35 @@ class RApiHalHelperSiteContent
2121
/**
2222
* Service for creating content.
2323
*
24-
* @param string $data content
24+
* @param string $data content
2525
*
2626
* @return boolean True on success. False otherwise.
27+
* @throws Exception
2728
*/
28-
public function save($data)
29+
public function save($data): string
2930
{
3031
if (version_compare(JVERSION, '3.0', 'lt')) {
3132
JTable::addIncludePath(JPATH_PLATFORM . 'joomla/database/table');
3233
}
3334

3435
$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+
{
3553
$article = JTable::getInstance('content');
3654
$article->title = $data->title;
3755
$article->alias = JFilterOutput::stringURLSafe($data->title);
@@ -45,12 +63,88 @@ public function save($data)
4563

4664
if (!$article->check()) {
4765
throw new Exception($article->getError());
48-
return FALSE;
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);
49105
}
50106

51-
if (!$article->store(TRUE)) {
107+
$article->set('introtext', '<p>'.$data->description.'</p>' . $imgSrc);
108+
109+
if (!$article->store()) {
52110
throw new Exception($article->getError());
53111
return FALSE;
54112
}
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+
}
55148
}
56149
}
150+

extensions/webservices/joomla/site.content.1.0.0.xml

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0"?>
22
<apiservice client="site">
3-
<name>content</name>
3+
<name>Aesir - Content Webservice</name>
44
<author/>
55
<copyright/>
66
<description/>
@@ -12,15 +12,8 @@
1212
<operations>
1313
<documentation authorizationNeeded="false" source="auto" url=""/>
1414
<create authorizationNeeded="false" strictFields="false" authorization="" dataMode="helper" optionName="" modelClassName="" modelClassPath="" isAdminClass="false" functionName="save" functionArgs="" validateData="none" validateDataFunction="validate" tableName="">
15-
<fields>
16-
<field name="data" transform="string" defaultValue="" isRequiredField="false" isPrimaryField="false"/>
17-
</fields>
1815
<resources>
19-
<resource displayGroup="_links" displayName="documentation" fieldFormat="{webserviceUrlPath}&amp;format=doc#{rel}" linkTitle="Documentation" linkName="{webserviceName}" hrefLang="" linkTemplated="true" linkRel="curies"/>
20-
<resource displayGroup="_links" displayName="base" fieldFormat="/" linkTitle="Default page"/>
21-
<resource displayGroup="_links" displayName="{webserviceName}:list" fieldFormat="{webserviceUrlPath}"/>
22-
<resource displayGroup="_links" displayName="{webserviceName}:self" fieldFormat="{webserviceUrlPath}&amp;id={id}"/>
23-
<resource displayName="result" fieldFormat="{result}" transform="boolean"/>
16+
<resource displayName="result" fieldFormat="{result}" transform="string"/>
2417
</resources>
2518
</create>
2619
</operations>

0 commit comments

Comments
 (0)