1

I got 2 problems and I need explanation ^^ Here my structure and files :

Structure :

app
 code 
  local 
   company
    ProductFinder
     Block
       Question.php
     controllers
       IndexController.php
     etc
       config.xml
     Helper
       Data.php
     Model
       Resource
         Answer
           Collection.php
         History
           Collection.php
         Proposal
           Collection.php
         Question
           Collection.php
         Answer.php
         History.php                 
         Proposal.php
         Question.php
design
 frontend
  company
    default
      layout
        company-productFinder.xml
      template
        company-productfinder
          index.html
          question.html

XML :

config.xml

<?xml version="1.0"?>
<config>
<modules>
    <Company_ProductFinder>
        <version>0.1.0</version>
    </Company_ProductFinder>
</modules>
<global>
    <blocks>
        <company_productfinder>
            <class>Company_ProductFinder_Block</class>
        </company_productfinder>
    </blocks>
    <models>
        <company_productfinder>
            <class>Company_ProductFinder_Model</class>
            <resourceModel>company_productfinder_resource</resourceModel>
        </company_productfinder>

        <company_productfinder_resource>
            <class>Company_ProductFinder_Model_Resource</class>
            <entities>
                <question>
                    <table>company_productfinder_question</table>
                </question>
                <answer>
                    <table>company_productfinder_answer</table>
                </answer>
                <proposal>
                    <table>company_productfinder_proposal</table>
                </proposal>
                <history>
                    <table>company_productfinder_history</table>
                </history>
            </entities>
        </company_productfinder_resource>
    </models>
    <resources>
        <company_productfinder_setup>
            <setup>
                <module>Company_ProductFinder</module>
                <class>Mage_Core_Model_Resource_Setup</class>
            </setup>
        </company_productfinder_setup>
    </resources>
</global>
<frontend>
    <translate>
        <modules>
            <Company_ProductFinder>
                <files>
                    <default>Company_ProductFinder.csv</default>
                </files>
            </Company_ProductFinder>
        </modules>
    </translate>
    <routers>
        <!--
          Can have several routers for i18n
        -->
        <company_pdt_finder>
            <use>standard</use>
            <args>
                <module>Company_ProductFinder</module>
                <frontName>trouverproduit</frontName>
            </args>
        </company_pdt_finder>

        <company_pdt_finder_us>
            <use>standard</use>
            <args>
                <module>Company_ProductFinder</module>
                <frontName>productfinder</frontName>
            </args>
        </company_pdt_finder_us>
    </routers>
    <layout>
        <updates>
            <company_productfinder>
                <file>company-productFinder.xml</file>
            </company_productfinder>
        </updates>
    </layout>
</frontend>
</config>

company-productFinder.xml

<?xml version="1.0"?>
<layout version="0.1.0">
<company_pdt_finder_index_index>
    <reference name="content">
        <block type="core/template" name="productfinder" template="company-productfinder/index.phtml">
            <block type="company_productfinder/question" name="company_pdt_question"
                   template="company-productfinder/question.phtml"/>
        </block>
    </reference>
</company_pdt_finder_index_index>

<company_pdt_finder_us_index_index>
    <update handle="company_pdt_finder_index_index"/>
</company_pdt_finder_us_index_index>

<company_pdt_finder_us_index_find>
    <reference name="content">
        <block type="company_productfinder/question" name="company_pdt_question" template="company-productfinder/question.phtml"/>
    </reference>
</company_pdt_finder_us_index_find>
</layout>

IndexController.php

class Company_ProductFinder_IndexController extends Mage_Core_Controller_Front_Action{

public function indexAction(){


    $this->loadLayout();
    // Load the first question
    $params = Mage::getModel('company_productfinder/question')->getCollection()
            ->addFieldToFilter('id_parent',array('null' => true ))
            ->getFirstItem();
    $this->getLayout()->getBlock('company_pdt_question')->setQuestion($params->getData())->toHtml();
    $this->renderLayout();
}

public function findAction(){
    // setWhateverYouWant to your current BLOCK and you can retrieve in your Block (backEnd)
    $this->getLayout()->getBlock('company_pdt_question')->setQuestion($this->getRequest()->getParams())->toHtml();
    // Mage::getModel('catalog/product')

}
}

Block : Question.php

<?php 
class Company_ProductFinder_Block_Question extends Mage_Core_Block_Template {

public function getNextStep(){
    $question = $this->_getNextQuestion();
    $answers = $this->_getNextAnswers($question['id']);

    return array ('question' => $question, 
                  'answers'  => $answers );

}

protected function _getNextAnswers($id){
    $collection = Mage::getModel('company_productfinder/proposal')->getCollection()
            ->addFieldToFilter('id_question', $id);
    $collection->join('answer',
        'main_table.id_answer = answer.id');
    $collection->load();
    return $collection;
}

protected function _getNextQuestion($idAnswer = null){

    if($idAnswer === null){
        return $this->getQuestion();
    }
    else{
    die('else condition in _getNextQuestion');
    }

}    
};

index.phtml

<form id="productfinder" action="<?php echo $this->getUrl('productfinder/index/find'); ?>" method="post"  >

<!-- main content-->
<div>
    <?php echo $this->getChildHtml('company_pdt_question'); ?>
</div>

<!-- progess bar-->
<div>
</div>
<input type="submit" value="submit">
</form>

question.phtml

<?php
$nextStep = $this->getNextStep();
?>
<p>
    <?php echo $nextStep['question']['label']; ?>
</p>
<?php
    foreach ( $nextStep['answers'] as $value) : 
?>
    <div>
        <label for="checkbox_id"><?php echo $value['label']; ?></label>
        <input type="radio" id="checkbox_id" name="checkbox_id" value="<?php echo $value['id']; ?>">
    </div>

<?php
    endforeach;
?>

My first problem is when I'm submitting my form (index.phtml), I got an error like :

Fatal error: Call to a member function setQuestion() on a non-object in /var/www/html/prod/app/code/local/Company/ProductFinder/controllers/IndexController.php on line 24

My second problem is when I go on localhost/productfinder/ my method in my file question.phtml $this->getNextStep is called twice.

I don't know if it's my method or my block who's called twice :/ Do you have any suggestion to fix that plz. I'm more interesting by explanation because I'm new on Magento and I really want to understand.

I don't have the choice to work on Magento 1.6.

EDIT : I forgot to mention: in my IndexController, I call the exact same block and I can set my params, so why when I'm submitting my form my method findAction in my IndexController is it not working for this method ?

1

I got it :)

My loop was because I was doing 2 ->toHml() The first one with my method ->toHtml() and the second one with the $this->renderLayout();

And for the next problem, it was because I forgot to load my Layout.

Thx anyway guys :)

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.