how to send mail in yiimailer extension in view form - yii

how exactly do i use this in view? it works fine in controller but I would like to have it set up like a form. My main question is, how do I submit? how would i make $mail->send() as a button? Not quite understanding the example from it. It looks like the default contact form.
Sorry for the rudimentary question.
$mail = new YiiMailer();
$name = Yii::app()->user->getName();
$email = Yii::app()->user->getEmail();
$mail->setFrom($email, $name);
$mail->setTo('hi#email.com', 'hi');
$mail->setTo(Yii::app()->params['adminWeb']);
$mail->setSubject('Mail subject');
**how to send?**

Add this to trigger the email.
if ($mail->send()) {
Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.');
} else {
Yii::app()->user->setFlash('error','Error while sending email: '.$mail->getError());
}
Ref : http://www.yiiframework.com/extension/yiimailer/

Related

How to reuse data between routes in Aurelia?

I have an user entity in the system and the following route fetches it from server and displays its details:
routerConfiguration.map([
// ...
{route: 'user/:id', name: 'user-details', moduleId: './user-details'}
]);
Now I want to display an edit form for the displayed user. I have the following requirements:
Edit form should have a separate URL address, so it can be sent to others easily.
When user clicks the Edit button on the user's details page, the edit form should use an already loaded instance of the user (i.e. it should not contact the API again for user details).
When user clicks the Edit button on the user's details page and then the Back button in the browser, he should see the details page without edit form again.
1st attempt
I tried to define the edit form as a separate page:
routerConfiguration.map([
// ...
{route: 'user/:id/edit', name: 'user-edit', moduleId: './user-edit'}
]);
This passes the #1 and #3 requirement but it has to load the user again when the edit form is opened.
I don't know any way to smuggle some custom data between the routes. It would be perfect if I could pass the preloaded user instance to the edit route and the edit component would use it or load a new one if it is not given (e.g. user accesses the URL directly). I have only found how to pass strings to the routes in a slighlty hacky way.
2nd attempt
I decided to display the edit form in a modal and show it automatically when there is a ?action=edit GET parameter. The code inspired by this and this question:
export class UserDetails {
// constructor
activate(params, routeConfig) {
this.user = /* fetch user */;
this.editModalVisible = params.action == 'edit';
}
}
and when the user clicks the Edit button, the following code is executed:
displayEditForm() {
this.router.navigateToRoute('user/details', {id: this.user.id, action: 'edit'});
this.editModalVisible = true;
}
This passes #1 (the edit url is user/123?action=edit) and #2 (the user instance is loaded only once). However, when user clicks the Back browser button, the URL changes as desired from user/123?action=edit to user/123 but I have no idea how to detect it and hide the edit form (the activate method is not called again). Therefore, this solution fails the #3 requirement.
EDIT:
In fact, I have found that I can detect the URL change and hide the edit form with event aggregator:
ea.subscribe("router:navigation:success",
(event) => this.editModalVisible = event.instruction.queryParams.action == 'edit');
But still, I want to know if there is a better way to achieve this.
The question is
How to cope with this situation in a clean and intuitive way?
How about adding a User class that will serve as the model and use dependency injection to use it in your view-models?
export class User {
currentUserId = 0;
userData = null;
retrieve(userId) {
if (userId !== this.currentUserId) {
retrieve the user data from the server;
place it into this.userData;
}
return this.userData;
}
}

Openerp "Save" button persists even after confirming the PO

I am not sure whether I broke the flow and introduced this bug. When I am editing a PO and confirming the PO (see Fig 2).
The changes get updated in database however the save button is still there. But the PO gets confirmed (See fig 3).
I need the save button to be replaced with "Edit" button (By default it was like that).
Can anyone suggest What could be wrong or any settings stuff??
Any help is appreciated..
In web addons-->web-static-src-->js-->view_form.js
add below lines of code:
on_button_save: function() {
var self = this;
var result = confirm("Do you want to save Record..?");
if (result==true) {
return this.save().done(function(result) {
self.trigger("save", result);
self.reload().then(function() {
self.to_view_mode();
var parent = self.ViewManager.ActionManager.getParent();
if(parent){
parent.menu.do_reload_needaction();
}
});
});
}
else{
return result;
}
},
This is the default behaviour to have the save button appear as it is if you did not click it and you clicked the button on the form.
Actually i written this code for my requirement.Before saving the record should ask the conformation to save.this code may help full you to further implements ion of you are requirement.

