/** * @mcms_message ru.molinos.cms.hook.node */ public static function on_node_change(Context $ctx, $node, $op) { try { list($sql, $params) = sql::getInsert('node__log', array('nid' => $node->id, 'uid' => $ctx->user->id, 'username' => $ctx->user->getNode()->getName(), 'operation' => $op, 'ip' => $_SERVER['REMOTE_ADDR'], 'timestamp' => mcms::now(), 'name' => $node->name)); $ctx->db->exec($sql, $params); } catch (TableNotFoundException $e) { } }
/** * Создание новой ноды. */ private function saveNew(array $data) { if (null !== $this->parent_id) { $position = $this->getDB()->getResult("SELECT `right` FROM `node` WHERE `id` = ?", array($this->parent_id)); $max = intval($this->getDB()->getResult("SELECT MAX(`right`) FROM `node`")); // Превращаем простую ноду в родительску. if (null === $position) { $this->getDB()->exec("UPDATE `node` SET `left` = ?, `right` = ? WHERE `id` = ?", array($max, $max + 4, $this->parent_id)); $data['left'] = $this->data['left'] = $max + 1; $data['right'] = $this->data['right'] = $max + 2; } else { $delta = $max - $position + 1; // mcms::debug($position, $max, $delta); // Вообще можно было бы обойтись сортированным обновлением, но не все серверы // это поддерживают, поэтому делаем в два захода: сначала выносим хвост за // пределы текущего пространства, затем — возвращаем на место + 2. $this->getDB()->exec("UPDATE `node` SET `left` = `left` + ? WHERE `left` >= ?", array($delta + 2, $position)); $this->getDB()->exec("UPDATE `node` SET `right` = `right` + ? WHERE `right` >= ?", array($delta + 2, $position)); $this->getDB()->exec("UPDATE `node` SET `left` = `left` - ? WHERE `left` >= ?", array($delta, $position + 2)); $this->getDB()->exec("UPDATE `node` SET `right` = `right` - ? WHERE `right` >= ?", array($delta, $position + 2)); $data['left'] = $this->data['left'] = $position; $data['right'] = $this->data['right'] = $position + 1; } } list($sql, $params) = sql::getInsert('node', $data); $sth = $this->getDB()->prepare($sql); $sth->execute($params); $this->id = $this->getDB()->lastInsertId(); }
public function testGetInsert() { list($sql, $params) = sql::getInsert('table', array('id' => 1, 'name' => 'test')); $this->assertEquals('INSERT INTO `table` (`id`, `name`) VALUES (?, ?)', $sql); $this->assertEquals(array(1, 'test'), $params); }