2014-09-29 12:19:19 +10:00
|
|
|
<?php namespace System\Traits;
|
2014-05-14 23:24:20 +10:00
|
|
|
|
|
|
|
use File;
|
|
|
|
use Lang;
|
|
|
|
use Block;
|
|
|
|
use System\Classes\SystemException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* View Maker Trait
|
|
|
|
* Adds view based methods to a class
|
|
|
|
*
|
|
|
|
* @package october\backend
|
|
|
|
* @author Alexey Bobkov, Samuel Georges
|
|
|
|
*/
|
|
|
|
|
|
|
|
trait ViewMaker
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var array A list of variables to pass to the page.
|
|
|
|
*/
|
|
|
|
public $vars = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string Specifies a path to the views directory.
|
|
|
|
*/
|
2014-08-03 11:57:51 +10:00
|
|
|
protected $viewPath;
|
2014-05-14 23:24:20 +10:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string Specifies a path to the layout directory.
|
|
|
|
*/
|
2014-08-03 11:57:51 +10:00
|
|
|
protected $layoutPath;
|
2014-05-14 23:24:20 +10:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string Layout to use for the view.
|
|
|
|
*/
|
|
|
|
public $layout;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool Prevents the use of a layout.
|
|
|
|
*/
|
|
|
|
public $suppressLayout = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render a partial file contents located in the views folder.
|
|
|
|
* @param string $partial The view to load.
|
|
|
|
* @param array $params Parameter variables to pass to the view.
|
2014-07-29 17:50:49 +10:00
|
|
|
* @param bool $throwException Throw an exception if the partial is not found.
|
|
|
|
* @return mixed Partial contents or false if not throwing an exception.
|
2014-05-14 23:24:20 +10:00
|
|
|
*/
|
2014-06-20 21:36:27 +10:00
|
|
|
public function makePartial($partial, $params = [], $throwException = true)
|
2014-05-14 23:24:20 +10:00
|
|
|
{
|
2014-09-29 12:32:07 +10:00
|
|
|
if (!File::isPathSymbol($partial) && realpath($partial) === false)
|
2014-05-14 23:24:20 +10:00
|
|
|
$partial = '_' . strtolower($partial) . '.htm';
|
|
|
|
|
|
|
|
$partialPath = $this->getViewPath($partial);
|
|
|
|
|
2014-06-20 21:36:27 +10:00
|
|
|
if (!File::isFile($partialPath)) {
|
|
|
|
if ($throwException)
|
|
|
|
throw new SystemException(Lang::get('backend::lang.partial.not_found', ['name' => $partialPath]));
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
2014-05-14 23:24:20 +10:00
|
|
|
|
|
|
|
return $this->makeFileContents($partialPath, $params);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads a view with the name specified. Applies layout if its name is provided by the parent object.
|
|
|
|
* The view file must be situated in the views directory, and has the extension "htm".
|
|
|
|
* @param string $view Specifies the view name, without extension. Eg: "index".
|
|
|
|
*/
|
|
|
|
public function makeView($view)
|
|
|
|
{
|
|
|
|
$viewPath = $this->getViewPath(strtolower($view) . '.htm');
|
|
|
|
$contents = $this->makeFileContents($viewPath);
|
|
|
|
return $this->makeViewContent($contents);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders supplied contents inside a layout.
|
|
|
|
* @param string $contents The inner contents as a string.
|
|
|
|
* @param string $name Specifies the layout name.
|
|
|
|
* If this parameter is omitted, the $layout property will be used.
|
|
|
|
* @return string The layout contents
|
|
|
|
*/
|
|
|
|
public function makeViewContent($contents, $layout = null)
|
|
|
|
{
|
|
|
|
if ($this->suppressLayout || $this->layout == '')
|
|
|
|
return $contents;
|
|
|
|
|
|
|
|
// Append any undefined block content to the body block
|
|
|
|
Block::set('undefinedBlock', $contents);
|
|
|
|
Block::append('body', Block::get('undefinedBlock'));
|
|
|
|
return $this->makeLayout();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render a layout.
|
|
|
|
* @param string $name Specifies the layout name.
|
|
|
|
* If this parameter is omitted, the $layout property will be used.
|
|
|
|
* @param array $params Parameter variables to pass to the view.
|
2014-06-20 21:36:27 +10:00
|
|
|
* @param bool $throwException Throw an exception if the layout is not found
|
2014-05-14 23:24:20 +10:00
|
|
|
* @return string The layout contents
|
|
|
|
*/
|
2014-06-20 21:36:27 +10:00
|
|
|
public function makeLayout($name = null, $params = [], $throwException = true)
|
2014-05-14 23:24:20 +10:00
|
|
|
{
|
|
|
|
$layout = ($name === null) ? $this->layout : $name;
|
|
|
|
if ($layout == '')
|
|
|
|
return;
|
|
|
|
|
|
|
|
$layoutPath = $this->getViewPath($layout . '.htm', $this->layoutPath);
|
|
|
|
|
2014-06-20 21:36:27 +10:00
|
|
|
if (!File::isFile($layoutPath)) {
|
|
|
|
if ($throwException)
|
|
|
|
throw new SystemException(Lang::get('cms::lang.layout.not_found', ['name' => $layoutPath]));
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
2014-05-14 23:24:20 +10:00
|
|
|
|
|
|
|
return $this->makeFileContents($layoutPath, $params);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders a layout partial
|
|
|
|
* @param string $partial The view to load.
|
|
|
|
* @param array $params Parameter variables to pass to the view.
|
|
|
|
* @return string The layout partial contents
|
|
|
|
*/
|
|
|
|
public function makeLayoutPartial($partial, $params = [])
|
|
|
|
{
|
2014-09-29 12:59:14 +10:00
|
|
|
if (!File::isLocalPath($partial) && !File::isPathSymbol($partial))
|
2014-05-14 23:24:20 +10:00
|
|
|
$partial = '_' . strtolower($partial);
|
|
|
|
|
|
|
|
return $this->makeLayout($partial, $params);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Locates a file based on it's definition. If the file starts with
|
|
|
|
* an "at symbol", it will be returned in context of the application base path,
|
|
|
|
* otherwise it will be returned in context of the view path.
|
|
|
|
* @param string $fileName File to load.
|
|
|
|
* @param mixed $viewPath Explicitly define a view path.
|
|
|
|
* @return string Full path to the view file.
|
|
|
|
*/
|
|
|
|
public function getViewPath($fileName, $viewPath = null)
|
|
|
|
{
|
|
|
|
if (!isset($this->viewPath))
|
|
|
|
$this->viewPath = $this->guessViewPath();
|
|
|
|
|
|
|
|
if (!$viewPath)
|
|
|
|
$viewPath = $this->viewPath;
|
|
|
|
|
2014-09-29 12:32:07 +10:00
|
|
|
$fileName = File::symbolizePath($fileName, $fileName);
|
2014-05-14 23:24:20 +10:00
|
|
|
|
2014-09-29 12:59:14 +10:00
|
|
|
if (File::isLocalPath($fileName) || realpath($fileName) !== false)
|
2014-05-14 23:24:20 +10:00
|
|
|
return $fileName;
|
|
|
|
|
|
|
|
if (!is_array($viewPath))
|
|
|
|
$viewPath = [$viewPath];
|
|
|
|
|
|
|
|
foreach ($viewPath as $path) {
|
|
|
|
$_fileName = $path . '/' . $fileName;
|
|
|
|
if (File::isFile($_fileName))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $_fileName;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Includes a file path using output buffering.
|
|
|
|
* Ensures that vars are available.
|
|
|
|
* @param string $filePath Absolute path to the view file.
|
|
|
|
* @param array $extraParams Parameters that should be available to the view.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function makeFileContents($filePath, $extraParams = [])
|
|
|
|
{
|
|
|
|
if (!strlen($filePath) || !File::isFile($filePath))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!is_array($extraParams))
|
|
|
|
$extraParams = [];
|
|
|
|
|
|
|
|
$vars = array_merge($this->vars, $extraParams);
|
|
|
|
|
|
|
|
ob_start();
|
|
|
|
extract($vars);
|
|
|
|
include $filePath;
|
|
|
|
return ob_get_clean();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Guess the package path for the called class.
|
|
|
|
* @param string $suffix An extra path to attach to the end
|
|
|
|
* @param bool $isPublic Returns public path instead of an absolute one
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function guessViewPath($suffix = '', $isPublic = false)
|
|
|
|
{
|
|
|
|
$class = get_called_class();
|
|
|
|
return $this->guessViewPathFrom($class, $suffix, $isPublic);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Guess the package path from a specified class.
|
|
|
|
* @param string $class Class to guess path from.
|
|
|
|
* @param string $suffix An extra path to attach to the end
|
|
|
|
* @param bool $isPublic Returns public path instead of an absolute one
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function guessViewPathFrom($class, $suffix = '', $isPublic = false)
|
|
|
|
{
|
2014-09-29 13:12:34 +10:00
|
|
|
$classFolder = strtolower(class_basename($class));
|
2014-05-14 23:24:20 +10:00
|
|
|
$classFile = realpath(dirname(File::fromClass($class)));
|
|
|
|
$guessedPath = $classFile ? $classFile . '/' . $classFolder . $suffix : null;
|
|
|
|
return ($isPublic) ? File::localToPublic($guessedPath) : $guessedPath;
|
|
|
|
}
|
|
|
|
}
|