/** * Saves a navigation. */ public function actionSaveNavigation() { $this->requirePostRequest(); // Get navigation if available $navId = craft()->request->getPost('navId'); if ($navId) { $navigation = craft()->amNav->getNavigationById($navId); if (!$navigation) { throw new Exception(Craft::t('No navigation exists with the ID “{id}”.', array('id' => $navId))); } } else { $navigation = new AmNav_NavigationModel(); } // Set attributes $attributes = craft()->request->getPost(); if (!isset($attributes['settings']['entrySources']) || $attributes['settings']['entrySources'] == '') { $attributes['settings']['entrySources'] = '*'; } if (!is_numeric($attributes['settings']['maxLevels'])) { $attributes['settings']['maxLevels'] = ''; } if (!is_numeric($attributes['settings']['canDeleteFromLevel'])) { $attributes['settings']['canDeleteFromLevel'] = ''; } if (!is_numeric($attributes['settings']['canMoveFromLevel'])) { $attributes['settings']['canMoveFromLevel'] = ''; } $navigation->setAttributes(array('name' => $attributes['name'], 'handle' => $attributes['handle'], 'settings' => $attributes['settings'])); // Save navigation if (craft()->amNav->saveNavigation($navigation)) { craft()->userSession->setNotice(Craft::t('Navigation saved.')); $this->redirectToPostedUrl($navigation); } else { craft()->userSession->setError(Craft::t('Couldn’t save navigation.')); // Send the navigation back to the template craft()->urlManager->setRouteVariables(array('navigation' => $navigation)); } }
/** * Saves a navigation. * * @param AmNav_NavigationModel $navigation * * @throws Exception * @return bool */ public function saveNavigation(AmNav_NavigationModel $navigation) { // Navigation data if ($navigation->id) { $navigationRecord = AmNav_NavigationRecord::model()->findById($navigation->id); if (!$navigationRecord) { throw new Exception(Craft::t('No navigation exists with the ID “{id}”.', array('id' => $navigation->id))); } } else { $navigationRecord = new AmNav_NavigationRecord(); } // Set attributes $navigationRecord->setAttributes($navigation->getAttributes()); $navigationRecord->setAttribute('settings', json_encode($navigation->settings)); // Validate $navigationRecord->validate(); $navigation->addErrors($navigationRecord->getErrors()); // Save navigation if (!$navigation->hasErrors()) { // Save in database return $navigationRecord->save(); } return false; }