implement a simple inbox with notifications in yii

I want to implement a simple inbox in yii. it reads messages from a database table and show it.
but i don't know how i should show read and unread messages in different styles and how i can implement a notification for new messages.
i searched a lot but only found some extensions and i don't want to use them.
it is so important to find how i can show unread messages in a different way
any initial idea would help me
a part of mailbox extension code :
public function actionInbox($ajax=null)
{
$this->module->registerConfig($this->getAction()->getId());
$cs =& $this->module->getClientScript();
$cs->registerScriptFile($this->module->getAssetsUrl().'/js/mailbox.js',CClientScript::POS_END);
//$js = '$("#mailbox-list").yiiMailboxList('.$this->module->getOptions().');console.log(1)';
//$cs->registerScript('mailbox-js',$js,CClientScript::POS_READY);
if(isset($_POST['convs']))
{
$this->buttonAction('inbox');
}
$dataProvider = new CActiveDataProvider( Mailbox::model()->inbox($this->module->getUserId()) );
if(isset($ajax))
$this->renderPartial('_mailbox',array('dataProvider'=>$dataProvider));
else{
if(!isset($_GET['Mailbox_sort']))
$_GET['Mailbox_sort'] = 'modified.desc';
$this->render('mailbox',array('dataProvider'=>$dataProvider));
}
}
First of all the scripts things should be in the view. For you problem I would do something like
In the controller
$mailbox = Mailbox::model()->inbox($this->module->getUserId()); //I assume this returns the mailbox from that user?
$this->renderPartial('_mailbox',compact('mailbox ')); //compact is the same as array('mailbox'=>$mailbox) so use whatever you prefer.
In the view I would simply do something like this
<?php foreach($mailbox->messages as $message):
$class = ''; //order unread if you want to give both a different class name
if($message->read): //if this is true
$class = 'read';
endif; ?>
<div id='<?= $message->id ?>'class='message $class'> <!-- insert whatever info from the message --></div>
<?php endforeach; ?>
So now it will add the class read to every message that has been read. Then in CSS you can simply change it style. I hope this is enough information? I use foreach(): endforeach; and if(): endif; in the view files, but you could use foreach() {}, but I prefer foreach, as it looks better combined with HTML.
EDIT about you second question, how do they become read. This you could do with JQUERY. example.
$(".message").on("click", function() {
var id = $(this).attr('id');
$.ajax {
type:"POST",
url: "controller/action/"+id; //the controller action that fetches the message, the Id is the action variable (ex: public function actionGetMessage($id) {})
completed: function(data) {
//data = the message information, you might do type: 'JSON' instead. Use it however you want it.
if(!$(this).hasClass("read"))
$(this).addClass("read"); //give it the class read if it does not have it already
}
}
});
This simply gives the div the class read and it should look like the other items with the class read.

Zend Form getValue. Need someone to explain?

