-
-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathSkin.php
More file actions
1607 lines (1309 loc) · 50.9 KB
/
Copy pathSkin.php
File metadata and controls
1607 lines (1309 loc) · 50.9 KB
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
/*
* Textpattern Content Management System
* https://textpattern.com/
*
* Copyright (C) 2026 The Textpattern Development Team
*
* This file is part of Textpattern.
*
* Textpattern is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, version 2.
*
* Textpattern is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Textpattern. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* Skin
*
* Manage Skins and their dependencies.
*
* @since 4.7.0
* @package Skin
*/
namespace Textpattern\Skin;
class Skin extends CommonBase implements SkinInterface
{
/**
* Skin assets related objects.
*
* @var array Page, Form and CSS class objects.
* @see setAssets().
*/
private $assets;
/**
* Class related main file.
*
* @var string Filename.
* @see getFile().
*/
protected static $filename = 'manifest.json';
/**
* {@inheritdoc}
*/
protected static $extension = 'json';
/**
* Importable skins.
*
* @var array Associative array of skin names and their infos from JSON files
* @see setUploaded(), getUploaded().
*/
protected $uploaded;
/**
* Class related directory path.
*
* @var string Path.
* @see setDirPath(), getDirPath().
*/
protected $dirPath;
/**
* {@inheritdoc}
*/
public function __construct()
{
parent::__construct();
}
public function __toString()
{
return $this->getName();
}
protected function mergeResults($asset, $status)
{
$this->results = array_merge_recursive($this->getResults(), $asset->getResults($status));
return $this;
}
/**
* $dirPath property setter.
*
* @param string $path Path (default: get_pref('path_to_site').DS.get_pref('skin_dir')).
* @return string $this->dirPath
*/
public function setDirPath($path = null)
{
$path !== null or $path = get_pref('path_to_site').DS.get_pref($this->getEvent().'_dir');
$this->dirPath = rtrim($path, DS);
$this->uploaded = null;
return $this->getDirPath();
}
/**
* $dirPath property getter
*
* @return string $this->dirPath
*/
protected function getDirPath()
{
$this->dirPath !== null or $this->setDirPath();
return $this->dirPath;
}
/**
* $assets property setter.
*
* @param array $pages Page names to work with;
* @param array $forms Form names to work with;
* @param array $styles CSS names to work with.
* @return object $this The current class object (chainable)
*/
public function setAssets($pages = null, $forms = null, $styles = null)
{
$assets = array(
'Page' => $pages,
'Form' => $forms,
'Css' => $styles,
);
foreach ($assets as $class => $assets) {
$this->assets[] = \Txp::get('Textpattern\Skin\\'.$class, $this)->setNames($assets);
}
return $this;
}
/**
* $assets property getter.
*
* @return array $this->$assets
*/
protected function getAssets()
{
$this->assets !== null or $this->setAssets();
return $this->assets;
}
/**
* $infos and $name properties setter.
*
* @param string $name Skin name;
* @param string $title Skin title;
* @param string $version Skin version;
* @param string $description Skin description;
* @param string $author Skin author;
* @param string $author_uri Skin author URL;
* @return object $this The current class object (chainable).
*/
public function setInfos(
$name,
$title = null,
$version = null,
$description = null,
$author = null,
$author_uri = null
) {
$name = $this->setName($name)->getName();
$title or $title = ucfirst($name);
$this->infos = compact('name', 'title', 'version', 'description', 'author', 'author_uri');
return $this;
}
/**
* Get a $dir property value related subdirectory path.
*
* @param string $name Directory(/skin) name (default: $this->getName()).
* @return string The Path
*/
public function getSubdirPath($name = null)
{
$name !== null or $name = $this->getName();
return $this->getDirPath().DS.$name;
}
/**
* $file property getter.
*
* @return string self::$filename.
*/
protected static function getFilename()
{
return self::$filename;
}
/**
* Get the $file property value related path.
*
* @return string Path.
*/
protected function getFilePath()
{
return $this->getSubdirPath().DS.self::getFilename();
}
/**
* Get and complete the skin related file contents.
*
* @return array Associative array of JSON fields and their related values / fallback values.
*/
protected function getFileContents()
{
$contents = json_decode(file_get_contents($this->getFilePath()), true);
if ($contents === null) {
$contents = array();
} else {
$contents = $this->parseInfos($contents);
}
return $contents;
}
/**
* Parse a skin related infos.
*
* @return array $infos Associative array of fields and their related values / fallback values.
*/
protected function parseInfos($infos)
{
extract($infos);
!empty($title) or $title = ucfirst($this->getName());
!empty($version) or $version = gTxt('unknown');
!empty($description) or $description = '';
!empty($author) or $author = gTxt('unknown');
!empty($author_uri) or $author_uri = '';
return compact('title', 'version', 'description', 'author', 'author_uri');
}
/**
* $sections property getter.
*
* @param array Section names.
*/
protected function getSections($skin = null)
{
$skin = doSlash(is_string($skin) ? $skin : $this->getName());
$event = $this->getEvent();
return array_values(
safe_column(
'name',
'txp_section',
"$event ='$skin' OR dev_{$event} ='$skin'"
)
);
}
/**
* Update the txp_section table.
*
* @param string $set The SET clause (default: "skin = '".doSlash($this->getName())."'")
* @param string $where The WHERE clause (default: "skin = '".doSlash($this->getBase())."'")
* @return bool FALSE on error.
*/
public function updateSections($set = null, $where = null, $dev = false)
{
$event = ($dev ? "{$dev}_" : '').$this->getEvent();
$set !== null or $set = $event." = '".doSlash($this->getName())."'";
if ($where === null) {
$base = $this->getBase();
$where = $base ? $event." = '".doSlash($base)."'" : '1 = 1';
}
return safe_update('txp_section', $set, $where);
}
/**
* {@inheritdoc}
*/
public function getEditing()
{
$editing = get_pref($this->getEvent().'_editing', '', true);
$installed = $this->getInstalled();
if (!$editing || !array_key_exists($editing, $installed)) {
reset($installed);
$editing = $this->setEditing(key($installed));
}
return $editing;
}
/**
* {@inheritdoc}
*/
public function setEditing($name = null)
{
global $prefs;
$event = $this->getEvent();
$name !== null or $name = $this->getName();
$prefs[$event.'_editing'] = $name;
set_pref($event.'_editing', $name, $event, PREF_HIDDEN, 'text_input', 0, PREF_PRIVATE);
return $this->getEditing();
}
/**
* Create a file in the $dir property value related directory.
*
* @param string $pathname The file related path (default: $this->getName().DS.self::getFilename()).
* @param mixed $contents The file related contents as as a string or
* as an associative array for a .json file
* (uses the $infos property related array).
* @return bool Written octets number or FALSE on error.
*/
protected function createFile($pathname = null, $contents = null)
{
$pathname !== null or $pathname = $this->getName().DS.self::getFilename();
if ($contents === null) {
$contents = array_merge(
$this->getInfos(),
array('txp-type' => 'textpattern-theme')
);
unset($contents['name']);
}
if (pathinfo($pathname, PATHINFO_EXTENSION) === 'json') {
$contents = json_encode($contents, TEXTPATTERN_JSON | JSON_PRETTY_PRINT);
}
return file_put_contents($this->getDirPath().DS.$pathname, $contents);
}
/**
* $uploaded property setter.
*
* @return object $this The current class object (chainable).
*/
protected function setUploaded()
{
$this->uploaded = array();
$files = $this->getFiles(array(self::getFilename()), 1);
if ($files) {
foreach ($files as $file) {
$name = basename($file->getPath());
if ($name === self::sanitize($name)) {
$infos = $file->getJSONContents();
if ($infos && $infos['txp-type'] === 'textpattern-theme') {
$this->uploaded[$name] = $this->setName($name)->parseInfos($infos);
}
}
}
}
return $this;
}
/**
* {@inheritdoc}
*/
public function getUploaded($expanded = true)
{
$this->uploaded !== null or $this->setUploaded();
if (!$expanded) {
$contracted = array();
foreach ($this->uploaded as $name => $infos) {
$contracted[$name] = $infos['title'] . ' ('.$infos['version'].')';
}
return $contracted;
}
return $this->uploaded;
}
/**
* $installed property merger.
*
* @param array $this->installed.
*/
protected function mergeInstalled($skins)
{
$this->installed = array_merge($this->getInstalled(), $skins);
return $this->getInstalled();
}
/**
* $installed property remover.
*
* @return array $this->installed.
*/
protected function removeInstalled($names)
{
$this->installed = array_diff_key(
$this->getInstalled(),
array_fill_keys($names, '')
);
return $this->getInstalled();
}
/**
* {@inheritdoc}
*/
protected function getTableData($criteria, $sortSQL, $offset, $limit)
{
$assets = array('section', 'page', 'form', 'css');
$things = array('*');
$table = $this->getTable();
foreach ($assets as $asset) {
$things[] = '(SELECT COUNT(*) '
.'FROM '.safe_pfx_j('txp_'.$asset).' '
.'WHERE txp_'.$asset.'.'.$this->getEvent().' = '.$table.'.name) '
.$asset.'_count';
}
$things[] = '(SELECT COUNT(*) '
.'FROM '.safe_pfx_j('txp_section').' '
.'WHERE dev_'.$this->getEvent().' = '.$table.'.name) '
.'dev_section_count';
return safe_rows_start(
implode(', ', $things),
$table,
$criteria.' order by '.$sortSQL.' limit '.$offset.', '.$limit
);
}
/**
* Create/CreateFrom a single skin (and its related assets)
* Merges results in the related property.
*
* @return object $this The current object (chainable).
*/
public function create()
{
$event = $this->getEvent();
$infos = $this->getInfos();
$name = $infos['name'];
$base = $this->getBase();
$callbackExtra = compact('infos', 'base');
$done = false;
callback_event('txp.'.$event, 'create', 1, $callbackExtra);
if (empty($name)) {
$this->mergeResult($event.'_name_invalid', $name);
} elseif ($base && !$this->isInstalled($base)) {
$this->mergeResult($event.'_unknown', $base);
} elseif ($this->isInstalled()) {
$this->mergeResult($event.'_already_exists', $name);
} elseif (is_dir($nameDirPath = $this->getSubdirPath())) {
// Create a skin which would already have a related directory could cause conflicts.
$this->mergeResult($event.'_already_exists', $nameDirPath);
} elseif (!$this->createRow()) {
$this->mergeResult($event.'_creation_failed', $name);
} else {
$this->mergeResult($event.'_created', $name, 'success');
// Start working with the skin related assets.
foreach ($this->getAssets() as $assetModel) {
if ($base) {
$this->setName($base);
$rows = $assetModel->getRows();
$this->setName($name);
} else {
$rows = null;
}
if (!$assetModel->createRows($rows)) {
$assetsfailed = true;
$this->mergeResult($assetModel->getEvent().'_creation_failed', $name);
}
}
// If the assets related process did not failed; that is a success…
isset($assetsfailed) or $done = $name;
}
callback_event('txp.'.$event, 'create', 0, $callbackExtra + compact('done'));
return $this; // Chainable.
}
/**
* Update a single skin (and its related dependencies)
* Merges results in the related property.
*
* @return object $this The current object (chainable).
*/
public function update()
{
$event = $this->getEvent();
$infos = $this->getInfos();
$name = $infos['name'];
$base = $this->getBase();
$callbackExtra = compact('infos', 'base');
$done = null;
$ready = false;
callback_event('txp.'.$event, 'update', 1, $callbackExtra);
if (empty($name)) {
$this->mergeResult($event.'_name_invalid', $name);
} elseif (!$this->isInstalled($base)) {
$this->mergeResult($event.'_unknown', $base);
} elseif ($base !== $name && $this->isInstalled()) {
$this->mergeResult($event.'_already_exists', $name);
} elseif (is_dir($nameDirPath = $this->getSubdirPath()) && $base !== $name) {
// Rename the skin with a name which would already have a related directory could cause conflicts.
$this->mergeResult($event.'_already_exists', $nameDirPath);
} elseif (!$this->updateRow()) {
$this->mergeResult($event.'_update_failed', $base);
$locked = $base;
} else {
$this->mergeResult($event.'_updated', $name, 'success');
$ready = true;
$locked = $base;
$baseDirPath = $this->getSubdirPath($base);
// Rename the skin related directory to allow new updates from files.
if (is_dir($baseDirPath) && !@rename($baseDirPath, $nameDirPath)) {
$this->mergeResult('path_renaming_failed', $base, 'warning');
} else {
$locked = $name;
}
}
if ($ready) {
// Update skin related sections.
if ($sections = $this->getSections($base)) {
$updated = $this->updateSections();
$updated = $this->updateSections(null, null, 'dev') && $updated;
$updated or $this->mergeResult($event.'_related_sections_update_failed', array($base => $sections));
}
// update the skin_editing pref if needed.
$this->getEditing() !== $base or $this->setEditing();
// Start working with the skin related assets.
$assetUpdateSet = $event." = '".doSlash($this->getName())."'";
$assetUpdateWhere = $event." = '".doSlash($this->getBase())."'";
foreach ($this->getAssets() as $assetModel) {
if (!$assetModel->updateRow($assetUpdateSet, $assetUpdateWhere)) {
$assetsFailed = true;
$this->mergeResult($assetModel->getEvent().'_update_failed', $base);
}
}
// If the assets related process did not failed; that is a success…
isset($assetsFailed) or $done = $name;
}
callback_event('txp.'.$event, 'update', 0, $callbackExtra + compact('done'));
return $this; // Chainable
}
/**
* Duplicate multiple skins (and their related $assets)
* Merges results in the related property.
*
* @return object $this The current object (chainable).
*/
public function duplicate()
{
$event = $this->getEvent();
$names = $this->getNames();
$callbackExtra = compact('names');
$ready = $done = array();
callback_event('txp.'.$event, 'duplicate', 1, $callbackExtra);
foreach ($names as $name) {
$nameDirPath = $this->setName($name)->getSubdirPath();
$copy = $name.'_copy';
if (!$this->isInstalled()) {
$this->mergeResult($event.'_unknown', $name);
} elseif ($this->isInstalled($copy)) {
$this->mergeResult($event.'_already_exists', $copy);
} elseif (is_dir($copyPath = $this->getSubdirPath($copy))) {
$this->mergeResult($event.'_already_exists', $copyPath);
} else {
$ready[] = $name;
}
}
if ($ready) {
$rows = $this->getRows(
"*",
"name IN ('".implode("', '", array_map('doSlash', $ready))."')"
);
if (!$rows) {
$this->mergeResult($event.'_not_found', $ready);
} else {
foreach ($rows as $row) {
extract($row);
$copy = $name.'_copy';
$copyTitle = $title.' (copy)';
$this->setInfos($copy, $copyTitle, $version, $description, $author, $author_uri);
if (!$this->createRow()) {
$this->mergeResult($event.'_creation_failed', $copy);
} else {
$this->mergeResult($event.'_created', $copy, 'success');
$this->mergeInstalled(array($copy => $copyTitle));
// Start working with the skin related assets.
foreach ($this->getAssets() as $assetModel) {
$this->setName($name);
$assetString = $assetModel->getEvent();
$assetRows = $assetModel->getRows();
if (!$assetRows) {
$deleteExtraFiles = true;
$this->mergeResult($assetString.'_not_found', array($skin => $nameDirPath));
} elseif ($this->setName($copy) && !$assetModel->createRows($assetRows)) {
$deleteExtraFiles = true;
$this->mergeResult($assetString.'_creation_failed', $copy);
}
}
$this->setName($name); // Be sure to restore the right $name.
// If the assets related process did not failed; that is a success…
isset($deleteExtraFiles) or $done[] = $name;
}
}
}
}
callback_event('txp.'.$event, 'duplicate', 0, $callbackExtra + compact('done'));
return $this; // Chainable
}
/**
* {@inheritdoc}
*/
public function import($sync = false, $override = false)
{
$event = $this->getEvent();
$syncPref = 'skin_delete_from_database';
$sync == $this->getSyncPref($syncPref) or $this->switchSyncPref($syncPref);
$names = $this->getNames();
$callbackExtra = compact('names', 'sync');
$done = array();
callback_event('txp.'.$event, 'import', 1, $callbackExtra);
foreach ($names as $name) {
$this->setName($name);
$this->setBase($name);
$isInstalled = $this->isInstalled();
$isInstalled or $sync = $override = false; // Avoid useless work.
if (!$override && $isInstalled) {
$this->mergeResult($event.'_already_exists', $name);
} elseif (!is_readable($filePath = $this->getFilePath())) {
$this->mergeResult('path_not_readable', $filePath);
} else {
$skinInfos = array_merge(array('name' => $name), $this->getFileContents());
if (count($skinInfos) == 1) {
$this->mergeResult('invalid_json', $filePath);
} else {
extract($skinInfos);
$this->setInfos($name, $title, $version, $description, $author, $author_uri);
if (!$override && !$this->createRow()) {
$this->mergeResult($event.'_import_failed', $name);
} elseif ($override && !$this->updateRow()) {
$this->mergeResult($event.'_import_failed', $name);
} else {
$this->mergeResult($event.'_imported', $name, 'success');
$this->mergeInstalled(array($name => $title));
// Start working with the skin related assets.
foreach ($this->getAssets() as $asset) {
$asset->import($sync, $override);
if (is_array($asset->getMessage())) {
$assetFailed = true;
$this->mergeResults($asset, array('warning', 'error'));
}
}
}
// If the assets related process did not failed; that is a success…
isset($assetFailed) or $done[] = $name;
}
}
}
callback_event('txp.'.$event, 'import', 0, $callbackExtra + compact('done'));
return $this;
}
/**
* {@inheritdoc}
*/
public function export($sync = false, $override = false)
{
$syncPref = 'skin_delete_from_disk';
$sync == $this->getSyncPref($syncPref) or $this->switchSyncPref($syncPref);
$event = $this->getEvent();
$names = $this->getNames();
$callbackExtra = compact('names', 'sync');
$ready = $done = array();
callback_event('txp.'.$event, 'export', 1, $callbackExtra);
foreach ($names as $name) {
$this->setName($name);
$nameDirPath = $this->getSubdirPath();
if (!is_writable($nameDirPath)) {
$sync = false;
$override = false;
}
if (!self::isExportable($name)) {
$this->mergeResult($event.'_name_unsafe', $name);
} elseif (!$override && is_dir($nameDirPath)) {
$this->mergeResult($event.'_already_exists', $nameDirPath);
} elseif (!is_dir($nameDirPath) && !@mkdir($nameDirPath)) {
$this->mergeResult('path_not_writable', $nameDirPath);
} else {
$ready[] = $name;
}
}
if ($ready) {
$rows = $this->getRows(
"*",
"name IN ('".implode("', '", array_map('doSlash', $ready))."')"
);
if (!$rows) {
$this->mergeResult($event.'_unknown', $names);
} else {
foreach ($rows as $row) {
extract($row);
$this->setInfos($name, $title, $version, $description, $author, $author_uri);
if ($this->createFile() === false) {
$this->mergeResult($event.'_export_failed', $name);
} else {
$this->mergeResult($event.'_exported', $name, 'success');
foreach ($this->getAssets() as $asset) {
$asset->export($sync, $override);
if (is_array($asset->getMessage())) {
$assetFailed = true;
$this->mergeResults($asset, array('warning', 'error'));
}
}
isset($assetFailed) or $done[] = $name;
}
}
}
}
callback_event('txp.'.$event, 'export', 0, $callbackExtra + compact('done'));
return $this;
}
/**
* Delete multiple skins (and their related $assets + directories if empty)
* Merges results in the related property.
*
* @return object $this The current object (chainable).
*/
public function delete($sync = false)
{
$syncPref = 'skin_delete_entirely';
$sync == $this->getSyncPref($syncPref) or $this->switchSyncPref($syncPref);
$event = $this->getEvent();
$names = $this->getNames();
$callbackExtra = compact('names', 'sync');
$ready = $done = array();
callback_event('txp.'.$event, 'delete', 1, $callbackExtra);
foreach ($names as $name) {
$isDir = is_dir($this->getSubdirPath($name));
if (!$this->setName($name)->isInstalled()) {
$this->mergeResult($event.'_unknown', $name);
} elseif ($sections = $this->getSections($name)) {
$this->mergeResult($event.'_in_use', array($name => $sections));
} else {
/**
* Start working with the skin related assets.
* Done first as assets won't be accessible
* once their parent skin will be deleted.
*/
$assetFailed = false;
foreach ($this->getAssets() as $assetModel) {
if (!$assetModel->deleteRows()) {
$assetFailed = true;
$this->mergeResult($assetModel->getEvent().'_deletion_failed', $name);
} elseif ($sync && $isDir) {
$notDeleted = $assetModel->deleteExtraFiles();
if ($notDeleted) {
$this->mergeResult($assetModel->getEvent().'_files_deletion_failed', array($name => $notDeleted));
}
}
}
$assetFailed or $ready[] = $name;
}
}
if ($ready) {
if ($this->deleteRows("name IN ('".implode("', '", array_map('doSlash', $ready))."')")) {
$done = $ready;
$this->removeInstalled($ready);
if (in_array($this->getEditing(), $ready)) {
$default = $this->getDefault();
!$default or $this->setEditing($default);
}
$this->mergeResult($event.'_deleted', $ready, 'success');
// Remove all skins files and directories if needed.
if ($sync) {
$notDeleted = $this->deleteFiles($ready);
!$notDeleted or $this->mergeResult($event.'_files_deletion_failed', $notDeleted);
}
update_lastmod($event.'.delete', $ready);
} else {
$this->mergeResult($event.'_deletion_failed', $ready);
}
}
callback_event('txp.'.$event, 'delete', 0, $callbackExtra + compact('done'));
return $this;
}
/**
* Delete Files from the $dir property value related directory.
*
* @param string $names directory/file names.
* @return bool 0 on error.
*/
protected function deleteFiles($names = null)
{
$notRemoved = array();
foreach ($names as $name) {
if (is_dir($this->getSubdirPath($name))) {
$filePath = $this->getFilePath();
if (file_exists($filePath) && !unlink($filePath)) {
$notRemoved[$name][] = $filePath;
}
$subdirPath = $this->getSubdirPath();
$isDirEmpty = self::isDirEmpty($subdirPath);
if (!isset($notRemoved[$name]) && ($isDirEmpty && !@rmdir($subdirPath) || !$isDirEmpty)) {
$notRemoved[$name][] = $subdirPath;
}
}
}
return $notRemoved;
}
/**
* Control the admin tab.
*/
public function admin()
{
if (!defined('TXPINTERFACE')) {
die('TXPINTERFACE is undefined.');
}
global $event, $step;
if ($event === $this->getEvent()) {
require_privs($event);
bouncer($step, array(
$event.'_change_pageby' => true, // Prefixed to make it work with the paginator…
'list' => false,
'edit' => false,
'save' => true,
'import' => false,
'multi_edit' => true,
));
switch ($step) {
case 'save':
$infos = array_map('assert_string', psa(array(
'name',
'title',
'old_name',
'old_title',
'version',
'description',
'author',
'author_uri',
'copy',
)));