/** * Moves a row in a database table. * * Uses $orderField to determine the order of objects in a table, usually this * is a placement of some kind. It uses this order field to figure out how move * the row, the row is either swapped with another row which is either above or * below according to whether $down is true or false, or it is swapped * with the first item or the last item depending on whether this row * is first or last. * Uses $conditions to figure out unique rows. * * Note: Transaction unsafe. If you call several transaction unsafe methods * you must enclose the calls within a db transaction; thus within db->begin * and db->commit. * * @param array $def A definition array of all fields, table name and sorting (see {@link eZPersistentObject::definition()} for more info) * @param array $orderField Associative array with one element, the key is the order id and values is order value. * @param array $conditions * @param bool $down * @return void */ public static function reorderObject($def, $orderField, $conditions, $down = true) { $db = eZDB::instance(); $table = $def["name"]; $keys = $def["keys"]; reset($orderField); $order_id = key($orderField); $order_val = $orderField[$order_id]; if ($down) { $order_operator = ">="; $order_type = "asc"; $order_add = -1; } else { $order_operator = "<="; $order_type = "desc"; $order_add = 1; } $fields = array_merge($keys, array($order_id)); $rows = eZPersistentObject::fetchObjectList($def, $fields, array_merge($conditions, array($order_id => array($order_operator, $order_val))), array($order_id => $order_type), array("length" => 2), false); if (count($rows) == 2) { $swapSQL1 = eZPersistentObject::swapRow($table, $keys, $order_id, $rows, 1, 0); $swapSQL2 = eZPersistentObject::swapRow($table, $keys, $order_id, $rows, 0, 1); $db->begin(); $db->query($swapSQL1); $db->query($swapSQL2); $db->commit(); } else { $tmp = eZPersistentObject::fetchObjectList($def, $fields, $conditions, array($order_id => $order_type), array("length" => 1), false); $where_text = eZPersistentObject::conditionTextByRow($keys, $rows[0]); $db->query("UPDATE {$table} SET {$order_id}='" . ($tmp[0][$order_id] + $order_add) . "'{$where_text}"); } }