コード例 #1
0
ファイル: advancetopmenu.php プロジェクト: abdoumej/libsamy
 public function getContent()
 {
     $output = '';
     $errors = array();
     $id_lang_default = (int) Configuration::get('PS_LANG_DEFAULT');
     $languages = Language::getLanguages(false);
     if (Tools::getValue('confirm_msg')) {
         $this->context->smarty->assign('confirmation', Tools::getValue('confirm_msg'));
     }
     if (Tools::isSubmit('submitnewItem')) {
         $id_item = (int) Tools::getValue('item_id');
         if ($id_item && Validate::isUnsignedId($id_item)) {
             $new_item = new Item($id_item);
         } else {
             $new_item = new Item();
         }
         $new_item->id_block = Tools::getValue('block_id');
         $new_item->type = Tools::getValue('linktype');
         $new_item->active = (int) Tools::getValue('active');
         $itemtitle_set = false;
         foreach ($languages as $language) {
             $item_title = Tools::getValue('item_title_' . $language['id_lang']);
             if (strlen($item_title) > 0) {
                 $itemtitle_set = true;
             }
             $new_item->title[$language['id_lang']] = $item_title;
         }
         if (!$itemtitle_set) {
             $lang_title = Language::getLanguage($this->context->language->id);
             if ($new_item->type == 'img') {
                 $errors[] = 'This Alt text field is required at least in ' . $lang_title['name'];
             } else {
                 $errors[] = 'This item title field is required at least in ' . $lang_title['name'];
             }
         }
         $new_item->class = Tools::getValue('custom_class');
         if ($new_item->type == 'link') {
             $new_item->icon = Tools::getValue('item_icon');
             $new_item->link = Tools::getValue('link_value');
         } elseif ($new_item->type == 'img') {
             if (isset($_FILES['item_img']) && strlen($_FILES['item_img']['name']) > 0) {
                 if (!($img_file = $this->moveUploadedImage($_FILES['item_img']))) {
                     $errors[] = 'An error occurred during the image upload.';
                 } else {
                     $new_item->icon = $img_file;
                     if (Tools::getValue('old_img') != '') {
                         $filename = Tools::getValue('old_img');
                         if (file_exists(dirname(__FILE__) . '/img/' . $filename)) {
                             @unlink(dirname(__FILE__) . '/img/' . $filename);
                         }
                     }
                 }
             } else {
                 $new_item->icon = Tools::getValue('old_img');
             }
             $new_item->link = Tools::getValue('link_value');
         } elseif ($new_item->type == 'html') {
             foreach ($languages as $language) {
                 $new_item->text[$language['id_lang']] = Tools::getValue('item_html_' . $language['id_lang']);
             }
         }
         if (!count($errors)) {
             if ($id_item && Validate::isUnsignedId($id_item)) {
                 if (!$new_item->update()) {
                     $errors[] = 'An error occurred while update data.';
                 }
             } else {
                 if (!$new_item->add()) {
                     $errors[] = 'An error occurred while saving data.';
                 }
             }
             if (!count($errors)) {
                 if ($id_item && Validate::isUnsignedId($id_item)) {
                     $this->context->smarty->assign('confirmation', $this->l('Item successfully updated.'));
                 } else {
                     $confirm_msg = $this->l('New item successfully added.');
                     Tools::clearCache(Context::getContext()->smarty, $this->getTemplatePath('topmenu.tpl'));
                     Tools::redirectAdmin(AdminController::$currentIndex . '&configure=' . $this->name . '&token=' . Tools::getAdminTokenLite('AdminModules') . '&confirm_msg=' . $confirm_msg);
                 }
             }
         }
     } elseif (Tools::isSubmit('submit_del_item')) {
         $item_id = Tools::getValue('item_id');
         if ($item_id && Validate::isUnsignedId($item_id)) {
             $subs = $this->getSupMenu($item_id);
             $del = true;
             if ($subs && count($subs) > 0) {
             }
             foreach ($subs as $sub) {
                 $del &= $this->deleteSub($sub['id_sub']);
             }
             $item = new Item($item_id);
             if (!$item->delete() || !$del) {
                 $errors[] = 'An error occurred while delete item.';
             } else {
                 Tools::clearCache(Context::getContext()->smarty, $this->getTemplatePath('topmenu.tpl'));
                 $this->context->smarty->assign('confirmation', $this->l('Delete successful.'));
             }
         }
     } elseif (Tools::isSubmit('submitnewsub')) {
         $id_sub = Tools::getValue('id_sub');
         if ($id_sub && Validate::isUnsignedId($id_sub)) {
             $sub = new Submenu($id_sub);
         } else {
             $sub = new Submenu();
         }
         $sub->id_parent = Tools::getValue('id_parent');
         $sub->width = Tools::getValue('subwidth');
         $sub->class = Tools::getValue('sub_class');
         $sub->active = Tools::getValue('active');
         if ($id_sub && Validate::isUnsignedId($id_sub)) {
             if (!$sub->update()) {
                 $errors[] = 'An error occurred while update data.';
             }
         } else {
             if (!$sub->checkAvaiable()) {
                 if (!$sub->add()) {
                     $errors[] = 'An error occurred while saving data.';
                 }
             } else {
                 $parent_item = new Item($sub->id_parent);
                 $errors[] = $parent_item->title[$this->context->language->id] . ' already have a sub.';
             }
         }
         if (!count($errors)) {
             if ($id_sub && Validate::isUnsignedId($id_sub)) {
                 $this->context->smarty->assign('confirmation', $this->l('Submenu successfully updated.'));
             } else {
                 $confirm_msg = $this->l('New sub successfully added.');
                 Tools::clearCache(Context::getContext()->smarty, $this->getTemplatePath('topmenu.tpl'));
                 Tools::redirectAdmin(AdminController::$currentIndex . '&configure=' . $this->name . '&token=' . Tools::getAdminTokenLite('AdminModules') . '&confirm_msg=' . $confirm_msg);
             }
         }
     } elseif (Tools::isSubmit('submit_del_sub')) {
         $id_sub = (int) Tools::getValue('id_sub');
         if ($id_sub && Validate::isUnsignedId($id_sub)) {
             if (!$this->deleteSub($id_sub)) {
                 $errors[] = 'An error occurred while delete sub menu.';
             } else {
                 Tools::clearCache(Context::getContext()->smarty, $this->getTemplatePath('topmenu.tpl'));
                 $this->context->smarty->assign('confirmation', $this->l('Delete successful.'));
             }
         }
     } elseif (Tools::isSubmit('submitnewblock')) {
         $id_block = Tools::getValue('id_block');
         if ($id_block && Validate::isUnsignedId($id_block)) {
             $block = new Block($id_block);
         } else {
             $block = new Block();
         }
         $block->id_sub = Tools::getValue('id_sub');
         $block->width = Tools::getValue('block_widh');
         $block->class = Tools::getValue('block_class');
         if ($id_block && Validate::isUnsignedId($id_block)) {
             if (!$block->update()) {
                 $errors[] = 'An error occurred while update block.';
             }
         } else {
             if (!$block->add()) {
                 $errors[] = 'An error occurred while saving data.';
             }
         }
         if (!count($errors)) {
             if ($id_block && Validate::isUnsignedId($id_block)) {
                 $this->context->smarty->assign('confirmation', $this->l('Block successfully updated.'));
             } else {
                 $confirm_msg = $this->l('New block successfully added.');
                 Tools::clearCache(Context::getContext()->smarty, $this->getTemplatePath('topmenu.tpl'));
                 Tools::redirectAdmin(AdminController::$currentIndex . '&configure=' . $this->name . '&token=' . Tools::getAdminTokenLite('AdminModules') . '&confirm_msg=' . $confirm_msg);
             }
         }
     } elseif (Tools::isSubmit('submit_del_block')) {
         $id_block = Tools::getValue('id_block');
         if ($id_block && Validate::isUnsignedId($id_block)) {
             if (!$this->deleteBlock($id_block)) {
                 $errors[] = 'An error occurred while delete block.';
             } else {
                 Tools::clearCache(Context::getContext()->smarty, $this->getTemplatePath('topmenu.tpl'));
                 $this->context->smarty->assign('confirmation', $this->l('Delete successful.'));
             }
         }
     } elseif (Tools::isSubmit('changeactive')) {
         $id_item = (int) Tools::getValue('item_id');
         if ($id_item && Validate::isUnsignedId($id_item)) {
             $item = new Item($id_item);
             $item->active = !$item->active;
             if (!$item->update()) {
                 $errors[] = $this->displayError('Could not change');
             } else {
                 $confirm_msg = $this->l('Successfully updated.');
                 Tools::clearCache(Context::getContext()->smarty, $this->getTemplatePath('topmenu.tpl'));
                 Tools::redirectAdmin(AdminController::$currentIndex . '&configure=' . $this->name . '&token=' . Tools::getAdminTokenLite('AdminModules') . '&confirm_msg=' . $confirm_msg);
             }
         }
     } elseif (Tools::isSubmit('changestatus')) {
         $id_sub = (int) Tools::getValue('id_sub');
         if ($id_sub && Validate::isUnsignedId($id_sub)) {
             $sub_menu = new Submenu($id_sub);
             $sub_menu->active = !$sub_menu->active;
             if (!$sub_menu->update()) {
                 $errors[] = $this->displayError('Could not change');
             } else {
                 $confirm_msg = $this->l('Submenu successfully updated.');
                 Tools::clearCache(Context::getContext()->smarty, $this->getTemplatePath('topmenu.tpl'));
                 Tools::redirectAdmin(AdminController::$currentIndex . '&configure=' . $this->name . '&token=' . Tools::getAdminTokenLite('AdminModules') . '&confirm_msg=' . $confirm_msg);
             }
         }
     }
     $this->context->smarty->assign(array('admin_tpl_path' => $this->admin_tpl_path, 'postAction' => AdminController::$currentIndex . '&configure=' . $this->name . '&token=' . Tools::getAdminTokenLite('AdminModules')));
     if (count($errors) > 0) {
         if (isset($errors) && count($errors)) {
             $output .= $this->displayError(implode('<br />', $errors));
         }
     }
     if (Tools::isSubmit('submit_edit_item') || Tools::isSubmit('submitnewItem') && count($errors) > 0) {
         $output .= $this->displayItemForm();
     } elseif (Tools::isSubmit('submit_edit_sub')) {
         $output .= $this->displaySubForm();
     } elseif (Tools::isSubmit('submit_new_block')) {
         $output .= $this->displayBlockForm();
     } else {
         $output .= $this->displayForm();
     }
     return $output;
 }
