/** * Move tree node * * @param Node $node * @param Node $newParent * @param Node $prevNode * @return void * @throws \Exception * @todo Use adapter for generate conditions */ public function move($node, $newParent, $prevNode = null) { $position = 1; $oldPath = $node->getData($this->_pathField); $newPath = $newParent->getData($this->_pathField); $newPath = $newPath . '/' . $node->getId(); $oldPathLength = strlen($oldPath); $newLevel = $newParent->getLevel() + 1; $levelDisposition = $newLevel - $node->getLevel(); $data = [$this->_levelField => new \Zend_Db_Expr("{$this->_levelField} + '{$levelDisposition}'"), $this->_pathField => new \Zend_Db_Expr("CONCAT('{$newPath}', RIGHT({$this->_pathField}, LENGTH({$this->_pathField}) - {$oldPathLength}))")]; $condition = $this->_conn->quoteInto("{$this->_pathField} REGEXP ?", "^{$oldPath}(/|\$)"); $this->_conn->beginTransaction(); $reorderData = [$this->_orderField => new \Zend_Db_Expr("{$this->_orderField} + 1")]; try { if ($prevNode && $prevNode->getId()) { $reorderCondition = "{$this->_orderField} > {$prevNode->getData($this->_orderField)}"; $position = $prevNode->getData($this->_orderField) + 1; } else { $reorderCondition = $this->_conn->quoteInto("{$this->_pathField} REGEXP ?", "^{$newParent->getData($this->_pathField)}/[0-9]+\$"); $select = $this->_conn->select()->from($this->_table, new \Zend_Db_Expr("MIN({$this->_orderField})"))->where($reorderCondition); $position = (int) $this->_conn->fetchOne($select); } $this->_conn->update($this->_table, $reorderData, $reorderCondition); $this->_conn->update($this->_table, $data, $condition); $this->_conn->update($this->_table, [$this->_orderField => $position, $this->_levelField => $newLevel], $this->_conn->quoteInto("{$this->_idField} = ?", $node->getId())); $this->_conn->commit(); } catch (\Exception $e) { $this->_conn->rollBack(); throw new \Exception("Can't move tree node due to error: " . $e->getMessage()); } }
/** * @param string|int $eId * @param string|int $pId * @param string|int $aId * @return void * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) * @SuppressWarnings(PHPMD.UnusedLocalVariable) * @SuppressWarnings(PHPMD.ExitExpression) */ public function moveNodes($eId, $pId, $aId = 0) { $eInfo = $this->getNodeInfo($eId); if ($pId != 0) { $pInfo = $this->getNodeInfo($pId); } if ($aId != 0) { $aInfo = $this->getNodeInfo($aId); } $level = $eInfo[$this->_level]; $leftKey = $eInfo[$this->_left]; $rightKey = $eInfo[$this->_right]; if ($pId == 0) { $levelUp = 0; } else { $levelUp = $pInfo[$this->_level]; } $rightKeyNear = 0; $leftKeyNear = 0; if ($pId == 0) { //move to root $rightKeyNear = $this->_db->fetchOne('SELECT MAX(' . $this->_right . ') FROM ' . $this->_table); } elseif ($aId != 0 && $pId == $eInfo[$this->_pid]) { // if we have after ID $rightKeyNear = $aInfo[$this->_right]; $leftKeyNear = $aInfo[$this->_left]; } elseif ($aId == 0 && $pId == $eInfo[$this->_pid]) { // if we do not have after ID $rightKeyNear = $pInfo[$this->_left]; } elseif ($pId != $eInfo[$this->_pid]) { $rightKeyNear = $pInfo[$this->_right] - 1; } $skewLevel = $pInfo[$this->_level] - $eInfo[$this->_level] + 1; $skewTree = $eInfo[$this->_right] - $eInfo[$this->_left] + 1; echo "alert('" . $rightKeyNear . "');"; if ($rightKeyNear > $rightKey) { // up echo "alert('move up');"; $skewEdit = $rightKeyNear - $leftKey + 1; $sql = 'UPDATE ' . $this->_table . ' SET ' . $this->_right . ' = IF(' . $this->_left . ' >= ' . $eInfo[$this->_left] . ', ' . $this->_right . ' + ' . $skewEdit . ', IF(' . $this->_right . ' < ' . $eInfo[$this->_left] . ', ' . $this->_right . ' + ' . $skewTree . ', ' . $this->_right . ')), ' . $this->_level . ' = IF(' . $this->_left . ' >= ' . $eInfo[$this->_left] . ', ' . $this->_level . ' + ' . $skewLevel . ', ' . $this->_level . '), ' . $this->_left . ' = IF(' . $this->_left . ' >= ' . $eInfo[$this->_left] . ', ' . $this->_left . ' + ' . $skewEdit . ', IF(' . $this->_left . ' > ' . $rightKeyNear . ', ' . $this->_left . ' + ' . $skewTree . ', ' . $this->_left . '))' . ' WHERE ' . $this->_right . ' > ' . $rightKeyNear . ' AND ' . $this->_left . ' < ' . $eInfo[$this->_right]; } elseif ($rightKeyNear < $rightKey) { // down echo "alert('move down');"; $skewEdit = $rightKeyNear - $leftKey + 1 - $skewTree; $sql = 'UPDATE ' . $this->_table . ' SET ' . $this->_left . ' = IF(' . $this->_right . ' <= ' . $rightKey . ', ' . $this->_left . ' + ' . $skewEdit . ', IF(' . $this->_left . ' > ' . $rightKey . ', ' . $this->_left . ' - ' . $skewTree . ', ' . $this->_left . ')), ' . $this->_level . ' = IF(' . $this->_right . ' <= ' . $rightKey . ', ' . $this->_level . ' + ' . $skewLevel . ', ' . $this->_level . '), ' . $this->_right . ' = IF(' . $this->_right . ' <= ' . $rightKey . ', ' . $this->_right . ' + ' . $skewEdit . ', IF(' . $this->_right . ' <= ' . $rightKeyNear . ', ' . $this->_right . ' - ' . $skewTree . ', ' . $this->_right . '))' . ' WHERE ' . $this->_right . ' > ' . $leftKey . ' AND ' . $this->_left . ' <= ' . $rightKeyNear; } $this->_db->beginTransaction(); try { $this->_db->query($sql); $this->_db->commit(); } catch (\Exception $e) { $this->_db->rollBack(); echo $e->getMessage(); echo "<br>\r\n"; echo $sql; echo "<br>\r\n"; exit; } echo "alert('node added')"; }
/** * Updates data when subscriber received * * @param \Magento\Newsletter\Model\Subscriber $subscriber * @param \Magento\Newsletter\Model\Queue $queue * @return $this * @throws \Magento\Framework\Exception\LocalizedException */ public function received(\Magento\Newsletter\Model\Subscriber $subscriber, \Magento\Newsletter\Model\Queue $queue) { $this->connection->beginTransaction(); try { $data['letter_sent_at'] = $this->_date->gmtDate(); $this->connection->update($this->_subscriberLinkTable, $data, ['subscriber_id = ?' => $subscriber->getId(), 'queue_id = ?' => $queue->getId()]); $this->connection->commit(); } catch (\Exception $e) { $this->connection->rollBack(); throw new \Magento\Framework\Exception\LocalizedException(__('We cannot mark as received subscriber.')); } return $this; }
/** * @param Node $node * @return $this * @throws \Exception */ public function removeNode($node) { // For reorder old node branch $dataReorderOld = [$this->_orderField => new \Zend_Db_Expr($this->_conn->quoteIdentifier($this->_orderField) . '-1')]; $conditionReorderOld = $this->_conn->quoteIdentifier($this->_parentField) . '=' . $node->getData($this->_parentField) . ' AND ' . $this->_conn->quoteIdentifier($this->_orderField) . '>' . $node->getData($this->_orderField); $this->_conn->beginTransaction(); try { $condition = $this->_conn->quoteInto("{$this->_idField}=?", $node->getId()); $this->_conn->delete($this->_table, $condition); // Update old node branch $this->_conn->update($this->_table, $dataReorderOld, $conditionReorderOld); $this->_conn->commit(); } catch (\Exception $e) { $this->_conn->rollBack(); throw new \Exception('Can\'t remove tree node'); } parent::removeNode($node); return $this; }
/** * Rollback transaction * * @return $this */ public function rollBackTransaction() { $this->connection->rollBack(); $this->_resourceHelper->restoreTransactionIsolationLevel(); return $this; }