2

I'm attempting to create a custom form in Magento that can be accessed from "adminhtml". So far, it's about 90% of the way there, except for (of course) the most important part, which is taking the data, and doing something with it. Possibly someone out there could help me in this endeavor?

I based my work off of a wiki entry that I found here:

http://www.magentocommerce.com/wiki/5_-_modules_and_development/0_-_module_development_in_magento/how_to_create_an_admin_form_module

It seemed like the perfect, "Hello, World!" kind of example I could find in building what I needed, so off I went.

I've declared my module /app/etc/modules/Map_Csv.xml

 <?xml version="1.0"?>
 <config>
     <modules>
         <Map_Csv>
             <active>true</active>
             <codePool>local</codePool>
         </Map_Csv>
     </modules>
 </config>

I've put in my Module Configuration file /app/code/local/Map/Csv/etc/config.xml

 <?xml version="1.0"?>
 <config>
     <modules>
         <Map_Csv>
             <version>0.1.0</version>
         </Map_Csv>
     </modules>

     <global>
         <models>
             <Csv>
                 <class>Map_Csv_Model</class>
                 <resourceModel>Csv_mysql4</resourceModel>
             </Csv>
             <Csv_mysql4>
                 <class>Map_Csv_Model_Mysql4</class>
             </Csv_mysql4>
         </models>
         <blocks>
             <Csv>
                 <class>Map_Csv_Block</class>
             </Csv>
         </blocks>
         <helpers>
             <Csv>
                 <class>Map_Csv_Helper</class>
             </Csv>  
         </helpers>
     </global>

     <admin>
         <routers>
             <Csv>
                 <use>admin</use>
                 <args>
                     <module>Map_Csv</module>
                     <frontName>Csv</frontName>
                 </args>
             </Csv>
         </routers>
     </admin>

     <adminhtml>
         <translate>
             <modules>
                 <Map_adminhtml>
                     <files>
                         <Csv>Map_Csv.csv</Csv>
                     </files>
                 </Map_adminhtml>
             </modules>
         </translate>

         <menu>
             <catalog>
                 <children>
                     <Csv_adminform translate="title" module="Csv">
                         <title>Goedekers Products Map</title>
                         <action>Csv/adminhtml_myform</action>
                     </Csv_adminform>
                 </children>
             </catalog>
         </menu>

         <acl>
             <resources>
                 <admin>
                     <children>
                         <catalog>
                             <children>
                                 <Csv_adminform>
                                     <title>Goedekers Products Map</title>
                                 </Csv_adminform>
                             </children>
                         </catalog>
                     </children>
                 </admin>
             </resources>
         </acl>

         <layout>
             <updates>
                 <Csv>
                     <file>csv.xml</file>
                 </Csv>
             </updates>
         </layout>
     </adminhtml>
 </config>

