Zend_Cache caches the entire site instead of a controller - zend-framework

I wanted to cache controller ajax
<?php
class Default_Bootstrap extends Zend_Application_Module_Bootstrap
{
public function _initCache()
{
$frontendOptions = array(
'lifetime' => 10800,
'automatic_serialization' => true,
'debug_header' => false,
'regexps' => array('^/ajax/' => array('cache' => true),
'^/admin/' => array('cache' => false)),
'default_options' => array(
'cache_with_cookie_variables' => true,
'make_id_with_cookie_variables' => false));
$backendOptions = array('cache_dir' => APPLICATION_PATH.'/data/cache');
$cache = Zend_Cache::factory('Page','File',$frontendOptions,$backendOptions);
$cache->start();
}
}
But caching the entire site, including the admin module.

Use debug_header to true and check , the code below. As we set not to cache all pages at first, and only to cache the page starting with ajax , I hope all the ajax pages starts with the name ajax else change accordingly the regexp.
class Default_Bootstrap extends Zend_Application_Module_Bootstrap
{
public function _initCache()
{
$frontendOptions = array(
'lifetime' => 10800,
'automatic_serialization' => true,
'debug_header' => true,
'regexps' => array(
'$' => array('cache' => false),
'/ajax' => array('cache' => true),
),
'default_options' => array(
'cache_with_cookie_variables' => true,
'make_id_with_cookie_variables' => false
)
);
$backendOptions = array('cache_dir' => APPLICATION_PATH.'/data/cache');
$cache = Zend_Cache::factory('Page','File',$frontendOptions,$backendOptions);
$cache->start();
}
}

Related

Prestashop HelperFrom/List - messy layout

I'm new to prestashop and I worked the whole day on creating a back office interface that allows the user to write, edit, and delete articles. It is sort of a blog. I used Prestashop's Helpers (Form and List) and everything works great. I also added a new tab in the back office to access this tool.
The problem is that the layout is messy and doesn't look like the other forms and listing pages. The layout is really not sexy. Maybe I should look at some css file, or add any function in my controller ? You'll find the source code of the latter here (I can't insert images, not enough reputation --'):
<?php
class Article extends ObjectModel
{
/** #var string Name */
public $id_article;
public $titre;
public $contenu;
public $url_photo;
/**
* #see ObjectModel::$definition
*/
public static $definition = array(
'table' => 'article',
'primary' => 'id_article',
'fields' => array(
'titre' => array(
'type' => self::TYPE_STRING,
'validate' => 'isGenericName',
'required' => true,
'class' => 'lg'
),
'contenu' => array(
'type' => self::TYPE_STRING,
'validate' => 'isGenericName',
'required' => true
),
'url_photo' => array(
'type' => self::TYPE_STRING,
'validate' => 'isGenericName',
'required' => false,
),
),
);
}
class AdminBlogController extends AdminController{
public function initContent(){
parent::initContent();
}
public function __construct(){
$this->table = 'article';
$this->className = 'Article';
$this->lang = false;
// Building the list of records stored within the "article" table
$this->fields_list = array(
'id_article' => array(
'title' => 'ID',
'align' => 'center',
'width' => 25
),
'titre' => array(
'title' => 'Titre',
'width' => 'auto'
),
'contenu' => array(
'title' => 'Contenu',
'width' => 'auto'
)
);
// This adds a multiple deletion button
$this->bulk_actions = array(
'delete' => array(
'text' => $this->l('Delete selected'),
'confirm' => $this->l('Delete selected items?')
)
);
parent::__construct();
}
// This method generates the list of results
public function renderList(){
// Adds an Edit button for each result
$this->addRowAction('edit');
// Adds a Delete button for each result
$this->addRowAction('delete');
return parent::renderList();
}
// This method generates the Add/Edit form
public function renderForm(){
// Building the Add/Edit form
$this->fields_form = array(
'tinymce' => true,
'legend' => array(
'title' => 'Article'
),
'input' => array(
array(
'type' => 'text',
'label' => 'Titre',
'name' => 'titre',
'class' => 'lg',
'required' => true,
//'desc' => 'Nom de l\'article',
),
array(
'type' => 'textarea',
'label' => 'Contenu',
'name' => 'contenu',
'class' => 'lg',
'required' => true,
'autoload_rte' => true,
//'desc' => 'Contenu de l\'article',
),
array(
'type' => 'file',
'label' => 'Photo',
'name' => 'url_photo',
'class' => 'lg',
'required' => true,
//'desc' => 'Contenu de l\'article',
)
),
'submit' => array(
'title' => $this->l('Save'),
'class' => 'button'
)
);
return parent::renderForm();
}
}
?>
Thank you.
I just needed to set $this->bootstrap = true

