92 parent::__construct();
95 throw new \Exception(
'Wrong "$connection" parametr');
111 throw new \Exception(
'"$fields" tree configuratin array');
119 $this->_select = $this->_conn->select();
120 $this->_select->from($this->_table, array_values(
$fields));
148 public function load($parentNode =
null, $recursionLevel = 100)
150 if ($parentNode ===
null) {
154 $parentId = $parentNode->getId();
155 }
elseif (is_numeric($parentNode)) {
156 $parentId = $parentNode;
159 throw new \Exception(
'root node id is not defined');
163 $select->order($this->_table .
'.' . $this->_orderField .
' ASC');
164 $condition = $this->_conn->quoteInto(
"{$this->_table}.{$this->_parentField}=?", $parentId);
166 $arrNodes = $this->_conn->fetchAll(
$select);
167 foreach ($arrNodes as $nodeInfo) {
168 $node =
new Node($nodeInfo, $this->_idField, $this, $parentNode);
169 $this->
addNode($node, $parentNode);
171 if ($recursionLevel) {
172 $node->loadChildren($recursionLevel - 1);
185 $condition = $this->_conn->quoteInto(
"{$this->_table}.{$this->_idField}=?", $nodeId);
200 $orderSelect = $this->_conn->select();
203 new \
Zend_Db_Expr(
'MAX(' . $this->_conn->quoteIdentifier($this->_orderField) .
')')
205 $this->_conn->quoteIdentifier($this->_parentField) .
'=' . $parentNode->getId()
208 $order = $this->_conn->fetchOne($orderSelect);
213 $this->_conn->insert($this->_table,
$data);
216 return parent::appendChild(
$data, $parentNode, $prevNode);
228 public function moveNodeTo($node, $parentNode, $prevNode =
null)
234 if ($prevNode ===
null || $prevNode->getData($this->_orderField) ===
null) {
239 $condition = $this->_conn->quoteInto(
"{$this->_idField}=?", $node->getId());
243 $this->_orderField => new \Zend_Db_Expr($this->_conn->quoteIdentifier($this->_orderField) .
'+1'),
245 $conditionReorderNew = $this->_conn->quoteIdentifier(
247 ) .
'=' . $parentNode->getId() .
' AND ' . $this->_conn->quoteIdentifier(
253 $this->_orderField => new \Zend_Db_Expr($this->_conn->quoteIdentifier($this->_orderField) .
'-1'),
255 $conditionReorderOld = $this->_conn->quoteIdentifier(
257 ) .
'=' . $node->getData(
259 ) .
' AND ' . $this->_conn->quoteIdentifier(
261 ) .
'>' . $node->getData(
265 $this->_conn->beginTransaction();
268 $this->_conn->update($this->_table, $dataReorderNew, $conditionReorderNew);
270 $this->_conn->update($this->_table,
$data, $condition);
272 $this->_conn->update($this->_table, $dataReorderOld, $conditionReorderOld);
274 $this->_conn->commit();
275 }
catch (\Exception $e) {
276 $this->_conn->rollBack();
277 throw new \Exception(
'Can\'t move tree node');
288 $select = $this->_conn->select()->from(
292 $this->_parentField .
'=?',
295 $ids = $this->_conn->fetchCol(
$select);
298 $this->_conn->update(
300 [$this->_levelField => $parentLevel + 1],
301 $this->_conn->quoteInto($this->_idField .
' IN (?)', $ids)
303 foreach ($ids as
$id) {
316 $select->order($this->_table .
'.' . $this->_levelField)->order($this->_table .
'.' . $this->_orderField);
318 $arrNodes = $this->_conn->fetchAll(
$select);
320 foreach ($arrNodes as $nodeInfo) {
321 $node =
new Node($nodeInfo, $this->_idField, $this);
322 $parentNode = $this->
getNodeById($nodeInfo[$this->_parentField]);
323 $this->
addNode($node, $parentNode);
338 $this->_orderField => new \Zend_Db_Expr($this->_conn->quoteIdentifier($this->_orderField) .
'-1'),
340 $conditionReorderOld = $this->_conn->quoteIdentifier(
342 ) .
'=' . $node->getData(
344 ) .
' AND ' . $this->_conn->quoteIdentifier(
346 ) .
'>' . $node->getData(
350 $this->_conn->beginTransaction();
352 $condition = $this->_conn->quoteInto(
"{$this->_idField}=?", $node->getId());
353 $this->_conn->delete($this->_table, $condition);
355 $this->_conn->update($this->_table, $dataReorderOld, $conditionReorderOld);
356 $this->_conn->commit();
357 }
catch (\Exception $e) {
358 $this->_conn->rollBack();
359 throw new \Exception(
'Can\'t remove tree node');
361 parent::removeNode($node);
load($parentNode=null, $recursionLevel=100)
elseif(isset( $params[ 'redirect_parent']))
__construct(\Magento\Framework\DB\Adapter\AdapterInterface $connection, $table, $fields)
addNode($node, $parent=null)
_updateChildLevels($parentId, $parentLevel)
moveNodeTo($node, $parentNode, $prevNode=null)
appendChild($data, $parentNode, $prevNode=null)