CakePHP: HTML in setFlash() - php

I'm wondering if there is any proper way to include HTML in the setFlash() function of the Session component.
Basically I have this admin interface on an e-commerce website which allows administrators to create and edit "shops" found on the website. Upon saving the "shop", I would like CakePHP to display something like "Your shop has been successfully saved. Return to Shop Index". "Return to Shop Index" would be a link. I'm currently using plain old HTML like:
$this->Session->setFlash("Shop has been successfully published. Return to Shop Index");
Works, but it's HTML in the Controller, which I think is a "bad thing".
Thanks!
EDIT:
Thanks #YonoRan for solution. Missed that out in the CakePHP documentation. Here's what I did:
1) Created new element session_flash_link.ctp in app/views/elements.
2) Added the following code in session_flash_link.ctp:
<div id="flashMessage" class="message">
<?php
echo $message;
echo $this->Html->link($link_text, $link_url, array("escape" => false));
?>
</div>
3) Code in controller:
$this->Session->setFlash("Shop has been successfully saved. ", "session_flash_link", array(
"link_text" => "Return to Shop Management ยป",
"link_url" => array(
"controller" => "shops",
"action" => "manage",
"admin" => true
)
));

This might be a solution for what you are trying to do, it loads a "Layout" with all the HTML in it as a setFlash message.
Custom CakePHP flash message
Update:
I just checked the Manual for setFlash
SetFlash Manual
And it shows that you can specify and element that holds the HTML for the setFlash message + a bunch of other properties.
setFlash($message, $element = 'default', $params = array(), $key = 'flash')
So it seems like a better way of doing whats suggested in the first link I posted, because it doesn't require a new layout but just uses Elements.
Good luck.

I've just found another way to do this that doesn't need new specific templates:
You can use the HtmlHelper - which you can access since it's probably loaded in the view, and we have access to that:
// access the html helper
$Html = (new View($this))->loadHelper('Html');
// use it to generate a link
$resend = $Html->link(__('resend'), array(
'controller' => 'users',
'action' => 'resend',
));
// sprintf to insert the link to your standard message text!
$this->Session->setFlash(sprintf(_("Do you want to %s the email?"), $resend));
Should work in any case where you need Helper functionality in a View. Works in 2.3.5.

Related

How to show custom module direct in catalog tpl file in oc2

I have read all the related questions in SO and Opencart Forum but couldn't find solution.
So I have copy featured module and edited some of code, not too much and Now i want to show this new module direct in front on only success page. So i have put this code in catalog/controller/checkout/success.php
$data['successpage'] = $this->load->controller('module/successpage');
and in ***catalog/view/theme/default/template/common/success.tpl
<?php echo $successpage; ?>
Now i getting
error Undefined index: limit in controller\module\successpage.php on line 20
* I think its b'coz some variables values need to send in module controller file
And i have try all the answer for this but can't get solution.
Thanks If you know and help me to sort out.
You might get an error as there is not any settings (data) is passed in index method of your class ControllerModuleSuccesspage. You are directly calling $data['successpage'] = $this->load->controller('module/successpage');
Try to display your successpage module by setting from admin side. (From layout). If you want to call it directly then first check for condition if ($setting['any index']) { ..... } else {... }.
If you want to pass any parameter without setting up from admin side than do this.
$parameters = array(
'name' => 'Your module name',
'product' => array(43,40,42,30), // product id
'limit' => 4,
'width' => 200 ,
'height' => 200,
'status' => 1
);
$data['successpage'] = $this->load->controller('module/successpage',$parameters);
You will get all details of $parameters in index method of your successpage controller file.
That's it. :)
Try this url " [yourhost]/index.php?route=module/successpage" to see if this module is working perfectly. There is no error in loading this module to parent controller. I think issue is present in this module itself. Maybe some value dependency.

Create new page in drupal

