Пример #1
0
 function app_model_settings($request, $app_label = false, $model = false)
 {
     $templateArr = array('current_admin_menu' => $app_label, 'current_admin_submenu' => $model, 'current_admin_submenu2' => 'Settings', 'title' => __(sprintf('%s %s Settings', $app_label, $model)));
     $coreApps = array('Post');
     $app_label0 = $app_label;
     if (in_array($app_label, $coreApps)) {
         $app_label = sprintf('Pjango\\Contrib\\%s', $app_label);
     }
     $contentType = ContentType::get_for_model($model, $app_label);
     $formClass = sprintf('%s\\Forms\\%sSettingsForm', $app_label, $model);
     $formData = array();
     $ignoredSettings = array('is_active', 'title', 'show_title', 'category_id', 'content');
     $settings = Doctrine_Query::create()->from('Settings o')->where('o.category = ? AND o.site_id = ? ', array($app_label, SITE_ID))->fetchArray();
     foreach ($settings as $settingsValue) {
         $formData[$settingsValue['name']] = $settingsValue['value'];
     }
     if (class_exists('PageLayout')) {
         $pageLayout = Doctrine_Query::create()->from('PageLayout o')->where('o.site_id = ? AND o.content_type_id = ?', array(SITE_ID, $contentType->id))->fetchOne();
         if ($pageLayout) {
             $formData = array_merge($formData, $pageLayout->toArray());
         }
     }
     if ($request->POST) {
         $form = new $formClass($request->POST);
         try {
             if (!$form->is_valid()) {
                 throw new Exception(pjango_gettext('There are some errors, please correct them below.'));
             }
             $formData = $form->cleaned_data();
             if (class_exists('PageLayout')) {
                 if (!$pageLayout) {
                     $pageLayout = new PageLayout();
                 }
                 $pageLayout->fromArray($formData);
                 $pageLayout->content_type_id = $contentType->id;
                 $pageLayout->site_id = SITE_ID;
                 $pageLayout->save();
             }
             foreach ($formData as $key => $value) {
                 if (in_array($key, $ignoredSettings)) {
                     unset($formData[$key]);
                 }
             }
             Settings::saveFromArray($app_label, $formData);
             Messages::Info('The operation completed successfully');
             HttpResponseRedirect(sprintf('/admin/%s/%s/settings/', $app_label0, $model));
         } catch (Exception $e) {
             Messages::Error($e->getMessage());
         }
     }
     if (!$form) {
         $form = new $formClass($formData);
     }
     $templateArr['addchange_form'] = $form;
     render_to_response('admin/addchange.html', $templateArr);
 }