Magento controller url redirects to dashboard - magento

Basically in trying to create an inbox message which "read details" should redirect the user to a custom controller, however i can see the desired url in the browser for a second and then it redirects to the dashboard; this is how, currently, im trying to achieve that:
$myId = $myJson['id'];
$title = "Title of my notice";
$description = $myJson['text'];
$url= Mage::helper("adminhtml")->getUrl('My_Module/Controller/index', array('id' => $myId));
$sendingMessage = Mage::getModel('adminnotification/inbox')->addNotice($title,$description,$url);
The code above successfully adds the message to the inbox, however as i said before, i can see the desired URL in the browser before it gets redirected to the dashboard.
I'm accessing the same controller from another one and it does it as expected, the one that is actually working is a Grid and it looks something like this:
$this->addColumn('action',
array(
'header' => __('Answer'),
'width' => '100',
'type' => 'action',
'getter' => 'getId',
'actions' => array(
array(
'caption' => __('Answer'),
'url' => array('base'=> '*/Controller'),
'field' => 'id'
)),
'filter' => false,
'sortable' => false,
'index' => 'stores',
'is_system' => true,
));
So, am i missing something here ?
BTW, is there any way to make the "read details" link to open in the same page instead of a new tab?
==================================================================
UPDATE
Disabling the "Add Secret Key to URLs" in the security options allowed me get it work, however i would like to make use of the secret keys.
The URLs i'm generating in the first code block actually have a key/value in the URLs, they look something like this:
https://example.com/index.php/mymodule/Controller/index/id/3963566814/key/f84701848a22d2ef36022accdb2a6a69/

It looks like you're trying to generate an admin URL. In modern versions of Magento, admin urls must use the adminhtml front name, using the Magento Front Name Sharing technique (described in this article). That's must as in if you don't, the URLs won't work. Magento removed the ability to create non-adminhtml URLs in the backend.
Second, here's where Magento generates the secret keys
#File: app/code/core/Mage/Adminhtml/Model/Url.php
public function getSecretKey($controller = null, $action = null)
{
$salt = Mage::getSingleton('core/session')->getFormKey();
$p = explode('/', trim($this->getRequest()->getOriginalPathInfo(), '/'));
if (!$controller) {
$controller = !empty($p[1]) ? $p[1] : $this->getRequest()->getControllerName();
}
if (!$action) {
$action = !empty($p[2]) ? $p[2] : $this->getRequest()->getActionName();
}
$secret = $controller . $action . $salt;
return Mage::helper('core')->getHash($secret);
}
and here's where it validates the secret key
#File: app/code/core/Mage/Adminhtml/Controller/Action.php
protected function _validateSecretKey()
{
if (is_array($this->_publicActions) && in_array($this->getRequest()->getActionName(), $this->_publicActions)) {
return true;
}
if (!($secretKey = $this->getRequest()->getParam(Mage_Adminhtml_Model_Url::SECRET_KEY_PARAM_NAME, null))
|| $secretKey != Mage::getSingleton('adminhtml/url')->getSecretKey()) {
return false;
}
return true;
}
Compare the pre/post hash values of $secret to see why Magento's generating the incorrect key on your page.

Related

CodeIgniter How to bring the id of image clicked in the url?

