0

I am getting mental break because I can't make my custom module which should rewrite 2 blocks and 1 model work. My module is loaded - I checked in admin panel System->Configuration->Advanced but Magento still reads core files. Overmore, I am trying to rewrite reviews form so when I am in admin panel watching reviews and make intentional mistake in name of class in Acme_Reviewed_Block_Adminhtml_Review_Edit_Form I get blank screen so it looks like it is reading my blocks and models. Maybe I made a mistake somewhere which I can't find.

config.xml

<config>
  <modules>
    <Acme_Reviewed>
      <version>1.0</version>
    </Acme_Reviewed>
  </modules>
  <global>
    <models>
        <reviewed>
            <class>Acme_Reviewed_Model</class>
        </reviewed>
        <review>
            <rewrite>
                <resource_review>Acme_Reviewed_Model_Resource_Review</resource_review>
            </rewrite>
        </review>
    </models>
    <blocks>
        <reviewed>
            <class>Acme_Reviewed_Block</class>
        </reviewed>
        <ajaxreviews>
            <rewrite>
                <reviews>Acme_Reviewed_Block_Reviews</reviews>
            </rewrite>
        </ajaxreviews>
        <adminhtml>
            <rewrite>
                <review_edit_form>Acme_Reviewed_Block_Adminhtml_Review_Edit_Form</review_edit_form>
            </rewrite>
        </adminhtml>
    </blocks>
  </global>
</config>

Acme_Reviewed.xml

<config>
  <modules>
    <Acme_Reviewed>
        <active>true</active>
        <codePool>local</codePool>
        <depends>
            <Mage_Adminhtml />
            <Mage_Review />
            <Magpleasure_Ajaxreviews />
        </depends>
    </Acme_Reviewed>
  </modules>
</config>

I try to override:

Mage/Review/Model/Resource/Review.php
Magpleasure/Ajaxreviews/Block/Reviews.php
Mage/Adminhtml/Block/Review/Edit/Form.php

With my files:

Acme/Reviewed/Model/Resource/Review.php
Acme/Reviewed/Block/Reviews.php
Acme/Reviewed/Block/Adminhtml/Review/Edit/Form.php

Acme/Reviewed/Model/Resource/Review.php

<?php

class Acme_Reviewed_Model_Resource_Review extends Mage_Review_Model_Resource_Review
{
    protected function _afterSave(Mage_Core_Model_Abstract $object)
    {
        $adapter = $this->_getWriteAdapter();
        /**
         * save detail
         */
        Mage::log(var_export($object->getGemail(), TRUE), null, 'furoris.log');
        $detail = array(
            'title'     => $object->getTitle(),
            'detail'    => $object->getDetail(),
            'nickname'  => $object->getNickname(),
            'gemail' => $object->getGemail(),
        );
        $select = $adapter->select()
            ->from($this->_reviewDetailTable, 'detail_id')
            ->where('review_id = :review_id');
        $detailId = $adapter->fetchOne($select, array(':review_id' => $object->getId()));

        if ($detailId) {
            $condition = array("detail_id = ?" => $detailId);
            Mage::log("a", null, 'furoris.log');
            $adapter->update($this->_reviewDetailTable, $detail, $condition);
            Mage::log("b", null, 'furoris.log');
        } else {
            Mage::log($this->_reviewDetailTable, null, 'furoris.log');
            $detail['store_id']   = $object->getStoreId();
            $detail['customer_id']= $object->getCustomerId();
            $detail['review_id']  = $object->getId();
            $detail['gemail'] = $object->getGemail();
            $adapter->insert($this->_reviewDetailTable, $detail);
        }


        /**
         * save stores
         */
        $stores = $object->getStores();
        if (!empty($stores)) {
            $condition = array('review_id = ?' => $object->getId());
            $adapter->delete($this->_reviewStoreTable, $condition);

            $insertedStoreIds = array();
            foreach ($stores as $storeId) {
                if (in_array($storeId, $insertedStoreIds)) {
                    continue;
                }

                $insertedStoreIds[] = $storeId;
                $storeInsert = array(
                    'store_id' => $storeId,
                    'review_id'=> $object->getId()
                );
                $adapter->insert($this->_reviewStoreTable, $storeInsert);
            }
        }

        // reaggregate ratings, that depend on this review
        $this->_aggregateRatings(
            $this->_loadVotedRatingIds($object->getId()),
            $object->getEntityPkValue()
        );

        return $this;
    }
}

Acme_Reviewed_Block_Reviews

<?php

class Acme_Reviewed_Block_Reviews extends Magpleasure_Ajaxreviews_Block_Reviews
{
    public function getTemplateHtml()
    {    
        /** @var Magpleasure_Common_Block_Template $block */
        $block = $this->getLayout()->createBlock('magpleasure/template');
        if ($block) {
            return $block->setTemplate('ajaxreviews/product/view/list/template-guest.phtml')->toHtml();
        }
    }
}

Acme/Reviewed/Block/Adminhtml/Review/Edit/Form.php

<?php

