| 1 |
<?php |
| 2 |
namespace Depicter\Document; |
| 3 |
|
| 4 |
|
| 5 |
use Averta\WordPress\Utility\JSON; |
| 6 |
use Depicter\Database\Repository\DocumentRepository; |
| 7 |
use Depicter\Document\Models\Document; |
| 8 |
use Depicter\Exception\DocumentNoContentException; |
| 9 |
use Depicter\Exception\EntityException; |
| 10 |
use Depicter\Front\Preview; |
| 11 |
|
| 12 |
class Manager |
| 13 |
{ |
| 14 |
|
| 15 |
public function bootstrap() |
| 16 |
{ |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Returns the instance of preview class |
| 21 |
* |
| 22 |
* @return Preview |
| 23 |
*/ |
| 24 |
public function preview() |
| 25 |
{ |
| 26 |
return \Depicter::resolve('depicter.front.document.preview'); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Returns the instance of documentRepository class |
| 31 |
* |
| 32 |
* @return DocumentRepository |
| 33 |
*/ |
| 34 |
public function repository() |
| 35 |
{ |
| 36 |
return \Depicter::resolve('depicter.database.repository.document'); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Retrieves object of document editor data |
| 41 |
* |
| 42 |
* @param $documentId |
| 43 |
* @param array $where |
| 44 |
* |
| 45 |
* @return bool|mixed |
| 46 |
* @throws EntityException |
| 47 |
*/ |
| 48 |
public function getEditorData( $documentId, $where = [] ){ |
| 49 |
if( ! $documentEditorJson = $this->repository()->geContent( $documentId, $where ) ){ |
| 50 |
throw new DocumentNoContentException( 'No content yet.', 0, $where ); |
| 51 |
} |
| 52 |
$data = JSON::decode( $documentEditorJson, false ); |
| 53 |
unset( $data->computedValues ); |
| 54 |
return $data; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Retrieves json of document editor data |
| 59 |
* |
| 60 |
* @param $documentId |
| 61 |
* @param array $where |
| 62 |
* |
| 63 |
* @return bool|mixed |
| 64 |
*/ |
| 65 |
public function getEditorRawData( $documentId, $where = [] ){ |
| 66 |
try{ |
| 67 |
if( ! $documentEditorJson = $this->repository()->geContent( $documentId, $where ) ){ |
| 68 |
return ''; |
| 69 |
} |
| 70 |
} catch( \Exception $e ){ |
| 71 |
return ''; |
| 72 |
} |
| 73 |
return $documentEditorJson; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Converts document slug or alias to document ID |
| 78 |
* |
| 79 |
* @param string|array $documentIDs A document ID/Slug/Alias or list of them |
| 80 |
* |
| 81 |
* @return mixed |
| 82 |
*/ |
| 83 |
public function getID( $documentIDs ){ |
| 84 |
if( empty( $documentIDs ) ){ |
| 85 |
return false; |
| 86 |
} |
| 87 |
|
| 88 |
try{ |
| 89 |
if( is_array( $documentIDs ) ){ |
| 90 |
$realDocumentIDs = []; |
| 91 |
foreach( $documentIDs as $documentID ){ |
| 92 |
$realDocumentIDs = $this->getID( $documentID ); |
| 93 |
} |
| 94 |
return $realDocumentIDs; |
| 95 |
|
| 96 |
} elseif ( ! is_numeric( $documentIDs ) && is_string( $documentIDs ) ) { |
| 97 |
if ( $document = \Depicter::document()->repository()->findOne( null, ['slug' => $documentIDs] ) ) { |
| 98 |
return $document->getID(); |
| 99 |
} |
| 100 |
} else { |
| 101 |
return $documentIDs; |
| 102 |
} |
| 103 |
} catch( \Exception $e ){ |
| 104 |
return false; |
| 105 |
} |
| 106 |
|
| 107 |
return false; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Get a document model by ID |
| 112 |
* |
| 113 |
* @param $documentId |
| 114 |
* @param array $where |
| 115 |
* |
| 116 |
* @return bool|Document |
| 117 |
* @throws EntityException |
| 118 |
* @throws \JsonMapper_Exception |
| 119 |
*/ |
| 120 |
public function getModel( $documentId, $where = [] ){ |
| 121 |
$startSection = 0; |
| 122 |
|
| 123 |
if( array_key_exists( 'start', $where ) ){ |
| 124 |
$startSection = (int) $where['start']; |
| 125 |
unset( $where['start'] ); |
| 126 |
} |
| 127 |
|
| 128 |
if ( ! $editorDataArray = $this->getEditorData( $documentId, $where ) ) { |
| 129 |
return false; |
| 130 |
} |
| 131 |
|
| 132 |
$editorDataArray->startSection = $startSection; |
| 133 |
|
| 134 |
// make document model base on editor data |
| 135 |
$mapper = new Mapper(); |
| 136 |
$documentModel = $mapper->hydrate( $editorDataArray, $documentId )->get(); |
| 137 |
|
| 138 |
// set document entity properties |
| 139 |
$documentModel->setDocumentId( $documentId ); |
| 140 |
$documentModel->setUnpublishedNotice( $documentId ); |
| 141 |
$documentModel->setEntityProperty( 'status', $this->getStatus( $documentId ) ); |
| 142 |
|
| 143 |
return $documentModel; |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Retrieves custom css file of a document if exists |
| 148 |
* |
| 149 |
* @param int $documentId |
| 150 |
* @param array $where |
| 151 |
* |
| 152 |
* @return bool|string |
| 153 |
*/ |
| 154 |
public function getCssFileUrl( $documentId, $where = [] ){ |
| 155 |
try{ |
| 156 |
if( $document = $this->getModel( $documentId, $where = [] ) ){ |
| 157 |
if( $cssFile = $document->styleGenerator()->getCssFileUrl() ){ |
| 158 |
return $cssFile; |
| 159 |
} |
| 160 |
} |
| 161 |
} catch( \Exception $e ){} |
| 162 |
|
| 163 |
return false; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
/** |
| 168 |
* Get status of document |
| 169 |
* |
| 170 |
* @param int $documentID |
| 171 |
* @return string |
| 172 |
*/ |
| 173 |
public function getStatus( $documentID ) { |
| 174 |
return \Depicter::document()->repository()->getStatus( $documentID ); |
| 175 |
} |
| 176 |
} |
| 177 |
|