@@ -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+
0 commit comments