1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByTIC\DocumentGenerator\PdfLetters\Models\PdfLetters; |
4
|
|
|
|
5
|
|
|
use ByTIC\DocumentGenerator\PdfLetters\Models\Downloads\DownloadsTrait; |
6
|
|
|
use ByTIC\DocumentGenerator\PdfLetters\Models\Stats\StatsTrait; |
7
|
|
|
use ByTIC\MediaLibrary\HasMedia\HasMediaRecordsTrait; |
8
|
|
|
use Nip\Records\AbstractModels\RecordManager; |
9
|
|
|
use Nip\Records\Traits\AbstractTrait\RecordsTrait as AbstractRecordsTrait; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class PdfLettersTrait |
13
|
|
|
* @package ByTIC\DocumentGenerator\PdfLetters\Models\PdfLetters |
14
|
|
|
* |
15
|
|
|
* @method PdfLetterTrait[] findByParams($params) |
16
|
|
|
*/ |
17
|
|
|
trait PdfLettersTrait |
18
|
|
|
{ |
19
|
|
|
use AbstractRecordsTrait; |
20
|
|
|
use HasMediaRecordsTrait; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param string $type |
24
|
|
|
* @param int $idItem |
25
|
|
|
* @return bool|PdfLetterTrait |
26
|
|
|
*/ |
27
|
|
|
public function getByItem($type, $idItem) |
28
|
|
|
{ |
29
|
|
|
/** @var PdfLetterTrait[] $letters */ |
30
|
|
|
$letters = $this->findByParams( |
31
|
|
|
[ |
32
|
|
|
'where' => [ |
33
|
|
|
['id_item = ?', $idItem], |
34
|
|
|
['type = ?', $type], |
35
|
|
|
], |
36
|
|
|
'order' => [['id', 'DESC']], |
37
|
|
|
] |
38
|
|
|
); |
39
|
|
|
|
40
|
|
|
$return = false; |
41
|
|
|
|
42
|
|
|
if (count($letters)) { |
43
|
|
|
foreach ($letters as $letter) { |
44
|
|
|
if ($letter->hasFile() && !$return) { |
45
|
|
|
$return = $letter; |
46
|
|
|
} else { |
47
|
|
|
// $item->delete(); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $return; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return DownloadsTrait|RecordManager |
57
|
|
|
*/ |
58
|
|
|
public function getDownloadsManager() |
59
|
|
|
{ |
60
|
|
|
return $this->getRelation('Downloads')->getWith(); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return StatsTrait|RecordManager |
65
|
|
|
*/ |
66
|
|
|
public function getStatsManager() |
67
|
|
|
{ |
68
|
|
|
return $this->getRelation('Stats')->getWith(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param $type |
73
|
|
|
* @return AbstractRecordsTrait |
74
|
|
|
*/ |
75
|
|
|
abstract public function getParentManagerFromType($type); |
76
|
|
|
|
77
|
|
|
protected function initRelations() |
78
|
|
|
{ |
79
|
|
|
/** @noinspection PhpUndefinedClassInspection */ |
80
|
|
|
parent::initRelations(); |
81
|
|
|
$this->initRelationsTrait(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
protected function initRelationsTrait() |
85
|
|
|
{ |
86
|
|
|
$this->initCustomFieldsRelation(); |
87
|
|
|
$this->initDownloadsRelation(); |
88
|
|
|
$this->initStatsRelation(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
protected function initCustomFieldsRelation() |
92
|
|
|
{ |
93
|
|
|
$this->hasMany('CustomFields', $this->getCustomFieldsRelationParams()); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
protected function initDownloadsRelation() |
97
|
|
|
{ |
98
|
|
|
if (method_exists($this, 'getDownloadsManagerClass')) { |
99
|
|
|
$this->hasMany('Downloads', [ |
100
|
|
|
'class' => $this->getDownloadsManagerClass(), |
101
|
|
|
'fk' => $this->getPrimaryFK(), |
102
|
|
|
]); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
protected function initStatsRelation() |
107
|
|
|
{ |
108
|
|
|
if (method_exists($this, 'getStatsManagerClass')) { |
109
|
|
|
$this->hasMany('Stats', [ |
110
|
|
|
'class' => $this->getStatsManagerClass(), |
111
|
|
|
'fk' => $this->getPrimaryFK(), |
112
|
|
|
]); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return array |
118
|
|
|
*/ |
119
|
|
|
protected function getCustomFieldsRelationParams() |
120
|
|
|
{ |
121
|
|
|
return [ |
122
|
|
|
'class' => $this->getCustomFieldsManagerClass(), |
123
|
|
|
'fk' => $this->getPrimaryFK(), |
124
|
|
|
]; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
|
|
abstract protected function getCustomFieldsManagerClass(); |
131
|
|
|
|
132
|
|
|
// /** |
133
|
|
|
// * @return string |
134
|
|
|
// */ |
135
|
|
|
// abstract protected function getDownloadsManagerClass(); |
136
|
|
|
|
137
|
|
|
// /** |
138
|
|
|
// * @return string |
139
|
|
|
// */ |
140
|
|
|
// abstract protected function getStatsManagerClass(); |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return string |
144
|
|
|
*/ |
145
|
|
|
abstract public function getPrimaryFK(); |
146
|
|
|
} |
147
|
|
|
|