%PDF- <> %âãÏÓ endobj 2 0 obj <> endobj 3 0 obj <>/ExtGState<>/ProcSet[/PDF/Text/ImageB/ImageC/ImageI] >>/Annots[ 28 0 R 29 0 R] /MediaBox[ 0 0 595.5 842.25] /Contents 4 0 R/Group<>/Tabs/S>> endobj ºaâÚÎΞ-ÌE1ÍØÄ÷{òò2ÿ ÛÖ^ÔÀá TÎ{¦?§®¥kuµùÕ5sLOšuY>endobj 2 0 obj<>endobj 2 0 obj<>endobj 2 0 obj<>endobj 2 0 obj<> endobj 2 0 obj<>endobj 2 0 obj<>es 3 0 R>> endobj 2 0 obj<> ox[ 0.000000 0.000000 609.600000 935.600000]/Fi endobj 3 0 obj<> endobj 7 1 obj<>/ProcSet[/PDF/Text/ImageB/ImageC/ImageI]>>/Subtype/Form>> stream
<?php
namespace mdm\widgets;
use Yii;
use yii\base\InvalidConfigException;
use yii\base\Model;
use yii\base\Widget;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\web\JsExpression;
use yii\widgets\ActiveForm;
/**
* Description of TabularWidget
*
* @author Misbahul D Munir <misbahuldmunir@gmail.com>
* @since 1.0
*/
abstract class TabularWidget extends Widget
{
public $tag = 'div';
/**
* @var array the HTML attributes for the container tag of the list view.
* The "tag" element specifies the tag name of the container element and defaults to "div".
* @see Html::renderTagAttributes() for details on how attributes are being rendered.
*/
public $options = [];
/**
* @var array the HTML attributes for the container of the rendering result of each data model.
* The "tag" element specifies the tag name of the container element and defaults to "div".
* If "tag" is false, it means no container element will be rendered.
* @see Html::renderTagAttributes() for details on how attributes are being rendered.
*/
public $itemOptions = [];
/**
* @var array the HTML attributes for the container of the rendering result of each data model.
* The "tag" element specifies the tag name of the container element and defaults to "div".
* If "tag" is false, it means no container element will be rendered.
* @see Html::renderTagAttributes() for details on how attributes are being rendered.
*/
public $containerOptions = [];
/**
* @var string the HTML code to be displayed between any two consecutive items.
*/
public $separator = "\n";
/**
* @var Model[]|array
*/
public $allModels = [];
/**
* @var mixed
*/
public $model;
/**
* @var ActiveForm
*/
public $form;
/**
* @var array Client option
*/
public $clientOptions = [];
/**
* @var string
*/
public $layout = "{header}\n{items}";
/**
* Header
* @var string
*/
public $header;
/**
* Footer
* @var string
*/
public $footer;
/**
* Part
* @var array
*/
public $sections = [];
protected $level;
private static $_level = 0;
/**
* Renders a single data model.
* @param mixed $model the data model to be rendered
* @param mixed $key the key value associated with the data model
* @param integer $index the zero-based index of the data model in the model array returned by [[dataProvider]].
* @return string the rendering result
*/
abstract public function renderItem($model, $key, $index);
/**
* @inheritdoc
*/
public function init()
{
$this->level = self::$_level === 0 ? '' : self::$_level;
self::$_level++;
if (!isset($this->options['id'])) {
$this->options['id'] = $this->getId();
}
if (!empty($this->model) && !is_object($this->model)) {
$this->model = Yii::createObject($this->model);
}
}
/**
* @inheritdoc
*/
public function run()
{
if (!empty($this->containerOptions['tag']) && empty($this->clientOptions['container'])) {
Html::addCssClass($this->containerOptions, 'mdm-container');
$this->clientOptions['container'] = "{$this->containerOptions['tag']}.mdm-container";
}
$this->registerClientScript();
$content = preg_replace_callback('/\\{([\w\-\/]+)\\}/', function ($matches) {
$method = 'render' . $matches[1];
if (method_exists($this, $method)) {
return $this->$method();
} elseif (isset($this->sections[$matches[1]])) {
return $matches[1];
}
return $matches[0];
}, $this->layout);
self::$_level--;
echo Html::tag($this->tag, $content, $this->options);
}
/**
* Renders all data models.
* @return string the rendering result
*/
public function renderHeader()
{
return $this->header;
}
/**
* Renders all data models.
* @return string the rendering result
*/
public function renderItems()
{
$rows = [];
$index = 0;
foreach ($this->allModels as $key => $model) {
$rows[] = $this->renderItem($model, $key, $index++);
}
$content = implode($this->separator, $rows);
$options = $this->containerOptions;
if (empty($options['tag'])) {
return $content;
}
$tag = $options['tag'];
unset($options['tag']);
return Html::tag($tag, $content, $options);
}
public function renderFooter()
{
return $this->footer;
}
/**
* Register script
*/
protected function registerClientScript()
{
$id = $this->options['id'];
$options = Json::htmlEncode($this->getClientOptions());
$view = $this->getView();
TabularAsset::register($view);
$view->registerJs("jQuery('#$id').mdmTabularInput($options);");
}
/**
* Get client options
* @return array
*/
protected function getClientOptions()
{
$counter = count($this->allModels) ? max(array_keys($this->allModels)) + 1 : 0;
$clientOptions = $this->clientOptions;
$clientOptions['counter'] = $counter;
if (empty($clientOptions['itemSelector'])) {
throw new InvalidConfigException('Value of "clientOptions[\'itemSelector\']" must be specified.');
}
if ($this->form instanceof ActiveForm) {
$clientOptions['formSelector'] = '#' . $this->form->options['id'];
$oldAttrs = $this->form->attributes;
$this->form->attributes = [];
}
// template and js
$view = $this->getView();
$oldJs = $view->js;
$view->js = [];
$template = $this->renderItem($this->model, "_dkey{$this->level}_", "_dindex{$this->level}_");
if (isset($oldAttrs)) {
$clientOptions['validations'] = $this->form->attributes;
$this->form->attributes = $oldAttrs;
}
$js = [];
ksort($view->js);
foreach ($view->js as $pieces) {
$js[] = implode("\n", $pieces);
}
if (count($js)) {
$clientOptions['templateJs'] = implode("\n", $js);
}
$view->js = $oldJs;
// ***
$clientOptions['template'] = $template;
$clientOptions['replaces'] = [
'key' => new JsExpression("/_dkey{$this->level}_/g"),
'index' => new JsExpression("/_dindex{$this->level}_/g"),
];
return $clientOptions;
}
}