I cannot instantiate a model in zend framwork 1.12

I am trying to build a Registration and login process how ever I am new to Zend I can't instantiate a model class:
application/models/users.php
class Users extends Zend_Db_Table_Abstract
{
protected $_name = 'users';
}
in my bootstrap I performed autoloading this way
application/bootstrap
protected function _initAutoload()
{
//Add autoloader empty namespace
require_once 'Zend/Loader/Autoloader.php';
$autoLoader = Zend_Loader_Autoloader::getInstance();
$resourceLoader = new Zend_Loader_Autoloader_Resource(array(
'basePath' => APPLICATION_PATH,
'namespace' => '',
'resourceTypes' => array(
'form' => array(
'path' => 'forms/',
'namespace' => 'Form_'
),
'model' => array(
'path' => 'models/',
'namespace' => 'Model_'
)
)
)
);
//Return it so that it can be stored by the bootstrap
return $autoLoader;
}
on my controller I have this
public function indexAction()
{
$frmuser = new Application_Form_Contact;
$users = new Application_Model_Users;
$this->view->form = $frmuser;
// action body
}
I have a form that works fine in application\forms\contact.php
class Application_Form_Contact extends Zend_Form
{
public function init()
{
$this->setMethod('post');
/* Form Elements & Other Definitions Here ... */
$this->addElement('text', 'username',array('filters' => array('StringTrim'), 'required' => true,'label' => 'Enter a Username:', 'validators' => array
(array('StringLength', false, array(2, 50)))));
$this->addElement('text', 'firstname',array('filters' => array('StringTrim'), 'required' => true,'label' => 'Enter your Firstname:', 'validators' =>
array(array('StringLength', false, array(2, 50)))));
$this->addElement('text', lastname, array('filters' => array('StringTrim'), 'required' => true,'label' => 'Enter your Lastname:', 'validators' => array
(array('StringLength', false, array(2, 50)))));
$this->addElement('text', 'email', array('filters' => array('StringTrim'), 'required' => true,'label' => 'Enter your Email:', 'validators' => array
('EmailAddress', array
('StringLength', false, array(2, 50)))));
$this->addElement('text', 'emailagain', array('filters' => array('StringTrim'), 'required' => true,'label' => 'Confirm your Email:', 'validators' =>
array('EmailAddress', array('StringLength', false, array(2, 50)))));
$this->addElement('password', 'password', array('filters' => array('StringTrim'), 'required' => true,'label' => 'Enter a password:', 'validators' =>
array(array('StringLength', false, array(2, 50)))));
$this->addElement('password', 'passwordagain', array('filters' => array('StringTrim'), 'required' => true,'label' => 'Confirm your password:',
'validators'=> array(array('StringLength', false, array(2, 50)))));
$this->addElement('submit', 'contact');
$this->view->form = $form;
}
}
if I comment out the $users = new Application_Model_Users the form displays well but if uncommented,
pointing to the url in the internet explorer browser will display the code like this:
class Users extends Zend_Db_Table_Abstract { protected $_name = 'users'; }
The easiest way to define a model is use zftool.
or you can make changes in your application like this to make your model work:
application\models\DbTable\users.php
class Application_Model_DbTable_Users extends Zend_Db_Table_Abstract {
protected $_name = 'users';
}
In your Controller now you are able to access it like this:
$users = new Application_Model_DbTable_Users();
Further details are provided in zend framework documentation:
Read it carefully to find out perfect way to implement it:
Zend Framework 1.12 Documentation