Iam new in drupal. I want to create a page and want to add data in page directly with out using backend content. I have created a page like this, page-test.tpl.php. How can I call this newly created page in browser?
First you have to create your own custom module.Let the module name be test.Create 2 files namely test.info and test.module.Declare a hook_menu function inside the test.module file.Since your module name is test your hook_menu will be named as test_menu.
Hook_menu enables modules to register paths in order to define how URL requests are handled. Paths may be registered for URL handling only, or they can register a link to be placed in a menu.
Test.module
function test_menu() {
$items = array();
$items['test'] = array(
'title' => 'My Page',
'description' => 'Study Hard',
'page callback' => 'test_simple', //Calls the function
'access arguments' => array('access content'),
);
return $items;
}
function test_simple() {
return array('#markup' => '<p>' . t('My Simple page') . '</p>');
}
Test.info
name = Test
description = Provides a sample page.
core = 7.x
After adding this try clearing the drupal cache and then navigate to /test.Try this link if you are a beginner.Try reading hook_theme which allows you to use your custom template.More about hook_theme here.Hope it might help u mate.. :)
Pages in drupal are stored in the database, not in actual code files like old websites used to. The template files (.tpl.php) are exactly that - templates. So for example, lets say you have blog content. You'd have to create a content type of 'Blog'. you could then create a template file (ex. node--blog.tpl.php) which would format the way the blog pages would look. But you would still need to add new pages through the drupal interface in order to add them to the site. You could always use an input type of PHP if you'd like to include php in the body of the page.
If you simply want to display a php file in your browser from your site, you can just upload a php file of your choice to your sites/default/files directory and access it directly via the file path.

Php and Drupal: how do I hide/display my link when needed using php within Drupal?

