public function actionSaveLayout() { if (craft()->dashCols->isCpSectionDisabled()) { throw new HttpException(404); } $this->requirePostRequest(); $request = craft()->request; $layout = new DashCols_LayoutModel(); $layout->id = ($layoutId = $request->getPost('layoutId')) ? $layoutId : null; $layout->sectionId = $request->getPost('sectionId'); $layout->categoryGroupId = $request->getPost('categoryGroupId'); $layout->listingHandle = $request->getPost('listingHandle'); if ($layout->sectionId) { $section = craft()->dashCols->getSectionById($layout->sectionId); } else { if ($layout->categoryGroupId) { $section = craft()->dashCols->getCategoryGroupById($layout->categoryGroupId); } else { if ($layout->listingHandle) { $section = craft()->dashCols->getListingByHandle($layout->listingHandle); } else { throw new HttpException(404); } } } $fieldLayout = craft()->fields->assembleLayoutFromPost(); $fieldLayout->type = ElementType::Asset; $layout->setFieldLayout($fieldLayout); // Get hidden fields $hiddenFields = array(); foreach ($request->getPost('hiddenFields') as $key => $value) { if ($value !== '1') { $hiddenFields[] = $key; } } $layout->hiddenFields = !empty($hiddenFields) ? $hiddenFields : false; // Get meta fields $metaFields = array(); foreach ($request->getPost('metaFields') as $key => $value) { if ($value === '1') { $metaFields[] = $key; } } $layout->metaFields = !empty($metaFields) ? $metaFields : false; if (craft()->dashCols_layouts->saveLayout($layout)) { craft()->userSession->setNotice(Craft::t('Layout for ' . $section->name . ' saved!')); $this->redirectToPostedUrl($layout); } else { craft()->userSession->setError(Craft::t('Something went wrong. Layout not saved.')); } craft()->urlManager->setRouteVariables(array('layout' => $layout)); }
public function saveLayout(DashCols_LayoutModel $dashColsLayout) { $existingLayout = false; if ($dashColsLayout->id) { if (!($dashColsLayoutRecord = DashCols_LayoutRecord::model()->findById($dashColsLayout->id))) { throw new Exception(Craft::t('Could not find layout with ID "{id}"', array('id' => $dashColsLayout->id))); } $existingLayout = DashCols_LayoutModel::populateModel($dashColsLayoutRecord); } else { $dashColsLayoutRecord = new DashCols_LayoutRecord(); } if ($dashColsLayout->sectionId) { $dashColsLayoutRecord->sectionId = $dashColsLayout->sectionId; } else { if ($dashColsLayout->categoryGroupId) { $dashColsLayoutRecord->categoryGroupId = $dashColsLayout->categoryGroupId; } else { if ($dashColsLayout->listingHandle) { $dashColsLayoutRecord->listingHandle = $dashColsLayout->listingHandle; } else { throw new Exception(Craft::t('Unknown target for layout')); } } } $dashColsLayoutRecord->hiddenFields = $dashColsLayout->hiddenFields; $dashColsLayoutRecord->metaFields = $dashColsLayout->metaFields; $dashColsLayoutRecord->validate(); $dashColsLayout->addErrors($dashColsLayoutRecord->getErrors()); if (!$dashColsLayout->hasErrors()) { $transaction = craft()->db->getCurrentTransaction() === null ? craft()->db->beginTransaction() : null; try { if ($existingLayout && $existingLayout->fieldLayoutId) { craft()->fields->deleteLayoutById($existingLayout->fieldLayoutId); } $fieldLayout = $dashColsLayout->getFieldLayout(); craft()->fields->saveLayout($fieldLayout); $dashColsLayout->fieldLayoutId = $fieldLayout->id; $dashColsLayoutRecord->fieldLayoutId = $fieldLayout->id; if (!$dashColsLayout->id) { $dashColsLayoutRecord->save(); } else { $dashColsLayoutRecord->update(); } $dashColsLayout->id = $dashColsLayoutRecord->id; if ($transaction !== null) { $transaction->commit(); } } catch (\Exception $e) { if ($transaction !== null) { $transaction->rollback(); } throw $e; } return true; } return false; }