| 1 |
<?php |
| 2 |
namespace Depicter\Front; |
| 3 |
|
| 4 |
|
| 5 |
use Averta\Core\Utility\Arr; |
| 6 |
use Depicter\Document\Models\Document; |
| 7 |
use Depicter\Html\Html; |
| 8 |
|
| 9 |
class Preview |
| 10 |
{ |
| 11 |
|
| 12 |
/** |
| 13 |
* Preview constructor. |
| 14 |
*/ |
| 15 |
public function __construct() |
| 16 |
{ |
| 17 |
|
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Prepares viewArgs for document preview |
| 22 |
* |
| 23 |
* @return array |
| 24 |
*/ |
| 25 |
public function prepare() |
| 26 |
{ |
| 27 |
// prevent enabling preview mode if render mode param was passed |
| 28 |
if( empty( $_REQUEST['renderMode'] ) ){ |
| 29 |
$this->enablePreviewMode(); |
| 30 |
} |
| 31 |
|
| 32 |
$viewArgs = [ |
| 33 |
'head' => '', |
| 34 |
'title' => __( 'Depicter Preview', 'depicter' ), |
| 35 |
'footer' => '' |
| 36 |
]; |
| 37 |
|
| 38 |
$styleLinks = \Depicter::front()->assets()->getStyles(['common', 'situational']); |
| 39 |
|
| 40 |
foreach ( $styleLinks as $styleId => $styleLink ) { |
| 41 |
$viewArgs['head'] .= Html::link([ |
| 42 |
'rel' => "stylesheet", |
| 43 |
'id' => $styleId . '-css', |
| 44 |
'href' => $styleLink, |
| 45 |
'media' => 'all' |
| 46 |
]) . "\n"; |
| 47 |
} |
| 48 |
|
| 49 |
$scriptLinks = \Depicter::front()->assets()->getScripts('player'); |
| 50 |
|
| 51 |
foreach ( $scriptLinks as $scriptId => $scriptLink ){ |
| 52 |
$viewArgs['footer'] .= Html::script([ |
| 53 |
'id' => $scriptId . '-js', |
| 54 |
'src' => $scriptLink |
| 55 |
]) . "\n"; |
| 56 |
} |
| 57 |
|
| 58 |
return $viewArgs; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Generates a complete HTML page to preview document |
| 63 |
* |
| 64 |
* @param $documentId |
| 65 |
* @param array $documentArgs |
| 66 |
* |
| 67 |
* @return string|array |
| 68 |
*/ |
| 69 |
public function document( $documentId, $documentArgs = [] ) |
| 70 |
{ |
| 71 |
$defaults = [ |
| 72 |
'status' => 'draft', |
| 73 |
'start' => null |
| 74 |
]; |
| 75 |
$documentArgs = Arr::merge( $documentArgs, $defaults ); |
| 76 |
|
| 77 |
$viewArgs = $this->prepare(); |
| 78 |
$where = [ 'status' => trim( $documentArgs['status'] ) ]; |
| 79 |
|
| 80 |
// support for multiple statuses (status=publish|draft) |
| 81 |
if( strpos( $where['status'], '|' ) !== false ){ |
| 82 |
$where['status'] = explode('|', $where['status'] ); |
| 83 |
} |
| 84 |
|
| 85 |
$where['start'] = $documentArgs['start']; |
| 86 |
|
| 87 |
try{ |
| 88 |
$documentModel = \Depicter::document()->getModel( $documentId, $where ); |
| 89 |
$documentModel->setUnpublishedNotice( false ); |
| 90 |
$viewArgs = $this->prepareToRender( $documentModel, $viewArgs ); |
| 91 |
|
| 92 |
if ( !empty( $documentArgs['viewParts'] ) ) { |
| 93 |
return $viewArgs; |
| 94 |
} |
| 95 |
} catch ( \Exception $exception ) { |
| 96 |
$viewArgs['content'] = '<span class="depicter-no-content">' . $exception->getMessage() . '</span>'; |
| 97 |
} |
| 98 |
|
| 99 |
return $this->view( $viewArgs ); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* prepare document model to render |
| 104 |
* |
| 105 |
* @param Document $documentModel |
| 106 |
* @param $viewArgs |
| 107 |
* |
| 108 |
* @return mixed |
| 109 |
*/ |
| 110 |
protected function prepareToRender( $documentModel, $viewArgs ) |
| 111 |
{ |
| 112 |
// Prepare and render document |
| 113 |
$viewArgs['content'] = Html::div( [ 'class' => 'depicter-preview-canvas' ], $documentModel->prepare() |
| 114 |
->render() ); |
| 115 |
|
| 116 |
// Link to special styles |
| 117 |
$stylesList = []; |
| 118 |
if( $fontsLink = $documentModel->getFontsLink() ){ |
| 119 |
$stylesList["depicter-google-fonts"] = $fontsLink; |
| 120 |
} |
| 121 |
foreach( $stylesList as $styleId => $styleLink ){ |
| 122 |
$viewArgs['head'] .= Html::link( [ 'rel' => "stylesheet", |
| 123 |
'id' => $styleId . '-css', |
| 124 |
'href' => $styleLink, |
| 125 |
'media' => 'all' ] ) . "\n"; |
| 126 |
} |
| 127 |
|
| 128 |
// Add custom styles |
| 129 |
$viewArgs['head'] .= Html::style( [ 'type' => 'text/css' ], $documentModel->getCss() ) . "\n"; |
| 130 |
|
| 131 |
$documentID = $documentModel->getDocumentID(); |
| 132 |
if ( $document = \Depicter::documentRepository()->findById($documentID) ) { |
| 133 |
$documentName = $document->getFieldValue('name'); |
| 134 |
$viewArgs['title'] = empty( $documentName ) ? $viewArgs['title'] : $documentName; |
| 135 |
} |
| 136 |
|
| 137 |
return $viewArgs; |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Renders a page with message |
| 142 |
* |
| 143 |
* @param $content |
| 144 |
* |
| 145 |
* @return string |
| 146 |
*/ |
| 147 |
public function message( $content ) |
| 148 |
{ |
| 149 |
$viewArgs = $this->prepare(); |
| 150 |
$viewArgs['content'] = '<span class="depicter-no-content">' . $content . '</span>'; |
| 151 |
|
| 152 |
return $this->view( $viewArgs ); |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Renders page view |
| 157 |
* |
| 158 |
* @param $args |
| 159 |
* |
| 160 |
* @return string |
| 161 |
*/ |
| 162 |
protected function view( $args ){ |
| 163 |
return \Depicter::view('canvas.php')->with( 'view_args', $args )->toString(); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Set a constant to specify document preview mode |
| 168 |
*/ |
| 169 |
protected function enablePreviewMode() { |
| 170 |
if ( !defined('IS_DEPICTER_PREVIEW') ) { |
| 171 |
define( 'IS_DEPICTER_PREVIEW', true ); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Check if preview mode is active or not |
| 177 |
* |
| 178 |
* @return bool |
| 179 |
*/ |
| 180 |
public function isPreview() { |
| 181 |
return defined( 'IS_DEPICTER_PREVIEW' ) && IS_DEPICTER_PREVIEW; |
| 182 |
} |
| 183 |
} |
| 184 |
|