public function __construct($name = null, Mesour\Components\ComponentModel\IContainer $parent = null) { parent::__construct($name, $parent); $attributes = $this->getOption(self::WRAPPER, 'attributes'); $attributes['data-type'] = 'date'; $this->setOption(self::WRAPPER, $attributes, 'attributes'); $oneDay = 60 * 60 * 24; $quarter = $this->dateQuarter(); $data = ['YESTERDAY' => date('Y-m-d', strtotime('yesterday midnight')), 'TODAY' => date('Y-m-d', strtotime('today midnight')), 'TOMORROW' => date('Y-m-d', strtotime('tomorrow midnight')), 'LAST_WEEK_FIRST' => date('Y-m-d', ($lastWeekMonday = strtotime('monday', strtotime('last week'))) - $oneDay), 'LAST_WEEK_SECOND' => date('Y-m-d', $lastWeekMonday + 7 * $oneDay), 'THIS_WEEK_FIRST' => date('Y-m-d', ($thisWeekMonday = strtotime('last monday midnight')) - $oneDay), 'THIS_WEEK_SECOND' => date('Y-m-d', $thisWeekMonday + 7 * $oneDay), 'NEXT_WEEK_FIRST' => date('Y-m-d', $thisWeekMonday + 7 * $oneDay - $oneDay), 'NEXT_WEEK_SECOND' => date('Y-m-d', $thisWeekMonday + 14 * $oneDay), 'LAST_MONTH_FIRST' => date('Y-m-d', strtotime(date('1-n-Y', $lastMonth = strtotime('last month'))) - $oneDay), 'LAST_MONTH_SECOND' => date('Y-m-d', strtotime(date('t-n-Y', $lastMonth)) + $oneDay), 'THIS_MONTH_FIRST' => date('Y-m-d', strtotime(date('1-n-Y')) - $oneDay), 'THIS_MONTH_SECOND' => date('Y-m-d', strtotime(date('t-n-Y')) + $oneDay), 'NEXT_MONTH_FIRST' => date('Y-m-d', strtotime(date('1-n-Y', $nextMonth = strtotime('next month'))) - $oneDay), 'NEXT_MONTH_SECOND' => date('Y-m-d', strtotime(date('t-n-Y', $nextMonth)) + $oneDay), 'LAST_QUARTER_FIRST' => date('Y-m-d', $this->getStartTimestampForQuarter($quarter - 1 < 1 ? 4 : $quarter - 1, $quarter - 1 < 1 ? date('Y', strtotime('last year')) : date('Y')) - $oneDay), 'LAST_QUARTER_SECOND' => date('Y-m-d', $this->getEndTimestampForQuarter($quarter - 1 < 1 ? 4 : $quarter - 1, $quarter - 1 < 1 ? date('Y', strtotime('last year')) : date('Y')) + $oneDay), 'THIS_QUARTER_FIRST' => date('Y-m-d', $this->getStartTimestampForQuarter($quarter) - $oneDay), 'THIS_QUARTER_SECOND' => date('Y-m-d', $this->getEndTimestampForQuarter($quarter) + $oneDay), 'NEXT_QUARTER_FIRST' => date('Y-m-d', $this->getStartTimestampForQuarter($quarter + 1 > 4 ? 1 : $quarter + 1, $quarter + 1 > 4 ? date('Y', strtotime('next year')) : date('Y')) - $oneDay), 'NEXT_QUARTER_SECOND' => date('Y-m-d', $this->getEndTimestampForQuarter($quarter + 1 > 4 ? 1 : $quarter + 1, $quarter + 1 > 4 ? date('Y', strtotime('next year')) : date('Y')) + $oneDay), 'LAST_YEAR_FIRST' => date('Y-m-d', strtotime(date('1-1-Y', $nextYear = strtotime('last year'))) - $oneDay), 'LAST_YEAR_SECOND' => date('Y-m-d', strtotime(date('31-12-Y', $nextYear)) + $oneDay), 'THIS_YEAR_FIRST' => date('Y-m-d', strtotime(date('1-1-Y')) - $oneDay), 'THIS_YEAR_SECOND' => date('Y-m-d', strtotime(date('31-12-Y')) + $oneDay), 'NEXT_YEAR_FIRST' => date('Y-m-d', strtotime(date('1-1-Y', strtotime('next year'))) - $oneDay), 'NEXT_YEAR_SECOND' => date('Y-m-d', strtotime(date('31-12-Y', strtotime('next year'))) + $oneDay)]; foreach ($this->filters as $currentId => $filter) { if (isset($filter['type']) && is_array($filter['type'])) { foreach ($filter['type'] as $currentInnerId => $currentFilter) { if (isset($currentFilter['attributes'])) { foreach ($currentFilter['attributes'] as $key => $value) { $this->filters[$currentId]['type'][$currentInnerId]['attributes'][$key] = Mesour\Components\Utils\Helpers::parseValue($value, $data); } } } } elseif (!isset($filter['type'])) { if (isset($filter['attributes'])) { foreach ($filter['attributes'] as $key => $value) { $this->filters[$currentId]['attributes'][$key] = Mesour\Components\Utils\Helpers::parseValue($value, $data); } } } } }
/** * @param bool|FALSE $isDisabled * @return array * @throws Mesour\InvalidArgumentException */ public function getAttributes($isDisabled = false) { $data = $this->getOption('data'); if ($data && count($data) > 0) { foreach ($this->attributes as $key => $value) { if ($value instanceof Mesour\Components\Link\IUrl) { continue; } $this->setAttribute($key, trim(Mesour\Components\Utils\Helpers::parseValue($value, $data))); } } foreach ($this->attributes as $key => $value) { if (!$isDisabled && $value instanceof Mesour\Components\Link\IUrl) { $this->setAttribute($key, $value->create($data)); } elseif ($isDisabled && $value instanceof Mesour\Components\Link\IUrl) { $this->removeAttribute($key); continue; } } if ($isDisabled) { $this->removeAttribute('onclick'); $this->setAttribute('class', 'disabled', true); } return $this->attributes; }
public function hasSection($section) { if (!Mesour\Components\Utils\Helpers::validateKeyName($section)) { throw new Mesour\InvalidArgumentException(sprintf('SessionSection name must be integer or string, %s given.', gettype($section))); } return isset($this->sections[$section]); }
public function toArray() { $out = parent::toArray(); $out['format'] = Mesour\Components\Utils\Helpers::convertDateToJsFormat($this->format); $out['nullable'] = $this->isNullable(); return $out; }
/** * @param $section * @return Mesour\Components\Session\ISessionSection * @throws Mesour\InvalidArgumentException */ public function getSection($section) { if (!Mesour\Components\Utils\Helpers::validateKeyName($section)) { throw new Mesour\InvalidArgumentException('SessionSection name must be integer or string, ' . gettype($section) . ' given.'); } $this->sections[$section] = $section; return new SessionSection($this->session->getSection($section)); }
public function set($key, $value) { if (!Mesour\Components\Utils\Helpers::validateKeyName($key)) { throw new Mesour\InvalidArgumentException('SessionSection name must be integer or string, ' . gettype($key) . ' given.'); } $this->session->setValueForSection($this->name, $key, $value); return $this; }
public function set($key, $val) { if (!Mesour\Components\Utils\Helpers::validateKeyName($key)) { throw new Mesour\InvalidArgumentException(sprintf('Key must be integer or string, %s given.', gettype($key))); } $this->data[$key] = $val; return $this; }
/** * @param array $data * @return string */ public function create($data = []) { if (count($data) > 0) { foreach ($this->args as $key => $value) { $this->args[$key] = Mesour\Components\Utils\Helpers::parseValue($value, $data); } } return $this->createUrl(); }
/** * @param string $name * @param bool $need * @return IComponent|null * @throws Mesour\InvalidStateException */ public function getComponent($name, $need = true) { Mesour\Components\Utils\Helpers::validateComponentName($name); if (!isset($this->components[$name])) { if ($need) { throw new Mesour\InvalidStateException(sprintf('Component with name %s does not exists.', $name)); } return null; } return $this->components[$name]; }
public function create($isAjax = false) { parent::create(); if (!$this->callback) { throw new Mesour\InvalidStateException('Callback is required for string content.'); } if (!$isAjax && $this->getParent()->hasAjaxLoading()) { return ''; } $this->onRender($this); return Mesour\Components\Utils\Helpers::invokeArgs($this->callback, [$this]); }
public function create($isAjax = false) { parent::create(); if (!$isAjax && $this->getParent()->hasAjaxLoading()) { return ''; } $template = $this->getTemplateFile(); if ($this->callback) { Mesour\Components\Utils\Helpers::invokeArgs($this->callback, [$this, $template]); } $this->onRender($this, $template); return $template->render(true); }
public function __call($name, $args) { try { if (substr($name, 0, 2) === 'on') { if (!$this->getReflection()->hasProperty($name)) { throw new Mesour\Components\MethodCallException(); } elseif ($this->getReflection()->getProperty($name)->isPrivate()) { throw new Mesour\InvalidStateException('Property ' . $name . ' must be public or protected.'); } elseif (!is_array($this->{$name})) { throw new Mesour\UnexpectedValueException('Property ' . $name . ' must be array.'); } else { foreach ($this->{$name} as $callback) { Mesour\Components\Utils\Helpers::invokeArgs($callback, $args); } } } else { throw new Mesour\Components\MethodCallException(); } } catch (Mesour\Components\MethodCallException $e) { throw new Mesour\MethodCallException(sprintf('Cannot call undefined method %s::$%s.', $name, static::class)); } }
public function create() { parent::create(); $id = $this->getRandomStringGenerator()->generate(); $wrapper = $this->getControlPrototype(); $oldWrapper = clone $wrapper; $menu = $this->getMenuPrototype(); $oldMenu = clone $menu; $this->onRender($this); $optionData = $this->getOption(self::DEFAULTS); foreach ($this->getOption(self::WRAPPER, 'attributes') as $key => $value) { if (!$wrapper->{$key} && $wrapper->{$key} !== false) { $menu->addAttributes([$key => trim(Mesour\Components\Utils\Helpers::parseValue($value, $optionData))]); } } $main = $this->getMainButton(); $main->setOption('data', $this->getOption('data')); $main->setAttribute('id', $id); foreach ($this->getOption(self::MENU, 'attributes') as $key => $value) { if (!$menu->{$key}) { $menu->addAttributes([$key => Mesour\Components\Utils\Helpers::parseValue($value, $optionData)]); } } $menu->addAttributes(['aria-labelledby' => $id]); if ($this->hasPullRight) { $menu->addAttributes(['class' => $menu->class . ' dropdown-menu-right']); } if (!$this->isDisabled()) { $isFirst = true; $items = $this->getItems(); foreach ($items as $key => $item) { if ($item instanceof Mesour\DropDown\Item) { if (!$item->isAllowed()) { continue; } $item->setOption('data', $this->getOption('data')); $currentItem = $this->getMenuItemPrototype(); $currentItem->class($this->getOption(self::ITEMS, 'button_class')); $currentItem->add($item->create()); $menu->add($currentItem); $isFirst = false; } else { $isNext = $this->isSomeNext($items, $key + 1); if (isset($items[$key + 1]) && $isNext) { $isNext = $this->isSomeNext($items, $key + 1); $menu->add($item); $isFirst = false; } if (!$isNext && ($this->isDivider($items[$key]) && !$isFirst) && $this->isHeader($items[$key + 1]) && (isset($items[$key + 2]) && $this->isSomeNext($items, $key + 2))) { $menu->add($item); $isFirst = false; } } } if ($isFirst) { $main->setDisabled(); } } else { $main->setDisabled(); } $wrapper->add($main->create()->add($this->getOption(self::CARET))); $wrapper->add($menu); $out = $wrapper; $this->menu = $oldMenu; $this->setHtmlElement($oldWrapper); return $out; }
/** * @param array $args * @return int|mixed */ protected function tryInvokeCallback(array $args = []) { return $this->callback ? Mesour\Components\Utils\Helpers::invokeArgs($this->callback, $args) : self::NO_CALLBACK; }
public function setAttribute($key, $value, $append = false) { Mesour\Components\Utils\Helpers::createAttribute($this->attributes, $key, $value, $append); return $this; }
public function setCallback($callback) { Mesour\Components\Utils\Helpers::checkCallback($callback); $this->callback = $callback; }
/** * @param array|null $permission * @param mixed $role * @param Mesour\Components\Security\IAuthorizator $authorizator * @return bool */ protected function checkIsAllowed($permission, $role, Mesour\Components\Security\IAuthorizator $authorizator) { return !$permission || Mesour\Components\Utils\Helpers::invokeArgs([$authorizator, 'isAllowed'], array_merge([$role], $permission)); }
/** * Called only if called component === $this and handler exists * @param string $methodName */ private function callHandler($methodName) { $method = $this->getReflection()->getMethod($methodName); $parameters = $method->getParameters(); $args = []; foreach ($parameters as $parameter) { $name = $parameter->getName(); $parsedName = $this->createLinkName() . '-' . $name; $currentValue = $this->getApplication()->getRequest()->get('m_' . $parsedName); if (!is_null($currentValue)) { if (($parameter->isArray() || $parameter->isDefaultValueAvailable() && is_array($parameter->getDefaultValue())) && !is_array($currentValue)) { throw new Mesour\UnexpectedValueException(sprintf('Invalid request. Argument must be an array. "%s" given.', gettype($currentValue))); } $value = $currentValue; } else { if ($parameter->isDefaultValueAvailable()) { $value = $parameter->getDefaultValue(); } else { throw new Mesour\InvalidArgumentException(sprintf('Invalid request. Required parameter %s doest not exists.', $parsedName)); } } $args[] = $value; } Mesour\Components\Utils\Helpers::invokeArgs([$this, $methodName], $args); $this->getSession()->saveState(); }
/** * Returns the rule type associated with the specified Resource, Role, and privilege. * @param string|Permission::ALL * @param string|Permission::ALL * @param string|Permission::ALL * @return mixed NULL if a rule does not exist or assertion fails, otherwise returns ALLOW or DENY */ private function getRuleType($resource, $role, $privilege) { if (!($rules = $this->getRules($resource, $role))) { return null; } if ($privilege === self::ALL) { if (isset($rules['allPrivileges'])) { $rule = $rules['allPrivileges']; } else { return null; } } elseif (!isset($rules['byPrivilege'][$privilege])) { return null; } else { $rule = $rules['byPrivilege'][$privilege]; } if ($rule['assert'] === null || Mesour\Components\Utils\Helpers::invokeArgs($rule['assert'], [$this, $role, $resource, $privilege])) { return $rule['type']; } elseif ($resource !== self::ALL || $role !== self::ALL || $privilege !== self::ALL) { return null; } elseif (self::ALLOW === $rule['type']) { return self::DENY; } else { return self::ALLOW; } }
public function create() { parent::create(); $wrapper = $this->getControlPrototype(); $oldWrapper = clone $wrapper; foreach ($oldWrapper->attrs as $key => $attr) { if (is_object($attr)) { $oldWrapper->attrs[$key] = clone $attr; } } if ($this->hasLeftIcon()) { $oldLeftIcon = clone $this->getLeftIcon(); } if ($this->hasRightIcon()) { $oldRightIcon = clone $this->getRightIcon(); } $this->onRender($this); $optionData = $this->getOption(self::DEFAULTS); if (!$this->isAllowed()) { $this->setDisabled(true); } foreach ($this->getOption(self::WRAPPER, 'attributes') as $key => $value) { if ($value instanceof Mesour\Components\Link\IUrl) { continue; } if (!$this->hasAttribute($key) || $this->getAttribute($key, false) !== false) { $value = trim(Mesour\Components\Utils\Helpers::parseValue($value, $optionData)); if ($this->hasAttribute($key)) { $value .= ' ' . $this->getAttribute($key); } $this->setAttribute($key, $value); } } if (!is_null($this->className)) { $this->setAttribute('class', $this->className === '' ? false : $this->className); } $this->getAttributes($this->isDisabled()); if ($this->hasLeftIcon()) { $leftIcon = $this->getLeftIcon(); $leftIcon->setOption('data', $this->getOption('data')); $wrapper->add($leftIcon->create()); if (strlen($this->text) > 0) { $wrapper->add(' '); } if (isset($oldLeftIcon)) { unset($this['leftIcon']); $this['leftIcon'] = $oldLeftIcon; } } if ($this->text) { $wrapper->add($this->getTranslator()->translate($this->text)); } if ($this->html) { $wrapper->add($this->html); } if ($this->hasRightIcon()) { $rightIcon = $this->getRightIcon(); $rightIcon->setOption('data', $this->getOption('data')); if (strlen($this->text) > 0) { $wrapper->add(' '); } $wrapper->add($rightIcon->create()); if (isset($oldRightIcon)) { unset($this['rightIcon']); $this['rightIcon'] = $oldRightIcon; } } $this->setHtmlElement($oldWrapper); return $wrapper; }
public function createHiddenInput($data = [], $referenceSettings = []) { /** @var Mesour\Icon\IIcon $icon */ $className = $this->getIconClass(); $icon = new $className(); $referenceData = []; foreach ($this->source->getDataStructure()->getTableStructures() as $tableStructure) { $source = $this->getSource()->getReferencedSource($tableStructure->getName()); if ($source->getTotalCount() <= self::$maxCheckboxCount) { $referenceData[$tableStructure->getName()] = $source->fetchFullData($this->getDateFormat()); } } $hidden = $this->getHiddenPrototype(); $attributes = ['data-mesour-data' => Nette\Utils\Json::encode($data), 'value' => Nette\Utils\Json::encode($this->getValues()), 'data-references' => Nette\Utils\Json::encode(array_merge([self::PREDEFINED_KEY => $this->predefinedData], $referenceData)), 'data-mesour-date' => $this->getDateFormat(), 'data-icon-prefix' => $icon->getPrefix(), 'data-icons' => Nette\Utils\Json::encode(self::$icons), 'data-mesour-js-date' => Mesour\Components\Utils\Helpers::convertDateToJsFormat($this->getDateFormat())]; $hidden->addAttributes($attributes); return $hidden; }
public function render() { $wrapper = $this->getWrapperPrototype(); $this->onRender($this, $wrapper); foreach ($this->items as $item) { $li = $this->createLiPrototype(); if ($this->callback) { Mesour\Components\Utils\Helpers::invokeArgs($this->callback, [$li, $item[0], $item[1]]); } else { $li->add($item[1]); } $this->onRenderRow($li, $item[0], $item[1]); $wrapper->add($li); } return $wrapper; }
public function isMatch(Mesour\Components\ComponentModel\IComponent $component, $value, array $parameters = []) { return Mesour\Components\Utils\Helpers::invokeArgs([$component, $this->getMethodName()], $parameters) === $value; }
/** * @param string $name * @return $this */ public function setName($name) { Mesour\Components\Utils\Helpers::validateComponentName($name); $this->name = $name; return $this; }