forked from joomla/joomla-cms
-
Notifications
You must be signed in to change notification settings - Fork 1
/
component.php
1343 lines (1166 loc) · 36.2 KB
/
component.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @package Joomla.Libraries
* @subpackage Installer
*
* @copyright Copyright (C) 2005 - 2016 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
defined('JPATH_PLATFORM') or die;
jimport('joomla.filesystem.folder');
/**
* Component installer
*
* @since 3.1
*/
class JInstallerAdapterComponent extends JInstallerAdapter
{
/**
* The list of current files fo the Joomla! CMS administrator that are installed and is read
* from the manifest on disk in the update area to handle doing a diff
* and deleting files that are in the old files list and not in the new
* files list.
*
* @var array
* @since 3.1
* */
protected $oldAdminFiles = null;
/**
* The list of current files that are installed and is read
* from the manifest on disk in the update area to handle doing a diff
* and deleting files that are in the old files list and not in the new
* files list.
*
* @var array
* @since 3.1
* */
protected $oldFiles = null;
/**
* A path to the PHP file that the scriptfile declaration in
* the manifest refers to.
*
* @var string
* @since 3.1
* */
protected $manifest_script = null;
/**
* For legacy installations this is a path to the PHP file that the scriptfile declaration in the
* manifest refers to.
*
* @var string
* @since 3.1
* */
protected $install_script = null;
/**
* Method to check if the extension is present in the filesystem
*
* @return boolean
*
* @since 3.4
* @throws RuntimeException
*/
protected function checkExtensionInFilesystem()
{
/*
* If the component site or admin directory already exists, then we will assume that the component is already
* installed or another component is using that directory.
*/
if (file_exists($this->parent->getPath('extension_site')) || file_exists($this->parent->getPath('extension_administrator')))
{
// Look for an update function or update tag
$updateElement = $this->getManifest()->update;
// Upgrade manually set or update function available or update tag detected
if ($this->parent->isUpgrade() || ($this->parent->manifestClass && method_exists($this->parent->manifestClass, 'update'))
|| $updateElement)
{
// If there is a matching extension mark this as an update
$this->setRoute('update');
}
elseif (!$this->parent->isOverwrite())
{
// We didn't have overwrite set, find an update function or find an update tag so lets call it safe
if (file_exists($this->parent->getPath('extension_site')))
{
// If the site exists say so.
throw new RuntimeException(
JText::sprintf(
'JLIB_INSTALLER_ERROR_COMP_INSTALL_DIR_SITE',
$this->parent->getPath('extension_site')
)
);
}
// If the admin exists say so
throw new RuntimeException(
JText::sprintf(
'JLIB_INSTALLER_ERROR_COMP_INSTALL_DIR_ADMIN',
$this->parent->getPath('extension_administrator')
)
);
}
}
return false;
}
/**
* Method to copy the extension's base files from the <files> tag(s) and the manifest file
*
* @return void
*
* @since 3.4
* @throws RuntimeException
*/
protected function copyBaseFiles()
{
// Copy site files
if ($this->getManifest()->files)
{
if ($this->route == 'update')
{
$result = $this->parent->parseFiles($this->getManifest()->files, 0, $this->oldFiles);
}
else
{
$result = $this->parent->parseFiles($this->getManifest()->files);
}
if ($result === false)
{
throw new RuntimeException(
JText::sprintf(
'JLIB_INSTALLER_ABORT_COMP_FAIL_SITE_FILES',
JText::_('JLIB_INSTALLER_' . strtoupper($this->route))
)
);
}
}
// Copy admin files
if ($this->getManifest()->administration->files)
{
if ($this->route == 'update')
{
$result = $this->parent->parseFiles($this->getManifest()->administration->files, 1, $this->oldAdminFiles);
}
else
{
$result = $this->parent->parseFiles($this->getManifest()->administration->files, 1);
}
if ($result === false)
{
throw new RuntimeException(
JText::sprintf(
'JLIB_INSTALLER_ABORT_COMP_FAIL_ADMIN_FILES',
JText::_('JLIB_INSTALLER_' . strtoupper($this->route))
)
);
}
}
// If there is a manifest script, let's copy it.
if ($this->manifest_script)
{
$path['src'] = $this->parent->getPath('source') . '/' . $this->manifest_script;
$path['dest'] = $this->parent->getPath('extension_administrator') . '/' . $this->manifest_script;
if (!file_exists($path['dest']) || $this->parent->isOverwrite())
{
if (!$this->parent->copyFiles(array($path)))
{
throw new RuntimeException(
JText::sprintf(
'JLIB_INSTALLER_ABORT_COMP_COPY_MANIFEST',
JText::_('JLIB_INSTALLER_' . strtoupper($this->route))
)
);
}
}
}
}
/**
* Method to create the extension root path if necessary
*
* @return void
*
* @since 3.4
* @throws RuntimeException
*/
protected function createExtensionRoot()
{
// If the component directory does not exist, let's create it
$created = false;
if (!file_exists($this->parent->getPath('extension_site')))
{
if (!$created = JFolder::create($this->parent->getPath('extension_site')))
{
throw new RuntimeException(
JText::sprintf(
'JLIB_INSTALLER_ERROR_COMP_FAILED_TO_CREATE_DIRECTORY',
JText::_('JLIB_INSTALLER_' . strtoupper($this->route)),
$this->parent->getPath('extension_site')
)
);
}
}
/*
* Since we created the component directory and we will want to remove it if we have to roll back
* the installation, let's add it to the installation step stack
*/
if ($created)
{
$this->parent->pushStep(
array(
'type' => 'folder',
'path' => $this->parent->getPath('extension_site')
)
);
}
// If the component admin directory does not exist, let's create it
$created = false;
if (!file_exists($this->parent->getPath('extension_administrator')))
{
if (!$created = JFolder::create($this->parent->getPath('extension_administrator')))
{
throw new RuntimeException(
JText::sprintf(
'JLIB_INSTALLER_ERROR_COMP_FAILED_TO_CREATE_DIRECTORY',
JText::_('JLIB_INSTALLER_' . strtoupper($this->route)),
$this->parent->getPath('extension_site')
)
);
}
}
/*
* Since we created the component admin directory and we will want to remove it if we have to roll
* back the installation, let's add it to the installation step stack
*/
if ($created)
{
$this->parent->pushStep(
array(
'type' => 'folder',
'path' => $this->parent->getPath('extension_administrator')
)
);
}
}
/**
* Method to finalise the installation processing
*
* @return void
*
* @since 3.4
* @throws RuntimeException
*/
protected function finaliseInstall()
{
/** @var JTableUpdate $update */
$update = JTable::getInstance('update');
// Clobber any possible pending updates
$uid = $update->find(
array(
'element' => $this->element,
'type' => $this->extension->type,
'client_id' => 1
)
);
if ($uid)
{
$update->delete($uid);
}
// We will copy the manifest file to its appropriate place.
if ($this->route != 'discover_install')
{
if (!$this->parent->copyManifest())
{
// Install failed, roll back changes
throw new RuntimeException(
JText::sprintf(
'JLIB_INSTALLER_ABORT_COMP_COPY_SETUP',
JText::_('JLIB_INSTALLER_' . strtoupper($this->route))
)
);
}
}
// Time to build the admin menus
if (!$this->_buildAdminMenus($this->extension->extension_id))
{
JLog::add(JText::_('JLIB_INSTALLER_ABORT_COMP_BUILDADMINMENUS_FAILED'), JLog::WARNING, 'jerror');
}
// Make sure that menu items pointing to the component have correct component id assigned to them.
// Prevents message "Component 'com_extension' does not exist." after uninstalling / re-installing component.
if (!$this->_updateSiteMenus($this->extension->extension_id))
{
JLog::add(JText::_('JLIB_INSTALLER_ABORT_COMP_UPDATESITEMENUS_FAILED'), JLog::WARNING, 'jerror');
}
/** @var JTableAsset $asset */
$asset = JTable::getInstance('Asset');
// Check if an asset already exists for this extension and create it if not
if (!$asset->loadByName($this->extension->element))
{
// Register the component container just under root in the assets table.
$asset->name = $this->extension->element;
$asset->parent_id = 1;
$asset->rules = '{}';
$asset->title = $this->extension->name;
$asset->setLocation(1, 'last-child');
if (!$asset->store())
{
// Install failed, roll back changes
throw new RuntimeException(
JText::sprintf(
'JLIB_INSTALLER_ABORT_ROLLBACK',
JText::_('JLIB_INSTALLER_' . strtoupper($this->route)),
$this->extension->getError()
)
);
}
}
}
/**
* Get the filtered extension element from the manifest
*
* @param string $element Optional element name to be converted
*
* @return string The filtered element
*
* @since 3.4
*/
public function getElement($element = null)
{
$element = parent::getElement($element);
if (substr($element, 0, 4) != 'com_')
{
$element = 'com_' . $element;
}
return $element;
}
/**
* Custom loadLanguage method
*
* @param string $path The path language files are on.
*
* @return void
*
* @since 3.1
*/
public function loadLanguage($path = null)
{
$source = $this->parent->getPath('source');
$client = $this->parent->extension->client_id ? JPATH_ADMINISTRATOR : JPATH_SITE;
if (!$source)
{
$this->parent->setPath('source', $client . '/components/' . $this->parent->extension->element);
}
$extension = $this->getElement();
$source = $path ? $path : $client . '/components/' . $extension;
if ($this->getManifest()->administration->files)
{
$element = $this->getManifest()->administration->files;
}
elseif ($this->getManifest()->files)
{
$element = $this->getManifest()->files;
}
else
{
$element = null;
}
if ($element)
{
$folder = (string) $element->attributes()->folder;
if ($folder && file_exists($path . '/' . $folder))
{
$source = $path . '/' . $folder;
}
}
$this->doLoadLanguage($extension, $source);
}
/**
* Method to parse optional tags in the manifest
*
* @return void
*
* @since 3.4
*/
protected function parseOptionalTags()
{
// Parse optional tags
$this->parent->parseMedia($this->getManifest()->media);
$this->parent->parseLanguages($this->getManifest()->languages);
$this->parent->parseLanguages($this->getManifest()->administration->languages, 1);
}
/**
* Prepares the adapter for a discover_install task
*
* @return void
*
* @since 3.4
* @throws RuntimeException
*/
public function prepareDiscoverInstall()
{
// Need to find to find where the XML file is since we don't store this normally
$client = JApplicationHelper::getClientInfo($this->extension->client_id);
$short_element = str_replace('com_', '', $this->extension->element);
$manifestPath = $client->path . '/components/' . $this->extension->element . '/' . $short_element . '.xml';
$this->parent->manifest = $this->parent->isManifest($manifestPath);
$this->parent->setPath('manifest', $manifestPath);
$this->parent->setPath('source', $client->path . '/components/' . $this->extension->element);
$this->parent->setPath('extension_root', $this->parent->getPath('source'));
$this->setManifest($this->parent->getManifest());
$manifest_details = JInstaller::parseXMLInstallFile($this->parent->getPath('manifest'));
$this->extension->manifest_cache = json_encode($manifest_details);
$this->extension->state = 0;
$this->extension->name = $manifest_details['name'];
$this->extension->enabled = 1;
$this->extension->params = $this->parent->getParams();
$stored = false;
try
{
$this->extension->store();
$stored = true;
}
catch (RuntimeException $e)
{
// Try to delete existing failed records before retrying
$db = $this->db;
$query = $db->getQuery(true)
->select($db->qn('extension_id'))
->from($db->qn('#__extensions'))
->where($db->qn('name') . ' = ' . $db->q($this->extension->name))
->where($db->qn('type') . ' = ' . $db->q($this->extension->type))
->where($db->qn('element') . ' = ' . $db->q($this->extension->element));
$db->setQuery($query);
$extension_ids = $db->loadColumn();
if (!empty($extension_ids))
{
foreach ($extension_ids as $eid)
{
// Remove leftover admin menus for this extension ID
$this->_removeAdminMenus($eid);
// Remove the extension record itself
/** @var JTableExtension $extensionTable */
$extensionTable = JTable::getInstance('extension');
$extensionTable->delete($eid);
}
}
}
if (!$stored)
{
try
{
$this->extension->store();
}
catch (RuntimeException $e)
{
throw new RuntimeException(JText::_('JLIB_INSTALLER_ERROR_COMP_DISCOVER_STORE_DETAILS'), $e->getCode(), $e);
}
}
}
/**
* Method to do any prechecks and setup the install paths for the extension
*
* @return void
*
* @since 3.4
* @throws RuntimeException
*/
protected function setupInstallPaths()
{
// Set the installation target paths
$this->parent->setPath('extension_site', JPath::clean(JPATH_SITE . '/components/' . $this->element));
$this->parent->setPath('extension_administrator', JPath::clean(JPATH_ADMINISTRATOR . '/components/' . $this->element));
// Copy the admin path as it's used as a common base
$this->parent->setPath('extension_root', $this->parent->getPath('extension_administrator'));
// Make sure that we have an admin element
if (!$this->getManifest()->administration)
{
throw new RuntimeException(JText::_('JLIB_INSTALLER_ERROR_COMP_INSTALL_ADMIN_ELEMENT'));
}
}
/**
* Method to setup the update routine for the adapter
*
* @return void
*
* @since 3.4
*/
protected function setupUpdates()
{
// Hunt for the original XML file
$old_manifest = null;
// Use a temporary instance due to side effects; start in the administrator first
$tmpInstaller = new JInstaller;
$tmpInstaller->setPath('source', $this->parent->getPath('extension_administrator'));
if (!$tmpInstaller->findManifest())
{
// Then the site
$tmpInstaller->setPath('source', $this->parent->getPath('extension_site'));
if ($tmpInstaller->findManifest())
{
$old_manifest = $tmpInstaller->getManifest();
}
}
else
{
$old_manifest = $tmpInstaller->getManifest();
}
if ($old_manifest)
{
$this->oldAdminFiles = $old_manifest->administration->files;
$this->oldFiles = $old_manifest->files;
}
}
/**
* Method to store the extension to the database
*
* @param bool $deleteExisting Should I try to delete existing records of the same component?
*
* @return void
*
* @since 3.4
* @throws RuntimeException
*/
protected function storeExtension($deleteExisting = false)
{
// The extension is stored during prepareDiscoverInstall for discover installs
if ($this->route == 'discover_install')
{
return;
}
// Add or update an entry to the extension table
$this->extension->name = $this->name;
$this->extension->type = 'component';
$this->extension->element = $this->element;
// If we are told to delete existing extension entries then do so.
if ($deleteExisting)
{
$db = $this->parent->getDbo();
$query = $db->getQuery(true)
->select($db->qn('extension_id'))
->from($db->qn('#__extensions'))
->where($db->qn('name') . ' = ' . $db->q($this->extension->name))
->where($db->qn('type') . ' = ' . $db->q($this->extension->type))
->where($db->qn('element') . ' = ' . $db->q($this->extension->element));
$db->setQuery($query);
$extension_ids = $db->loadColumn();
if (!empty($extension_ids))
{
foreach ($extension_ids as $eid)
{
// Remove leftover admin menus for this extension ID
$this->_removeAdminMenus($eid);
// Remove the extension record itself
/** @var JTableExtension $extensionTable */
$extensionTable = JTable::getInstance('extension');
$extensionTable->delete($eid);
}
}
}
// If there is not already a row, generate a heap of defaults
if (!$this->currentExtensionId)
{
$this->extension->folder = '';
$this->extension->enabled = 1;
$this->extension->protected = 0;
$this->extension->access = 0;
$this->extension->client_id = 1;
$this->extension->params = $this->parent->getParams();
$this->extension->custom_data = '';
$this->extension->system_data = '';
}
$this->extension->manifest_cache = $this->parent->generateManifestCache();
$couldStore = $this->extension->store();
if (!$couldStore && $deleteExisting)
{
// Install failed, roll back changes
throw new RuntimeException(
JText::sprintf(
'JLIB_INSTALLER_ABORT_COMP_INSTALL_ROLLBACK',
$this->extension->getError()
)
);
}
if (!$couldStore && !$deleteExisting)
{
// Maybe we have a failed installation (e.g. timeout). Let's retry after deleting old records.
$this->storeExtension(true);
}
}
/**
* Custom uninstall method for components
*
* @param integer $id The unique extension id of the component to uninstall
*
* @return mixed Return value for uninstall method in component uninstall file
*
* @since 3.1
*/
public function uninstall($id)
{
$db = $this->db;
$retval = true;
// First order of business will be to load the component object table from the database.
// This should give us the necessary information to proceed.
if (!$this->extension->load((int) $id))
{
JLog::add(JText::_('JLIB_INSTALLER_ERROR_COMP_UNINSTALL_ERRORUNKOWNEXTENSION'), JLog::WARNING, 'jerror');
return false;
}
// Is the component we are trying to uninstall a core one?
// Because that is not a good idea...
if ($this->extension->protected)
{
JLog::add(JText::_('JLIB_INSTALLER_ERROR_COMP_UNINSTALL_WARNCORECOMPONENT'), JLog::WARNING, 'jerror');
return false;
}
// Get the admin and site paths for the component
$this->parent->setPath('extension_administrator', JPath::clean(JPATH_ADMINISTRATOR . '/components/' . $this->extension->element));
$this->parent->setPath('extension_site', JPath::clean(JPATH_SITE . '/components/' . $this->extension->element));
// Copy the admin path as it's used as a common base
$this->parent->setPath('extension_root', $this->parent->getPath('extension_administrator'));
/**
* ---------------------------------------------------------------------------------------------
* Manifest Document Setup Section
* ---------------------------------------------------------------------------------------------
*/
// Find and load the XML install file for the component
$this->parent->setPath('source', $this->parent->getPath('extension_administrator'));
// Get the package manifest object
// We do findManifest to avoid problem when uninstalling a list of extension: getManifest cache its manifest file
$this->parent->findManifest();
$this->setManifest($this->parent->getManifest());
if (!$this->getManifest())
{
// Make sure we delete the folders if no manifest exists
JFolder::delete($this->parent->getPath('extension_administrator'));
JFolder::delete($this->parent->getPath('extension_site'));
// Remove the menu
$this->_removeAdminMenus($this->extension->extension_id);
// Raise a warning
JLog::add(JText::_('JLIB_INSTALLER_ERROR_COMP_UNINSTALL_ERRORREMOVEMANUALLY'), JLog::WARNING, 'jerror');
// Return
return false;
}
// Set the extensions name
$this->set('name', $this->getName());
$this->set('element', $this->getElement());
// Attempt to load the admin language file; might have uninstall strings
$this->loadLanguage(JPATH_ADMINISTRATOR . '/components/' . $this->element);
/**
* ---------------------------------------------------------------------------------------------
* Installer Trigger Loading and Uninstall
* ---------------------------------------------------------------------------------------------
*/
$this->setupScriptfile();
try
{
$this->triggerManifestScript('uninstall');
}
catch (RuntimeException $e)
{
// Ignore errors for now
}
/**
* ---------------------------------------------------------------------------------------------
* Database Processing Section
* ---------------------------------------------------------------------------------------------
*/
// Let's run the uninstall queries for the component
try
{
$this->parseQueries();
}
catch (RuntimeException $e)
{
JLog::add($e->getMessage(), JLog::WARNING, 'jerror');
$retval = false;
}
$this->_removeAdminMenus($this->extension->extension_id);
/**
* ---------------------------------------------------------------------------------------------
* Filesystem Processing Section
* ---------------------------------------------------------------------------------------------
*/
// Let's remove those language files and media in the JROOT/images/ folder that are
// associated with the component we are uninstalling
$this->parent->removeFiles($this->getManifest()->media);
$this->parent->removeFiles($this->getManifest()->languages);
$this->parent->removeFiles($this->getManifest()->administration->languages, 1);
// Remove the schema version
$query = $db->getQuery(true)
->delete('#__schemas')
->where('extension_id = ' . $id);
$db->setQuery($query);
$db->execute();
// Remove the component container in the assets table.
$asset = JTable::getInstance('Asset');
if ($asset->loadByName($this->element))
{
$asset->delete();
}
// Remove categories for this component
$query->clear()
->delete('#__categories')
->where('extension=' . $db->quote($this->element), 'OR')
->where('extension LIKE ' . $db->quote($this->element . '.%'));
$db->setQuery($query);
$db->execute();
// Clobber any possible pending updates
$update = JTable::getInstance('update');
$uid = $update->find(
array(
'element' => $this->extension->element,
'type' => 'component',
'client_id' => 1,
'folder' => ''
)
);
if ($uid)
{
$update->delete($uid);
}
// Now we need to delete the installation directories. This is the final step in uninstalling the component.
if (trim($this->extension->element))
{
// Delete the component site directory
if (is_dir($this->parent->getPath('extension_site')))
{
if (!JFolder::delete($this->parent->getPath('extension_site')))
{
JLog::add(JText::_('JLIB_INSTALLER_ERROR_COMP_UNINSTALL_FAILED_REMOVE_DIRECTORY_SITE'), JLog::WARNING, 'jerror');
$retval = false;
}
}
// Delete the component admin directory
if (is_dir($this->parent->getPath('extension_administrator')))
{
if (!JFolder::delete($this->parent->getPath('extension_administrator')))
{
JLog::add(JText::_('JLIB_INSTALLER_ERROR_COMP_UNINSTALL_FAILED_REMOVE_DIRECTORY_ADMIN'), JLog::WARNING, 'jerror');
$retval = false;
}
}
// Now we will no longer need the extension object, so let's delete it
$this->extension->delete($this->extension->extension_id);
return $retval;
}
else
{
// No component option defined... cannot delete what we don't know about
JLog::add(JText::_('JLIB_INSTALLER_ERROR_COMP_UNINSTALL_NO_OPTION'), JLog::WARNING, 'jerror');
return false;
}
}
/**
* Method to build menu database entries for a component
*
* @param int|null $component_id The component ID for which I'm building menus
*
* @return boolean True if successful
*
* @since 3.1
*/
protected function _buildAdminMenus($component_id = null)
{
$db = $this->parent->getDbo();
$option = $this->get('element');
// If a component exists with this option in the table then we don't need to add menus
$query = $db->getQuery(true)
->select('m.id, e.extension_id')
->from('#__menu AS m')
->join('LEFT', '#__extensions AS e ON m.component_id = e.extension_id')
->where('m.parent_id = 1')
->where('m.client_id = 1')
->where('e.element = ' . $db->quote($option));
$db->setQuery($query);
// In case of a failed installation (e.g. timeout error) we may have duplicate menu item and extension records.
$componentrows = $db->loadObjectList();
// Check if menu items exist
if (!empty($componentrows))
{
// Don't do anything if overwrite has not been enabled
if (!$this->parent->isOverwrite())
{
return true;
}
// Remove all menu items
foreach ($componentrows as $componentrow)
{
// Remove existing menu items if overwrite has been enabled
if ($option)
{
// If something goes wrong, there's no way to rollback TODO: Search for better solution
$this->_removeAdminMenus($componentrow->extension_id);
}
}
}
// Only try to detect the component ID if it's not provided
if (empty($component_id))
{
// Lets find the extension id
$query->clear()
->select('e.extension_id')
->from('#__extensions AS e')
->where('e.type = ' . $db->quote('component'))
->where('e.element = ' . $db->quote($option));
$db->setQuery($query);
$component_id = $db->loadResult();
}
// Ok, now its time to handle the menus. Start with the component root menu, then handle submenus.
$menuElement = $this->getManifest()->administration->menu;
// Just do not create the menu if $menuElement not exist
if (!$menuElement)
{
return true;
}
// If the menu item is hidden do nothing more, just return
if (in_array((string) $menuElement['hidden'], array('true', 'hidden')))
{
return true;
}
// Let's figure out what the menu item data should look like
$data = array();
if ($menuElement)
{
// I have a menu element, use this information
$data['menutype'] = 'main';
$data['client_id'] = 1;
$data['title'] = (string) trim($menuElement);
$data['alias'] = (string) $menuElement;
$data['link'] = 'index.php?option=' . $option;
$data['type'] = 'component';
$data['published'] = 0;
$data['parent_id'] = 1;
$data['component_id'] = $component_id;
$data['img'] = ((string) $menuElement->attributes()->img) ? (string) $menuElement->attributes()->img : 'class:component';
$data['home'] = 0;
$data['path'] = '';
$data['params'] = '';
}
else
{
// No menu element was specified, Let's make a generic menu item
$data = array();
$data['menutype'] = 'main';
$data['client_id'] = 1;
$data['title'] = $option;
$data['alias'] = $option;
$data['link'] = 'index.php?option=' . $option;
$data['type'] = 'component';
$data['published'] = 0;
$data['parent_id'] = 1;
$data['component_id'] = $component_id;
$data['img'] = 'class:component';
$data['home'] = 0;
$data['path'] = '';
$data['params'] = '';
}
// Try to create the menu item in the database
$parent_id = $this->_createAdminMenuItem($data, 1);
if ($parent_id === false)
{
return false;
}
/*
* Process SubMenus
*/
if (!$this->getManifest()->administration->submenu)
{
// No submenu? We're done.
return true;
}
foreach ($this->getManifest()->administration->submenu->menu as $child)
{
$data = array();
$data['menutype'] = 'main';
$data['client_id'] = 1;