3

I'm working on a module in magento admin where I would like to show the scope selector as in manage products, manage categories, system configuration for my grid edit form.

![screenshot][1]

Can anyone help me how could I add that and store values in database specific to the scope selected?

Update
saveAction() from adminhtml controller

public function saveAction() {
    if ($data = $this->getRequest()->getPost()) {

        if (isset($_FILES['smalllogo']['name']) && $_FILES['smalllogo']['name'] != '') {
            try {
                /* Starting upload */
                $uploader = new Varien_File_Uploader('smalllogo');

                // Any extention would work
                $uploader->setAllowedExtensions(array('jpg', 'jpeg', 'gif', 'png'));
                $uploader->setAllowRenameFiles(false);

                // Set the file upload mode
                // false -> get the file directly in the specified folder
                // true -> get the file in the product like folders
                //  (file.jpg will go in something like /media/f/i/file.jpg)
                $uploader->setFilesDispersion(false);

                // We set media as the upload dir
                $path = Mage::getBaseDir('media') . DS;
                $logo = $uploader->save($path, $_FILES['smalllogo']['name']);
            } catch (Exception $e) {

            }

            // $mediaPath = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA);
            $data['smalllogo'] = $logo['file']; //$_FILES['smalllogo']['name']; //$mediaPath . $_FILES['smalllogo']['name'];
        } else if (isset($data['smalllogo']['delete']) && $data['smalllogo']['delete'] == 1) {
            unlink(Mage::getBaseDir('media') . DS . $data['smalllogo']['value']);
            $data['smalllogo'] = '';
        } else {
            unset($data['smalllogo']);
        }

        if (isset($_FILES['banner']['name']) && $_FILES['banner']['name'] != '') {
            try {
                /* Starting upload */
                $uploader = new Varien_File_Uploader('banner');

                // Any extention would work
                $uploader->setAllowedExtensions(array('jpg', 'jpeg', 'gif', 'png'));
                $uploader->setAllowRenameFiles(false);

                // Set the file upload mode
                // false -> get the file directly in the specified folder
                // true -> get the file in the product like folders
                //  (file.jpg will go in something like /media/f/i/file.jpg)
                $uploader->setFilesDispersion(false);

                $path = Mage::getBaseDir('media') . DS;
                $banner = $uploader->save($path, $_FILES['banner']['name']);
            } catch (Exception $e) {

            }

            // $mediaPath = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA);
            $data['banner'] = $banner['file']; //$_FILES['banner']['name']; //$mediaPath . $_FILES['banner']['name'];
        } else if (isset($data['banner']['delete']) && $data['banner']['delete'] == 1) {
            unlink(Mage::getBaseDir('media') . DS . $data['banner']['value']);
            $data['banner'] = '';
        } else {
            unset($data['banner']);
        }


        $model = Mage::getModel('designer/designer');
        $model->setData($data)
                ->setId($this->getRequest()->getParam('id'));

        try {
            if ($model->getCreatedTime == NULL || $model->getUpdateTime() == NULL) {
                $model->setCreatedTime(now())
                        ->setUpdateTime(now());
            } else {
                $model->setUpdateTime(now());
            }

            $model->save();
            Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('designer')->__('Designer was successfully saved'));
            Mage::getSingleton('adminhtml/session')->setFormData(false);

            if ($this->getRequest()->getParam('back')) {
                $this->_redirect('*/*/edit', array('id' => $model->getId()));
                return;
            }
            $this->_redirect('*/*/');
            return;
        } catch (Exception $e) {
            Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
            Mage::getSingleton('adminhtml/session')->setFormData($data);
            $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
            return;
        }
    }
    Mage::getSingleton('adminhtml/session')->addError(Mage::helper('designer')->__('Unable to find designer to save'));
    $this->_redirect('*/*/');
}
4

Add this to the layout handle for the add/edit page:

<reference name="left">
    <block type="adminhtml/store_switcher" name="store_switcher" before="-" />
    <!-- add other blocks if you have in here -->
</reference>

[EDIT]
If your models extend the EAV models you can save the values for a specific store by replacing

$model->setData($data)->setId($this->getRequest()->getParam('id'));

with

$storeId = $this->getRequest()->getParam('store', 0);
$model->setData($data)->setStoreId($storeId)->setId($this->getRequest()->getParam('id'));
| improve this answer | |
  • 1
    but how would I be able to save the selected scope in database so that the saved record will be visible only in that particular store or website – Mohammad Faisal Mar 21 '14 at 12:25
  • also, If I do change the scope it shows me an alert message. but in case of magento's default modules it do not show any alert – Mohammad Faisal Mar 21 '14 at 12:29
  • Let me get the big picture strait. You are trying to build a module that should contain an EAV entity. One that behaves as the products and categories, right? – Marius Mar 21 '14 at 12:31
  • exactly. you are right. Could you please help me with that? – Mohammad Faisal Mar 21 '14 at 12:33
  • This may not solve your problem, but It can help you kick start you extension. You can give this tool a try: github.com/tzyganu/UMC1.9. It's a module creator that can also create EAV entities. It is still under development, but it seams to work OK for simple EAV entities. It still has some issues with the API files (if you need an API for your entity) but I'm working on it. Anyway, even if it has a few more bugs in it, you can at least start your module with it and just change something here and there. – Marius Mar 21 '14 at 12:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.