The developer guide for SuiteCRM is kind of incomplete (at least here in Q4 2017) compared to the old SugarCRM one that it was based on before the software fork. So, by downloading the WordPress Plugin for SugarCRM, I was able to figure out the REST API and JSON for adding a sales lead into SuiteCRM with the following code.
Now how do I prevent duplicates by home phone, mobile phone, or email address?
<?php
error_reporting(E_ALL);
ini_set('display_errors','On');
header('Content-Type: text/plain');
CRM::loginCRM('admin','xxxxPASSWORDxxxxxx');
$aResult = CRM::addLead(array(
'name' => 'John Doe',
'description' => 'sample description',
'salutation' => 'Mr.',
'first_name' => 'John',
'last_name' => 'Doe',
'do_not_call' => 'No',
'phone_home' => '202-111-2222',
'phone_mobile' => '202-111-2222',
'email1' => 'test#example.com',
'primary_address_street' => '123 Main Street',
'primary_address_street2' => '',
'primary_address_street3' => '',
'primary_address_city' => 'New York',
'primary_address_state' => 'NY'
));
print_r($aResult);
CRM::logoutCRM();
die('OK');
/////////////////////////
class CRM {
private static $SessionID = '';
private static $URL = 'https://mycrmserver-example.com/service/v4_1/rest.php';
private static $User = '';
private static $Shadow = '';
public static function sendJSON($a) {
$s = file_get_contents(
self::$URL,
false,
stream_context_create(
array(
'http' => array (
'method' => 'POST',
'header' => 'Content-Type: application/x-www-form-urlencoded',
'content' => http_build_query($a)
)
)
)
);
$a2 = json_decode($s);
return $a2;
}
public static function loginCRM($sUser,$sPass) {
$sShadow = md5($sPass);
self::$User = $sUser;
self::$Shadow = $sShadow;
$asLogin = array (
'method' => 'login',
'input_type' => 'JSON',
'response_type' => 'JSON',
'rest_data' => json_encode(array(
'user_auth' => array(
'user_name' => $sUser,
'password' => $sShadow,
'version' => 1
),
'application_name' => 'RestTest',
'name_value_list' => array()
))
);
$a = self::sendJSON($asLogin);
self::$SessionID = $a->id;
}
public static function logoutCRM() {
$asLogin = array (
'method' => 'logout',
'input_type' => 'JSON',
'response_type' => 'JSON',
'rest_data' => json_encode(array(
'user_auth' => array(
'user_name' => self::$User,
'password' => self::$Shadow,
'version' => 1
),
'application_name' => 'RestTest',
'name_value_list' => array()
))
);
self::sendJSON($asLogin);
}
public static function addLead($a) {
$asNameValueList = array();
foreach($a as $sKey => $sVal) {
$asNameValueList[] = array('name'=>$sKey,'value'=>$sVal);
}
$asAddEntry = array (
'method' => 'set_entry',
'input_type' => 'JSON',
'response_type' => 'JSON',
'rest_data' => json_encode(array(
'session' => self::$SessionID,
'module_name' => 'Leads',
'name_value_list' => $asNameValueList
))
);
$a = self::sendJSON($asAddEntry);
return $a;
}
} // end CRM
Add these functions into your CRM class and check them before adding a lead. I had a little help from this answer that incidentally gave me some insights. Also, I recommend you do things to tighten down your security such as add an .htaccess or NGINX rule that only allows certain IP addresses or require certain headers to reach anything in your /service folder, and /service/* subfolders either over HTTP or HTTPS.
public static function leadExistsByPhone($sHomePhone,$sMobilePhone) {
$sHomePhone = (empty($sHomePhone)) ? 'xxxxxinvalid' : $sHomePhone;
$sMobilePhone = (empty($sMobilePhone)) ? 'xxxxxinvalid' : $sMobilePhone;
$asCheck = array (
'method' => 'get_entry_list',
'input_type' => 'JSON',
'response_type' => 'JSON',
'rest_data' => json_encode(array(
'session' => self::$SessionID,
'module_name' => 'Leads',
'query' => "
leads.phone_home = '$sHomePhone'
OR leads.phone_mobile = '$sMobilePhone'
",
'order_by' => 'leads.date_entered DESC',
'offset' => '0',
'select_fields' => array(),
'link_name_to_fields_array' => array(),
'max_results' => 999999,
'deleted' => false
))
);
$a = self::sendJSON($asCheck);
$nCount = # $a->result_count;
$nCount = intval($nCount);
return ($nCount > 0);
}
public static function leadExistsByEmail($sEmail) {
if (!filter_var($sEmail, FILTER_VALIDATE_EMAIL)) {
die('DENIED: invalid email address format');
}
$asCheck = array (
'method' => 'get_entry_list',
'input_type' => 'JSON',
'response_type' => 'JSON',
'rest_data' => json_encode(array(
'session' => self::$SessionID,
'module_name' => 'Leads',
'query' => "
leads.id IN
(
SELECT email_addr_bean_rel.bean_id
FROM email_addr_bean_rel
JOIN email_addresses
ON email_addr_bean_rel.email_address_id = email_addresses.id
WHERE
email_addresses.email_address = '$sEmail'
)
",
'order_by' => 'leads.date_entered DESC',
'offset' => '0',
'select_fields' => array(),
'link_name_to_fields_array' => array(),
'max_results' => 999999,
'deleted' => false
))
);
$a = self::sendJSON($asCheck);
$nCount = # $a->result_count;
$nCount = intval($nCount);
return ($nCount > 0);
}
I want to add an image from back-end for each CMS page I'm adding in Prestashop, just like we add featured images for posts/page in Wordpress.
I couldn't find any codes/modules which support this feature in prestashop.
It is possible, but it isn't straightforward. Here are the steps you need to do to implement image upload to the CMS page module. This approach is not the most elegant way to implement this in PrestaShop, but I hope it helps you to move forward.
Step 1, Update the model so it can contain the image:
First override 'classes/CMS.php' to 'override/classes/CMS.php'.
class CMS extends CMSCore
{
// add a public field to store the CMS image
public $CMS_IMG;
/**
* #see ObjectModel::$definition
*/
public static $definition = array(
'table' => 'cms',
'primary' => 'id_cms',
'multilang' => true,
'fields' => array(
'id_cms_category' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'),
'position' => array('type' => self::TYPE_INT),
'active' => array('type' => self::TYPE_BOOL),
// Lang fields
'meta_description' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'size' => 255),
'meta_keywords' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'size' => 255),
'meta_title' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'required' => true, 'size' => 128),
'content' => array('type' => self::TYPE_HTML, 'lang' => true, 'validate' => 'isString', 'size' => 3999999999999),
// add one image per page
'CMS_IMG' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isString', 'size' => 3999999999999), ),
);
}
Step 2, Implement the code needed to upload the image in the backoffice:
Override 'controllers/admin/AdminCmsController.php' in 'override/controllers/admin/AdminCmsController.php'
class AdminCmsController extends AdminCmsControllerCore
{
public function renderForm()
{
$this->display = 'edit';
$this->toolbar_btn['save-and-preview'] = array(
'href' => '#',
'desc' => $this->l('Save and preview')
);
$this->initToolbar();
if (!$this->loadObject(true))
return;
$categories = CMSCategory::getCategories($this->context->language->id, false);
$html_categories = CMSCategory::recurseCMSCategory($categories, $categories[0][1], 1, $this->getFieldValue($this->object, 'id_cms_category'), 1);
// Add code to get image url
$image_url = '';
$imgName = $this->getImageValue($this->object);
if($imgName) {
$image = _PS_IMG_DIR_ . 'cms/' . $imgName;
$image_url = ImageManager::thumbnail($image, $this->table.'_'.(int)$this->object->id.'.'.$this->imageType, 350,
$this->imageType, true, true);
}
$this->fields_form = array(
'tinymce' => true,
'legend' => array(
'title' => $this->l('CMS Page'),
'image' => '../img/admin/tab-categories.gif'
),
'input' => array(
// custom template
array(
'type' => 'select_category',
'label' => $this->l('CMS Category'),
'name' => 'id_cms_category',
'options' => array(
'html' => $html_categories,
),
),
array(
'type' => 'text',
'label' => $this->l('Meta title:'),
'name' => 'meta_title',
'id' => 'name', // for copy2friendlyUrl compatibility
'lang' => true,
'required' => true,
'class' => 'copy2friendlyUrl',
'hint' => $this->l('Invalid characters:').' <>;=#{}',
'size' => 50
),
array(
'type' => 'text',
'label' => $this->l('Meta description'),
'name' => 'meta_description',
'lang' => true,
'hint' => $this->l('Invalid characters:').' <>;=#{}',
'size' => 70
),
array(
'type' => 'tags',
'label' => $this->l('Meta keywords'),
'name' => 'meta_keywords',
'lang' => true,
'hint' => $this->l('Invalid characters:').' <>;=#{}',
'size' => 70,
'desc' => $this->l('To add "tags" click in the field, write something, then press "Enter"')
),
array(
'type' => 'text',
'label' => $this->l('Friendly URL'),
'name' => 'link_rewrite',
'required' => true,
'lang' => true,
'hint' => $this->l('Only letters and the minus (-) character are allowed')
),
array(
'type' => 'textarea',
'label' => $this->l('Page content'),
'name' => 'content',
'autoload_rte' => true,
'lang' => true,
'rows' => 5,
'cols' => 40,
'hint' => $this->l('Invalid characters:').' <>;=#{}'
),
/* Add an fileupload component to the form */
array(
'type' => 'file',
'label' => $this->l('Page image'),
'name' => 'CMS_IMG',
'desc' => $this->l('Upload an image for this page'),
'lang' => true,
'display_image' => true,
'image' => $image_url ? $image_url : false,
),
array(
'type' => 'radio',
'label' => $this->l('Displayed:'),
'name' => 'active',
'required' => false,
'class' => 't',
'is_bool' => true,
'values' => array(
array(
'id' => 'active_on',
'value' => 1,
'label' => $this->l('Enabled')
),
array(
'id' => 'active_off',
'value' => 0,
'label' => $this->l('Disabled')
)
),
),
),
'submit' => array(
'title' => $this->l(' Save '),
'class' => 'button'
)
);
if (Shop::isFeatureActive())
{
$this->fields_form['input'][] = array(
'type' => 'shop',
'label' => $this->l('Shop association:'),
'name' => 'checkBoxShopAsso',
);
}
$this->tpl_form_vars = array(
'active' => $this->object->active
);
return AdminControllerCore::renderForm();
}
public function postProcess()
{
$languages = Language::getLanguages(false);
$update_images_values = false;
foreach ($languages as $lang)
{
if (isset($_FILES['CMS_IMG'])
&& isset($_FILES['CMS_IMG']['tmp_name'])
&& !empty($_FILES['CMS_IMG']['tmp_name']))
{
if ($error = ImageManager::validateUpload($_FILES['CMS_IMG'], 4000000))
return $error;
else
{
$ext = substr($_FILES['CMS_IMG']['name'], strrpos($_FILES['CMS_IMG']['name'], '.') + 1);
$file_name = md5($_FILES['CMS_IMG']['name']).'.'.$ext;
if (!move_uploaded_file($_FILES['CMS_IMG']['tmp_name'],
_PS_IMG_DIR_ .'cms'.DIRECTORY_SEPARATOR.$file_name))
return Tools::displayError($this->l('An error occurred while attempting to upload the file.'));
else
{
$values['CMS_IMG'][$lang['id_lang']] = $file_name;
}
}
$update_images_values = true;
$cms = new CMS((int)Tools::getValue('id_cms'));
$cms->CMS_IMG = $file_name;
$cms->update();
}
}
parent::postProcess();
}
public function getImageValue()
{
$db = Db::getInstance();
$sql = 'SELECT CMS_IMG FROM '._DB_PREFIX_.'cms_lang WHERE id_cms = ' . $this->object->id;
return $db->getValue($sql);
}
}
Step 3, Implement the code for the frontend
Overide 'controllers/front/CmsController.php' in 'override/controllers/front/CmsController.php'
class CmsController extends CmsControllerCore
{
/**
* Assign template vars related to page content
* #see CmsControllerCore::initContent()
*/
public function initContent()
{
if(!empty($this->cms->CMS_IMG)) {
$this->context->smarty->assign('cms_image', _PS_IMG_ . 'cms/' . $this->cms->CMS_IMG);
}
parent::initContent();
}
}
Step 4, use your image in the template
Now you can, e.g. in cms.tpl use the following code:
{if $cms_image != ''}
<img src="{$cms_image}">
{/if}
Based on: https://www.prestashop.com/forums/topic/141903-add-custom-field-to-cms-module/
Adding all these as separate answer, since some moderators seem to reject even most simplest fix/change to original answer as "This edit deviates from the original intent of the post" or "This edit was intended to address the author of the post" ..right.
Improving answer by 11mb
Step 0, Note that new field needs to be manually added in SQL table (use SQL or PhpMyAdmin structure edit):
ALTER TABLE `ps_cms_lang` ADD `CMS_IMG` TEXT NULL DEFAULT NULL;
Step 1.2, Class cache file needs to be deleted for changes to be active:
/cache/class_index.php
Step 4, use your image in the template
..
Or more universal form (also usable in category subpages - use CMS_IMG attribute (-> or . depends on code):
{if isset($cms->CMS_IMG) && $cms->CMS_IMG}
<img src="{$img_ps_dir}/cms/{$cms->CMS_IMG}">
{/if}
1. For a non-multiligual field - make these adjustments:
ALTER TABLE `ps_cms` ADD `CMS_IMG` TEXT NULL DEFAULT NULL;
'CMS_IMG' => array('type' => self::TYPE_STRING, 'lang' => false, ..
'lang' => false,
$sql = 'SELECT CMS_IMG FROM '._DB_PREFIX_.'cms WHERE id_cms = ' . $this->object->id;
+And remove all that lang stuff in postProcess()
2. Multiple fixes to postProcess()
code does not correctly save data - should not use $cms->update(); in postProcess() as triggers exception on adding new page, due partial data = just set $_POST['CMS_IMG'] value..
should generate unique filename (start with id) for each page.
should check for isSubmit to avoid processing in wrong chain (if same field name used in CMS categories).
Updated code:
public function postProcess()
{
if ( (Tools::isSubmit('submitAddcms') || Tools::isSubmit('viewcms'))
&& isset($_FILES['CMS_IMG'])
&& isset($_FILES['CMS_IMG']['tmp_name'])
&& !empty($_FILES['CMS_IMG']['tmp_name']))
{
$id = (int)Tools::getValue('id_cms');
if ($error = ImageManager::validateUpload($_FILES['CMS_IMG'], 4000000))
return $error;
else
{
$ext = substr($_FILES['CMS_IMG']['name'], strrpos($_FILES['CMS_IMG']['name'], '.') + 1);
$file_name = $id .'_cms_'. md5($_FILES['CMS_IMG']['name']).'.'.$ext;
if (!move_uploaded_file($_FILES['CMS_IMG']['tmp_name'],
_PS_IMG_DIR_.'cms'.DIRECTORY_SEPARATOR.$file_name))
return Tools::displayError($this->l('An error occurred while attempting to upload the file.'));
else {
$_POST['CMS_IMG'] = $file_name;
#chmod(_PS_IMG_DIR_.'cms'.DIRECTORY_SEPARATOR.$file_name, 0666);
}
}
}
parent::postProcess();
}
The above solutions just do not fit with PrestaShop's philosophy.
Adding a featured image for a CMS page should mimic existing mecanisms (see creation/edition of categories or suppliers logo).
Following is my solution, working for PrestaShop 1.6.1.7. It will add an image field, upload image to img/cms folder and create all thumbnails.
I did not figure out how to delete the image yet, you are more than welcome to comment my answer.
Be careful when updating PrestaShop, files out of the override folder may be updated.
1/ Add the following to defines.inc.php (after line 137):
define('_PS_CMS_IMG_DIR_', _PS_IMG_DIR_.'cms/');
2/ Add the following to defines_uri.inc.php (after line 61):
define('_THEME_CMS_DIR_', _PS_IMG_.'cms/');
3/ Add rules to .htaccess file:
RewriteRule ^cms/([0-9]+)(\-[\.*_a-zA-Z0-9-]*)(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/cms/$1$2$3.jpg [L]
RewriteRule ^cms/([a-zA-Z_-]+)(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/cms/$1$2.jpg [L]
4/ Override classes/CMS.php to override/classes/CMS.php:
<?php
class CMS extends CMSCore
{
// Add an id_image attribute to allow further tests in cms.tpl
public $id_image = 'default';
public function __construct($id = null, $id_lang = null, $id_shop = null)
{
parent::__construct($id, $id_lang, $id_shop);
$this->id_image = ($this->id && file_exists(_PS_CMS_IMG_DIR_.(int)$this->id.'.jpg')) ? (int)$this->id : false;
$this->image_dir = _PS_CMS_IMG_DIR_;
}
}
?>
5/ Override classes/ImageType.php to override/classes/ImageType.php
Add a public attribute to the class: public $cms
Append to $definition=>fields: 'cms' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool')
In getByNameNType, append to $types var: 'cms'
6/ Override classes/Link.php to override/classes/Link.php:
class Link extends LinkCore
{
public function getCmsImageLink($name, $id_cms, $type = null)
{
if ($this->allow == 1 && $type) {
$uri_path = __PS_BASE_URI__.'cms/'.$id_cms.'-'.$type.'/'.$name.'.jpg';
} else {
$uri_path = _THEME_CAT_DIR_.$id_cms.($type ? '-'.$type : '').'.jpg';
}
return $this->protocol_content.Tools::getMediaServer($uri_path).$uri_path;
}
}
7/ Override controllers/AdminCmsController.php to override/controllers/AdminCmsController.php:
Append the following to the __contruct method, right before parent::__construct():
$this->fieldImageSettings = array(
'name' => 'banner_img',
'dir' => 'cms',
);
Append the following to the postProcess method, right after line 351 (in one of the elseif blocks, look for a condition on submitAddcms and submitAddcmsAndPreview):
$object = $this->loadObject();
$image_is_posted = $this->postImage($object->id);
if (! $image_is_posted) {
throw new PrestaShopException("image not posted...");
}
In the renderForm method, start with:
if (!($obj = $this->loadObject(true))) {
return;
}
$image = _PS_CMS_IMG_DIR_.$obj->id.'.jpg';
$image_url = ImageManager::thumbnail($image, $this->table.'_'.$obj->id.'.'.$this->imageType, 350,
$this->imageType, true, true);
$image_size = file_exists($image) ? filesize($image) / 1000 : false;
if (Validate::isLoadedObject($this->object)) {
$this->display = 'edit';
} else {
$this->display = 'add';
}
and append the following to the attribute $this->fields_form, for example after the content field:
array(
'type' => 'file',
'label' => $this->l('Banner image'),
'name' => 'banner_img',
'display_image' => true,
'image' => $image_url ? $image_url : false,
'size' => $image_size,
'hint' => $this->l('Upload a banner image from your computer.')
)
Add and afterImageUpload method:
protected function afterImageUpload()
{
$return = true;
$generate_hight_dpi_images = (bool)Configuration::get('PS_HIGHT_DPI');
$object = $this->loadObject(true);
/* Generate image with differents size */
if (($object->id = (int)Tools::getValue('id_cms')) &&
isset($_FILES) && count($_FILES) && file_exists(_PS_CMS_IMG_DIR_.$object->id.'.jpg')) {
$images_types = ImageType::getImagesTypes('cms');
foreach ($images_types as $k => $image_type) {
$file = _PS_CMS_IMG_DIR_.$object->id.'.jpg';
if (!ImageManager::resize($file, _PS_CMS_IMG_DIR_.$object->id.'-'.stripslashes($image_type['name']).'.jpg', (int)$image_type['width'], (int)$image_type['height'])) {
$return = false;
}
if ($generate_hight_dpi_images) {
if (!ImageManager::resize($file, _PS_CMS_IMG_DIR_.$object->id.'-'.stripslashes($image_type['name']).'2x.jpg', (int)$image_type['width']*2, (int)$image_type['height']*2)) {
$return = false;
}
}
}
$current_logo_file = _PS_CMS_IMG_DIR_.'cms_mini_'.$object->id.'_'.$this->context->shop->id.'.jpg';
if (file_exists($current_logo_file)) {
unlink($current_logo_file);
}
}
return $return;
}
8/ Override controllers/admin/AdminImagesController.php to override/controllers/admin/AdminImagesController.php
In the __construct method, append to $this->fields_list: 'cms' => array('title' => $this->l('CMS'), 'align' => 'center', 'type' => 'bool', 'callback' => 'printEntityActiveIcon', 'orderby' => false)
In the __contruct method, append to $this->fields_form=>input:
array(
'type' => 'switch',
'label' => $this->l('Pages CMS'),
'name' => 'cms',
'required' => false,
'is_bool' => true,
'hint' => $this->l('This type will be used for CMS banner images.'),
'values' => array(
array(
'id' => 'cms_on',
'value' => 1,
'label' => $this->l('Enabled')
),
array(
'id' => 'cms_off',
'value' => 0,
'label' => $this->l('Disabled')
),
)
),
In initRegenerate method, append to $types var: 'cms' => $this->l('CMS pages')
In the _regenerateThumbnails method, append to $process array: array('type' => 'cms', 'dir' => _PS_CMS_IMG_DIR_)
9/ Update your CMS page template cms.tpl with something like this:
<!-- Check out the default theme's category_header.tpl for original PrestaShop code -->
<div class="cms_banner"{if $cms->id_image} style="background:url({$link->getCmsImageLink($cms->link_rewrite, $cms->id_image, 'large')|escape:'html':'UTF-8'}) right center no-repeat; background-size:cover; "{/if}>
<div class="container">
<!-- Your content here -->
</div>
</div>
To prevent the loose of the image when you're updating the datas. (This appends with the 1.7) I suggest the following improvement in the postProcess method :
public function postProcess()
{
$languages = Language::getLanguages(false);
$update_images_values = false;
$cms = new CMS((int)Tools::getValue('id_cms'));
foreach ($languages as $lang)
{
if (isset($_FILES['CMS_IMG'])
&& isset($_FILES['CMS_IMG']['tmp_name'])
&& !empty($_FILES['CMS_IMG']['tmp_name']))
{
if ($error = ImageManager::validateUpload($_FILES['CMS_IMG'], 4000000))
return $error;
else
{
$ext = substr($_FILES['CMS_IMG']['name'], strrpos($_FILES['CMS_IMG']['name'], '.') + 1);
$file_name = md5($_FILES['CMS_IMG']['name']).'.'.$ext;
if (!move_uploaded_file($_FILES['CMS_IMG']['tmp_name'],
_PS_IMG_DIR_ .'cms'.DIRECTORY_SEPARATOR.$file_name))
return Tools::displayError($this->l('An error occurred while attempting to upload the file.'));
else
{
$values['CMS_IMG'][$lang['id_lang']] = $file_name;
}
}
$update_images_values = true;
$cms->CMS_IMG = $file_name;
$cms->update();
}
else
{
$_POST['CMS_IMG'] = $cms->CMS_IMG;
}
}
self::saveProducts();
parent::postProcess();
}
That adds an else condition to the original one
// add one image per page
'CMS_IMG' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isString', 'size' => 3999999999999), ),
Above argument can not create a field on cms_lang table.
So system can gives the following error
Unknown column 'cms_image' in 'field list'
if ($webservice_call && $errno) {
$dbg = debug_backtrace();
WebserviceRequest::getInstance()->setError(500, '[SQL Error] '.$this->getMsgError().'. From '.(isset($dbg[3]['class']) ? $dbg[3]['class'] : '').'->'.$dbg[3]['function'].'() Query was : '.$sql, 97);
} elseif (_PS_DEBUG_SQL_ && $errno && !defined('PS_INSTALLATION_IN_PROGRESS')) {
if ($sql) {
throw new PrestaShopDatabaseException($this->getMsgError().'<br /><br /><pre>'.$sql.'</pre>');
}
throw new PrestaShopDatabaseException($this->getMsgError());
}
}
I am using Symfony2. When the pdf file is generated using this code :
public function printAction($id)
{
// initialiser $demande
$html = $this->renderView('PFETimeBundle:Demande:print.html.twig',
array('demande'=> $demande)
);
return new Response(
$this->get('knp_snappy.pdf')->getOutputFromHtml($html),
200,
array(
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'attachment; filename="file.pdf"'
)
);
}
I get this content (french characters appear in bad characters) :
try to add the encoding property
'encoding' => 'utf-8',
heres a full copy of my working code, pls note that i pass an options array as second argument to getOutPutFromHtml()
return new Response(
$this->get('knp_snappy.pdf')->getOutputFromHtml($html, array(
'orientation' => 'landscape',
'enable-javascript' => true,
'javascript-delay' => 1000,
'no-stop-slow-scripts' => true,
'no-background' => false,
'lowquality' => false,
'encoding' => 'utf-8',
'images' => true,
'cookie' => array(),
'dpi' => 300,
'image-dpi' => 300,
'enable-external-links' => true,
'enable-internal-links' => true
)),
200,
array(
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'attachment; filename="report.pdf"'
)
);
If you are using the generateFromHtml method, you have to use it like this, at third parameter:
$this->container->get('knp_snappy.pdf')->generateFromHtml(
$this->container->get('templating')->render(
'YourBundle:Template:pdfTemplate.html.twig',
array(
'var' => $var,
)
),
'/path/to/file.pdf',
array(
'encoding' => 'utf-8',
)
);
I'm developping with Yii framework. I create two dialog boxes in a Yii page to create some model. The first one pops up correctly, but the second one doesn't.
I've written the view file. The view.php is as follow:
<?php
$this->menu=array(
//array('label' => Yii::t('app', 'Publier à un contact'), 'url' => array('publierAContact', 'id_p'=> $model->id_publication, 'id_e' => $model->id_evenement)),
array('label' => Yii::t('app', 'Publier à un contact'), 'url' =>'#','linkOptions' => array(
'onclick' => "{publierAContact(); $('#dialogContact').dialog('open');}",
)),
array('label' => Yii::t('app', 'Publier à un groupe'), 'url' => '#', 'linkOptions' => array(
'onclick' => "{publierAGroupe(); $('#dialogGroupe').dialog('open');}",
))
);
$this->beginWidget('zii.widgets.jui.CJuiDialog', array(
'id' => 'dialogContact',
'options' => array(
'title' => 'Publier à un contact',
'autoOpen' => false,
'modal' => true,
'width' => 550,
'height' => 200,
),
));
echo "<div class='divForForm'></div>";
$this->endWidget('zii.widgets.jui.CJuiDialog');
$this->beginWidget('zii.widgets.jui.CJuiDialog', array(
'id' => 'dialogGroupe',
'options' => array(
'title' => 'Publier à un groupe',
'autoOpen' => false,
'modal' => true,
'width' => 550,
'height' => 200,
),
));
echo "<div class='divForForm'></div>";
$this->endWidget('zii.widgets.jui.CJuiDialog');
?>
<script type='text/javascript'>
function publierAContact()
{
<?php echo CHtml::ajax(array(
'url' => array('Publication/PublierAContact', 'id_p'=>$id_p, 'id_e' =>$id_e),
'data' => "js:$(this).serialize()",
'type' => 'post',
'dataType' => 'json',
'success' => "function(data)
{
if(data.status == 'failure')
{
$('#dialogContact div.divForForm').html(data.div);
//Here is the trick: on submit-> once again this function!
$('#dialogContact div.divForForm form').submit(add);
}
else
{
$('#dialogContact div.divForForm').html(data.div);
setTimeout(\"$('#dialogContact').dialog('close');refreshDisciplines();\", 1000);
document.location.reload();
}
}"
));?>
return false;
}
function PublierAGroupe()
{
<?php echo CHtml::ajax(array(
'url' => array('Publication/PublierAGroupe', 'id_p' => $id_p, 'id_e' => $id_e),
'data' => "js:$(this).serialize()",
'type' => 'post',
'dataType' => 'json',
'success' => "function(data)
{
if(data.status == 'failure')
{
$('#dialogGroupe div.divForForm').html(data.div);
//Here is the trick: on submit-> once again this function!
$('#dialogGroupe div.divForForm form').submit(add);
}
else
{
$('#dialogGroupe div.divForForm').html(data.div);
setTimeout(\"$('#dialogGroupe').dialog('close');refreshDisciplines();\", 1000);
document.location.reload();
}
}"
));?>
return false;
}
</script>
Unfortunately, my second dialog can not appear. Can somebody help me know what's wrong ?
Edit :
The function must be called properly :
'onclick' => "{PublierAGroupe(); $('#dialogContact').dialog('open');}",
as the function's identifier is publierAGroupe.