13
13
use Joomla \CMS \HTML \HTMLHelper ;
14
14
use Joomla \CMS \Toolbar \Toolbar ;
15
15
use Joomla \CMS \Filesystem \Path ;
16
+ use Joomla \CMS \Uri \Uri ;
16
17
use Joomla \CMS \Layout \FileLayout ;
17
18
use Joomla \CMS \Session \Session ;
18
19
use Joomla \CMS \Log \Log ;
19
20
use Joomla \CMS \Language \Text ;
20
21
use Joomla \CMS \Table \Table ;
22
+ use Joomla \CMS \Form \Form ;
21
23
use Joomla \CMS \Filter \OutputFilter ;
22
24
use Joomla \CMS \MVC \Model \BaseDatabaseModel ;
23
25
24
26
\JLoader::register ('FieldsHelper ' , JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php ' );
25
27
26
28
class plgSystemCfi extends CMSPlugin
27
29
{
28
- private $ BOM = "\xEF" . " \xBB" . " \xBF" ; // UTF BOM signature
29
-
30
+ private $ BOM = "\xEF\xBB\xBF" ; // UTF BOM signature
31
+
30
32
private $ _app ;
31
33
private $ _doc ;
32
34
private $ _user ;
@@ -38,23 +40,22 @@ class plgSystemCfi extends CMSPlugin
38
40
public function __construct (&$ subject , $ config )
39
41
{
40
42
parent ::__construct ($ subject , $ config );
41
- $ this ->_app = Factory::getApplication ();
43
+ $ this ->_app = Factory::getApplication (' administrator ' );
42
44
$ 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
+
48
49
$ user = Factory::getUser ();
49
50
$ this ->_user = $ user ->id . ': ' . $ user ->username ;
50
51
51
52
$ this ->_cp = $ this ->params ->get ('cp ' , 'CP1251 ' );
52
53
53
54
$ this ->_fieldPlugins = [
54
- 'imagelist ' => 0 ,
55
+ 'imagelist ' => 0 ,
55
56
'integer ' => 0 ,
56
- 'list ' => 0 ,
57
- 'sql ' => 0 ,
57
+ 'list ' => 0 ,
58
+ 'sql ' => 0 ,
58
59
'usergrouplist ' => 0
59
60
];
60
61
$ plugins = PluginHelper::getPlugin ('fields ' );
@@ -71,6 +72,7 @@ public function __construct(&$subject, $config)
71
72
}
72
73
73
74
BaseDatabaseModel::addIncludePath (JPATH_ADMINISTRATOR . '/components/com_fields/models ' , 'FieldsModel ' );
75
+ BaseDatabaseModel::addIncludePath (JPATH_ADMINISTRATOR . '/components/com_content/models/ ' , 'ContentModel ' );
74
76
}
75
77
76
78
public function onBeforeRender ()
@@ -84,13 +86,13 @@ public function onBeforeRender()
84
86
if (!($ option == 'com_content ' && (in_array ($ view , ['articles ' , 'featured ' , '' ])))) {
85
87
return ;
86
88
}
87
-
89
+
88
90
$ toolbar = new FileLayout ('toolbar ' , Path::clean (JPATH_PLUGINS . '/system/cfi/layouts ' ));
89
91
ToolBar::getInstance ('toolbar ' )->appendButton ('Custom ' , $ toolbar ->render ([]), 'cfi ' );
90
-
92
+
91
93
return true ;
92
94
}
93
-
95
+
94
96
public function onAfterRender ()
95
97
{
96
98
if ($ this ->_doc ->getType () != 'html ' || !$ this ->_app ->isClient ('administrator ' )) {
@@ -102,7 +104,7 @@ public function onAfterRender()
102
104
if (!($ option == 'com_content ' && (in_array ($ view , ['articles ' , 'featured ' , '' ])))) {
103
105
return ;
104
106
}
105
-
107
+
106
108
$ html = $ this ->_app ->getBody ();
107
109
108
110
if (strpos ($ html , '</head> ' ) !== false ) {
@@ -128,14 +130,13 @@ public function onAfterRender()
128
130
$ categories = [];
129
131
}
130
132
131
-
132
133
$ well = new FileLayout ('well ' , Path::clean (JPATH_PLUGINS . '/system/cfi/layouts ' ));
133
134
$ matches = [];
134
135
preg_match ('#id="j-main-container" (\w+)(.*?)>#i ' , $ content , $ matches );
135
136
if ($ matches && $ matches [0 ]) {
136
137
$ wellParams = [
137
- 'cp ' => $ this ->_cp ,
138
- 'categories ' => $ categories ,
138
+ 'cp ' => $ this ->_cp ,
139
+ 'categories ' => $ categories ,
139
140
'showdesc ' => $ this ->params ->get ('showdesc ' , 1 )
140
141
];
141
142
$ content = str_replace ($ matches [0 ], $ matches [0 ] . $ well ->render ($ wellParams ), $ content );
@@ -152,7 +153,7 @@ public function onAjaxCfi()
152
153
Log::addLogger (['textfile ' => 'cfi.php ' , 'text_entry_format ' => "{DATETIME} \t{PRIORITY} \t{MESSAGE} " ], Log::ALL );
153
154
154
155
$ state = $ this ->_app ->input ->get ('cfistate ' , '' );
155
-
156
+
156
157
if (!Session::checkToken ($ state == 'download ' ? 'get ' : 'post ' )) {
157
158
$ data = [
158
159
'result ' => Text::_ ('JINVALID_TOKEN ' ),
@@ -201,32 +202,32 @@ private function _checkFile($file)
201
202
];
202
203
203
204
if (is_array ($ file ) && count ($ file )) {
204
-
205
+
205
206
if ($ file ['error ' ] != 0 ) {
206
207
$ data ['result ' ] = Text::_ ('PLG_CFIfile_ERROR ' );
207
208
Log::add (json_encode ($ data ), Log::ERROR );
208
209
$ this ->_printJson ($ data ['result ' ]);
209
210
}
210
-
211
+
211
212
if (!$ file ['size ' ]) {
212
213
$ data ['result ' ] = Text::_ ('PLG_CFIfile_SIZE ' );
213
214
Log::add (json_encode ($ data ), Log::ERROR );
214
215
$ this ->_printJson ($ data ['result ' ]);
215
216
}
216
-
217
+
217
218
if (pathinfo ($ file ['name ' ], PATHINFO_EXTENSION ) !== 'csv ' ) {
218
219
$ data ['result ' ] = Text::_ ('PLG_CFIfile_TYPE ' );
219
220
Log::add (json_encode ($ data ), Log::ERROR );
220
221
$ this ->_printJson ($ data ['result ' ]);
221
222
}
222
-
223
+
223
224
$ this ->_file = Path::clean (Factory::getConfig ()->get ('tmp_path ' ) . '/cfi_ ' . date ('Y-m-d-H-i-s ' ) . '.csv ' );
224
225
if (!@move_uploaded_file ($ file ['tmp_name ' ], $ this ->_file )) {
225
226
$ data ['result ' ] = Text::_ ('PLG_CFIfile_MOVE ' );
226
227
Log::add (json_encode ($ data ), Log::ERROR );
227
228
$ this ->_printJson ($ data ['result ' ]);
228
229
}
229
-
230
+
230
231
return true ;
231
232
}
232
233
@@ -251,18 +252,18 @@ private function _importData()
251
252
Log::add (json_encode ($ data ), Log::ERROR );
252
253
$ this ->_printJson ($ data ['result ' ]);
253
254
}
254
-
255
+
255
256
// get file content
256
257
$ content = trim (file_get_contents ($ this ->_file ));
257
-
258
+
258
259
// convert to UTF-8
259
260
if ((bool ) $ this ->_app ->input ->get ('cficonvert ' , false )) {
260
261
$ content = mb_convert_encoding ($ content , 'UTF-8 ' , $ this ->_cp );
261
262
}
262
263
263
264
// unset utf-8 bom
264
265
$ content = str_replace ($ this ->BOM , '' , $ content );
265
-
266
+
266
267
// line separator definition
267
268
$ rowDelimiter = "\r\n" ;
268
269
if (false === strpos ($ content , "\r\n" )) {
@@ -304,12 +305,21 @@ private function _importData()
304
305
$ inserts = 0 ;
305
306
$ updates = 0 ;
306
307
$ continues = 0 ;
308
+
307
309
$ 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
+
308
317
set_time_limit (0 );
318
+
309
319
foreach ($ lines as $ strNum => $ str ) {
310
320
// get string in file
311
321
$ fieldsData = str_getcsv ($ str , '; ' );
312
-
322
+
313
323
// check count columns
314
324
if (count ($ fieldsData ) != count ($ columns )) {
315
325
$ errors [$ strNum ] = Text::_ ('PLG_CFI_IMPORT_COLUMN_EXCEPT ' );
@@ -333,43 +343,50 @@ private function _importData()
333
343
$ articleData ['articlelang ' ] = array_key_exists ('articlelang ' , $ articleData ) ? $ articleData ['articlelang ' ] : '* ' ;
334
344
$ articleData ['articleintrotext ' ] = array_key_exists ('articleintrotext ' , $ articleData ) ? $ articleData ['articleintrotext ' ] : '' ;
335
345
$ articleData ['articlefulltext ' ] = array_key_exists ('articlefulltext ' , $ articleData ) ? $ articleData ['articlefulltext ' ] : '' ;
336
-
346
+
337
347
// get article instance
338
- $ article = Table ::getInstance ('content ' );
348
+ $ model = BaseDatabaseModel ::getInstance ('Article ' , ' ContentModel ' );
339
349
340
- if ($ articleData ['articleid ' ]) {
350
+ if ($ articleData ['articleid ' ] > 0 ) {
341
351
// load existing article item
342
- if (!$ article ->load ($ articleData ['articleid ' ])) {
352
+ $ article = $ model ->getItem ($ articleData ['articleid ' ]);
353
+
354
+ if (!$ article ) {
343
355
unset($ article );
344
356
$ errors [$ strNum ] = Text::sprintf ('PLG_CFI_IMPORT_LOAD_ARTICLE ' , $ articleData ['articleid ' ]);
345
357
$ continues ++;
346
358
continue ;
347
359
}
360
+
361
+ $ article = (array )$ article ;
362
+ unset($ article [array_key_first ($ article )]);
363
+ $ article ['tags ' ] = explode (', ' , $ article ['tags ' ]->tags );
364
+
348
365
// 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 ' ];
352
369
} else {
353
370
//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":""} ' ) ;
369
386
}
370
387
371
388
// save article item
372
- if (! $ article -> check () || ! $ article-> store ( $ articleData [ ' articleid ' ] ? false : true ) ) {
389
+ if ($ model -> save ( $ article) === false ) {
373
390
unset($ article );
374
391
$ errors [$ strNum ] = Text::_ ('PLG_CFI_IMPORT_SAVE_ARTICLE ' );
375
392
$ continues ++;
@@ -398,20 +415,20 @@ private function _importData()
398
415
$ fieldValue = json_last_error () === JSON_ERROR_NONE ? $ decode : [$ fieldValue ];
399
416
} elseif (strpos ($ fieldValue , 'array:: ' ) === 0 ) {
400
417
$ 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 )) {
403
420
$ fieldsErrors [] = $ fieldName ;
404
421
}
405
422
}
406
423
}
407
424
if ($ fieldsErrors ) {
408
425
$ errors [$ strNum ] = Text::sprintf ('PLG_CFI_IMPORT_SAVE_FIELDS ' , implode (', ' , $ fieldsErrors ));
409
426
}
410
-
427
+
411
428
// destroy article instance
412
429
unset($ article , $ jsFields );
413
430
}
414
-
431
+
415
432
// show result
416
433
$ data ['result ' ] = Text::sprintf ('PLG_CFI_RESULT ' , $ inserts + $ updates , $ inserts , $ updates ) . ($ errors ? '<br> ' . Text::sprintf ('PLG_CFI_RESULT_ERROR ' , $ continues ) : '' );
417
434
if ($ errors ) {
@@ -510,7 +527,7 @@ private function _exportData()
510
527
$ outItem [] = str_replace (["\n" , "\r" ], '' , $ article ->language );
511
528
$ outItem [] = str_replace (["\n" , "\r" ], '' , $ article ->introtext );
512
529
$ outItem [] = str_replace (["\n" , "\r" ], '' , $ article ->fulltext );
513
-
530
+
514
531
$ jsFields = FieldsHelper::getFields ('com_content.article ' , $ article , true );
515
532
foreach ($ jsFields as $ jsField ) {
516
533
if ($ jsField ->type === 'checkboxes ' || in_array ($ jsField ->type , array_keys ($ this ->_fieldPlugins ))) {
@@ -527,7 +544,7 @@ private function _exportData()
527
544
// save file
528
545
fclose ($ fileHandle );
529
546
unset($ articles , $ jsFields );
530
-
547
+
531
548
// convert
532
549
if ((bool ) $ this ->_app ->input ->get ('cficonvert ' , false )) {
533
550
$ contentIn = file_get_contents ($ this ->_file );
@@ -583,3 +600,13 @@ private function _fileDownload($file)
583
600
}
584
601
}
585
602
}
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