/** * Saves a redirect. * * @param SproutSeo_RedirectModel $redirect * @throws Exception * @return bool */ public function saveRedirect(SproutSeo_RedirectModel $redirect) { $isNewRedirect = !$redirect->id; // Event data if (!$isNewRedirect) { $redirectRecord = SproutSeo_RedirectRecord::model()->findById($redirect->id); if (!$redirectRecord) { throw new Exception(Craft::t('No redirect exists with the ID “{id}”', array('id' => $redirect->id))); } } else { $redirectRecord = new SproutSeo_RedirectRecord(); } $redirectRecord->oldUrl = $redirect->oldUrl; $redirectRecord->newUrl = $redirect->newUrl; $redirectRecord->method = $redirect->method; $redirectRecord->regex = $redirect->regex; $redirectRecord->validate(); $redirect->addErrors($redirectRecord->getErrors()); if (!$redirect->hasErrors()) { $transaction = craft()->db->getCurrentTransaction() === null ? craft()->db->beginTransaction() : null; try { if (craft()->elements->saveElement($redirect)) { // Now that we have an element ID, save it on the other stuff if ($isNewRedirect) { $redirectRecord->id = $redirect->id; } $redirectRecord->save(false); if ($transaction !== null) { $transaction->commit(); } return true; } } catch (\Exception $e) { if ($transaction !== null) { $transaction->rollback(); } throw $e; } } return false; }
/** * Populates an element model based on a query result. * * @param array $row * @return array */ public function populateElementModel($row) { return SproutSeo_RedirectModel::populateModel($row); }