public function editAction() { $id = $this->_getParam('id'); $this->view->site = Yadda_Model_Site::find($id); $form = new Admin_Form_Site_Edit($this->view->site); if ($this->getRequest()->isPost()) { if ($form->isValid($_POST)) { $values = $form->getValues(); try { Yadda_Model_Site::update($id, $values); $this->getHelper('FlashMessenger')->addMessage('Site updated.'); $this->_redirect($this->getHelper('ReturnUrl')->getUrl('/site/edit/id/' . $id)); } catch (Yadda_Model_Exception $e) { $this->view->flashMessages[] = 'Update failed: ' . $e->getMessage(); } } } else { $form->populate($this->view->site); } $this->view->form = $form; // get feeds $this->view->feeds = Yadda_Model_Feed::all(array('site' => $this->view->site['id'])); }
/** * Updates a deal. * * @param int $id * @param array $values * @throws Yadda_Model_Exception * @return void */ public static function update($id, array $values) { $deal = self::find($id); $update = array(); $where = array('id = ?' => $deal['id']); // validate - title if (isset($values['title'])) { if (empty($values['title'])) { throw new Yadda_Model_Exception('Please provide a title for the deal.'); } else { $update['title'] = trim($values['title']); } } // validate - description if (isset($values['description'])) { $update['description'] = empty($values['description']) ? null : trim($values['description']); } // validate - site if (isset($values['site'])) { $site = Yadda_Model_Site::find($values['site']); $update['site_id'] = $site['id']; } // validate - region if (isset($values['region'])) { $region = Yadda_Model_Region::find($values['region']); $update['region_id'] = $region['id']; } // validate - price if (isset($values['price'])) { if (empty($values['price'])) { $update['price'] = null; } else { $update['price'] = (double) $values['price']; } } // validate - value if (isset($values['value'])) { if (empty($values['value'])) { $update['value'] = null; } else { $update['value'] = (double) $values['value']; } } // validate - discount if (isset($values['discount'])) { if (empty($values['discount'])) { $update['discount'] = null; } else { $update['discount'] = (double) $values['discount']; } } // validate - lat if (isset($values['lat'])) { if (preg_match('/^\\s*$/', $values['lat'])) { $update['lat'] = null; } else { $lat = (double) $values['lat']; if ($lat <= -180 || $lat >= 180) { throw new Yadda_Model_Exception('Please provide a valid latitude.'); } $update['lat'] = $lat; } } // validate - long if (isset($values['long'])) { if (preg_match('/^\\s*$/', $values['long'])) { $update['long'] = null; } else { $long = (double) $values['long']; if ($long <= -180 || $long >= 180) { throw new Yadda_Model_Exception('Please provide a valid longitude.'); } $update['long'] = $long; } } // check if there's anything to do? if (sizeof($update) == 0) { return; } // do the update $dealDb = Yadda_Db_Table::getInstance('deal'); $dealDb->getAdapter()->beginTransaction(); try { $update['modified'] = Yadda_Db::now(); $dealDb->update($update, $where); $dealDb->getAdapter()->commit(); } catch (Exception $e) { $dealDb->getAdapter()->rollBack(); throw $e; } }
/** * Updates a feed. * * @param int $id * @param array $values * @throws Yadda_Model_Exception * @return void */ public static function update($id, array $values) { $feed = self::find($id); $update = array(); $where = array('id = ?' => $feed['id']); // validate - site if (isset($values['site'])) { $site = Yadda_Model_Site::find($values['site']); $update['site_id'] = $site['id']; } // validate - region if (isset($values['region'])) { $region = Yadda_Model_Region::find($values['region']); $update['region_id'] = $region['id']; } // validate - engine if (isset($values['engine'])) { if (!isset(self::$engines[$values['engine']])) { throw new Yadda_Model_Exception('Please select a valid engine.'); } $update['engine'] = $values['engine']; } // validate - url if (isset($values['url'])) { if (empty($values['url'])) { throw new Yadda_Model_Exception('Please supply a URL for this feed.'); } else { try { $uri = Zend_Uri::factory($values['url']); $update['url'] = $values['url']; } catch (Zend_Uri_Exception $e) { throw new Yadda_Model_Exception('Please supply a valid URL for this feed.'); } } } // check if there's anything to do? if (sizeof($update) == 0) { return; } // do the update $feedDb = Yadda_Db_Table::getInstance('feed'); $feedDb->getAdapter()->beginTransaction(); try { $feedDb->update($update + array('modified' => Yadda_Db::now()), $where); $feedDb->getAdapter()->commit(); } catch (Exception $e) { $feedDb->getAdapter()->rollBack(); throw new Yadda_Model_Exception('Database error: ' . $e->getMessage()); } }