My ajax Drupal form shows up old values

I have developed ajax drupal and add a form (textfield and button) in it using #ajax key and callback function where I do my process and return my form new element.
So when I starts adding my data form, it works fine for me and form_state['values'] are updated fine.
The problem here is when I reload my form and I add some data, form_state['values'] are not the same in my form fields.
Here is my code:
function my_horoscope_menu() {
$items = array();
$items['admin/horoscopes'] = array(
'title' => 'Horoscopes',
'page callback' => 'drupal_get_form',
'page arguments' => array('my_horoscope_admin_form'),
'access arguments' => array('Administer site configuration '),
//'type' => MENU_LOCAL_TASK,
);
return $items;
}
function my_horoscope_admin_form($form, &$form_state) {
$form['settings']['horoscopes']['add_horoscopes'] = array(
'#type' => 'fieldset',
'#title' => t('Add horoscopes'),
'#collapsible' => TRUE,
'#collaspsed' => TRUE,
);
$form['settings']['horoscopes']['add_horoscopes']['name'] = array(
'#type' => 'textfield',
'#title' => t('Horoscope name'),
'#default_value' => t('Horoscope name'),
'#size' => 20,
'#maxlength' => 60,
'#required' => FALSE,
);
$form['settings']['horoscopes']['add_horoscopes']['beginning_date_rang'] = array(
'#type' => 'date',
'#title' => t('Horoscope beginning date rang'),
'#description' => t('Set the beginning date rang of this horoscope.'),
'#required' => FALSE,
);
$form['settings']['horoscopes']['add_horoscopes']['ending_date_rang'] = array(
'#type' => 'date',
'#title' => t('Horoscope ending date rang'),
'#description' => t('Set the ending date rang of this horoscope.'),
'#required' => FALSE,
);
$form['settings']['horoscopes']['add_horoscopes']['add_button'] = array(
'#type' => 'button',
'#value' => t('Add this horoscope'),
'#submit' => array(''),
'#ajax' => array(
'event' => 'click',
'callback' => 'add_horoscope_ajax_process',
'wrapper' => 'add_horoscope_wrapper',
),
);
$form['settings']['horoscopes']['add_horoscopes']['adding_horoscope_wrapper'] = array(
'#type' => 'markup',
'#prefix' => '<div id="add_horoscope_wrapper">',
'#suffix' => '</div>',
);
return $form;
}
function add_horoscope_ajax_process ($form, &$form_state) {
if (isset($form_state['values'])) {
if (isset($form_state['values']['name']) AND $form_state['values']['name'] != '') {
$name = $form_state['values']['name'];
}
if (isset($form_state['values']['beginning_date_rang']) AND $form_state['values']['beginning_date_rang'] != '') {
$beginning_date_rang = $form_state['values']['beginning_date_rang'];
$beginning_date_rang1 = sprintf("%04d-%02d-%02d", $beginning_date_rang['year'], $beginning_date_rang['month'], $beginning_date_rang['day']);
}
if (isset($form_state['values']['ending_date_rang']) AND $form_state['values']['ending_date_rang'] != '') {
$ending_date_rang = $form_state['values']['ending_date_rang'];
$ending_date_rang1 = sprintf("%04d-%02d-%02d", $ending_date_rang['year'], $ending_date_rang['month'], $ending_date_rang['day']);
}
// Prepare record to add
$record = array(
'h_name' => $name,
'h_date_begin' => $beginning_date_rang1,
'h_date_end' => $ending_date_rang1,
);
// Add the record
$res = drupal_write_record('my_horoscope_structure', $record);
if($res != FALSE) {
drupal_set_message(t('Horoscope #name is inserted successfully!', array('#name' => $name)));
}
}
// return $form
return $form['settings']['horoscopes']['add_horoscopes']['adding_horoscope_wrapper'];
}