Perhaps it is easier if I ask the question this way - Using the coding snippet below, how would I write the code to display the "Change" link only when the value in status is "A"?
I think i'm making this harder than it has to be, but the use of both drupal module structure and php are throwing me, since i am just learning module development.
The link showing in the array below appears at the end of each of my rows. I only want it to appear when the status equals "A".
Because this link is within this array, i can't figure out how and where to implement it. I just want an [if statement] like this:
if (drupal_render($form['status'][$key]['status']) == 'A',)
echo l(t('Change'), 'rooms/avail_room/' . $key . '/' . arg(1), array('attributes' => array('class' => 'room-stat-avail'))),
else{
echo "";
}
so when the html table is displayed, the 'Change' link will only appear in the last column when the value of status is "A"
Can't believe I got the module working, but can't turn this link on/off as i want. See code snippet below:
function team_room_available_form($form) {
$output = "No space available.";
if ($form['roomid']) {
foreach (element_children($form['roomid']) as $key) {
$rows[] = array(
'data' => array(
drupal_render($form[''][$key]),
drupal_render($form['room_title'][$key]),
drupal_render($form['username'][$key]),
drupal_render($form['status'][$key]['status']),
l(t('Change'), 'rooms/avail_room/' . $key . '/' . arg(1), array('attributes' => array('class' => 'room-stat-avail'))),
),
'class' => $form['status'][$key]['#value'],
);
}
You shouldn't really call drupal_render() on every element of your array.
drupal_render is a theming function to be applied on a renderable array. Check the drupal docs on drupal.org ... Render Arrays in Drupal 7
Practically this means that you should apply your logic within your module before creating your renderable array. Practically your renderable array is just a bunch of values that follows Drupal theming structure.
When you create your array, your logic is just typical PHP. With Drupal you should really try the devel module. It will help you to output your results and help you debug your code.

Symfony 2, how to redirect form response to a new window

I have a form that generates a pdf document that will be served to the user when submitted. I would like to have the pdf document open in a new window. How would I do that using Symfony forms? I've been doing some Google searching but have not been able to find any ready answers to how to do this.
I found the answer to my question and added it as reference for other Symfony developers. See my response below to learn how to use it in your project.
To get your form to redirect to a new window is insanely simple, once you know how. When you create your form ( if you are using form classes, which you should ), your form creation will look something like this:
$form = $this->createForm(new MyFormName(), $myObject);
To add the ability for your form to redirect, you need to add the third argument to your constructor, like this:
$form = $this->createForm(
new MyFormName(),
$myObject,
array(
'attr' => array(
'target' => '_blank'
)
)
);
Now, when your form is submitted, it opens the results in a new window.
I hope this information helps other developers from hitting their heads on their keyboards like I was doing.

Having difficulty with YII Crugeconnector

I am trying to get a log in with facebook working on Yii site using Crugeconnector. http://yiiframeworkenespanol.org/wiki/index.php?title=CrugeConnector . It is in Spanish but Google Translate works well with it.
Anyway my problem at the moment is that the successfunction is not being called and I have no idea why. The following code is in my main.php under components array.
'crugeconnector'=>array(
'class'=>'ext.crugeconnector.CrugeConnector',
'hostcontrollername'=>'site',
'onSuccess'=>array('site/loginsuccess'),
'onError'=>array('site/loginerror'),
'clients'=>array(
'facebook'=>array(
// required by crugeconnector:
'enabled'=>true,
'class'=>'ext.crugeconnector.clients.Facebook',
'callback'=>'http://smecontracts.ie/6/facebook-callback.php',
// required by remote interface:
'client_id'=>"290200194448698",
'client_secret'=>"0608f1e94db5a3a8b6612af1c61fb25d",
'scope'=>'email, read_stream',
),
The next batch of code is under my site controller.
public function actionLoginSuccess($key){
error_log("In Sitecontroller successful login page", 0);
$loginData = Yii::app()->crugeconnector->getStoredData();
// loginData: remote user information in JSON format.
$info = $loginData;
error_log($info, 0);
$this->renderText('<h1>Welcome!</h1><p>'.$info.'</p> key='.$key);
$this->redirect(Yii::app()->params["GREETINGS_PAGE_LINK"]);
}
The actionLoginSuccess function is never being called. I have calls to the log files that are never being called as can be seen above. Finally the facebook-callback.php looks as follows.
<?php
error_log("In facebook-callback page", 0);
$yii=dirname(__FILE__).'/../yii/framework/yii.php';
$config=dirname(__FILE__).'/protected/config/main.php';
defined('YII_DEBUG') or define('YII_DEBUG',false);
defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);
$_GET['r'] = '/site/crugeconnector'; // <--using 'site' ?
$_GET['crugekey'] = 'facebook'; // <--facebook key
$_GET['crugemode'] = 'callback';
require_once($yii);
Yii::createWebApplication($config)->run();
?>
This page is being called. The error_log above does get called however the only thing happening is that the user is being brought back to the main welcome page on my website which was set the in facebook app creation. I have no idea where to go from here, I need to retrieve the information from facebook and create an account using Yii, which would be done fairly easily if the login success function worked but there lies my problem.
Thanks for any help.
im the author on this yii extension.
the problem is not the callback itself. the problem is your own access rules. try making that actions required by the extension to be user-free, in other words, those actions should be out of any access rule. In your yii applicacion tell the system that this action are enabled to be executed by a anonymouse user (the * (asterisc) in the access rules).
hope it helps you.
I think you put this function in site controller and if function exist than put only one line.
public function actions() {
return array(
'crugeconnector' => array('class' => 'CrugeConnectorAction'),
);
}
And for Facebook button use this:
<?php if(Yii::app()->crugeconnector->hasEnabledClients){ ?>
<div class='crugeconnector'>
<span>Use your Facebook or Google account:</span>
<ul>
<?php
$cc = Yii::app()->crugeconnector;
foreach($cc->enabledClients as $key=>$config){
$image = CHtml::image($cc->getClientDefaultImage($key));
echo "<li>".CHtml::link($image,
$cc->getClientLoginUrl($key))."</li>";
}
?>
</ul>
</div>
<?php } ?>

Categories