/**
  * @param int $to
  * @param int $depth
  * @throws Exception
  */
 protected function moveNode($to, $depth = 0)
 {
     $left = $this->owner->getAttribute($this->leftAttribute);
     $right = $this->owner->getAttribute($this->rightAttribute);
     $depth = $this->owner->getAttribute($this->depthAttribute) - $this->node->getAttribute($this->depthAttribute) - $depth;
     if ($this->treeAttribute === null || $this->owner->getAttribute($this->treeAttribute) === $this->node->getAttribute($this->treeAttribute)) {
         // same root
         $this->owner->updateAll([$this->depthAttribute => new Expression("-[[{$this->depthAttribute}]]" . sprintf('%+d', $depth))], $this->getDescendants(null, true)->where);
         $delta = $right - $left + 1;
         if ($left >= $to) {
             $this->shift($to, $left - 1, $delta);
             $delta = $to - $left;
         } else {
             $this->shift($right + 1, $to, -$delta);
             $delta = $to - $right;
         }
         $this->owner->updateAll([$this->leftAttribute => new Expression("[[{$this->leftAttribute}]]" . sprintf('%+d', $delta)), $this->rightAttribute => new Expression("[[{$this->rightAttribute}]]" . sprintf('%+d', $delta)), $this->depthAttribute => new Expression("-[[{$this->depthAttribute}]]")], ['and', $this->getDescendants(null, true)->where, ['<', $this->depthAttribute, 0]]);
     } else {
         // move from other root
         $tree = $this->node->getAttribute($this->treeAttribute);
         $this->shift($to, null, $right - $left + 1, $tree);
         $delta = $to - $left;
         $this->owner->updateAll([$this->leftAttribute => new Expression("[[{$this->leftAttribute}]]" . sprintf('%+d', $delta)), $this->rightAttribute => new Expression("[[{$this->rightAttribute}]]" . sprintf('%+d', $delta)), $this->depthAttribute => new Expression("[[{$this->depthAttribute}]]" . sprintf('%+d', -$depth)), $this->treeAttribute => $tree], $this->getDescendants(null, true)->where);
         $this->shift($right + 1, null, $left - $right - 1);
     }
 }
 /**
  * @param int $to
  * @param bool $forward
  */
 protected function moveTo($to, $forward)
 {
     $this->owner->setAttribute($this->sortAttribute, $to);
     $tableName = $this->owner->tableName();
     $path = $this->getParentPath($this->node->getAttribute($this->pathAttribute));
     $like = strtr($path . $this->delimiter, ['%' => '\\%', '_' => '\\_', '\\' => '\\\\']);
     $unallocated = (new Query())->select("{$tableName}.[[{$this->sortAttribute}]]")->from("{$tableName}")->leftJoin("{$tableName} n", ['and', ['like', "n.[[{$this->pathAttribute}]]", $like . '%', false], ["n.[[{$this->depthAttribute}]]" => $this->node->getAttribute($this->depthAttribute), "n.[[{$this->sortAttribute}]]" => new Expression("{$tableName}.[[{$this->sortAttribute}]] " . ($forward ? '+' : '-') . " 1")]])->where(['and', ['like', "{$tableName}.[[{$this->pathAttribute}]]", $like . '%', false], [$forward ? '>=' : '<=', "{$tableName}.[[{$this->sortAttribute}]]", $to], ["{$tableName}.[[{$this->depthAttribute}]]" => $this->node->getAttribute($this->depthAttribute), "n.[[{$this->sortAttribute}]]" => null]])->orderBy(["{$tableName}.[[{$this->sortAttribute}]]" => $forward ? SORT_ASC : SORT_DESC])->limit(1)->scalar($this->owner->getDb());
     $this->owner->updateAll([$this->sortAttribute => new Expression("[[{$this->sortAttribute}]] " . ($forward ? '+' : '-') . " 1")], ['and', ['like', "[[{$this->pathAttribute}]]", $like . '%', false], ["[[{$this->depthAttribute}]]" => $this->node->getAttribute($this->depthAttribute)], ['between', $this->sortAttribute, $forward ? $to : $unallocated, $forward ? $unallocated : $to]]);
 }
 /**
  * @return void
  */
 public function afterFindSingle()
 {
     $file = array_map(function ($attribute) {
         return $this->owner->getAttribute($attribute);
     }, $this->fields());
     if (array_key_exists('path', $file) && $file['path']) {
         $this->owner->{$this->attribute} = $this->enrichFileData($file);
     }
 }
 /**
  * Insert operation internal handler
  * @param bool $forward
  * @throws Exception
  */
 protected function insertNearInternal($forward)
 {
     $this->checkNode(true);
     $this->owner->setAttribute($this->parentAttribute, $this->node->getAttribute($this->parentAttribute));
     if ($this->sortable !== false) {
         if ($forward) {
             $this->owner->moveAfter($this->node);
         } else {
             $this->owner->moveBefore($this->node);
         }
     }
 }
 /**
  * @param int $left
  * @param int $right
  * @return array|null
  */
 protected function findInsertAfterRange(&$left, &$right)
 {
     $result = null;
     $left = $this->node->getAttribute($this->rightAttribute);
     $right = $this->getNodeBehavior()->getNext()->select($this->leftAttribute)->scalar();
     if ($right === false) {
         $result = $this->getNodeBehavior()->getParent()->select([$this->leftAttribute, $this->rightAttribute])->createCommand()->queryOne();
         $right = $result[$this->rightAttribute];
     }
     $right = (int) $right;
     return $result;
 }
 /**
  * @return \yii\db\ActiveQuery
  */
 protected function getSortableQuery()
 {
     switch ($this->operation) {
         case self::OPERATION_PREPEND_TO:
         case self::OPERATION_APPEND_TO:
             $path = $this->node->getAttribute($this->pathAttribute);
             $depth = $this->node->getAttribute($this->depthAttribute) + 1;
             break;
         case self::OPERATION_INSERT_BEFORE:
         case self::OPERATION_INSERT_AFTER:
             $path = $this->node->getParentPath();
             $depth = $this->node->getAttribute($this->depthAttribute);
             break;
         default:
             $path = $this->getParentPath();
             $depth = $this->owner->getAttribute($this->depthAttribute);
     }
     $tableName = $this->owner->tableName();
     return $this->owner->find()->andWhere($this->treeCondition())->andWhere($path !== null ? ['like', "{$tableName}.[[{$this->pathAttribute}]]", $this->getLike($path), false] : '1=0')->andWhere(["{$tableName}.[[{$this->depthAttribute}]]" => $depth]);
 }
