-
Notifications
You must be signed in to change notification settings - Fork 9
/
Text.php
1223 lines (1038 loc) · 33.3 KB
/
Text.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 namespace App\Entity;
use App\Generator\TextFb2Generator;
use App\Generator\TextFbiGenerator;
use App\Generator\TextHtmlGenerator;
use App\Legacy\Setup;
use App\Legacy\SfbParserSimple;
use App\Service\ContentService;
use App\Util\Char;
use App\Util\File;
use App\Util\Stringy;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Sfblib\SfbConverter;
use Sfblib\SfbToHtmlConverter;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass="App\Entity\TextRepository")
* @ORM\HasLifecycleCallbacks
* @ORM\Cache(usage="NONSTRICT_READ_WRITE")
* @ORM\Table(name="text",
* indexes={
* @ORM\Index(name="title_idx", columns={"title"}),
* @ORM\Index(name="subtitle_idx", columns={"subtitle"}),
* @ORM\Index(name="orig_title_idx", columns={"orig_title"}),
* @ORM\Index(name="orig_subtitle_idx", columns={"orig_subtitle"}),
* @ORM\Index(name="lang_idx", columns={"lang"}),
* @ORM\Index(name="orig_lang_idx", columns={"orig_lang"})}
* )
*/
class Text extends BaseWork implements \JsonSerializable {
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class="App\Doctrine\CustomIdGenerator")
*/
protected $id;
/**
* @var string
* @ORM\Column(type="string", length=50)
*/
private $slug;
/**
* @var string
* @ORM\Column(type="string", length=255)
*/
private $title = '';
/**
* @var string
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $subtitle;
/**
* @var Language
* @ORM\ManyToOne(targetEntity="Language")
* @ORM\JoinColumn(name="lang", referencedColumnName="code", nullable=false)
*/
private $lang;
/**
* @var int
* @ORM\Column(type="smallint", nullable=true)
*/
private $transYear;
/**
* @var int
* @ORM\Column(type="smallint", nullable=true)
*/
private $transYear2;
/**
* @var string
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $origTitle;
/**
* @var string
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $origSubtitle;
/**
* @var Language
* @ORM\ManyToOne(targetEntity="Language")
* @ORM\JoinColumn(name="orig_lang", referencedColumnName="code", nullable=false)
*/
private $origLang;
/**
* @var string
* @ORM\Column(type="string", length=10, nullable=true)
*/
private $year;
/**
* @var int
* @ORM\Column(type="smallint", nullable=true)
*/
private $year2;
/**
* @var License
* @ORM\ManyToOne(targetEntity="License")
*/
private $origLicense;
/**
* @var License
* @ORM\ManyToOne(targetEntity="License")
*/
private $transLicense;
/**
* @var TextType
* @ORM\ManyToOne(targetEntity="TextType")
* @ORM\JoinColumn(name="type", referencedColumnName="code", nullable=false)
*/
private $type;
/**
* @var Series
* @ORM\ManyToOne(targetEntity="Series", inversedBy="texts")
*/
private $series;
/**
* @var int|string
* @ORM\Column(type="decimal", precision=5, scale=2, nullable=true)
*/
private $sernr;
/**
* @var int
* @ORM\Column(type="smallint")
*/
private $headlevel = 0;
/**
* @var int
* @ORM\Column(type="integer", nullable=true)
*/
private $size;
/**
* @var float
* @ORM\Column(type="integer", nullable=true)
*/
private $zsize;
/**
* @var \DateTime
* @ORM\Column(type="date")
*/
private $createdAt;
/**
* @var string
* @ORM\Column(type="string", length=1000, nullable=true)
*/
private $source;
/**
* @var TextRevision
* @ORM\ManyToOne(targetEntity="TextRevision")
*/
private $curRev;
/**
* @var int
* @ORM\Column(type="integer")
*/
private $commentCount = 0;
/**
* @var float
* @ORM\Column(type="float")
*/
private $rating = 0;
/**
* @var int
* @ORM\Column(type="integer")
*/
private $votes = 0;
/**
* @var bool
* @ORM\Column(type="boolean")
*/
private $hasAnno = false;
// /*
// * @var bool
// * @ORM\Column(type="boolean")
// */
// private $hasTitleNote;
/**
* @var bool
* @ORM\Column(type="boolean")
*/
private $isCompilation = false;
/**
* An extra note about the text
* @ORM\Column(type="string", length=100, nullable=true)
*/
private $note;
/**
* @var string
* @ORM\Column(type="string", nullable=true)
*/
private $article;
/**
* A notice if the content is removed
* @ORM\Column(type="text", nullable=true)
*/
private $removedNotice;
/**
* @var ArrayCollection|TextAuthor[]
* @ORM\OneToMany(targetEntity="TextAuthor", mappedBy="text", cascade={"persist", "remove"}, orphanRemoval=true)
* @ORM\OrderBy({"pos" = "ASC"})
* @ORM\Cache(usage="NONSTRICT_READ_WRITE")
*/
private $textAuthors;
/**
* @var ArrayCollection|TextTranslator[]
* @ORM\OneToMany(targetEntity="TextTranslator", mappedBy="text", cascade={"persist", "remove"}, orphanRemoval=true)
* @ORM\OrderBy({"pos" = "ASC"})
* @ORM\Cache(usage="NONSTRICT_READ_WRITE")
*/
private $textTranslators;
/**
* @var Person[]
*/
private $authors;
/**
* @var Person[]
*/
private $translators;
/**
* @var ArrayCollection|BookText[]
* @ORM\OneToMany(targetEntity="BookText", mappedBy="text")
* @ORM\Cache(usage="NONSTRICT_READ_WRITE")
*/
private $bookTexts;
/** FIXME doctrine:schema:create does not allow this relation
* @var ArrayCollection|Book[]
* @ORM\ManyToMany(targetEntity="Book", mappedBy="texts")
* @ORM\JoinTable(name="book_text",
* joinColumns={@ORM\JoinColumn(name="text_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="book_id", referencedColumnName="id")})
* @ORM\OrderBy({"title" = "ASC"})
*/
private $books;
/**
* @var ArrayCollection|Label[]
* @ORM\ManyToMany(targetEntity="Label", inversedBy="texts")
* @ORM\OrderBy({"name" = "ASC"})
*/
private $labels;
/**
* @var ArrayCollection|TextHeader[]
* @ORM\OneToMany(targetEntity="TextHeader", mappedBy="text", cascade={"persist", "remove"}, orphanRemoval=true)
* @ORM\OrderBy({"nr" = "ASC", "level" = "ASC"})
* @ORM\Cache(usage="NONSTRICT_READ_WRITE")
*/
private $headers;
/** FIXME doctrine:schema:create does not allow this relation
* @var ArrayCollection|User[]
* @ORM\ManyToMany(targetEntity="User")
* @ORM\JoinTable(name="user_text_read",
* joinColumns={@ORM\JoinColumn(name="text_id")},
* inverseJoinColumns={@ORM\JoinColumn(name="user_id")})
*/
private $readers;
/**
* @var ArrayCollection|UserTextContrib[]
* @ORM\OneToMany(targetEntity="UserTextContrib", mappedBy="text", cascade={"persist", "remove"}, orphanRemoval=true)
* @ORM\Cache(usage="NONSTRICT_READ_WRITE")
*/
private $userContribs;
/**
* @var ArrayCollection|TextRevision[]
* @ORM\OneToMany(targetEntity="TextRevision", mappedBy="text", cascade={"persist", "remove"}, orphanRemoval=true)
*/
private $revisions;
/**
* @var ArrayCollection|TextCombination[]
* @ORM\OneToMany(targetEntity="TextCombination", mappedBy="text1", cascade={"persist", "remove"}, orphanRemoval=true)
*/
private $textCombinations1;
/**
* @var ArrayCollection|TextCombination[]
* @ORM\OneToMany(targetEntity="TextCombination", mappedBy="text2", cascade={"persist", "remove"}, orphanRemoval=true)
*/
private $textCombinations2;
/**
* @var ArrayCollection|TextLink[]
* @ORM\OneToMany(targetEntity="TextLink", mappedBy="text", cascade={"persist", "remove"}, orphanRemoval=true)
* @ORM\Cache(usage="NONSTRICT_READ_WRITE")
*/
private $links;
/**
* @var array
* @ORM\Column(type="array", nullable=true)
*/
private $alikes;
/** @var array Cache storage */
private $c = [];
public function __construct($id = null) {
$this->id = $id;
$this->textAuthors = new ArrayCollection;
$this->textTranslators = new ArrayCollection;
$this->authors = [];
$this->translators = [];
$this->bookTexts = new ArrayCollection;
$this->books = new ArrayCollection;
$this->labels = new ArrayCollection;
$this->headers = new ArrayCollection;
$this->readers = new ArrayCollection;
$this->userContribs = new ArrayCollection;
$this->links = new ArrayCollection;
}
public function __toString() {
return $this->getTitle();
}
public function getId() { return $this->id; }
public function setSlug($slug) { $this->slug = Stringy::slugify($slug); }
public function getSlug() { return $this->slug; }
public function setTitle($title) { $this->title = $title; }
public function getTitle() { return $this->title; }
public function setSubtitle($subtitle) { $this->subtitle = $subtitle; }
public function getSubtitle() { return $this->subtitle; }
public function setLang($lang) { $this->lang = $lang; }
public function getLang() { return $this->lang; }
public function setTransYear($transYear) { $this->transYear = $transYear; }
public function getTransYear() { return $this->transYear; }
public function setTransYear2($transYear2) { $this->transYear2 = $transYear2; }
public function getTransYear2() { return $this->transYear2; }
public function setOrigTitle($origTitle) { $this->origTitle = $origTitle; }
public function getOrigTitle() { return $this->origTitle; }
public function setOrigSubtitle($origSubtitle) { $this->origSubtitle = $origSubtitle; }
public function getOrigSubtitle() { return $this->origSubtitle; }
public function setOrigLang($origLang) { $this->origLang = $origLang; }
public function getOrigLang() { return $this->origLang; }
public function setYear($year) { $this->year = $year; }
public function getYear() { return $this->year; }
public function setYear2($year2) { $this->year2 = $year2; }
public function getYear2() { return $this->year2; }
public function setOrigLicense($origLicense) { $this->origLicense = $origLicense; }
public function getOrigLicense() { return $this->origLicense; }
public function getOrigLicenseCode() {
return $this->origLicense ? $this->origLicense->getCode() : null;
}
public function setTransLicense($transLicense) { $this->transLicense = $transLicense; }
public function getTransLicense() { return $this->transLicense; }
public function getTransLicenseCode() {
return $this->transLicense ? $this->transLicense->getCode() : null;
}
public function setType($type) { $this->type = $type; }
public function getType() { return $this->type; }
public function setSeries($series) { $this->series = $series; }
public function getSeries() { return $this->series; }
public function getSeriesSlug() {
return $this->series ? $this->series->getSlug() : null;
}
public function setSernr($sernr) { $this->sernr = $sernr; }
public function getSernr() {
if ($this->sernr == (int) $this->sernr && $this->sernr > 0) {
return (int) $this->sernr;
}
return $this->sernr ? rtrim($this->sernr, '0.') : null;
}
public function setCreatedAt($createdAt) { $this->createdAt = $createdAt; }
public function getCreatedAt() { return $this->createdAt; }
public function setSource($source) { $this->source = $source; }
public function getSource() { return $this->source; }
public function setCurRev($curRev) { $this->curRev = $curRev; }
public function getCurRev() { return $this->curRev; }
public function setCommentCount($commentCount) { $this->commentCount = $commentCount; }
public function getCommentCount() { return $this->commentCount; }
public function setRating($rating) { $this->rating = $rating; }
public function getRating() { return $this->rating; }
public function setVotes($votes) { $this->votes = $votes; }
public function getVotes() { return $this->votes; }
/**
* @param bool $hasAnno
*/
public function setHasAnno($hasAnno) { $this->hasAnno = $hasAnno; }
public function hasAnno() { return $this->hasAnno; }
// public function setHasTitleNote($hasTitleNote) { $this->hasTitleNote = $hasTitleNote; }
// public function getHasTitleNote() { return $this->hasTitleNote; }
public function isCompilation() { return $this->isCompilation; }
public function setNote($note) { $this->note = $note; }
public function getNote() { return $this->note; }
public function setArticle($article) { $this->article = $article; }
public function getArticle() { return $this->article; }
public function setRemovedNotice($removedNotice) { $this->removedNotice = $removedNotice; }
public function getRemovedNotice() { return $this->removedNotice; }
public function getUserContribs() { return $this->userContribs; }
public function setUserContribs($userContribs) { $this->userContribs = $userContribs; }
public function addUserContrib(UserTextContrib $userContrib) {
$this->userContribs[] = $userContrib;
}
public function removeUserContrib(UserTextContrib $userContrib) {
$this->userContribs->removeElement($userContrib);
}
public function addAuthor(Person $author) {
$this->authors[] = $author;
}
/** @return Person[] */
public function getAuthors() {
if (!isset($this->authors)) {
$this->authors = array_filter(array_map(function(TextAuthor $author) {
return $author->getPos() >= 0 ? $author->getPerson() : null;
}, $this->getTextAuthors()->toArray()));
}
return $this->authors;
}
public function addTranslator(Person $translator) {
$this->translators[] = $translator;
}
/** @return Person[] */
public function getTranslators() {
if (!isset($this->translators)) {
$this->translators = array_filter(array_map(function(TextTranslator $translator) {
return $translator->getPos() >= 0 ? $translator->getPerson() : null;
}, $this->getTextTranslators()->toArray()));
}
return $this->translators;
}
public function addTextAuthor(TextAuthor $textAuthor) {
$this->textAuthors[] = $textAuthor;
}
public function removeTextAuthor(TextAuthor $textAuthor) {
$this->textAuthors->removeElement($textAuthor);
}
// TODO needed by admin; why?
public function addTextAuthors(TextAuthor $textAuthor) { $this->addTextAuthor($textAuthor); }
public function setTextAuthors($textAuthors) { $this->textAuthors = $textAuthors; }
/** @return ArrayCollection|TextAuthor[] */
public function getTextAuthors() { return $this->textAuthors; }
public function addTextTranslator(TextTranslator $textTranslator) {
$this->textTranslators[] = $textTranslator;
}
public function removeTextTranslator(TextTranslator $textTranslator) {
$this->textTranslators->removeElement($textTranslator);
}
// TODO needed by admin; why?
public function addTextTranslators(TextTranslator $textTranslator) { $this->addTextTranslator($textTranslator); }
public function setTextTranslators($textTranslators) { $this->textTranslators = $textTranslators; }
/** @return ArrayCollection|TextTranslator[] */
public function getTextTranslators() { return $this->textTranslators; }
public function getBooks() { return $this->books; }
public function getRevisions() { return $this->revisions; }
public function addRevision(TextRevision $revision) {
$this->revisions[] = $revision;
}
/** @return JuxtaposedText[] */
public function getJuxtaposedTexts() {
return $this->c[__FUNCTION__] ?? $this->c[__FUNCTION__] = array_merge(
array_map(function(TextCombination $tc) { return new JuxtaposedText($this, $tc->getText2()); }, $this->textCombinations1->toArray()),
array_map(function(TextCombination $tc) { return new JuxtaposedText($this, $tc->getText1()); }, $this->textCombinations2->toArray())
);
}
/**
* @param string $comment
* @param User $user
* @param \DateTime $date
*/
public function addNewRevision($comment = null, User $user = null, \DateTime $date = null) {
$revision = new TextRevision;
$revision->setComment($comment ?: 'Добавяне');
$revision->setText($this);
$revision->setUser($user);
$revision->setDate($date ?: new \DateTime);
$isFirst = count($this->getRevisions()) == 0;
$revision->setFirst($isFirst);
$this->addRevision($revision);
}
public function setLinks($links) { $this->links = $links; }
public function getLinks() { return $this->links; }
public function addLink(TextLink $link) {
$this->links[] = $link;
}
// needed by SonataAdmin
public function addLinks(TextLink $link) {
$this->addLink($link);
}
public function removeLink(TextLink $link) {
$this->links->removeElement($link);
}
public function setAlikes($alikes) { $this->alikes = $alikes; }
public function getAlikes() { return (array) $this->alikes; }
private $_book;
/**
* Return the main book for the text
*/
public function getBook() {
if ( ! isset($this->_book)) {
$this->_book = false;
foreach ($this->bookTexts as $bookText) {
if ($bookText->getShareInfo()) {
$this->_book = $bookText->getBook();
break;
}
}
}
return $this->_book;
}
public function addLabel(Label $label) { $this->labels[] = $label; }
public function getLabels() { return $this->labels; }
public function getLabelsByGroup($group = null) {
$labelsByGroup = [];
foreach ($this->getLabels() as $label) {
$labelsByGroup[$label->getGroup()][] = $label;
}
if ($group === null) {
return $labelsByGroup;
}
return isset($labelsByGroup[$group]) ? $labelsByGroup[$group] : [];
}
public function getAvailableLabels() {
return Label::getAvailableGroups();
}
public function addReader(User $reader) { $this->readers[] = $reader; }
public function getReaders() { return $this->readers; }
protected static $annotationDir = 'text-anno';
protected static $infoDir = 'text-info';
public function getDocId() {
return 'http://chitanka.info/text/' . $this->getId();
}
public function getYearHuman() {
$year2 = empty($this->year2) ? '' : '–'. abs($this->year2);
return $this->year >= 0
? $this->year . $year2
: abs($this->year) . $year2 .' пр.н.е.';
}
public function getTransYearHuman() {
return $this->transYear . (empty($this->transYear2) ? '' : '–'.$this->transYear2);
}
public function isGamebook() {
return $this->type->is('gamebook');
}
public function isTranslation() {
return $this->lang != $this->origLang;
}
private function getTranslatorSlugs() {
return array_map(function(Person $translator) {
return $translator->getSlug();
}, $this->getTranslators());
}
private function getTranslatorNames() {
return array_map(function(Person $translator) {
return $translator->getName();
}, $this->getTranslators());
}
public function getTitleAsSfb() {
$title = "|\t" . $this->escapeForSfb($this->title);
if ( !empty($this->subtitle) ) {
$title .= "\n|\t" . strtr($this->escapeForSfb($this->subtitle), [
self::TITLE_NEW_LINE => "\n|\t",
'\n' => "\n|\t",
]);
}
if ( $this->hasTitleNote() ) {
$title .= '*';
}
return $title;
}
/**
* @param string $string
*/
private function escapeForSfb($string) {
return strtr($string, [
'*' => '\*',
]);
}
public function hasTitleNote() {
if ( ! is_null( $this->hasTitleNote ) ) {
return $this->hasTitleNote;
}
$conv = new SfbToHtmlConverter( ContentService::getInternalContentFilePath( 'text', $this->getId() ) );
return $this->hasTitleNote = $conv->hasTitleNote();
}
public function getImages() {
return $this->getImagesFromDir(ContentService::getInternalContentFilePath('img', $this->getId()));
}
public function getThumbImages() {
return $this->getImagesFromDir(ContentService::getInternalContentFilePath('img', $this->getId()) . '/thumb');
}
/**
* @param string $dir
*/
public function getImagesFromDir($dir) {
$images = [];
if (is_dir($dir) && ($dh = opendir($dir)) ) {
while (($file = readdir($dh)) !== false) {
$fullname = "$dir/$file";
if ( $file[0] == '.' || /*$file[0] == '_' ||*/
File::isArchive($file) || is_dir($fullname) ) {
continue;
}
$images[] = $fullname;
}
closedir($dh);
}
return $images;
}
public function getFullExtraInfo() {
return $this->getExtraInfo() . $this->getBookExtraInfo();
}
public function getFullExtraInfoForHtml($imgDirPrefix = '') {
return $this->_getContentHtml($this->getFullExtraInfo(), $imgDirPrefix);
}
public function getAnnotationHtml($imgDirPrefix = '') {
return $this->_getContentHtml($this->getAnnotation(), $imgDirPrefix);
}
/**
* @param string $content SFB content
* @param string $imgDirPrefix
*/
protected function _getContentHtml($content, $imgDirPrefix) {
$imgDir = $imgDirPrefix . ContentService::getContentFilePath('img', $this->getId());
$conv = new SfbToHtmlConverter($content, $imgDir);
return $conv->convert()->getContent();
}
public function getBookExtraInfo() {
$info = '';
foreach ($this->bookTexts as $bookText) {
if ($bookText->getShareInfo()) {
$file = ContentService::getInternalContentFilePath('book-info', $bookText->getBook()->getId());
if ( file_exists($file) ) {
$info .= "\n\n" . file_get_contents($file);
}
}
}
return $info;
}
public function getPlainTranslationInfo() {
if (!$this->isTranslation()) {
return '';
}
$lang = $this->getOrigLang()->getName();
if ($lang) {
$lang = ' от '.$lang;
}
$translators = implode(', ', $this->getTranslatorNames()) ?: '[Неизвестен]';
$year = $this->getTransYearHuman() ?: '—';
return sprintf('Превод%s: %s, %s', $lang, $translators, $year);
}
public function getPlainSeriesInfo() {
if (empty($this->series)) {
return null;
}
return sprintf('Част %d от „%s“', $this->sernr, $this->series->getName());
}
// TODO reimplement if needed
// public function getNextFromSeries() {
// if ( empty($this->series) ) {
// return false;
// }
// $dbkey = array('series_id' => $this->seriesId);
// if ($this->sernr == 0) {
// $dbkey['t.id'] = array('>', $this->getId());
// } else {
// $dbkey[] = 'sernr = '. ($this->sernr + 1)
// . " OR (sernr > $this->sernr AND t.id > $this->getId())";
// }
// return self::newFromDB($dbkey);
// }
// TODO reimplement if needed
// public function getNextFromBooks() {
// $nextWorks = array();
// foreach ($this->books as $id => $book) {
// $nextWorks[$id] = $this->getNextFromBook($id);
// }
// return $nextWorks;
// }
// TODO reimplement if needed
// public function getNextFromBook($book) {
// if ( empty($this->books[$book]) ) {
// return false;
// }
// $bookDescr = ContentService::getContentFile('book', $book);
// if ( preg_match('/\{'. $this->getId() . '\}\n\{(\d+)\}/m', $bookDescr, $m) ) {
// return self::newFromId($m[1]);
// }
// return false;
// }
// TODO reimplement if needed
// public function getPrefaceOfBook($book) {
// if ( empty($this->books[$book]) || $this->type == 'intro' ) {
// return false;
// }
// $subkey = array('book_id' => $book);
// $subquery = Setup::db()->selectQ(DBT_BOOK_TEXT, $subkey, 'text_id');
// $dbkey = array("t.id IN ($subquery)", 't.type' => 'intro');
// return self::newFromDB($dbkey);
// }
public function getHistoryAndVersion() {
$history = [];
$historyRows = $this->getHistoryInfo();
$verNo = 1;
$ver = '0';
foreach ( $historyRows as $data ) {
$ver = '0.' . ($verNo++);
$history[] = "$ver ($data[date]) — $data[comment]";
}
return [$history, $ver];
}
public function getFbi() {
$generator = new TextFbiGenerator();
return $generator->generateFbiForText($this);
}
public function getDataAsPlain() {
$authors = implode($this->getAuthorSlugs());
$translators = implode($this->getTranslatorSlugs());
$labels = implode($this->getLabelSlugs());
$contributors = [];
foreach ($this->getUserContribs() as $userContrib/*@var $userContrib UserTextContrib*/) {
$contributors[] = implode(',', [
$userContrib->getUsername(),
$userContrib->getPercent(),
'"'.$userContrib->getComment().'"',
$userContrib->getHumandate(),
]);
}
$contributors = implode(';', $contributors);
return <<<EOS
title = {$this->getTitle()}
subtitle = {$this->getSubtitle()}
authors = $authors
slug = {$this->getSlug()}
type = {$this->getType()->getCode()}
lang = {$this->getLang()}
year = {$this->getYear()}
orig_license = {$this->getOrigLicenseCode()}
orig_title = {$this->getOrigTitle()}
orig_subtitle = {$this->getOrigSubtitle()}
orig_lang = {$this->getOrigLang()}
translators = $translators
trans_license = {$this->getTransLicenseCode()}
series = {$this->getSeriesSlug()}
ser_nr = {$this->getSernr()}
source = {$this->getSource()}
labels = $labels
toc_level = {$this->getHeadlevel()}
users = $contributors
id = {$this->getId()}
EOS;
}
public function getNameForFile() {
$lengthLimits = [
'AUTHOR' => 50,
'SERIES' => 50,
'TITLE' => 100,
];
$parts = [
'AUTHOR' => $this->getAuthorNameEscaped(),
'SERIES' => empty($this->series) ? '' : Stringy::createAcronym(Char::cyr2lat($this->series->getName())),
'SERNO' => $this->getSernr() ?? '',
'TITLE' => Char::cyr2lat($this->title),
'ID' => $this->getId(),
];
array_walk($parts, function(string &$value, string $name) use ($lengthLimits) {
$value = substr($value, 0, $lengthLimits[$name] ?? 20);
});
return strtr(Setup::setting('download_file'), $parts);
}
public static function getMinRating() {
return self::$minRating ?: self::$minRating = min(array_keys(self::$ratings));
}
public static function getMaxRating() {
return self::$maxRating ?: self::$maxRating = max(array_keys(self::$ratings));
}
public static function getRatings($id) {
return Setup::db()->getFields(DBT_TEXT,
['id' => $id],
['rating', 'votes']);
}
public function getHistoryInfo() {
$rows = Setup::dbal()->fetchAll('select * from '.DBT_EDIT_HISTORY.' where text_id = ? order by date asc', [$this->getId()]);
if (!empty($rows)) {
$isoEntryDate = $this->createdAt->format('Y-m-d');
if ( "$isoEntryDate 24" < $rows[0]['date'] ) {
array_unshift($rows, ['date' => $isoEntryDate, 'comment' => 'Добавяне']);
}
}
return $rows;
}
/**
* @Assert\File
* @var UploadedFile
*/
private $content_file;
public function getContentFile() {
return $this->content_file;
}
/** @param UploadedFile $file */
public function setContentFile(UploadedFile $file = null) {
$this->content_file = $file;
if ($file) {
$this->setSize($file->getSize() / 1000);
$this->rebuildHeaders($file->getRealPath());
}
}
public function isContentFileUpdated() {
return $this->getContentFile() !== null;
}
public function setHeadlevel($headlevel) {
$this->headlevel = $headlevel;
if ( !$this->isContentFileUpdated()) {
$this->rebuildHeaders();
}
}
public function getHeadlevel() { return $this->headlevel; }
/**
* @param int $size
*/
public function setSize($size) {
$this->size = $size;
$this->setZsize($size / 3.5);
}
public function getSize() { return $this->size; }
/**
* @param float $zsize
*/
public function setZsize($zsize) { $this->zsize = $zsize; }
public function getZsize() { return $this->zsize; }
/**
* @ORM\PrePersist()
*/
public function onPreInsert() {
$this->setCreatedAt(new \DateTime());
}
private $revisionComment;
public function getRevisionComment() {
return $this->revisionComment;
}
public function setRevisionComment($comment) {
$this->revisionComment = $comment;
}
public function getContentAsSfb() {
$sfb = $this->getFullTitleAsSfb() . "\n\n\n";
$anno = $this->getAnnotation();
if ( ! empty($anno) ) {
$sfb .= "A>\n$anno\nA$\n\n";
}
$sfb .= $this->getRawContent();
$extra = $this->getExtraInfoForDownload();
$extra = preg_replace('/\n\n+/', "\n\n", $extra);
$sfb .= "\nI>\n".trim($extra, "\n")."\nI$\n";
return $sfb;
}
public function getRawContent($asFileName = false) {
if ( ! $this->isCompilation) {
if ($asFileName) {
return ContentService::getInternalContentFilePath('text', $this->getId());
} else {
return ContentService::getContentFile('text', $this->getId());
}
}