class Acme_Reviewed_Block_Adminhtml_Review_Edit_Form extends Mage_Adminhtml_Block_Review_Edit_Form
{
    protected function _prepareForm()
    {
        $review = Mage::registry('review_data');
        $product = Mage::getModel('catalog/product')->load($review->getEntityPkValue());
        $customer = Mage::getModel('customer/customer')->load($review->getCustomerId());
        $form = new Varien_Data_Form(array(
            'id'        => 'edit_form',
            'action'    => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'), 'ret' => Mage::registry('ret'))),
            'method'    => 'post'
        ));

        $fieldset = $form->addFieldset('review_details', array('legend' => Mage::helper('review')->__('Review Details'), 'class' => 'fieldset-wide'));

        $fieldset->addField('product_name', 'note', array(
            'label'     => Mage::helper('review')->__('Product'),
            'text'      => '<a href="' . $this->getUrl('*/catalog_product/edit', array('id' => $product->getId())) . '" onclick="this.target=\'blank\'">' . $product->getName() . '</a>'
        ));

        if ($customer->getId()) {
            $customerText = Mage::helper('review')->__('<a href="%1$s" onclick="this.target=\'blank\'">%2$s %3$s</a> <a href="mailto:%4$s">(%4$s)</a>', $this->getUrl('*/customer/edit', array('id' => $customer->getId(), 'active_tab'=>'review')), $this->escapeHtml($customer->getFirstname()), $this->escapeHtml($customer->getLastname()), $this->escapeHtml($customer->getEmail()));
        } else {
            if (is_null($review->getCustomerId())) {
                $customerText = Mage::helper('review')->__($review->getGemail());
            } elseif ($review->getCustomerId() == 0) {
                $customerText = Mage::helper('review')->__('Administrator');
            }
        }

        $fieldset->addField('customer', 'note', array(
            'label'     => Mage::helper('review')->__('Posted By'),
            'text'      => $customerText,
        ));

        $fieldset->addField('summary_rating', 'note', array(
            'label'     => Mage::helper('review')->__('Summary Rating'),
            'text'      => $this->getLayout()->createBlock('adminhtml/review_rating_summary')->toHtml(),
        ));

        $fieldset->addField('detailed_rating', 'note', array(
            'label'     => Mage::helper('review')->__('Detailed Rating'),
            'required'  => true,
            'text'      => '<div id="rating_detail">'
                           . $this->getLayout()->createBlock('adminhtml/review_rating_detailed')->toHtml()
                           . '</div>',
        ));

        $fieldset->addField('status_id', 'select', array(
            'label'     => Mage::helper('review')->__('Status'),
            'required'  => true,
            'name'      => 'status_id',
            'values'    => Mage::helper('review')->getReviewStatusesOptionArray(),
        ));

        /**
         * Check is single store mode
         */
        if (!Mage::app()->isSingleStoreMode()) {
            $field = $fieldset->addField('select_stores', 'multiselect', array(
                'label'     => Mage::helper('review')->__('Visible In'),
                'required'  => true,
                'name'      => 'stores[]',
                'values'    => Mage::getSingleton('adminhtml/system_store')->getStoreValuesForForm(),
            ));
            $renderer = $this->getLayout()->createBlock('adminhtml/store_switcher_form_renderer_fieldset_element');
            $field->setRenderer($renderer);
            $review->setSelectStores($review->getStores());
        }
        else {
            $fieldset->addField('select_stores', 'hidden', array(
                'name'      => 'stores[]',
                'value'     => Mage::app()->getStore(true)->getId()
            ));
            $review->setSelectStores(Mage::app()->getStore(true)->getId());
        }

        $fieldset->addField('nickname', 'text', array(
            'label'     => Mage::helper('review')->__('Nickname'),
            'required'  => true,
            'name'      => 'nickname'
        ));

        $fieldset->addField('title', 'text', array(
            'label'     => Mage::helper('review')->__('Summary of Review'),
            'required'  => true,
            'name'      => 'title',
        ));

        $fieldset->addField('detail', 'textarea', array(
            'label'     => Mage::helper('review')->__('Review'),
            'required'  => true,
            'name'      => 'detail',
            'style'     => 'height:24em;',
        ));

        $form->setUseContainer(true);
        $form->setValues($review->getData());
        $this->setForm($form);
        return parent::_prepareForm();
    }
}
  • show your class files too.. – Rajeev K Tomy Apr 6 '16 at 12:15
  • If you're using cache system, in development mode, I suggest that you clean it. – Eduan Lenine Apr 6 '16 at 12:20
  • do you added <?xml version="1.0"?> at the top of all xml files?? – Asish Hira Apr 6 '16 at 12:27
  • yes, it is there – Furoris Apr 6 '16 at 12:29
  • edited with class – Furoris Apr 6 '16 at 12:36
0

Finally, I resolved this riddle. My code is example how much traps are waiting for unexperienced developer. I made 3 mistakes.

  1. Acme/Reviewed/Block/Adminhtml/Review/Edit/Form.php

Whole code of extended function is taken from core file Mage_Adminhtml_Block_Review_Edit_Form which I extend. I just added a simple changes. So at the end of function is line:

return parent::_prepareForm();

But the problem is Mage_Adminhtml_Block_Review_Edit_Form which I extend also extends other file Mage_Adminhtml_Block_Widget_Form.

So in my file 'parent' means Mage_Adminhtml_Block_Review_Edit_Form but in the core it stands for return Mage_Adminhtml_Block_Widget_Form

I had to change this line to this and works perfect:

return Mage_Adminhtml_Block_Widget_Form::_prepareForm();

  1. Acme/Reviewed/Model/Resource/Review.php

I wrote config well, but for extending model, not for extending model resource. So when I changed config.xml to the code below it is working well also.

<review_resource>
    <rewrite>
        <review>Acme_Reviewed_Model_Resource_Review</resource_review>
    </rewrite>
</review_resource>
  1. Acme_Reviewed_Block_Reviews

So here I wanted to extend function which was located in Magpleasure_Ajaxreviews_Block_Reviews, but I didn't notice that it is not the end of 'extending chain'. I found that the last file which extends Magpleasure_Ajaxreviews_Block_Reviews is Magpleasure_Ajaxreviews_Block_Placing_Additional so I had to extend this class with my changed function.

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.