My Controller /app/code/local/Map/Csv/controllers/Adminhtml/MyformController.php

 <?php

 class Map_Csv_Adminhtml_MyformController extends Mage_Adminhtml_Controller_Action
 {
     public function indexAction()
     {
         $this->loadLayout()->renderLayout();
     }

     public function postAction()
     {
         $post = $this->getRequest()->getPost();
         try {
             if (empty($post)) {
                 Mage::throwException($this->__('Invalid form data.'));
             }
             $mysqli = Mage::getSingleton('core/resource')->getConnection('core_write');
             if (isset($post['subButton'])) {
                 $msg = "";
                 switch ($post['subButton']) {
                     case "CHANGE":
                         $sql = "UPDATE tblMAP SET 
                             strBrand = '" . mysqli_real_escape_string($mysqli,$post['brand']) . "',
                             strSeries = '" . mysqli_real_escape_string($mysqli,$post['series']) . "',
                             strMPN = '" . mysqli_real_escape_string($mysqli,$post['mpn']) . "',
                             txtMessage = '" . mysqli_real_escape_string($mysqli,$post['message']) . "'
                             WHERE intKey = " . mysqli_real_escape_string($mysqli,$post['intKey']);
                         $msg = "Record = <strong>" . $post['brand'] . "</strong> - " . $post['series'] . " - " . $post['mpn'] . " - <em>" . $post['message'] . "</em> has been changed.";
                         $typ = "info";
                         break;  
                     case "REMOVE":
                         $sql = "DELETE FROM tblMAP 
                             WHERE intKey = " . mysqli_real_escape_string($mysqli,$post['intKey']);
                         $msg = "Record = <strong>" . $post['brand'] . "</strong> - " . $post['series'] . " - " . $post['mpn'] . " - <em>" . $post['message'] . "</em> has been removed.";
                         $typ = "danger";
                         break;  
                     case "ADD":
                         $sql = "INSERT INTO tblMAP SET 
                             strBrand = '" . mysqli_real_escape_string($mysqli,$post['brand']) . "',
                             strSeries = '" . mysqli_real_escape_string($mysqli,$post['series']) . "',
                             strMPN = '" . mysqli_real_escape_string($mysqli,$post['mpn']) . "',
                             txtMessage = '" . mysqli_real_escape_string($mysqli,$post['message']) . "'";
                         $msg = "Record = <strong>" . $post['brand'] . "</strong> - " . $post['series'] . " - " . $post['mpn'] . " - <em>" . $post['message'] . "</em> has been added.";
                         $typ = "success";
                         break;  
                 }
                 $mysqli->query($sql);
                 $exportSQL = "SELECT strBrand AS Brand,strSeries AS Series, strMPN as MPN, txtMessage AS Message FROM tblMAP";
                 $exportResult   = $mysqli->query($exportSQL);
                 $filePath       = $_SERVER['DOCUMENT_ROOT'] . "/assets/MAP.csv";
                 file_put_contents($filePath,"Brand,Series,MPN,Message\n");
                 while ($row = mysqli_fetch_array($exportResult)) {
                     file_put_contents($filePath,$row['Brand'] . "," . $row['Series'] . "," . $row['MPN'] . "," . $row['Message'] . "\n",FILE_APPEND);
                 }
             }

             $message = $this->__('Your form has been submitted successfully.');
             Mage::getSingleton('adminhtml/session')->addSuccess($message);
         } catch (Exception $e) {
             Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
         }
         $this->loadLayout()->renderLayout();
         // $this->_redirect('*/*');
     }
 }

 ?>

I skipped the form block as the step was optional

My layout updates for my form /app/design/adminhtml/default/hellowired/layout/csv.xml

Note: In my instance of Magento, "hellowired" is the name of the layout

 <?xml version="1.0"?>
 <layout>
     <csv_adminhtml_myform_index>
         <update handle="csv_myform_index"/>
         <reference name="content">
             <block type="adminhtml/template" name="myform" template="csv/myform.phtml"/>
         </reference>
     </csv_adminhtml_myform_index>
 </layout>