示例#7
0
 /**
  * @param integer $value
  * @param integer $depth
  */
 protected function moveNode($value, $depth)
 {
     $db = $this->owner->getDb();
     $leftValue = $this->owner->getAttribute($this->leftAttribute);
     $rightValue = $this->owner->getAttribute($this->rightAttribute);
     $depthValue = $this->owner->getAttribute($this->depthAttribute);
     $depthAttribute = $db->quoteColumnName($this->depthAttribute);
     $depth = $this->node->getAttribute($this->depthAttribute) - $depthValue + $depth;
     if ($this->treeAttribute === false || $this->owner->getAttribute($this->treeAttribute) === $this->node->getAttribute($this->treeAttribute)) {
         $delta = $rightValue - $leftValue + 1;
         $this->shiftLeftRightAttribute($value, $delta);
         if ($leftValue >= $value) {
             $leftValue += $delta;
             $rightValue += $delta;
         }
         $condition = ['and', ['>=', $this->leftAttribute, $leftValue], ['<=', $this->rightAttribute, $rightValue]];
         $this->applyTreeAttributeCondition($condition);
         $this->owner->updateAll([$this->depthAttribute => new Expression($depthAttribute . sprintf('%+d', $depth))], $condition);
         foreach ([$this->leftAttribute, $this->rightAttribute] as $attribute) {
             $condition = ['and', ['>=', $attribute, $leftValue], ['<=', $attribute, $rightValue]];
             $this->applyTreeAttributeCondition($condition);
             $this->owner->updateAll([$attribute => new Expression($db->quoteColumnName($attribute) . sprintf('%+d', $value - $leftValue))], $condition);
         }
         $this->shiftLeftRightAttribute($rightValue + 1, -$delta);
     } else {
         $leftAttribute = $db->quoteColumnName($this->leftAttribute);
         $rightAttribute = $db->quoteColumnName($this->rightAttribute);
         $nodeRootValue = $this->node->getAttribute($this->treeAttribute);
         foreach ([$this->leftAttribute, $this->rightAttribute] as $attribute) {
             $this->owner->updateAll([$attribute => new Expression($db->quoteColumnName($attribute) . sprintf('%+d', $rightValue - $leftValue + 1))], ['and', ['>=', $attribute, $value], [$this->treeAttribute => $nodeRootValue]]);
         }
         $delta = $value - $leftValue;
         $this->owner->updateAll([$this->leftAttribute => new Expression($leftAttribute . sprintf('%+d', $delta)), $this->rightAttribute => new Expression($rightAttribute . sprintf('%+d', $delta)), $this->depthAttribute => new Expression($depthAttribute . sprintf('%+d', $depth)), $this->treeAttribute => $nodeRootValue], ['and', ['>=', $this->leftAttribute, $leftValue], ['<=', $this->rightAttribute, $rightValue], [$this->treeAttribute => $this->owner->getAttribute($this->treeAttribute)]]);
         $this->shiftLeftRightAttribute($rightValue + 1, $leftValue - $rightValue - 1);
     }
 }
