Zend Framework email content generation - email

With Zend_Framework, I wondered what is considered best practice for building up the content to send in a HTML email. In my case the content of the email that is sent is determined by a number of factors, for example the number of returned rows for a specific value in the database. Because of this it makes sense for me that the content is built up within the controller that sends the email which talks to the relevant database models and determines what the content should be. Where i'm not sure this works is that our designers and copyrighters will often want to adjust the copy in emails and this would then require them to make changes to a model or ask me to. Should i be handling this differently? Should i perhaps be storing HTML snippets somewhere containing the different text and then calling these somehow?
EDIT following from the answer by fireeyedboy, would it be acceptable to do something like this. Create a folder inside views called "partials" and use this to store text/html snippets that i can then call in where i need and replace special strings with dynamic values using regexp(or similar).
$nview = new Zend_View();
$nview->setScriptPath(APPLICATION_PATH.'/views/partials/');
$bodytext = $nview->render('response.phtml');
$mail = new Zend_Mail();
$mail->setBodyText($bodytext);
// etc ...
e.g. in this context where two different templates could be used depending on variables returned from a Model:
// within a controller
public function emailAction()
{
$images = new Model_ApplicationImages();
$totimages = count($images->fetchImages($wsid));
$acceptedImages = $images->fetchImages($wsid,'approved');
$accepted = count($acceptedImages);
$rejectedImages = $images->fetchImages($wsid,'rejected');
$rejected = count($rejectedImages);
$response = ($rejected == $totimages)?'rejected':'approved';
$nview = new Zend_View();
$nview->setScriptPath(APPLICATION_PATH.'/views/partials/');
$content = $nview->render($response.'.phtml');
$mail = new Zend_Mail();
$mail->setBodyText($content);
// etc
}
Is there a more elegant way i can/should be doing this?

Not sure if this is best practice, but what I did is extend Zend_Mail with methods like these:
setTemplatePath( $templatePath );
setTemplateHtml( $templateHtml );
setTemplateText( $templateText );
setTemplateArguments( array $templateArguments );
...then at some point in my overwrittensend() I do:
$view = new Zend_View();
$view->setScriptPath( $this->_templatePath );
foreach( $this->_templateArguments as $key => $value )
{
$view->assign( $key, $value );
}
if( null !== $this->_templateText )
{
$bodyText = $view->render( $this->_templateText );
$this->setBodyText( $bodyText );
}
if( null !== $this->_templateHtml )
{
$bodyHtml = $view->render( $this->_templateHtml );
$this->setBodyHtml( $bodyHtml );
}
So to utilize this you would do something like:
$mail = new My_Extended_Zend_Mail();
$mail->setTemplatePath( 'path/to/your/mail/templates' );
$mail->setTemplateHtml( 'mail.html.phtml' );
$mail->setTemplateText( 'mail.text.phtml' );
$mail->setTemplateArguments(
'someModel' => $someFunkyModel,
/* etc, you get the point */
)
$mail->send();
In other words, with this you can let your designers and copywriters simply edit views (templates) like they are used to already. Hope this helps and has inspired you to come up with something funky that suits your needs.
PS:
Since you mention arbitrary data rows, you can, for instance, utilize the partialLoop view helper that comes with ZF for this. But you probably were aware of this already?
PPS:
I actually agree with chelmertz' comment about not extending Zend_Mail but wrapping it in my own component.

Related

How do you access Gravity Form fields pre-submission?

I'm trying to access the Gravity Forms fields pre-submission to check for gibberish entries. I've tried pretty much everything on StackOverflow and no luck.
The form is id #1. Some of the 1000 things I've tried include:
GFFormsModel::get_form_meta(1);
GFFormsModel::get_leads(1);
$_POST['input_1']
and reading this:
https://docs.gravityforms.com/gform_pre_submission/
How do I do this?
If you want to fail silently for spam entries, I would recommend using the gform_validation filter and, if you determine that the submission is spam, dynamically enable the honey pot. Gravity Forms will then fail the submission silently. The confirmation will be displayed but the entry will not be created.
Here's how my GP Blacklist plugin handles this:
$honeypot_field_id = GFFormDisplay::get_max_field_id( $form ) + 1;
$_POST[ "input_{$honeypot_field_id}" ] = true;
$form['enableHoneypot'] = true;
$result['is_valid'] = true;
EDIT
To actually retrieve the value, the simplest way is to just fetch it from the $_POST (FYI, there are more thorough techniques). Here's a more robust example.
add_filter( 'gform_validation', function( $result ) {
// Get the value of field ID 1
$value = rgpost( 'input_1' );
if( $value == 'gibberish' ) {
// activate honeypot
}
return $result;
} );