I've put in my Form Template /app/design/adminhtml/default/hellowired/template/csv/myform.phtml

 <div class="jumbotron">
     <h1>EDITS TO MAP TABLE</h1>
 </div>
 <?php
     $mysqli = Mage::getSingleton('core/resource')->getConnection('core_write');
     $result = $mysqli->query("SELECT * FROM tblMAP ORDER BY strBrand");
 ?>
 <div class="container">
     <?php
         if (@$msg != "") {
             ?><div class="alert alert-<?php echo $typ; ?>"><?php echo $msg; ?></div><?php   
         }
     ?>
     <div class="grid">
         <table style="width: 100%;">
             <thead>             
                 <tr>
                     <td>BRAND</td>
                     <td>SERIES</td>
                     <td>MPN</td>
                     <td>MESSAGE</td>
                     <td>ACTION</td>
                 </tr>
             </thead>
         <?php
             $currentRow = 0;
             while ($row = $result->fetch() ) {
                     $currentRow++;
                 ?>
                     <?php 
                         $rowLight = "default";
                         if ($currentRow % 3 == 0) { $rowLight = "info"; }
                         if ($currentRow % 3 == 1) { $rowLight = "other"; }
                         if (@$_REQUEST['subButton'] != "REMOVE") {
                             if ((@$_REQUEST['brand'] == $row['strBrand']) && (@$_REQUEST['series'] == $row['strSeries'])) { $rowLight = "primary"; }
                         }
                     ?>

                 <tr class="<?php echo $rowLight; ?>">
                 <form method="post" action="<?php echo $this->getUrl('Csv/adminhtml_myform/post'); ?>" id="edit_form<?php echo $currentRow; ?>" name="edit_form<?php echo $currentRow; ?>">
                         <input type="hidden" name="intKey" value="<?php echo $row['intKey']; ?>">
                         <td class="col-md-2"><input class="form-control" type="text" value="<?php echo $row['strBrand']; ?>" name="brand"></td>
                         <td class="col-md-2"><input class="form-control" type="text" value="<?php echo $row['strSeries']; ?>" name="series"></td>
                         <td class="col-md-2"><input class="form-control" type="text" value="<?php echo $row['strMPN']; ?>" name="mpn"></td>
                         <td class="col-md-3"><input class="form-control" type="text" value="<?php echo $row['txtMessage']; ?>" name="message"></td>
                         <td class="col-md-3 text-center"><input type="submit"  name="subButton" value="CHANGE" class="btn form-button" ><input type="submit" name="subButton" value="REMOVE" class="btn form-button" ></td>
                 </form>
                 <script type="text/javascript">
                     var edit_form<?php echo $currentRow; ?> = new varienForm('edit_form<?php echo $currentRow; ?>');
                 </script>
                 </tr>
                 <?php
              }
         ?>
         <form method="post" action="<?php echo $this->getUrl('Csv/adminhtml_myform/post'); ?>"  id="edit_form_a" name="edit_form_a">
             <tr class="col-md-12 bg-success" style="border: 1px solid Silver;padding: 5px;">
                 <td class="col-md-2"><input class="form-control" type="text" value="" name="brand"></td>
                 <td class="col-md-2"><input class="form-control" type="text" value="" name="series"></td>
                 <td class="col-md-2"><input class="form-control" type="text" value="" name="mpn"></td>
                 <td class="col-md-3"><input class="form-control" type="text" value="See Price in Cart" name="message"></td>
                 <td class="col-md-3 text-center"><input type="submit" name="subButton" value="ADD" class="btn form-button"></td>
             </tr>
         </form>
         <script type="text/javascript">
             var edit_form_a = new varienForm('edit_form_a');
         </script>
         </table>
     </div>
 </div>

I've left out the Translations as they are optional

I've put in my Helper (although there's nothing in it) /app/code/local/Map/Csv/Helper/Data.php

 <?php
 class Map_Csv_Helper_Data extends Mage_Core_Helper_Abstract {}
 ?>

So, that's everything. If all worked as well as it should, this series of forms would allow a user to create, edit and remove data in a table, and write a CSV file made from those edits. Currently, submitting anything in the forms returns me to the index page without any changes whatsoever to the table. What am I doing wrong?

Also, as a side note, is it problematic for Magento to have so many individual forms on a given page?

closed as unclear what you're asking by Marius Mar 11 '15 at 14:17

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • My question here is what is the url that you have from the index action, assuming that works, and compare that to the action set against the form. If you are not getting into the postAction then I think the error is with building your form action – David Manners Jun 18 '14 at 7:42
0

Most likely the Secret Keys for URLs are not being set with your form:

Mage::getSingleton('adminhtml/url')->getSecretKey("adminhtml_mycontroller","myaction");

So your form action should be:

<form method="post" action="<?php echo Mage::getSingleton('adminhtml/url')->getSecretKey('Csv/adminhtml_myform','post'); ?>" id="edit_form<?php echo $currentRow; ?>" name="edit_form<?php echo $currentRow; ?>">
  • Does it make a difference when System -> Configuration -> Admin -> Security shows Add Secret Key to URLs = No? – Vinnie Saletto May 30 '14 at 16:51
  • If that is set to No, then my answer would become invalid. yes. – B00MER May 30 '14 at 17:18

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