I'm just working with the Zend_Form in Zend Framework and came across something pretty weird.
I have the following inside my loginAction
$form = new Application_Model_FormLogin();
if ($this->getRequest()->isPost()) {
$email = $form->getValue('email');
$pswd = $form->getValue('pswd');
echo "<p>Your e-mail is {$email}, and password is {$pswd}</p>";
}
Which when submitted only outputs
Your e-mail is, and password is
So I checked to see what's going on with print_r ,
print_r($form->getValues());
print_r($_POST);
Which displayed the following,
Array ( [email] => [pswd] => ) Array ( [email] => asd [pswd] => asd [submit] => Login )
So the forms values array has both values as null and the global post array had the correct values. Now I can't work out the problem?
Now I did manage to fix the problem, but I need help understanding why this works? All I did was change the loginAction to this.
$form = new Application_Model_FormLogin();
if ($this->getRequest()->isPost()) {
//Added this in
if ($form->isValid($this->_request->getPost())) {
$email = $form->getValue('email');
$pswd = $form->getValue('pswd');
echo "<p>Your e-mail is {$email}, and password is {$pswd}</p>";
}
}
I don't get how this made it work? Considering there is no validation on the fields?
Any thoughts? All I can think is maybe I have something setup weird in my server configuration?
Thanks
You didnt load the Values in your form object.
Normaly you check if the form is valid and for this load it with the post data, in the next step you can use getValue() to get the (filtered) value from the form.
if($this->getRequest()->isPost()) {
$form = new My_Form();
if($form->isValid($this->getRequest()->getPost())){
echo $form->getValue('fieldname');
}
}
isValid() is what actually populates the fields in your form object, until you do that the values do not exist in your form object yet.
modifying your original code would be as simple as this
if ($this->getRequest()->isPost()) {
//your $form object has none of your POSTed values
$form->isValid($this->getRequest()->getPost())
//now your form object has the POSTed values and you can access them
$email = $form->getValue('email');
$pswd = $form->getValue('pswd');
echo "<p>Your e-mail is {$email}, and password is {$pswd}</p>";
}
This skims over it extremely lightly http://framework.zend.com/manual/1.11/en/zend.form.quickstart.html#zend.form.quickstart.validate
Consider this example also and it might make more sense. Here you just grab the values from the POST.
if ($this->getRequest()->isPost()) {
$email = $this->getRequest()->getPost('email');
$password = $this->getRequest()->getPost('password');
echo "<p> Your email is $email and your password is $password </p>";
}

How does one add a 'plain text node' to a zend form?

I'm trying to add a plain text node in a zend form - the purpose is to only dispay some static text.
The problem is - im not aware of any such way to do it.
I have used 'description' but that HAS to be attached to a form element.
Is there any way to simply display some text as part of a form? Zend considers everything as a form element so I cannot just print it out.
Eg:
The following will test your ability on so and so.
.
.
.
etc...
Any thoughts?
Zend has a form note view helper (Zend_View_Helper_FormNote), which you can use to add text.
Just create a new form element (/application/forms/Element/Note.php):
class Application_Form_Element_Note extends Zend_Form_Element_Xhtml
{
public $helper = 'formNote';
}
In your form:
$note = new Application_Form_Element_Note(
'test',
array('value' => 'This is a <b>test</b>')
);
$this->addElement($note);
Adding a hidden element with non-escaped description does the thing.
$form->addElement('hidden', 'plaintext', array(
'description' => 'Hello world! Check it out',
'ignore' => true,
'decorators' => array(
array('Description', array('escape'=>false, 'tag'=>'')),
),
));
Works perfectly. It is still attached to an element, which is, however, not rendered this way.
Code taken from: http://paveldubinin.com/2011/04/7-quick-tips-on-zend-form/
There might be a better way, but I created a paragraph by using a custom form element and view helper. Seems like alot of code for something so simple. Please let me know if you've found a more simplistic way to do it.
//From your form, add the MyParagraph element
$this->addElement(new Zend_Form_Element_MyParagraph('myParagraph'));
class Zend_Form_Element_MyParagraph extends Zend_Form_Element
{
public $helper = 'myParagraph';
public function init()
{
$view = $this->getView();
}
}
class Zend_View_Helper_MyParagraph extends Zend_View_Helper_FormElement {
public function init() {
}
public function myParagraph() {
$html = '<p>hello world</p>';
return $html;
}
}
A little late but thought I'd throw it in anyway for the benefit of the community.
Aine has hit the nail on the head. FormNote is what you need if you want to use text in Zend_Form. However, you can use it without needing to extend Zend_Form_Element_Xhtml. See example below:
$text = new Zend_Form_Element_Text('myformnote');
$text->setValue("Text goes here")
->helper = 'formNote';
Note that you can use both text and html with the formNote helper.
This functionality is built into Zend via Zend_Form_Element_Note.
$note = new Zend_Form_Element_Note('forgot_password');
$note->setValue('Forgot Password?');
I faced the same problem and decided is better not to use Zend_Form at all, but to use directly view helpers (like Ruby on Rails does) and validate on the model.
This one-liner works for me:
$objectForm->addElement(new Zend_Form_Element_Note('note', array('value' => 'Hello World')));