Zend_Form not valid but no error messages

i have a strange Problem with one of my Zend_Forms: isValid() correctly states that my form isn't valid but i do not get any error messages. How could that happen?
Heres the code, nothing special here. $data is an array of post data. The problem occurs when no file is sended.
$form = $this->getForm('Foto');
if(!$form->isValid($data)) {
var_dump( $form->getErrors() ); die;
return false;
}
getForm() initializes the form if not allready done. The form itself is pretty straight forward.
class Media_Forms_Foto extends Zend_Form
{
/**
* Initializer function. Setup forms fields.
*/
public function init()
{
$this->setName('add Image');
$this->setAction('media/gallery/addImage');
$this->addElement('Hidden', 'gallery', array(
'filters' => array(),
'validators' => array('Digits'),
'required' => false,
'label' => '',
));
$this->addElement('File', 'foto', array(
'required' => true,
'destination' => ROOT_PATH .'public/upload/tmp/',
'label' => 'Foto',
'validators' => array(
new Zend_Validate_File_IsImage(array(
'image/jpeg', 'image/gif', 'image/png'
))
),
'maxFileSize' => 2097152,
));
$this->addElement('Submit', 'add', array(
'required' => false,
'ignore' => true,
'label' => 'add Foto',
));
$this->setAttrib('enctype', 'multipart/form-data');
}
}
Output:
array(3) {
["gallery"]=>
array(0) {
}
["foto"]=>
array(0) {
}
["add"]=>
array(0) {
}
}
Are you using the jquery form plugin by any chance? if so, try using the iframe mode to upload files:
var options = {
url: '/test/upload',
success: function(response) {
},
beforeSubmit: disableSubmitButtons,
iframe: true,
dataType : 'json'
};
$('#testForm').ajaxForm(options);

zend framework upload image false extension file upload

This is my code for a form and i am getting an error when i am uploading :"File 'swing-layout-1.0.4-src.zip' has a false extension"
<?php
class Admin_Form_Banner extends ZendX_Form_Designed {
public function init() {
$this->setEnctype(self::ENCTYPE_MULTIPART);
$this->setMethod(self::METHOD_POST);
$this->setMethod('post');
// Add an email element
$this->addElement('text', 'banner_title', array(
'label' => 'Banner Title',
'required' => true,
'filters' => array('StringTrim')
));
$this->addElement('text', 'banner_type', array(
'label' => 'Banner Type',
'required' => true,
'filters' => array('StringTrim')
));
$this->addElement('checkbox', 'is_active', array(
'label' => 'Is Active',
'required' => true,
'filters' => array('StringTrim')
));
$banner_position = new Zend_Form_Element_Select('banner_position');
$banner_position->setMultiOptions($this->getBannerPositions())->setLabel('Banner Position');
$this->addElement($banner_position, 'banner_position');
$file = new Zend_Form_Element_File('file');
$file->addValidator('Count', FALSE, 1);
$file->addValidator('Size', FALSE, 67633152);
$file->addValidator('Extension', false,'.zip,rar');
$file->setRequired(FALSE);
$file->setAllowEmpty(false);
$this->addElement($file, 'file_path', array('label' => 'Attacehd file'));
$this->addElement('submit', 'submit', array(
'ignore' => true,
'label' => ''
));
}
public function getBannerPositions() {
$db = Zend_Db_Table::getDefaultAdapter();
$bannerPosition = $db->fetchPairs($db
->select()
->from('banner_position'), array('id', 'banner_position'));
return $bannerPosition;
}
}
I think there should be 'zip,rar' rather than '.zip,rar'.