I'm developing an web app using codeigniter. When I try to click
an image (using detail method), it doesn't include the ID of the image in the link.
I have tried many times, but it still doesn't work. I've even run the same project on another team member's PC, but it still doesn't work on my PC.
Here's the code:
public function detail($url_title, $id = '') {
// Get user logged in id
$user_id = $this->auth->userid();
// Decode url id
$id = decode_safe_url(#$id);
// Get survey master info
// division 0 mean survey, status 0 is not publish
$survey = $this->egsurveymaster->get(array('id' => #$id, 'division' => '1', 'status != ' => "0"));
// Redirect if product review is not exist
if (!#$survey) {
redirect('andrasurvey');
}
// Update read count
if (isset($_COOKIE['eblo_pcid'])) {
$this->egsurveymaster->count_read(#$id, $_COOKIE['andra_pcid']);
}
// Title
$this->template->title = asset_title('Andra Survey - ' . #$survey->title);
// Vars
$vars = array(
'contents' => array(
'master_id' => #$survey->id,
'review' => $this->_overviewContent(#$survey->id, #$survey),
),
'snsContents' => array(
'title' => #$survey->title,
'image' => asset_url(#$survey->image),
'url' => base_url('andrasurvey/detail/' . url_title(#$survey->title) . '/' . encode_safe_url(#$survey->id)),
'content' => character_limiter(strip_tags(#$survey->content, 150)),
'keywords' => trim(#$survey->title) . ', ',
'username' => 'andraadmin',
),
);
// Content
$this->template->content->view(asset_content('detail'), #$vars);
// publish the template
$this->template->publish();
}
When I click the image, this is what I get:
http://localhost:1234/andrasurvey/survey/detail/umur-anda-yang-seharusnya/
I really need your help,
Thank you.

Add my custom attribute to customer object in Magento (1.7.x)

I have created an custom attribute using the Magento installer script below:
$installer = new Mage_Eav_Model_Entity_Setup();
$installer->startSetup();
$installer->addAttribute('customer', 'organisation_id', array(
'input' => 'select', //or select or whatever you like
'type' => 'int', //or varchar or anything you want it
'label' => 'Organisation ID',
'visible' => 1,
'required' => 0, //mandatory? then 1
'user_defined' => 1,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
));
$installer->endSetup();
On the onepage checkout screen I have a dropdown where a customer selects an organisation from a dropdown menu - once they click the 'Continue' button it posts an ajax request and goes to the next step 'Billing' e.g the post request looks like this..
org[organisation_id] - 12345
org[name] - ACME Inc
My function that deals with this is follows - can anyone assist how to add this to the customer & order object?
public function saveOrgAction()
{
if ($this->_expireAjax()) {
return;
}
if ($this->getRequest()->isPost()) {
$data = $this->getRequest()->getPost('org', array());
// todo:hook up saving the org id passed here to customer & order object
// UPDATE... I believe this is correct?
$customer = Mage::getSingleton('customer/session')->getCustomer(); // get customer object
$customer->setOrganisationId($org_id)->save();
if (!isset($result['error'])) {
$result['goto_section'] = 'billing';
}
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
}
}
What is the best way to add this 'organisation_id' attribute to the customer object (note I am using Magento 1.7.2)
$customer->setOrganisationId('123')->save();
if you need it in the order you have to set it in the current quote object
via
$quote->setOrganisationId('123');
and use this explanation to push the attribute automatically through quote to order conversion. But dont forget to create the correct labled column in sales_flat_order!
explanation how to push any attribute automatically through quote to order conversion

Magento allow user to change theme colors via admin panel

Ok so I want to allow the end user (who most likely won't know anything about code) to be able to change the theme colors I'm creating. I saw you can create custom variables, but it's not really efficient to put your CSS as a php file.
So how do I go about this where the user can just put the hex code of a color for the navigation background or button background or whatever.. via the admin panel?
One way of doing this is by adding a customer attribute and letting your customer change it.
Adding a customer attribute can be achieved with this sort of method:
public function addCustomerAttribute($params){
try{
$setup = Mage::getModel('customer/entity_setup', 'core_setup');
$setup->addAttribute('customer', $params['code'], array(
'type' => $params['type'] ,
'input' => $params['input'],
'label' => $params['name'],
'global' => $params['scope'],
'visible' => 1,
'required' => $params['required'],
'user_defined' => 1,
'default' => $params['defaultvalue'],
'visible_on_front' => $params['visible_on_front'],
'used_in_forms', array('adminhtml_customer','customer_account_edit')
));
Mage::getSingleton('eav/config')
->getAttribute('customer', $params['code'] )
->setData(
'used_in_forms', array('adminhtml_customer','customer_account_edit')
)
->save();
return true;
}catch(Exception $e){
throw new Exception('Probleme de création d\'attribut client ' . $e->getMessage());
}
}
Then if your customer is logged in, just let him change the value and save it.
You can then retrieve it with
$customerColor = Mage::getSingleton('customer/session')->getCustomer()->getAttributecode()
and use this value to set CSS classes on your templates

Elimiating the need to check for the user in controller

To the point, I'm trying to tighten up my code and stop repeating myself. It's a constant struggle, haha
In both my views and my controllers I am checking to see if the user is signed in. That is fine, but I would like to only have to check to see if the user is in the view and then display the right content.
Originally, in my controller, I'm seeing if the user is there and then if it is I load the same views with different varables. It seems a bit repetative.
public function recentStories(){
//This pulls back all the recent stories
if (empty($this->user)) {
$this->load->view('header_view', array(
'title' => 'Recent Stories',
));
$this->load->view('storyList_view', array(
'query' => $this->data_model->pullAllStories(),
));
$this->load->view('footer_view');
}else{
$this->load->view('header_view',array(
'user' => $this->users_model->getUser($this->user->user_id),
'title' => 'Recent Stories',
));
$this->load->view('storyList_view', array(
'query' => $this->data_model->pullAllStories(),
'admin' => $this->admin_model->pulladminRecent(),
'user' => $this->users_model->getUser($this->user->user_id),
));
$this->load->view('footer_view');
}
}
Ultimitly, I would like to reduce that chunk down to something like this.
public function recentStories(){
//This pulls back all the recent stories
$this->load->view('header_view',array(
'title' => 'Recent Stories',
));
$this->load->view('storyList_view', array(
'query' => $this->data_model->pullAllStories(),
'admin' => $this->admin_model->pulladminRecent(),
'user' => $this->users_model->getUser($this->user->user_id),
));
$this->load->view('footer_view');
}
but when I do reduce that code and take out the part that checks the user I am getting back an error saying that I'm trying to get property of a non-object. Obviously, that is talking about the user.
To try and fix this I have tried both
<?if(isset($user)){
user header code in here
}else{
non-user header code in here
}?>
and
<?if(!empty($user)){
user header code in here
}else{
non-user header code in here
}?>
both have returned back "Trying to get property of non-object" Any ideas would be much welcomed. I really don't like having excess code.
public function recentStories(){
// default values
$header_data = array('title' => 'Recent Stories');
$storyList_data = array('query' => $this->data_model->pullAllStories());
// extended data for logged user
if (!empty($this->user)) {
$header_data['user'] = $this->users_model->getUser($this->user->user_id);
$storyList_data['admin'] = $this->admin_model->pulladminRecent();
$storyList_data['user'] = $this->users_model->getUser($this->user->user_id);
}
// load your views
$this->load->view('header_view', $header_data);
$this->load->view('storyList_view', $storyList_data);
$this->load->view('footer_view');
}
then in your views check "if (isset($user)) {" etc.

How to add categories in Joomla 2.5

Does anyone know how to properly insert new content categories to the DB programatically?
For each post in the categories table, there is also a post saved in the assets table with lft and rgt set.
Is there any native Joomla class I can use for this instead of plain SQL?
Please Please Only use the native classes, which categories will handle for you seamlessly. As soon as you add categories the whole thing will be handled automagically. Just look at any core component to see how.
It is not easy to update the assets table using sql, it is all very specifically managed and part of a complex series of foreign keyed tables.
Extend JTable or JTableContent to handle this.
Here is some code I just whipped together that just uses the JTableCategory class, so it can be used simply on the front or admin side of Joomla
$table = JTable::getInstance('category');
$data = array();
// name the category
$data['title'] = $title;
// set the parent category for the new category
$data['parent_id'] = $parent_id;
// set what extension the category is for
$data['extension'] = $extension;
// Set the category to be published by default
$data['published'] = 1;
// setLocation uses the parent_id and updates the nesting columns correctly
$table->setLocation($data['parent_id'], 'last-child');
// push our data into the table object
$table->bind($data);
// some data checks including setting the alias based on the name
if ($table->check()) {
// and store it!
$table->store();
// Success
} else {
// Error
}
Naturally you would want to get the data pieces set correctly, but these are the core ones to set.
Here is a function I've created just for this purpose, after some digging & experimenting.
It uses core classes, so it needs an access to them (for me it's basically a part of Joomla component).
Mind, it's for Joomla 3, for Joomla 2.5 and before, you need to change JModelLegacy to JModel.
function createCategory( $name, $parent_id, $note )
{
JTable::addIncludePath( JPATH_ADMINISTRATOR . '/components/com_categories/tables' );
$cat_model = JModelLegacy::getInstance( 'Category', 'CategoriesModel' );
$data = array (
'id' => 0,
'parent_id' => $parent_id,
'extension' => 'com_content',
'title' => $name,
'alias' => '',
'note' => $note,
'description' => '',
'published' => '1',
'access' => '1',
'metadesc' => '',
'metakey' => '',
'created_user_id' => '0',
'language' => '*',
'rules' => array(
'core.create' => array(),
'core.delete' => array(),
'core.edit' => array(),
'core.edit.state' => array(),
'core.edit.own' => array(),
),
'params' => array(
'category_layout' => '',
'image' => '',
),
'metadata' => array(
'author' => '',
'robots' => '',
),
);
if( !$cat_model->save( $data ) )
{
return NULL;
}
$categories = JCategories::getInstance( 'Content' );
$subcategory = $categories->get( $cat_model->getState( "category.id" ) );
return $subcategory;
}
You can perhaps use the save() in category.php file.
File location: root\administrator\components\com_categories\models\category.php
It saves the form data supplied to it!
The JOS_assets table is to store the ACL for each asset that is created.
If you do not update this table while programatically creating the category, the default ACL will apply. And when later you open and save the category in the administrative panel, the ACL will be updated as it should have been by core Joomla!.
You can create an SQL query very easily though and update the asset table as well. Its easy to understand once you open the table's content in phpmyadmin.

Resources