PluginProbe
Depicter — Popup & Slider Builder / trunk
Depicter — Popup & Slider Builder vtrunk
4.8.1 trunk 1.0.0 1.1.0 1.1.2 1.1.4 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.5 1.3.8 1.5.0 1.5.1 1.5.2 1.5.5 1.6.0 1.6.1 1.6.2 1.7.0 All 76 releases
depicter / app / src / Document / Manager.php

Manager.php in Depicter — Popup & Slider Builder trunk, at app/src/Document/Manager.php

259 lines 5.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Depicter\Document;
3
4
5 use Averta\WordPress\Utility\JSON;
6 use Depicter\Database\Repository\DocumentRepository;
7 use Depicter\Database\Repository\MetaRepository;
8 use Depicter\Document\Models\Document;
9 use Depicter\Exception\DocumentNoContentException;
10 use Depicter\Exception\EntityException;
11 use Depicter\Front\Preview;
12 use Depicter\Rules\DisplayRules;
13
14 class Manager
15 {
16
17 public function bootstrap()
18 {
19 }
20
21 /**
22 * Returns the instance of preview class
23 *
24 * @return Preview
25 */
26 public function preview()
27 {
28 return \Depicter::resolve('depicter.front.document.preview');
29 }
30
31 /**
32 * Returns the instance of documentRepository class
33 *
34 * @return DocumentRepository
35 */
36 public function repository()
37 {
38 return \Depicter::resolve('depicter.database.repository.document');
39 }
40
41 /**
42 * Returns the instance of metaRepository class
43 *
44 * @return MetaRepository
45 */
46 public function meta()
47 {
48 return \Depicter::resolve('depicter.database.repository.meta');
49 }
50
51 /**
52 * Retrieves object of document editor data
53 *
54 * @param $documentId
55 * @param array $where
56 *
57 * @return bool|mixed
58 * @throws EntityException
59 */
60 public function getEditorData( $documentId, $where = [] ){
61 if( ! $documentEditorJson = $this->repository()->getContent( $documentId, $where ) ){
62 throw new DocumentNoContentException( 'No content yet.', 0, $where );
63 }
64 $data = JSON::decode( $documentEditorJson, false );
65 unset( $data->computedValues );
66 return $data;
67 }
68
69 /**
70 * Retrieves json of document editor data
71 *
72 * @param $documentId
73 * @param array $where
74 *
75 * @return bool|mixed
76 */
77 public function getEditorRawData( $documentId, $where = [] ){
78 try{
79 if( ! $documentEditorJson = $this->repository()->getContent( $documentId, $where ) ){
80 return '';
81 }
82 } catch( \Exception $e ){
83 return '';
84 }
85 return $documentEditorJson;
86 }
87
88 /**
89 * Converts document slug or alias to document ID
90 *
91 * @param string|array $documentIDs A document ID/Slug/Alias or list of them
92 *
93 * @return mixed
94 */
95 public function getID( $documentIDs ){
96 if( empty( $documentIDs ) ){
97 return false;
98 }
99
100 try{
101 if( is_array( $documentIDs ) ){
102 $realDocumentIDs = [];
103 foreach( $documentIDs as $documentID ){
104 $realDocumentIDs = $this->getID( $documentID );
105 }
106 return $realDocumentIDs;
107
108 } elseif ( ! is_numeric( $documentIDs ) && is_string( $documentIDs ) ) {
109 if ( $document = \Depicter::document()->repository()->findOne( null, ['slug' => $documentIDs] ) ) {
110 return $document->getID();
111 }
112 } else {
113 return $documentIDs;
114 }
115 } catch( \Exception $e ){
116 return false;
117 }
118
119 return false;
120 }
121
122 /**
123 * Get a document model by ID
124 *
125 * @param $documentId
126 * @param array $where
127 *
128 * @return bool|Document
129 * @throws EntityException
130 * @throws \JsonMapper_Exception
131 */
132 public function getModel( $documentId, $where = [] ){
133 $startSection = 0;
134
135 if( array_key_exists( 'start', $where ) ){
136 $startSection = (int) $where['start'];
137 unset( $where['start'] );
138 }
139
140 if ( ! $editorDataArray = $this->getEditorData( $documentId, $where ) ) {
141 return false;
142 }
143
144 $editorDataArray = \Depicter::document()->migrations()->apply( $editorDataArray );
145
146 $editorDataArray->startSection = $startSection;
147
148 // make document model base on editor data
149 $mapper = new Mapper();
150 $documentModel = $mapper->hydrate( $editorDataArray, $documentId )->get();
151
152 // set document entity properties
153 $documentModel->setDocumentId( $documentId );
154 $documentModel->setShowAdminNotice( $documentId );
155 $documentModel->setEntityProperty( 'status', $this->getStatus( $documentId ) );
156
157 return $documentModel;
158 }
159
160 /**
161 * Retrieves custom css file of a document if exists
162 *
163 * @param int $documentId
164 * @param array $where
165 *
166 * @return bool|string
167 */
168 public function getCssFileUrl( $documentId, $where = [] ){
169 try{
170 if( $document = $this->getModel( $documentId, $where = [] ) ){
171 if( $cssFile = $document->styleGenerator()->getCssFileUrl() ){
172 return $cssFile;
173 }
174 }
175 } catch( \Exception $e ){}
176
177 return false;
178 }
179
180
181 /**
182 * Cache custom styles for a document
183 *
184 * @param $documentId
185 *
186 * @return void
187 */
188 public function cacheCustomStyles( $documentId )
189 {
190 if( ! \Depicter::cache( 'document' )->get( $documentId . '_css_files' ) && ! \Depicter::authorization()->currentUserCanPublishDocument() ){
191 $where = [ 'status' => 'publish' ];
192 try {
193 if( $documentModel = $this->getModel( $documentId, $where ) ){
194
195 $documentModel->prepare()->render();
196 $documentModel->styleGenerator();
197 $documentModel->saveCss();
198
199 $cssLinksToEnqueue = $documentModel->getCustomCssFiles( 'all' );
200
201 \Depicter::cache('document')->set( $documentId . '_css_files', $cssLinksToEnqueue, WEEK_IN_SECONDS );
202 }
203 } catch( EntityException|\JsonMapper_Exception $e ){
204 }
205 }
206 }
207
208 /**
209 * Get status of document
210 *
211 * @param int $documentID
212 * @return string
213 */
214 public function getStatus( $documentID ) {
215 return $this->repository()->getStatus( $documentID );
216 }
217
218 /**
219 * Get displayRules for a document
220 *
221 * @param int $documentID
222 *
223 * @return DisplayRules
224 */
225 public function displayRules( $documentID ){
226 return new DisplayRules( $documentID );
227 }
228
229
230 /**
231 * Retrieves IDs of all conditional documents
232 *
233 * @return array
234 */
235 public function getConditionalDocumentIDs( $force_flush = false ){
236
237 $conditionalDocumentIDs = \Depicter::cache('base')->get( '_conditional_document_ids' );
238
239 if( ! $force_flush && ( false !== $conditionalDocumentIDs ) ){
240 return $conditionalDocumentIDs;
241 }
242
243 $conditionalDocumentIDs = $this->repository()->getConditionalDocumentIDs();
244
245 \Depicter::cache('base')->set( '_conditional_document_ids', $conditionalDocumentIDs, HOUR_IN_SECONDS );
246
247 return $conditionalDocumentIDs;
248 }
249
250 /**
251 * do migration related to document Data
252 *
253 * @return \Depicter\Document\Migrations\DocumentMigration
254 */
255 public function migrations() {
256 return \Depicter::documentMigration();
257 }
258 }
259