Skip to content

Instantly share code, notes, and snippets.

@dmpatierno
Created January 30, 2010 02:25
Show Gist options
  • Save dmpatierno/290365 to your computer and use it in GitHub Desktop.
Save dmpatierno/290365 to your computer and use it in GitHub Desktop.
@@ -820,72 +905,19 @@
*/
public function rearrangeSteps($moves) {
- $db = GuideDB::db();
- $guideid = $this->guideid;
- $langid = $this->langid;
-
- // Get the stepids that correspond with the orderbys, so that we can use
- // something unique to reference steps by.
- $q_stepids = <<<EOT
- SELECT `orderby`, `stepid`
- FROM `guide_steps`
- WHERE `guideid` = ? AND
- `langid` = ?
-EOT;
- $map = $db->getAssocCol($q_stepids, 'orderby', 'stepid', array(
- T_I, $guideid,
- T_S, $langid
- ));
-
+ $steps = $this->steps;
$numMoves = count($moves); // to avoid recounting
- $updates = array(); // delta => list of stepids
- // Run through the interlaced (from, to) pairs and pair them up, then
- // group them by (to - from); we'll do one update per set of steps which
- // have the same delta between their new orderby and their current
- // orderby.
+ // Run through the interlaced (from, to) pairs
+ // and update the guide steps array
for ($i = 0; $i < $numMoves; $i += 2) {
$from = $moves[$i];
$to = $moves[$i+1];
- $delta = $to - $from;
-
- // We use $map from above to store the stepid instead of the orderby;
- // note that $delta is used as the grouping key.
- if (isset($updates[$delta]))
- $updates[$delta][] = $map[$from];
- else
- $updates[$delta] = array($map[$from]);
+ $this->steps[$to] = $steps[$from];
}
- $db->begin();
-
- // Run one update per $delta group, pushing the old orderby by $delta.
- foreach ($updates as $delta => $stepids) {
- $stepids = DBUtils::createInList($stepids);
- $q_update = <<<EOT
- UPDATE `guide_steps`
- SET `orderby` = `orderby` + ?
- WHERE `stepid` IN ({$stepids})
-EOT;
- $db->execute($q_update, array(
- T_I, $delta
- ));
- }
-
- // Repopulates $this->steps (could be made more efficient).
- $this->onLoad();
-
- // Add a new revision and save the latest revisionid.
- $this->saveHistory();
- $this->update();
-
- // This will update the guide image if it's not already set manually.
- $this->setImageid();
-
- $db->commit();
-
- self::invalidate($this->guideid, $this->langid);
-
- return true;
+ $this->save();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment