Fatal Error when loading Catalog-settings in backend - php

On a fresh 1.5.0.1 Magento install when choosing Catalog from the settings->settings menu I get the following error:
Fatal error: Undefined class constant 'RANGE_CALCULATION_AUTO' in
/my-install-dir/app/code/core/Mage/Adminhtml/Model/System/Config/Source/Price/Step.php on line 33
Checked Step.php and it does not look damaged and contains the following:
class Mage_Adminhtml_Model_System_Config_Source_Price_Step
{
public function toOptionArray()
{
return array(
array(
'value' => Mage_Catalog_Model_Layer_Filter_Price::RANGE_CALCULATION_AUTO,
'label' => Mage::helper('adminhtml')->__('Automatic')
),
array(
'value' => Mage_Catalog_Model_Layer_Filter_Price::RANGE_CALCULATION_MANUAL,
'label' => Mage::helper('adminhtml')->__('Manual')
),
);
}
}`
Anyone know this error or how to fix it?

PHP is complaining that it can't find the constant on RANGE_CALCULATION_AUTO defined on the class Mage_Catalog_Model_Layer_Filter_Price
Based on your comments above, it sounds like you already checked the file at
app/code/core/Mage/Catalog/Model/Layer/Filter/Price.php
to ensure is had the correct constant defined.
const RANGE_CALCULATION_AUTO = 'auto';
Based on that, my guess would be there's a different Price.php being loaded for this class. This can happen if
Someone's placed a different version in community or local
Someone's monkied with the include path beyond Magento's normal monkey business
Check for files at
app/community/core/Mage/Catalog/Model/Layer/Filter/Price.php
app/local/core/Mage/Catalog/Model/Layer/Filter/Price.php
If that doesn't work, add some temporary debugging code to
app/code/core/Mage/Adminhtml/Model/System/Config/Source/Price/Step.php
that uses reflection to figure out what file PHP is loading the class from
class Mage_Adminhtml_Model_System_Config_Source_Price_Step
{
public function toOptionArray()
{
//NEW LINES HERE
$r = new ReflectionClass('Mage_Catalog_Model_Layer_Filter_Price');
var_dump($r->getFileName());
//echo $r->getFileName(); // if too long for var_dump
exit("Bailing at line ".__LINE__." in ".__FILE__);
//END NEW LINES
return array(
array(
'value' => Mage_Catalog_Model_Layer_Filter_Price::RANGE_CALCULATION_AUTO,
'label' => Mage::helper('adminhtml')->__('Automatic')
),
array(
'value' => Mage_Catalog_Model_Layer_Filter_Price::RANGE_CALCULATION_MANUAL,
'label' => Mage::helper('adminhtml')->__('Manual')
),
);
}
}`
This will dump out a file path that points to the exact place PHP is loading the class from, which should get you where you need to go.

Related

PHP, OOP: Why can't the array property of my class use properties & methods as values like an array outside of a class can?

So like the title says, I am having a hard time making an array property of one of my classes have it's values be declared as properties & methods.
I can successfully do this if the array is not a property of a class, but as soon as the array is dropped into a class, the script doesn't like those values, and throws me this error.
Fatal error: Constant expression contains invalid operations in C:\xampp\htdocs_webdev\repos\mcf\static\inc\classes\class.catalogue.php on line 17
I have both classes being included in a different .php called inc.classes.php. That file is then included in each page. Here is some code to better illustrate my issue,
Master Class File: inc.classes.php
// config
require_once('config/config.php'); // config file
// other tools
require_once(ROOT_DIR . 'inc/parsedown/Parsedown.php'); // tool that I am using for parsing .md files
// my classes
require_once(ROOT_DIR . 'inc/classes/class.vendor.php');
require_once(ROOT_DIR . 'inc/classes/class.catalogue.php');
Class A: class.vendor.php
class Vendor
{
public $vendor = array(
'foo' => array(
'name' => 'Foo Inc.',
'image' => (VENDOR_IMG . 'foo/foo-logo.png'),
),
'bar' => array(
'name' => 'Bar Co.',
'image' => (VENDOR_IMG . 'bar/bar-logo.png'),
),
);
public function get($data) {
if (array_key_exists($data, $this->vendors)) {
return $this->vendors[$data];
} else {
// throw error
}
}
// Class methods...
}
Class B: class.catalogue.php
class Catalogue
{
public $catalogue = array(
'1' => array(
$section = $markdown->text(file_get_contents(ROOT_DIR . catalogue/markdown/section1.md')),
$link = 'catalogue/pdf/section1.pdf,
$pdf = (ROOT_DIR . $link),
'title' => 'Section One',
'content' => mdReplace($section, $pdf, $link),
'theme' => 'purple',
'vendors' => array(
1 => $vendor->get('foo'),
2 => $vendor->get('bar'),
),
),
// '2' ...
);
// Class methods...
}
(mdReplace() is a small function located in a seperate php file called inc.functions.php. It's purpose is to replace a few keywords inside of the .md files that contain the sections' content.)
Apologies in advance if I am just blind as a bat right now and am missing something obvious.
You can't run methods on a class property like that. You'd need to set that up inside your construct:
class Catalogue
{
public $catalogue = array();
public function __construct()
{
$this->catalogue = array(
'1' => array(
$section = $markdown->text(file_get_contents(ROOT_DIR . catalogue/markdown/section1.md')),
$link = 'catalogue/pdf/section1.pdf,
$pdf = (ROOT_DIR . $link),
'title' => 'Section One',
'content' => mdReplace($section, $pdf, $link),
'theme' => 'purple',
'vendors' => array(
1 => $vendor->get('foo'),
2 => $vendor->get('bar'),
),
),
// '2' ...
);
}
// Class methods...
}
If you read php oop manual carefully, here what you will see:
Class member variables are called "properties"... They are defined by using one of the keywords public, protected, or private, followed by a normal variable declaration. This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.
See the words
must not depend on run-time information
And your current definition of public $catalogue is dependant of some data that will be evaluated later. That's why you have fatal error.
So, as said the solution is to fill $catalogue data by calling some function - either explicitly or in a __construct for example.
As Farkie said, you can't run method calls on a class property like the way you did.
The reason is, those objects which you are trying to use are not initialised, and in order for them to work they must be initialised first.
Ex variables which can't directly be used in properly as they are not available to be used
$markdown
$pdf
$section
So for Class B you need to have your code written inside the constructor.
However whatever you have done for Class A is perfectly acceptable and it should work. I could see that you have a typo in the variable name. It should be $vendors as you are trying to refer it inside the function get() as $this->vendors[$data];
The following will work
class Vendor
{
public $vendors = array(
'foo' => array(
'name' => 'Foo Inc.',
'image' => (VENDOR_IMG . 'foo/foo-logo.png'),
),
'bar' => array(
'name' => 'Bar Co.',
'image' => (VENDOR_IMG . 'bar/bar-logo.png'),
),
);
public function get($data) {
if (array_key_exists($data, $this->vendors)) {
return $this->vendors[$data];
} else {
// throw error
}
}
// Class methods...
}

File upload in module configuration panel, Prestashop

I'm trying to create a XML import module that will convert given file to CSV format and then use that CSV to import categories and products.
I have a working configuration page made with getContent() it basically calls a method that generates this form via $helper->generateForm(). $helper is a HelperForm() object.
protected function getConfigForm()
{
return array(
'form' => array(
'legend' => array(
'title' => $this->l('Settings'),
'icon' => 'icon-cogs',
),
'input' => array(
array(
'type' => 'file',
'label' => $this->l('XML file'),
'name' => 'XMLIMPORT_XML_FILE',
'desc' => $this->l('Select file you wish to import.'),
'required' => true
),
array(
'col' => 3,
'type' => 'text',
'prefix' => '<i class="icon icon-envelope"></i>',
'desc' => $this->l('Enter a valid email address'),
'name' => 'XMLIMPORT_LINES',
'label' => $this->l('Records per file'),
),
),
'submit' => array(
'title' => $this->l('Save'),
),
),
);
}
I need to get this data to my XML converter. How do I upload a file (around 10-20MB) to Prestashop to be then able to do other stuff with it? How to save it permanently on the server?
I tried doing this:
return array(
'XMLIMPORT_XML_FILE' => Configuration::get('XMLIMPORT_XML_FILE', null),
'XMLIMPORT_LINES' => Configuration::get('XMLIMPORT_LINES', 1000)
);
And after that this:
$form_values = $this->getConfigFormValues(); // returned array from above
foreach (array_keys($form_values) as $key)
Configuration::updateValue($key, Tools::getValue($key));
And later using my own class for XML conversion like this, hoping that it will give me file handle.
$xml_converter = new XMLToCSVConverter(Configuration::get('XMLIMPORT_XML_FILE'), 'output', 'example_products.php');
Apparently it didn't as nothing happens. The class itself is working fine outside of Prestashop module. The constructor is __construct($xml_file, $csv_filename, $template_file).
I need to pass the file I upload to that constructor. I've been struggling for days now.
#edit: I can see the contents of the file inside the HTTP call when submit is clicked. But how do I pass that file to my class?
As far as I remember 'type' => 'file', doesn't actually save any values in the database. This type is only meant to output a file field in your form.
After submitting, you should then do you custom processing with $_FILES['XMLIMPORT_XML_FILE'] : move to upload/ or whereever you want.
'XMLIMPORT_XML_FILE' => Configuration::get('XMLIMPORT_XML_FILE', null), won't return you anything. After uploading you my wish to save the uploaded file path here, but it won't show up next time in the form unless you build the output yourself.
Module configratuon is meant to save text config values. Handling files is trickier and you have to do them yourself each time.
EDIT:
To intercept the saving process, look up the submit button name and make an if statement:
public function getContent() {
if(Tools::isSubmit('submitButtonName')) {
error_log(print_r($_FILES, 1));
}
}
There's probably a function postProcess which does the same (it looks like you copied the methods from a default module).
prestashop handles the image upload with ImageManager class, this class contains more methods which are useful for handling image upload, resize etc.. so its better refer the default homeslider module for the image upload using a module. This module is handling the image upload process in postProcess method with the help of ImageManager class, this class methods will do the all the processes related to upload.

Extending page table with field using itemsProcFunc in TYPO3 6.2

I'm trying to extend the 'page' properties form by extending the page db table and TCA array from within an extension. This works, except that my custom function won't be called. If I replace my own itemsProcFunc line with a TYPO3 core function itemsProcFunc line it works, but with my own function it never works (I just get an empty result/selectlist, even when I simply return a dummy array: "return array('title','1');"....
Here's my code in my extension's ext_tables.php:
<?php
$TCA['pages']['columns'] += array(
'targetelement' => array(
'exclude' => 0,
'label' => 'Target element (first select a target page!)',
'config' => array (
'type' => 'select',
'items' => Array (
Array('',0),
),
'size' => 1,
'minitems' => 1,
'maxitems' => 1,
//'itemsProcFunc' => 'TYPO3\CMS\Backend\View\BackendLayoutView->addBackendLayoutItems',
'itemsProcFunc' => 'Vendor\Myextension\Controller\Hooks\CustomTargetElementSelector->getContentElements',
),
)
);
t3lib_extMgm::addToAllTCAtypes('pages', 'targetelement,', '2', 'after:nav_title');
t3lib_extMgm::addToAllTCAtypes('pages', 'targetelement', '1,5,4,199,254', 'after:title');
P.s. I replace Vendor\Myextension for my own namespace of course.
I don't know where to put my function file exactly, I assume in extension\Classes\Controllers\Hooks\CustomTargetElementSelector.php.
My ultimate goal is to display a list of content elements of the selected shortcut page UID..
P.s.2 my CustomTargetElementSelect.php file looks like this (contents just return a single item, dummy list result:
<?php
namespace Vendor\Myextension\Controller;
class CustomTargetElementsSelector extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController {
public function getContentElements(array &$params,$pObj){
return array('title','uid');
}
}
First of all, an itemsProcFunc should be a simple class; I never tested if an Extbase controller context is available in an itemsProcFunc.
Your hook should (this is just a recommendation) reside in
yourext/Classes/Hook/CustomTargetElementSelector.php
Namespace:
namespace Vendor\Yourext\Hook;
class CustomTargetElementSelector {
[method inside]
}
After flushing the system cache, if the hook still has no function, set a die() statement within the function to find out if the function is called at all. Currently it cannot work because the location of your class (Controllers/Hooks) and the namespace (Controller) don't fit.
For the sake of full 6.2/7 compatibility, replace
t3lib_extMgm::
by
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::

CakePhp 2.x - plugin model is not being loaded

I a making a plugin in CakePhp and I am using multiple models inside the plugin. I created those models by using the console and baked them. I ran into a problem when using 1 of them and it only appears with this specific model.
The model file name:
GamificationBadgeUnlockActionProgression.php
Content of model:
<?php
App::uses('GamificationAppModel', 'Gamification.Model');
class GamificationBadgeUnlockActionProgression extends GamificationAppModel {
function __construct() {
parent::__construct();
echo 'oleej';
exit;
}
public $belongsTo = array(
'GamificationBadgeProgression' => array(
'className' => 'GamificationBadgeProgression',
'foreignKey' => 'gamification_badge_progression_id'
),
'GamificationBadgeUnlockAction' => array(
'className' => 'GamificationBadgeUnlockAction',
'foreignKey' => 'gamification_badge_unlock_action_id'
)
);
public function addMissingProgression($missingProgression)
{
/*$this->saveMany($missingProgression, array(
'fieldlist' => array('gamification_badge_progression_id', 'gamification_badge_unlock_action_id')
));*/
}
}
In the controller of the plugin I have:
public $uses = array('Gamification.GamificationBadge',
'Gamification.GamificationBadgeProgression',
'Gamification.GamificationBadgeUnlockAction',
'Gamification.GamificationBadgeUnlockActionProgression',
'Gamification.GamificationBadgeUnlockType');
All the other models are being loaded, except 'Gamification.GamificationBadgeUnlockActionProgression'. Which is very weird. It has all required stuff and it follows the naming conventions and everything.
It just isn't being loaded as the constructor is never called and the echo is never shown.
When I call the function from the model like this with an array as parameter:
$this->GamificationBadgeUnlockActionProgression->addMissingProgression($missingProgression);
Then I get an error
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'addMissingProgression' at line 1
Because the model is not loaded.
However CakePhp does create a magic model with the same name, which results in the error above, because the model magically excists but the function ofcourse doesnt and neither do the relations.
What am I missing here?
That kind of error is thrown when the Model isn't properly loaded, it tries to execute the method as an SQL function, which obvisouly doesn't exist. At the top of your Controller, check if it has:
App::uses('GamificationBadgeUnlockActionProgression', 'Gamification.Model');
That, combined with the Gamification.GamificationBadgeUnlockActionProgression in the $uses array that you already have should be enough for it to load. If it still fails, as a last resort you could manually load it in your method using a fallback like:
if (!is_object($this->GamificationBadgeUnlockActionProgression)) {
ClassRegistry::init('Gamification.GamificationBadgeUnlockActionProgression');
$this->GamificationBadgeUnlockActionProgression = new GamificationBadgeUnlockActionProgression();
}
That would manually load the Model and create an instance for it under $this->GamificationBadgeUnlockActionProgression.
There also appears to be an error in the associations in the model, it should include the Plugin prefixes, like:
public $belongsTo = array(
'GamificationBadgeProgression' => array(
'className' => 'Gamification.GamificationBadgeProgression',
'foreignKey' => 'gamification_badge_progression_id'
),
'GamificationBadgeUnlockAction' => array(
'className' => 'Gamification.GamificationBadgeUnlockAction',
'foreignKey' => 'gamification_badge_unlock_action_id'
)
);

Autoloading custom Zend_Form_Element

I can't get Zend to autoload a custom form element class. I did things exactly as Marcin describes here (except that my classes start with 'Zend' and not 'my' but I'm getting this error:
Warning: include_once(Zend\Form\Element\Div.php) [function.include-once]: failed to open stream: No such file or directory
I have Zend_Form_Element_Div inside forms\elements\ and Zend_View_Helper_FormDiv inside views\helpers\
Basically, every folder in the error message is missng an 's', the right path is Zend\Forms\Elements\Div.php
I also have this in my bootstrap, though I'm not sure if it's necessary, but I'm also using this for my forms and models folder (and some others, but I don't think there's need to post them all):
<?php
$resourceLoader->addResourceTypes(array(
'model' => array(
'namespace' => 'Model',
'path' => 'models'
),
'element' => array(
'namespace' => 'Element',
'path' => 'elements'
),
'form' => array(
'namespace' => 'Form',
'path' => 'forms'
)
));
?>
(Is there actually any other way of doing this autoloading? Instead of declaring every single folder?)
Update:
Element_Div in application/forms/elements/Div.php
In my forms init() method: $this->addElementPrefixPath('Element_', APPLICATION_PATH . '/forms/elements');
Error I'm getting: Fatal error: Class 'Element_Div' not found in C:\xampplite\htdocs\code\application\forms\PostForm.php on line 63
You essentially have to tell the form where to find custom elements by using:
$form->addElementPrefixPath()
In your case, you would use - either within the form's init() or __construct() method - something like:
$this->addElementPrefixPath('Zend_Form_Element_', APPLICATION_PATH . '/elements);;
However, I have agree with #Marcin. Naming your own classes with the Zend_ pseudo-namespace is ill-advised. Either:
Decide on an application namespace and declare it in your Bootstrap when you create your $resourceLoader
Create an custom library that resides on your include path - probably at the same level as the Zend library - and put your custom stuff out there.
Let me know if you need more details on either of these suggestions and I'll fatten up the explanations a bit.
Update based on comments
Using an empty appnamespace, your call to addElementPrefixPath() now changes to:
$this->addElementPrefixPath('Element_', APPLICATION_PATH . '/elements);
And I guess you could remove the elements entry from the $resourceLoader definition in your Bootstrap since it's really not doing anything.
Update 2
I assumed that you were adding the element to the form using the shortname, something like:
$form->addElement('div', 'my_div');
In this circumstance, we need to tell the $form and its plugin registry where to find an element of type 'div'. That's why we dealt with $form->addElementPrefixPath().
However, from the error message you are reporting, it appears that you are adding your custom element to the form using something like:
$div = new Element_Div();
$form->addElement($div, 'my_div');
In this case, it is not the $form and its plugin registry that has to worry about finding/loading/instantiating the custom element; it is the $autoloader via its $resourceLoader. In that case, there is no need for the $form->addElementPrefixPath(), which is essentially a hint to the form on how to find custom elements invoked by shortname.
What we need is to configure the $resourceLoader back in Bootstrap so it knows where to find the class. Assuming you stick with empty appnamespace (so your class is named Element_Div) and you place the file in application/forms/elements/Div.php, then the $resourceLoader call is as follows:
$resourceLoader->addResourceTypes(array(
'model' => array(
'namespace' => 'Model_',
'path' => 'models'
),
'element' => array(
'namespace' => 'Element_',
'path' => 'forms/elements'
),
'form' => array(
'namespace' => 'Form_',
'path' => 'forms'
)
));
That should do it. [Famous last words, eh?]
I prefer creating forms like this:
$form->addElement(new My_Form_Element_Whatever(array(
'name' => 'my_element',
'label' => 'My element',
)));
or
$form->addElement($whatever = new My_Form_Element_Whatever(array(
'name' => 'my_element',
'label' => 'My element',
)));
$whatever->removeDecorator('Errors');
when I need to further modify the element.

Categories