コード例 #2
0
ファイル: Formr.php プロジェクト: Metrakit/dynamix
 public function generate($data, $pageId, $order)
 {
     $form = new self();
     $fromObject = false;
     if (is_object($data)) {
         $data = $data->formr();
         $form->i18n_title = NULL;
         $form->i18n_description = NULL;
         $fromObject = true;
     }
     $form->finish_on = $data['method'];
     $form->type = $data['type'];
     $form->save();
     $orderMap = 0;
     foreach ($data['data'] as $dataInput) {
         // Increments the order
         $orderMap++;
         if ($fromObject) {
             $dataInput['title'] = NULL;
             $dataInput['label'] = NULL;
             $dataInput['placeholder'] = NULL;
             $dataInput['helper'] = NULL;
         } else {
             $dataInput['title'] = i18n::add($dataInput['title'], 'title');
             $dataInput['label'] = i18n::add($dataInput['label'], 'label');
             $dataInput['placeholder'] = i18n::add($dataInput['placeholder'], 'placeholder');
             $dataInput['helper'] = i18n::add($dataInput['helper'], 'helper');
         }
         $inputType = InputType::add($dataInput);
         $input = InputView::add($inputType->id, $dataInput);
         // Add options if the input type is a select
         if ($input->name = "select") {
             if (isset($dataInput['options'])) {
                 foreach ($dataInput['options'] as $option) {
                     if ($fromObject) {
                         $option['key'] = NULL;
                         $option['value'] = NULL;
                     } else {
                         $option['key'] = i18n::add($option['key'], 'option_key');
                         $option['value'] = i18n::add($option['value'], 'option_value');
                     }
                     SelectOption::add($input->id, $option);
                 }
             }
         }
         // Add form map
         FormMap::add($input->id, $form->id, $orderMap);
     }
     // Add a block if the form is not by a model
     if (!$fromObject) {
         $block = Block::add($pageId, $order, 'Formr');
         BlockResponsive::add($block->id, 12, 3);
     }
     if ($fromObject) {
         // Add form model
         ModelForm::add($form->id, $data['model']);
     }
     // Generate a migrate file
     if ($form->finish_on == "database") {
         // Prepare migrate content
         $contentMigrate = $this->prepareMigrate($data['data']);
         $bob = new Migrator('form_' . $form->id, $contentMigrate);
         $bob->generate();
     }
     return $form;
 }