Get Line Items in an Invoice logic hook in SuiteCRM

Via a logic hook I'm trying to update fields of my products, after an invoice has been saved.
What I understand so far is, that I need to get the invoice related AOS_Products_Quotes and from there I could get the products, update the required fields and save the products. Does that sound about right?
The logic hook is being triggered but relationships won't load.
function decrement_stocks ( $bean, $event, $arguments) {
//$bean->product_value_c = $bean->$product_unit_price * $bean->product_qty;
$file = 'custom/modules/AOS_Invoices/decrement.txt';
// Get the Invoice ID:
$sInvoiceID = $bean->id;
$oInvoice = new AOS_Invoices();
$oInvoice->retrieve($sInvoiceID);
$oInvoice->load_relationship('aos_invoices_aos_product_quotes');
$aProductQuotes = $oInvoice->aos_invoices_aos_product_quotes->getBeans();
/*
$aLineItemslist = array();
foreach ($oInvoice->aos_invoices_aos_product_quotes->getBeans() as $lineitem) {
$aLineItemslist[$lineitem->id] = $lineitem;
}
*/
$sBean = var_export($bean, true);
$sInvoice = var_export($oInvoice, true);
$sProductQuotes = var_export($aProductQuotes, true);
$current = $sProductQuotes . "\n\n\n------\n\n\n" . $sInvoice . "\n\n\n------\n\n\n" . $sBean;
file_put_contents($file, $current);
}
The invoice is being retrieved just fine. But either load_relationship isn't doing anything ($sInvoice isn't changing with or without it) and $aProductQuotes is Null.
I'm working on SuiteCRM 7.8.3 and tried it on 7.9.1 as well without success. What am I doing wrong?
I'm not familiar with SuiteCRM specifics, however I'd always suggest to check:
Return value of retrieve(): bean or null?
If null, then no bean with the given ID was found.
In such case $oInvoice would stay empty (Your comment suggests that's not the case here though)
Return value of load_relationship(): true (success) or false (failure, check logs)
And I do wonder, why don't you use $bean?
Instead you seem to receive another copy/reference of $bean (and calling it $oInvoice)? Why?
Or did you mean to receive a different type bean that is somehow connected to $bean?
Then its surely doesn't have the same id as $bean, unless you specifically coded it that way.

Zend_Translate Strategy for a huge grown web site

since I'm thinking in a good way to handle translation did part of a implementation and going toward a concept that still don't know if it's good and I would like to share it and get the pros and cons of it with someone that think it's a good point to explore.
The architecture is meant to work in a componentized site with translations comming from Actions, Forms, Views, View_Helpers and even Action_Helpers.
The ideis is simple:
Zend_Translate will the got from registry in every component and will receive __FILE__ as parameter. Since it was initialized with 'clear' on bootstrap it will be possible to load just the array file that correspont to this calling compoment. When comes to the missing translations they will be logged to a database (to avoid log duplicates) and/or be added to the corresponding array file in the remaining untranslated languages (as well as have the array file created) with a null value where it's is not set yet.
My guess is that using cache and specializing Translate i can ignore the translations that are set with null (by the addition made before) without log it again (displayin just the key) it will overhead a little bit the firt call for a large untraslated page and then gain performance later as well as maintainability and work ability with the automation of the translation process that would like to supply for the user.
But after that I was figuring out that I could build a array with the missing translations from every component to be save at the request end, and that is my question.
Had you folks had some experience with this that could be helpful to determine what's the best strategy?
bootstrap
protected function _initLocale() {
$translateSession = new Zend_Session_Namespace('translate');
$locale = isset($translateSession->locale) ? $translateSession->locale : 'auto';
try {
$zendLocale = new Zend_Locale($locale);
} catch (Zend_Locale_Exception $e) {
$zendLocale = new Zend_Locale('en_US');
}
Zend_Registry::set('Zend_Locale', $zendLocale);
$translate = new Engine_Translate('customarray', array('clear'));
$logger = Engine_Logger::getLogger();
$translate->setOptions( array('log2db' => $logger ,'log' => $logger, 'logPriority' => Zend_Log::ALERT, 'logUntranslated' => true));
Zend_Registry::set('Zend_Translate', $translate);
}
simple library
function getAvailableTranslationLanguages() {
return array("pt_BR"=>"Português","en_US"=>"Inglês");
}
function setTranslationLanguage($code) {
$translateSession = new Zend_Session_Namespace('translate');
$translateSession->locale = $code;
}
function getTranslator($file) {
$relative = str_replace(APPLICATION_PATH, '', $file);
$code = Zend_Registry::get('Zend_Locale');
$path = APPLICATION_PATH . '\\lang\\' . $code . $relative;
$translator = Zend_Registry::get('Zend_Translate');
try {
$translator->addTranslation($path, $code);
} catch (Exception $e) {
createTranslationFile($path);
}
return $translator;
}
function createTranslationFile($path) {
if(!file_exists(dirname($path)))
mkdir(dirname($path), 0777, true);
$file = fopen($path, 'w');
if($file) {
$stringData = "<?php\n return array(\n );";
fwrite($file, $stringData);
fclose($file);
} else {
$logger = Engine_Logger::getLogger();
$logger->info(Engine_Logger::get_string('ERRO ao abrir arquivo de tradução: ' . $path));
}
}
The use
class App_Views_Helpers_Loginbox extends Zend_View_Helper_Abstract
{
public function loginbox() {
$translate = getTranslator(__FILE__);
Translation resources
If I understand correctly, you want to create new adapter for each action helper/ view helper/ etc. This is IMO wrong and hugely ineffective. I would stick the translations to URLs. Make one common.php for translations used everywhere, module.php for module-specific and page-name.php for page specific translation. Then array_merge them together and create one adapter in Bootstrap. Then cache it (using URL as the cache key) somewhere - prefferably in memory (=memcached, apc). That way you would create the translate adapter from cache very effectively - only load+unserialize. Many translations (for each helper) means many disc accesses, means lower speed and scalability as disc will soon be the bottleneck.

Set values to form builds by zend framework

I use Zend framework to build the forms, I want to make the edit action, since the user click on edit, the form appears with users data, how can I set the data to the form which is built dynamically??
$form->populate($data);
where $data is an array of key value pairs containing your data.
$form = new Zend_Form;
if ($this->_request->isPost()) {
//to just populate
$form->populate($this->_getAllParams());
//or auto populate during validation
if ($form->isValid($this->_getAllParams()) {
//do stuff if valid
}
}
I know that ZF maunal is pretty messy and not everything is clear, but I think forms are explained pretty nice (with examples). You should research more by yourself.
http://framework.zend.com/manual/en/zend.form.html
$Menu = new Admin_Model_DbTable_Menu();
$row = $Menu->fetchRow($Menu->select()->where('id = ?', $id));
$Addmenu = new Admin_Form_Addmenu();
$Addmenu->populate($row->toArray());

How to add two numbers in sugar crm

I made module addition and in this made three fields amount1_c, amount2_c and total_amount_c to add the two numbers and display the result in the third field. I done coding in the logic looks here is my code
<?
$hook_version = 1;
$hook_array = Array();
$hook_array['before_save'] = Array();
$hook_array['before_save'][] = Array(1,'calculate_field', 'custom/modules/cases/LogicHookMath.php','LogicHookMath', 'calculate_field');
?>
and made one more file logic hook math. here is my code for
<?php
class LogicHookMath {
function calculate_field(&$bean, $event, $arguments) {
$field1 = $bean->amount1_c;
$field2 = $bean->amount2_c;
$field3 = $field1 + $field2;
$bean->amount_total_c = $field3;
}
}
?>
but still i did not get any result. Please help me out for this.
The code looks correct.
Some common "mistakes" when custom logic hooks are not working:
Make sure, the custom logic hook has the correct name (LogicHookMath.php)
Make sure, that the $bean variable is prefixed with &, so the variable is passed as a reference
Make sure the logic_hooks.php and the LogicHookMath.php files are readable by the web server user
The entire custom directory should also be writeable for the web server user
If the above does not help, try logging the progress to the sugarcrm.log using $GLOBALS['log']->info( "Value 3: ". $field3); in the custom logic hook.