Magento Adminhtml: Where do forms come from? - model-view-controller

My question comes from seeing this question and not being able to find the correct answer.
When adding a new product, where does the actual code come from for the input fields? In the aforementioned question, the desire is to add maxlength attribute to the input box. I dug around for over an hour and did find plenty of form helpers but not the one for this exact case.
How do I find the true origin of this (or any) form in Magento?

If I'm understanding your question correctly, majority of Magento's form fields come from Varian_Data_Form However you can easily specify a maxlength property via a higher up call, as in:
$fieldset->addField('title', 'text', array(
'label' => Mage::helper('form')->__('Title3'),
'maxlength' => '30', // <-- change here
'class' => 'required-entry',
'required' => true,
'name' => 'title',
'onclick' => "alert('on click');",
'onchange' => "alert('on change');",
'style' => "border:10px",
'value' => 'hello !!',
'disabled' => false,
'readonly' => true,
'after_element_html' => '<small>Comments</small>',
'tabindex' => 1
));
Example (and modified) from:
http://www.excellencemagentoblog.com/magento-admin-form-field
Related:
Extend a Varien Form Element for a Custom Module
Is it good practice to add own file in lib/Varien/Data/Form/Element folder

I accepted B00MER's answer because it led me to what I believe to be the Real Answer...
Clicking on the links he listed, the pattern of $fieldset->addField presented itself as a key way to grep in files.
user#magento:~/www/app$ grep -rin "addField.*text" * | grep -i product
code/core/Mage/Adminhtml/Block/Catalog/Product/Edit/Tab/Super/Config/Simple.php:140: $fieldset->addField('simple_product_inventory_qty', 'text', array(
There were about a dozen results which were quickly narrowed down (we're not caring about giftcard or Attribute sets). I'm not 100% sure THIS file is the answer but it seems that some logic could be added to catch whether or not the input's name=="Name" and then a maxlength could then be added in.

Related

What is the best way to use one layout for different pages? (without database)

I'm working on a website and I have a layout for at least 20 services which are only different in description and image. I'm trying to find a way to implement one layout and add data for a specific link and I don't want to use a database because the data isn't important to save. Is there another way or I have to use 20 blades that extend one layout?
You can have an array like this that you will pass to a layout
$servicesLayout = [
'service_1' => [
'description' => 'Service 1 description',
'image' => 'www.host.com/assets/img/section_1.jpg'],
'service_2' => [
'description' => 'Service 2 description',
'image' => 'www.host.com/assets/img/section_2.jpg']
];
Now when viewing a section, you pass the section key for example 'section_1' to the layout
To display section description in layout
{{$servicesLayout['service_1']['description']}}

New (Form) Element in Phalcon

There's a dynamic list of phone numbers, so I thought it would be a good idea to abstract this in a custom element.
There is a problem, though I don't know how to reuse existing elements, or how validation should work ($form->isValid() should check if the phone numbers match a certain pattern, for example).
How would I be able to implement that element?
You can use a regex, there's an example on the official documentation:
<?php
use Phalcon\Validation;
use Phalcon\Validation\Validator\Regex;
$validation = new Validation();
$validation->add(
'telephone',
new Regex(
[
'message' => 'The telephone is required',
'pattern' => '/\+44 [0-9]+/',
'allowEmpty' => true,
]
)
);
https://docs.phalconphp.com/hu/3.2/validation#cancelling
Or you can use a better regex pattern:
$regex = "/^(\d[\s-]?)?[\(\[\s-]{0,2}?\d{3}[\)\]\s-]{0,2}?\d{3}[\s-]?\d{4}$/i";
https://ericholmes.ca/php-phone-number-validation-revisited/

different currencies in ci_merchant?

I want to do a webshop, where users pay via paypal. Everything works perfectly, if I use the default currencies. However I want to pay with Hungarian Forint, which is not working.
In the merchant library I have found this:
public static $NUMERIC_CURRENCY_CODES = array(
'AUD' => '036',
'CAD' => '124',
'EUR' => '978',
'GBP' => '826',
'NZD' => '554',
'USD' => '840',
);
So I added a new line: 'HUF' => '348', but regardless when I want to pass the HUF parameter to the CI-Merchant, its not working. Matter of fact, I can change anything in this array, the results will be the same
This is how I pass the parameters:
$params = array(
'amount' => $osszeg,
'currency' => 'HUF',
'return_url' => 'something',
'cancel_url' => 'something');
(paypal supports Hungarian forint, so the problem is not there)
Is there any way to make this work? Thank you very much for your answers in advance.
Firstly, don't use CI Merchant, it has been unsupported for nearly a year now. Omnipay is its replacement.
If you must use CI Merchant, then the currency parameter is correct. You don't need to add the numeric currency code, since paypal uses standard 3 letter currency codes. It's passed through to paypal here:
https://github.com/expressodev/ci-merchant/blob/master/libraries/merchant/merchant_paypal_base.php#L94
Now since you only say it's 'not working', I don't know what the actual problem is so can't really help diagnose. If there is an error message, what is it? I suspect that the currency is not enabled on your paypal account.

magento add last login to customer grid

I am new to Magento. I am trying to add a last login value displaying on customer grid. it returns a null value. I have read other tutorials, but it does not help much. The Magento version is 1.7. Here is my code:
$customer = Mage::getSingleton('customer/session')->getCustomer();
$logCustomer = Mage::getModel('log/customer')->load($customer ->getId());
$lastVisited = $logCustomer->getLoginAt();
$this->addColumn('$lastVisited', array(
'header' => Mage::helper('customer')->__('Last Login'),
'type' => 'datetime',
'align' => 'center',
'index' => '$lastVisited',
'gmtoffset' => true
));
Magento stores the login time in the following table:
log_customer
But also, this data is cleaned periodically (see: Mage_Log_Model_Resource_Log::_cleanCustomers which is triggered via Magento cron).
There are different ways to approach your task.
1) Non-persistent - I am just interested to see recent data (I can ignore that log_customer is cleaned periodically)
In this case you can just rely on the data from log_customer and display it in Manage Customers Grid.
Extend Mage_Adminhtml_Block_Customer_Grid and in _prepareCollection add the following:
$collection->getSelect()->columns(array('last_login_at' => new Zend_Db_Expr ("(SELECT login_at
FROM `log_customer`
WHERE customer_id =e.entity_id
ORDER BY log_id DESC
LIMIT 1)")));
before: $this->setCollection($collection);
Note: use the proper Magento function to get log_customer table name, my query is just for example
2) Persistent - I want to always to see the data
add a new attribute to the customer entity called: last_login_at
(datetime).
add an observer to custom_login event to update this
attribute.
use addColumn function in the grid to display this new attribute.
#user1414056
Regarding your code:
bixe made a fair point related to '$lastVisited' (this just shows
lack of experience in php programming
you seem to also be new to programming (in general) because the addColumn is called only once... do how do you expect your code to make sense?
With a better understanding of Zend Framework and OOP Programming in general you will be able to actually work and get things done with Magento.
Your '$lastVisited' can't work : in php variables are evaluated in a String only when they're in a double quote.
EDIT:
Ok the column system of magento only display value when they're available in the collection linked to the grid..
You'll have to add the log information you want to display in the grid collection.
For an example, take a look at Mage_Adminhtml_Block_Customer_Online_Grid::_prepareCollection()
When it's done, you will add your column with :
$this->addColumn('login_at', array(
'header' => Mage::helper('customer')->__('Last Login'),
'type' => 'datetime',
'align' => 'center',
'index' => 'login_at',
'gmtoffset' => true
));

Re-order sort orders in toolbar

What is the best way to re-order the available sort orders shown in the product list toolbar? Currently, we have 3 sorting options available: Name, Price, Most Popular (in that order). I want to have Most Popular be the left-most item since it is our default sort option. I can write a custom module to extend "getAvailableSortOrders" or something like that, but I thought there had to be an easier way. Anyone have a recommendation?
Brian
The best way I've found so far is to make a copy of
/app/code/core/Mage/Catalog/Model/Config.php
at
/app/code/local/Mage/Catalog/Model/Config.php
and then edit your local copy of the file at line 341
//before
$options = array(
'position' => Mage::helper('catalog')->__('Position')
);
//after
$options = array(
'name' => Mage::helper('catalog')->__('Name'),
'price' => Mage::helper('catalog')->__('Price'),
'position' => Mage::helper('catalog')->__('Best Value')
);
I wanted name to be first, followed by price, and I wanted position to be renamed "Best Value" on the frontend. Since position wouldn't really mean anything to a customer.
I was inspired by this comment on Inchoo.
#jon.niesen: your solution worked only partially. I had no problems renaming 'position' but when it comes to renaming 'name', Magento was very stubborn on this and was still displaying "Name" in the select drop down box.
Maybe in 1.4.1.1 "Name" is hardcoded or something?

Resources