示例#8
0
 /**
  * @inheritDoc
  */
 public function compareValue(ActiveRecord $model, $value)
 {
     /** @var ActiveRecord $model */
     return $model->getAttribute($this->attributeName) == $value;
 }
示例#9
0
 /**
  * @param ActiveRecord $model
  * @return string
  */
 public function editAttribute($model, $field, $params = [])
 {
     if (!isset($params['options'])) {
         $params['options'] = [];
     }
     $options = $params['options'];
     unset($params['options']);
     /** @var \yii\widgets\ActiveField $form */
     $form = $params['formField'];
     unset($params['formField']);
     $return = $form->fileInput($options);
     $file = $model->getAttribute($field->varname);
     if ($file) {
         $return .= "<div class='form-group'><fieldset>";
         $finfo = finfo_open(FILEINFO_MIME_TYPE);
         $root = Yii::getAlias('@webroot/' . $file);
         $type = explode("/", finfo_file($finfo, $root));
         $return .= Html::activeCheckBox($model, '[uwfdel]' . $field->varname, ['label' => Module::t('Delete file')]) . "<br>";
         if ($type[0] == 'image') {
             $return .= Html::img(Url::base() . "/" . $file, ['class' => 'UWfile-image-preview']);
         } else {
             $assetsPath = Yii::$app->assetManager->getBundle('marsoltys\\yii2user\\assets\\UserAssets')->basePath;
             $assetsUrl = Yii::$app->assetManager->getBundle('marsoltys\\yii2user\\assets\\UserAssets')->baseUrl;
             $img = "/img/" . pathinfo($file, PATHINFO_EXTENSION) . ".png";
             if (file_exists($assetsPath . $img)) {
                 $return .= Html::img($assetsUrl . $img, ['class' => 'UWfile-image-preview']);
             } else {
                 $return .= Html::img($assetsUrl . "/img/file.png", ['class' => 'UWfile-image-preview']);
             }
             $return .= pathinfo($file, PATHINFO_BASENAME) . " ";
             $return .= Html::a(pathinfo($file, PATHINFO_BASENAME) . " ", Url::base() . "/" . $file);
         }
         $return .= "</fieldset></div>";
     }
     return $return;
 }
示例#10
0
 /**
  * @param ActiveRecord|TreeInterface $node
  * @return array
  */
 protected function getNode($node)
 {
     $items = [];
     /** @var ActiveRecord[]|TreeInterface[] $children */
     $children = $node->children;
     foreach ($children as $n => $node) {
         $items[$n]['id'] = $node->getPrimaryKey();
         $items[$n]['name'] = $node->getAttribute($this->nameAttribute);
         $items[$n]['children'] = $this->getNode($node);
         $items[$n]['update-url'] = Url::to([$this->advancedUpdateRoute, 'id' => $node->getPrimaryKey()]);
     }
     return $items;
 }
