protected function _prepareForm() { $form = new Varien_Data_Form(); $fieldset = $form->addFieldset('categories', array('legend' => $this->__('Categories'))); $fieldset->addField('category_ids', 'multiselect', array('name' => 'category_ids[]', 'label' => $this->__('Categories'), 'values' => AW_Kbase_Model_Mysql4_Category::toOptionArray(), 'required' => true)); $data = Mage::registry('kbase_article'); if (!is_array($data)) { $data = array(); } $form->setValues($data); $this->setForm($form); return parent::_prepareForm(); }
protected function _prepareColumns() { $this->addColumn('article_title', array('index' => 'article_title', 'header' => $this->__('Title'))); $this->addColumn('article_author', array('index' => 'u_name', 'header' => $this->__('Author'), 'filter_condition_callback' => array($this, '_filterAuthorCondition'))); $categoryOptions = AW_Kbase_Model_Mysql4_Category::getCategories(); $allOptions = array(0 => $this->__('No category')); foreach ($categoryOptions as $key => $value) { $allOptions[$key] = $value; } $this->addColumn('categories', array('index' => 'category_ids', 'type' => 'options', 'header' => $this->__('Categories'), 'renderer' => 'AW_Kbase_Block_Adminhtml_Grid_Column_Multiselect', 'options' => $allOptions, 'value_separator' => ',', 'line_separator' => '<br>', 'filter_condition_callback' => array($this, '_filterCategoryCondition'))); if (!Mage::app()->isSingleStoreMode()) { $this->addColumn('store_ids', array('header' => $this->__('Store views'), 'index' => 'store_ids', 'type' => 'store', 'filter' => false, 'store_all' => true, 'store_view' => true, 'sortable' => false)); } $this->addColumn('article_rating', array('index' => 'article_rating', 'type' => 'number', 'header' => $this->__('Rating'), 'width' => '50px', 'align' => 'right', 'renderer' => 'AW_Kbase_Block_Adminhtml_Grid_Column_Rating')); $this->addColumn('article_rating_votes', array('index' => 'article_rating_votes', 'type' => 'number', 'header' => $this->__('Votes'), 'width' => '50px', 'align' => 'right')); $this->addColumn('article_status', array('index' => 'article_status', 'type' => 'options', 'header' => $this->__('Status'), 'width' => '80px', 'options' => AW_Kbase_Model_Source_Status::toShortOptionArray())); $this->addColumn('article_date_created', array('index' => 'article_date_created', 'header' => $this->__('Created at'), 'width' => '120px', 'type' => 'datetime')); $this->addColumn('article_date_updated', array('index' => 'article_date_updated', 'header' => $this->__('Updated at'), 'width' => '120px', 'type' => 'datetime')); $this->addColumn('action', array('header' => $this->__('Action'), 'width' => '50px', 'type' => 'action', 'getter' => 'getId', 'actions' => array(array('caption' => $this->__('Edit'), 'url' => array('base' => '*/*/edit'), 'field' => 'id')), 'filter' => false, 'sortable' => false, 'index' => 'article_id', 'is_system' => true)); // $this->addExportType('*/*/exportCsv', $this->__('CSV')); // $this->addExportType('*/*/exportXml', $this->__('XML')); return parent::_prepareColumns(); }
public function saveAction() { $session = Mage::getSingleton('adminhtml/session'); if ($data = $this->getRequest()->getPost()) { try { $data['article_tags'] = implode(', ', Mage::helper('kbase')->prepareTags($data['article_tags'])); $id = $this->getRequest()->getParam('id'); $_article = Mage::getModel('kbase/article')->load($id); $_oldSelectedCategories = $_article->getData('category_ids'); $_currentAllCategories = AW_Kbase_Model_Mysql4_Category::toOptionArray(); $_currentAllCategoriesFlat = array(); foreach ($_currentAllCategories as $k => $v) { if (isset($v['value'])) { $_currentAllCategoriesFlat[] = $v['value']; } } $data['category_ids'] = array_merge($data['category_ids'], array_values(array_diff($_oldSelectedCategories, $_currentAllCategoriesFlat))); unset($_article); $fileUploadNeeded = isset($_FILES['article_attachment']['name']) && $_FILES['article_attachment']['name']; // delete file if checkbox checked if (isset($data['article_attachment_delete']) && isset($data['article_attachment_old']) && $data['article_attachment_old']) { @unlink(AW_Kbase_Helper_Data::attachmentDirName($id) . DS . $data['article_attachment_old']); $data['article_attachment'] = ''; } // checking creation & updating date if (!isset($data['article_date_created']) || !$data['article_date_created']) { $data['article_date_created'] = now(); } $data['article_date_updated'] = now(); // end of time checking // checking rating $data['article_rating_votes'] = abs($data['article_rating_votes']); if (1 > $data['article_rating'] || 5 < $data['article_rating']) { $session->addError($this->__('Rating should be between 1 and 5')); $session->setKBaseArticleData($data); $this->_redirect('*/*/edit', array('id' => $id, 'tab' => 'rating')); return; } // checking URL key if (!isset($data['article_url_key']) || !$data['article_url_key']) { $data['article_url_key'] = $data['article_title']; } $data['article_url_key'] = AW_Kbase_Helper_Url::nameToUrlKey($data['article_url_key']); $model = Mage::getModel('kbase/article')->setData($data)->setId($id); //checking URL keys uniqueness $_origUrlKey = $data['article_url_key']; $_cnt = 1; $_categoryModel = Mage::getModel('kbase/category'); $_categoryModel->setCategoryStoreIds($model->getResource()->getCategorySetStoreIds($model->getCategoryIds()))->setCategoryUrlKey($data['article_url_key']); while ($model->isUrlKeyUsed() || $_categoryModel->isUrlKeyUsed()) { $model->setData('article_url_key', sprintf("%s-%d", $_origUrlKey, $_cnt)); $_categoryModel->setCategoryUrlKey(sprintf("%s-%d", $_origUrlKey, $_cnt++)); } if (!$id && $fileUploadNeeded) { $model->save(); $id = $model->getId(); } if ($fileUploadNeeded) { $uploader = new Varien_File_Uploader('article_attachment'); $uploader->setAllowedExtensions(null)->setAllowRenameFiles(false)->setAllowCreateFolders(true)->setFilesDispersion(false); $uploader->save(AW_Kbase_Helper_Data::attachmentDirName($id), $_FILES['article_attachment']['name']); $model->setArticleAttachment($uploader->getUploadedFileName()); } $model->save(); $session->addSuccess($this->__('Article was successfully saved')); $session->setKBaseArticleData(false); if ($this->getRequest()->getParam('back')) { $this->_redirect('*/*/edit', array('id' => $model->getId())); return; } $this->_redirect('*/*/'); return; } catch (Exception $e) { Mage::logException($e); $session->addError($e->getMessage()); $session->setKBaseArticleData($data); $this->_redirect('*/*/edit', array('id' => $id)); return; } } else { $session->addError($this->__('Unable to find an article to save')); $this->_redirect('*/*/'); } }