1313use Joomla \CMS \HTML \HTMLHelper ;
1414use Joomla \CMS \Toolbar \Toolbar ;
1515use Joomla \CMS \Filesystem \Path ;
16+ use Joomla \CMS \Uri \Uri ;
1617use Joomla \CMS \Layout \FileLayout ;
1718use Joomla \CMS \Session \Session ;
1819use Joomla \CMS \Log \Log ;
1920use Joomla \CMS \Language \Text ;
2021use Joomla \CMS \Table \Table ;
22+ use Joomla \CMS \Form \Form ;
2123use Joomla \CMS \Filter \OutputFilter ;
2224use Joomla \CMS \MVC \Model \BaseDatabaseModel ;
2325
2426\JLoader::register ('FieldsHelper ' , JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php ' );
2527
2628class plgSystemCfi extends CMSPlugin
2729{
28- private $ BOM = "\xEF" . " \xBB" . " \xBF" ; // UTF BOM signature
29-
30+ private $ BOM = "\xEF\xBB\xBF" ; // UTF BOM signature
31+
3032 private $ _app ;
3133 private $ _doc ;
3234 private $ _user ;
@@ -38,23 +40,22 @@ class plgSystemCfi extends CMSPlugin
3840 public function __construct (&$ subject , $ config )
3941 {
4042 parent ::__construct ($ subject , $ config );
41- $ this ->_app = Factory::getApplication ();
43+ $ this ->_app = Factory::getApplication (' administrator ' );
4244 $ this ->_doc = Factory::getDocument ();
43-
44- HTMLHelper::_ ('jquery.framework ' , false , null , false );
45- $ this ->_doc ->addScript (JURI ::root (true ) . '/plugins/system/cfi/assets/cfi.js ' );
46- $ this ->_doc ->addStylesheet (JURI ::root (true ) . '/plugins/system/cfi/assets/cfi.css ' );
47-
45+
46+ $ this ->_doc ->addScript (URI ::root (true ) . '/plugins/system/cfi/assets/cfi.js ' );
47+ $ this ->_doc ->addStylesheet (URI ::root (true ) . '/plugins/system/cfi/assets/cfi.css ' );
48+
4849 $ user = Factory::getUser ();
4950 $ this ->_user = $ user ->id . ': ' . $ user ->username ;
5051
5152 $ this ->_cp = $ this ->params ->get ('cp ' , 'CP1251 ' );
5253
5354 $ this ->_fieldPlugins = [
54- 'imagelist ' => 0 ,
55+ 'imagelist ' => 0 ,
5556 'integer ' => 0 ,
56- 'list ' => 0 ,
57- 'sql ' => 0 ,
57+ 'list ' => 0 ,
58+ 'sql ' => 0 ,
5859 'usergrouplist ' => 0
5960 ];
6061 $ plugins = PluginHelper::getPlugin ('fields ' );
@@ -71,6 +72,7 @@ public function __construct(&$subject, $config)
7172 }
7273
7374 BaseDatabaseModel::addIncludePath (JPATH_ADMINISTRATOR . '/components/com_fields/models ' , 'FieldsModel ' );
75+ BaseDatabaseModel::addIncludePath (JPATH_ADMINISTRATOR . '/components/com_content/models/ ' , 'ContentModel ' );
7476 }
7577
7678 public function onBeforeRender ()
@@ -84,13 +86,13 @@ public function onBeforeRender()
8486 if (!($ option == 'com_content ' && (in_array ($ view , ['articles ' , 'featured ' , '' ])))) {
8587 return ;
8688 }
87-
89+
8890 $ toolbar = new FileLayout ('toolbar ' , Path::clean (JPATH_PLUGINS . '/system/cfi/layouts ' ));
8991 ToolBar::getInstance ('toolbar ' )->appendButton ('Custom ' , $ toolbar ->render ([]), 'cfi ' );
90-
92+
9193 return true ;
9294 }
93-
95+
9496 public function onAfterRender ()
9597 {
9698 if ($ this ->_doc ->getType () != 'html ' || !$ this ->_app ->isClient ('administrator ' )) {
@@ -102,7 +104,7 @@ public function onAfterRender()
102104 if (!($ option == 'com_content ' && (in_array ($ view , ['articles ' , 'featured ' , '' ])))) {
103105 return ;
104106 }
105-
107+
106108 $ html = $ this ->_app ->getBody ();
107109
108110 if (strpos ($ html , '</head> ' ) !== false ) {
@@ -128,14 +130,13 @@ public function onAfterRender()
128130 $ categories = [];
129131 }
130132
131-
132133 $ well = new FileLayout ('well ' , Path::clean (JPATH_PLUGINS . '/system/cfi/layouts ' ));
133134 $ matches = [];
134135 preg_match ('#id="j-main-container" (\w+)(.*?)>#i ' , $ content , $ matches );
135136 if ($ matches && $ matches [0 ]) {
136137 $ wellParams = [
137- 'cp ' => $ this ->_cp ,
138- 'categories ' => $ categories ,
138+ 'cp ' => $ this ->_cp ,
139+ 'categories ' => $ categories ,
139140 'showdesc ' => $ this ->params ->get ('showdesc ' , 1 )
140141 ];
141142 $ content = str_replace ($ matches [0 ], $ matches [0 ] . $ well ->render ($ wellParams ), $ content );
@@ -152,7 +153,7 @@ public function onAjaxCfi()
152153 Log::addLogger (['textfile ' => 'cfi.php ' , 'text_entry_format ' => "{DATETIME} \t{PRIORITY} \t{MESSAGE} " ], Log::ALL );
153154
154155 $ state = $ this ->_app ->input ->get ('cfistate ' , '' );
155-
156+
156157 if (!Session::checkToken ($ state == 'download ' ? 'get ' : 'post ' )) {
157158 $ data = [
158159 'result ' => Text::_ ('JINVALID_TOKEN ' ),
@@ -201,32 +202,32 @@ private function _checkFile($file)
201202 ];
202203
203204 if (is_array ($ file ) && count ($ file )) {
204-
205+
205206 if ($ file ['error ' ] != 0 ) {
206207 $ data ['result ' ] = Text::_ ('PLG_CFIfile_ERROR ' );
207208 Log::add (json_encode ($ data ), Log::ERROR );
208209 $ this ->_printJson ($ data ['result ' ]);
209210 }
210-
211+
211212 if (!$ file ['size ' ]) {
212213 $ data ['result ' ] = Text::_ ('PLG_CFIfile_SIZE ' );
213214 Log::add (json_encode ($ data ), Log::ERROR );
214215 $ this ->_printJson ($ data ['result ' ]);
215216 }
216-
217+
217218 if (pathinfo ($ file ['name ' ], PATHINFO_EXTENSION ) !== 'csv ' ) {
218219 $ data ['result ' ] = Text::_ ('PLG_CFIfile_TYPE ' );
219220 Log::add (json_encode ($ data ), Log::ERROR );
220221 $ this ->_printJson ($ data ['result ' ]);
221222 }
222-
223+
223224 $ this ->_file = Path::clean (Factory::getConfig ()->get ('tmp_path ' ) . '/cfi_ ' . date ('Y-m-d-H-i-s ' ) . '.csv ' );
224225 if (!@move_uploaded_file ($ file ['tmp_name ' ], $ this ->_file )) {
225226 $ data ['result ' ] = Text::_ ('PLG_CFIfile_MOVE ' );
226227 Log::add (json_encode ($ data ), Log::ERROR );
227228 $ this ->_printJson ($ data ['result ' ]);
228229 }
229-
230+
230231 return true ;
231232 }
232233
@@ -251,18 +252,18 @@ private function _importData()
251252 Log::add (json_encode ($ data ), Log::ERROR );
252253 $ this ->_printJson ($ data ['result ' ]);
253254 }
254-
255+
255256 // get file content
256257 $ content = trim (file_get_contents ($ this ->_file ));
257-
258+
258259 // convert to UTF-8
259260 if ((bool ) $ this ->_app ->input ->get ('cficonvert ' , false )) {
260261 $ content = mb_convert_encoding ($ content , 'UTF-8 ' , $ this ->_cp );
261262 }
262263
263264 // unset utf-8 bom
264265 $ content = str_replace ($ this ->BOM , '' , $ content );
265-
266+
266267 // line separator definition
267268 $ rowDelimiter = "\r\n" ;
268269 if (false === strpos ($ content , "\r\n" )) {
@@ -304,12 +305,21 @@ private function _importData()
304305 $ inserts = 0 ;
305306 $ updates = 0 ;
306307 $ continues = 0 ;
308+
307309 $ fieldModel = BaseDatabaseModel::getInstance ('Field ' , 'FieldsModel ' , ['ignore_request ' => true ]);
310+
311+ Table::addIncludePath (JPATH_ADMINISTRATOR . '/components/com_content/tables/ ' );
312+ Form::addFormPath (JPATH_ADMINISTRATOR . '/components/com_content/models/forms ' );
313+ Form::addFormPath (JPATH_ADMINISTRATOR . '/components/com_content/model/form ' );
314+ Form::addFieldPath (JPATH_ADMINISTRATOR . '/components/com_content/models/fields ' );
315+ Form::addFieldPath (JPATH_ADMINISTRATOR . '/components/com_content/model/field ' );
316+
308317 set_time_limit (0 );
318+
309319 foreach ($ lines as $ strNum => $ str ) {
310320 // get string in file
311321 $ fieldsData = str_getcsv ($ str , '; ' );
312-
322+
313323 // check count columns
314324 if (count ($ fieldsData ) != count ($ columns )) {
315325 $ errors [$ strNum ] = Text::_ ('PLG_CFI_IMPORT_COLUMN_EXCEPT ' );
@@ -333,43 +343,50 @@ private function _importData()
333343 $ articleData ['articlelang ' ] = array_key_exists ('articlelang ' , $ articleData ) ? $ articleData ['articlelang ' ] : '* ' ;
334344 $ articleData ['articleintrotext ' ] = array_key_exists ('articleintrotext ' , $ articleData ) ? $ articleData ['articleintrotext ' ] : '' ;
335345 $ articleData ['articlefulltext ' ] = array_key_exists ('articlefulltext ' , $ articleData ) ? $ articleData ['articlefulltext ' ] : '' ;
336-
346+
337347 // get article instance
338- $ article = Table ::getInstance ('content ' );
348+ $ model = BaseDatabaseModel ::getInstance ('Article ' , ' ContentModel ' );
339349
340- if ($ articleData ['articleid ' ]) {
350+ if ($ articleData ['articleid ' ] > 0 ) {
341351 // load existing article item
342- if (!$ article ->load ($ articleData ['articleid ' ])) {
352+ $ article = $ model ->getItem ($ articleData ['articleid ' ]);
353+
354+ if (!$ article ) {
343355 unset($ article );
344356 $ errors [$ strNum ] = Text::sprintf ('PLG_CFI_IMPORT_LOAD_ARTICLE ' , $ articleData ['articleid ' ]);
345357 $ continues ++;
346358 continue ;
347359 }
360+
361+ $ article = (array )$ article ;
362+ unset($ article [array_key_first ($ article )]);
363+ $ article ['tags ' ] = explode (', ' , $ article ['tags ' ]->tags );
364+
348365 // set new data on existing article item
349- $ article-> title = $ articleData ['articletitle ' ];
350- $ article-> introtext = $ articleData ['articleintrotext ' ];
351- $ article-> fulltext = $ articleData ['articlefulltext ' ];
366+ $ article[ ' title ' ] = $ articleData ['articletitle ' ];
367+ $ article[ ' introtext ' ] = $ articleData ['articleintrotext ' ];
368+ $ article[ ' fulltext ' ] = $ articleData ['articlefulltext ' ];
352369 } else {
353370 //set data on new article item
354- $ article-> id = 0 ;
355- $ article-> title = $ articleData ['articletitle ' ];
356- $ article-> alias = OutputFilter::stringURLSafe ($ article ->title );
357- $ article-> introtext = $ articleData ['articleintrotext ' ];
358- $ article-> fulltext = $ articleData ['articlefulltext ' ];
359- $ article-> catid = $ articleData ['articlecat ' ];
360- $ article-> language = $ articleData ['articlelang ' ];
361- $ article-> created = Factory::getDate ()->toSql ();
362- $ article-> created_by = explode (': ' , $ this ->_user )[0 ];
363- $ article-> state = 1 ;
364- $ article-> access = 1 ;
365- $ article-> metadata = '{"robots":"","author":"","rights":"","xreference":""} ' ;
366- $ article-> images = '{"image_intro":"","float_intro":"","image_intro_alt":"","image_intro_caption":"","image_fulltext":"","float_fulltext":"","image_fulltext_alt":"","image_fulltext_caption":""} ' ;
367- $ article-> urls = '{"urla":false,"urlatext":"","targeta":"","urlb":false,"urlbtext":"","targetb":"","urlc":false,"urlctext":"","targetc":""} ' ;
368- $ article-> attribs = '{"article_layout":"","show_title":"","link_titles":"","show_tags":"","show_intro":"","info_block_position":"","info_block_show_title":"","show_category":"","link_category":"","show_parent_category":"","link_parent_category":"","show_associations":"","show_author":"","link_author":"","show_create_date":"","show_modify_date":"","show_publish_date":"","show_item_navigation":"","show_icons":"","show_print_icon":"","show_email_icon":"","show_vote":"","show_hits":"","show_noauth":"","urls_position":"","alternative_readmore":"","article_page_title":"","show_publishing_options":"","show_article_options":"","show_urls_images_backend":"","show_urls_images_frontend":""} ' ;
371+ $ article[ ' id ' ] = 0 ;
372+ $ article[ ' title ' ] = $ articleData ['articletitle ' ];
373+ $ article[ ' alias ' ] = OutputFilter::stringURLSafe ($ article ->title );
374+ $ article[ ' introtext ' ] = $ articleData ['articleintrotext ' ];
375+ $ article[ ' fulltext ' ] = $ articleData ['articlefulltext ' ];
376+ $ article[ ' catid ' ] = $ articleData ['articlecat ' ];
377+ $ article[ ' language ' ] = $ articleData ['articlelang ' ];
378+ $ article[ ' created ' ] = Factory::getDate ()->toSql ();
379+ $ article[ ' created_by ' ] = explode (': ' , $ this ->_user )[0 ];
380+ $ article[ ' state ' ] = 1 ;
381+ $ article[ ' access ' ] = $ this -> _app -> get ( ' access ' , 1 ) ;
382+ $ article[ ' metadata ' ] = json_decode ( '{"robots":"","author":"","rights":"","xreference":""} ' ) ;
383+ $ article[ ' images ' ] = json_decode ( '{"image_intro":"","float_intro":"","image_intro_alt":"","image_intro_caption":"","image_fulltext":"","float_fulltext":"","image_fulltext_alt":"","image_fulltext_caption":""} ' ) ;
384+ $ article[ ' urls ' ] = json_decode ( '{"urla":false,"urlatext":"","targeta":"","urlb":false,"urlbtext":"","targetb":"","urlc":false,"urlctext":"","targetc":""} ' ) ;
385+ $ article[ ' attribs ' ] = json_decode ( '{"article_layout":"","show_title":"","link_titles":"","show_tags":"","show_intro":"","info_block_position":"","info_block_show_title":"","show_category":"","link_category":"","show_parent_category":"","link_parent_category":"","show_associations":"","show_author":"","link_author":"","show_create_date":"","show_modify_date":"","show_publish_date":"","show_item_navigation":"","show_icons":"","show_print_icon":"","show_email_icon":"","show_vote":"","show_hits":"","show_noauth":"","urls_position":"","alternative_readmore":"","article_page_title":"","show_publishing_options":"","show_article_options":"","show_urls_images_backend":"","show_urls_images_frontend":""} ' ) ;
369386 }
370387
371388 // save article item
372- if (! $ article -> check () || ! $ article-> store ( $ articleData [ ' articleid ' ] ? false : true ) ) {
389+ if ($ model -> save ( $ article) === false ) {
373390 unset($ article );
374391 $ errors [$ strNum ] = Text::_ ('PLG_CFI_IMPORT_SAVE_ARTICLE ' );
375392 $ continues ++;
@@ -398,20 +415,20 @@ private function _importData()
398415 $ fieldValue = json_last_error () === JSON_ERROR_NONE ? $ decode : [$ fieldValue ];
399416 } elseif (strpos ($ fieldValue , 'array:: ' ) === 0 ) {
400417 $ fieldValue = json_decode (explode (':: ' , $ fieldValue , 2 )[1 ]);
401- }
402- if (!$ fieldModel ->setFieldValue ($ jsFields [$ fieldName ]->id , $ article-> id , $ fieldValue )) {
418+ }
419+ if (!$ fieldModel ->setFieldValue ($ jsFields [$ fieldName ]->id , $ article[ ' id ' ] , $ fieldValue )) {
403420 $ fieldsErrors [] = $ fieldName ;
404421 }
405422 }
406423 }
407424 if ($ fieldsErrors ) {
408425 $ errors [$ strNum ] = Text::sprintf ('PLG_CFI_IMPORT_SAVE_FIELDS ' , implode (', ' , $ fieldsErrors ));
409426 }
410-
427+
411428 // destroy article instance
412429 unset($ article , $ jsFields );
413430 }
414-
431+
415432 // show result
416433 $ data ['result ' ] = Text::sprintf ('PLG_CFI_RESULT ' , $ inserts + $ updates , $ inserts , $ updates ) . ($ errors ? '<br> ' . Text::sprintf ('PLG_CFI_RESULT_ERROR ' , $ continues ) : '' );
417434 if ($ errors ) {
@@ -510,7 +527,7 @@ private function _exportData()
510527 $ outItem [] = str_replace (["\n" , "\r" ], '' , $ article ->language );
511528 $ outItem [] = str_replace (["\n" , "\r" ], '' , $ article ->introtext );
512529 $ outItem [] = str_replace (["\n" , "\r" ], '' , $ article ->fulltext );
513-
530+
514531 $ jsFields = FieldsHelper::getFields ('com_content.article ' , $ article , true );
515532 foreach ($ jsFields as $ jsField ) {
516533 if ($ jsField ->type === 'checkboxes ' || in_array ($ jsField ->type , array_keys ($ this ->_fieldPlugins ))) {
@@ -527,7 +544,7 @@ private function _exportData()
527544 // save file
528545 fclose ($ fileHandle );
529546 unset($ articles , $ jsFields );
530-
547+
531548 // convert
532549 if ((bool ) $ this ->_app ->input ->get ('cficonvert ' , false )) {
533550 $ contentIn = file_get_contents ($ this ->_file );
@@ -583,3 +600,13 @@ private function _fileDownload($file)
583600 }
584601 }
585602}
603+
604+ if (!function_exists ('array_key_first ' )) {
605+ function array_key_first (array $ array )
606+ {
607+ foreach ($ array as $ key => $ unused ) {
608+ return $ key ;
609+ }
610+ return null ;
611+ }
612+ }
0 commit comments