示例#11
0
 /**
  * Формирует массив конструкции Where() ActiveQuery.
  * @param ActiveRecord $ModelSearch Модель, для который создаем конструкцию Where().
  * @param string $Field Атрибут модели, по которому осуществляем поиск, фильтрацию.
  * @param integer $Type Тип значения в атрибуте модели (Proc::Time, Proc::Date, Proc::DateTime).
  * @return array Массив конструкции Where().
  */
 static function WhereConstruct($ModelSearch, $Field, $Type = 0)
 {
     $AttributeModelValue = $ModelSearch->getAttribute($Field);
     $AttributeValue = empty($AttributeModelValue) ? $ModelSearch->{$Field} : $ModelSearch->getAttribute($Field);
     preg_match('/(>=|<=|>|<|=)?(.*)/', $AttributeValue, $Matches);
     $Operator = $Matches[1];
     $Value = $Matches[2];
     if (!empty($Value)) {
         switch ($Type) {
             case self::Time:
                 $Value = date("H:i:s", strtotime($Value));
                 break;
             case self::Date:
                 $Value = date("Y-m-d", strtotime($Value));
                 break;
             case self::DateTime:
                 $Value = date("Y-m-d H:i:s", strtotime($Value));
                 break;
         }
     }
     return [empty($Operator) ? '=' : $Operator, $Field, $Value];
 }
示例#12
0
 /**
  * @param \yii\db\ActiveRecord $model
  * @return bool
  */
 private function deleteTranslation($model)
 {
     if (!$model->isNewRecord) {
         $model->delete();
         unset($this->_models[$model->getAttribute($this->languageAttribute)]);
     }
     return true;
 }
 /**
  * @return int
  */
 public function getValue()
 {
     return $this->owner->getAttribute($this->attributeName);
 }
示例#14
0
 public function getAttribute($name)
 {
     return isset($this->{$name}) ? $this->{$name} : parent::getAttribute($name);
 }
示例#15
0
 /**
  * Returns the named attribute value.
  * If this record is the result of a query and the attribute is not loaded,
  * null will be returned.
  * @param string $name the attribute name
  * @return mixed the attribute value. Null if the attribute is not set or does not exist.
  * @see hasAttribute()
  */
 public function getAttribute($name)
 {
     if ($this->hasAttribute($name)) {
         // we will now have optionally applied default values if this is a new record
         return parent::getAttribute($name);
     }
     return null;
 }
示例#16
0
 /**
  * @param ActiveRecord $model
  * @return ActiveRecord
  */
 public function moveAfter($model)
 {
     return $this->moveTo($model->getAttribute($this->sortAttribute) + 1, true);
 }
 /**
  * Performs a check of publish state for translation record
  * @param ActiveRecord $translationRecord
  * @return bool
  */
 public function checkIsPublished(ActiveRecord $translationRecord)
 {
     if ($this->translationPublishedAttribute === false) {
         return true;
     }
     return $translationRecord->getAttribute($this->translationPublishedAttribute) == $this->translationPublishedAttributeValue;
 }
 /**
  * Determines if node is descendant of subject node.
  * @param ActiveRecord $subj the subject node.
  * @return boolean whether the node is descendant of subject node.
  */
 public function isDescendantOf($subj)
 {
     $result = $this->owner->getAttribute($this->leftAttribute) > $subj->getAttribute($this->leftAttribute) && $this->owner->getAttribute($this->rightAttribute) < $subj->getAttribute($this->rightAttribute);
     if ($this->hasManyRoots) {
         $result = $result && $this->owner->getAttribute($this->rootAttribute) === $subj->getAttribute($this->rootAttribute);
     }
     return $result;
 }
 /**
  * Determines whether the node is previous sibling to given node.
  * @param ActiveRecord $target the parent node
  * @return boolean whether the node is child of the parent node
  */
 public function isPrevSiblingOf($target)
 {
     $result = $this->owner->getAttribute($this->leftAttribute) < $target->getAttribute($this->leftAttribute) && $this->owner->getAttribute($this->rightAttribute) < $target->getAttribute($this->rightAttribute);
     if ($result && $this->treeAttribute !== false) {
         $result = $this->owner->getAttribute($this->treeAttribute) === $target->getAttribute($this->treeAttribute);
     }
     if ($result) {
         $result = $this->owner->getAttribute($this->depthAttribute) === $target->getAttribute($this->depthAttribute);
     }
     return $result;
 }