zfdatagrid date format - zend-framework

I am using ZF 1.11, PHP 5.3, Windows and the most recent version of zfdatagrid.
I use
$grid->updateColumn('birthday', array('format'=> array('date',array('date_format' => 'dd-MM-yyyy'))));
to display an attribute "birthday" as dd-MM-yyyy. When I click on the Edit button (CRUD enabled), the value of this attribute is being displayed as 'yyyy-MM-dd'. When the user clicks the save button, he gets an error message (Please, enter date as dd-MM-yyyy).
How can I tell the $form to display the value as dd-MM-yyyy instead of yyyy-MM-dd?

looking at the docks I think your code maybe slighty incorrect:
$grid->updateColumn('birthday', array('format'=> array('date',array('date_format' => 'dd-MM-yyyy'))));
Maybe:
$grid->updateColumn('birthday', array('format'=> 'date',array('date_format' => 'dd-MM-yyyy')));
looks like you had an extra array() at date.
reference:
Date ZFDatagrid will for a key in Zend_Registry with the name
Zend_Locale and use it. You can also pass as argument a instance of
Zend_Locale or an array with the following options
locale
date_format
type
$grid->updateColumn('field',array('format'=>'date'));

thanks, but that doesn't solve the problem. Actually, what I did now is to add an event listener for the crud.form_built event. The method called in this event simply creates a new Zend_Validate_Date object and assigns this validator to the corresponding zfdatagrid element.
This is a hack indeed, but it works. Actually, zfdatagrid doesn't really work with PostgreSQL and the manual is incorrent in many places.

!Warning maybe bad English!
for i.e. "localized" values in forms i do something like this..
after passing form to the grid $grid->setForm($myform);
but before the call of $grid->deploy();
i get the form element and set the value manually like the following
$Form=$grid->getForm(1);
$dateVal = $Form->getElement('birthday')->getValue();
if(Zend_Date::isDate($dateVal,'yyyy-MM-dd', 'en'))
$dateObject = new Zend_Date($dateVal);
$Form->getElement('birthday')
->setValue($dateObject->toString('dd.MM.yyyy'))
->setValidators(array(new Zend_Validate_Date('dd.MM.yyyy')))
;
then register a zfdatagrid event trigger "crud.before_update"
$grid->listenEvent('crud.before_update', array($this, '_updateCallback')
that calls a function from this controller _updateCallback() with something custom like this
public function _updateCallback(Bvb_Grid_Event $e)
{
$n = $e->getParam('newValues');
list($d,$m,$y) = explode('.', $n['birthday']);
$sqldate = $y.'-'.$m.'-'.$d;
$n['birthday'] = $sqldate;
$e->setParam('newValues', $n);
}
or just use Zend Date like before to refactor date to ensure that
the datevalue we want to save, is in a sql valid US date format..
hope this will help..
z.

Related

Passing arguments to Access Forms created with 'New'

I have a form called 'detail' which shows a detailed view of a selected record. The record is selected from a different form called 'search'. Because I want to be able to open multiple instances of 'detail', each showing details of a different record, I used the following code:
Public detailCollection As New Collection
Function openDetail(patID As Integer, pName As String)
'Purpose: Open an independent instance of form
Dim frm As Form
Debug.Print "ID: " & patID
'Open a new instance, show it, and set a caption.
Set frm = New Form_detail
frm.Visible = True
frm.Caption = pName
detailCollection.Add Item:=frm, Key:=CStr(frm.Hwnd)
Set frm = Nothing
End Function
PatID is the Primary Key of the record I wish to show in this new instance of 'detail.' The debug print line prints out the correct PatID, so i have it available. How do I pass it to this new instance of the form?
I tried to set the OpenArgs of the new form, but I get an error stating that OpenArgs is read only. After researching, OpenArgs can only be set by DoCmd (which won't work, because then I don't get independent instances of the form). I can find no documentation on the allowable parameters when creating a Form object. Apparently, Microsoft doesn't consider a Constructor to be a Method, at least according to the docs. How should I handle this? (plz don't tell me to set it to an invisible text box or something) Thanks guys, you guys are the best on the net at answering these questions for me. I love you all!
Source Code for the multi-instance form taken from: http://allenbrowne.com/ser-35.html
Inside your Form_detail, create a custom property.
Private mItemId As Long
Property Let ItemID(value as Long)
mItemId = value
' some code to re query Me
End Property
Property Get ItemId() As Long
ItemId = mItemId
End Property
Then, in the code that creates the form, you can do this.
Set frm = New Form_detail
frm.ItemId = patId
frm.Visible = True
frm.Caption = pName
This will allow you to pass an ID to the new form instance, and ensure it gets requeried before making it visible. No need to load all of the results every time if you're always opening the form by Newing it. You let the property load the data instead of the traditional Form_Load event.
This works because Access Form modules are nothing more than glorified classes. Hope this helps.
You could try applying a filter:
frm.Filter = "[ID] = " & patID
frm.FilterOn = True
The Record Source of the Detail form will need to be set to the table to which the ID belongs.
UPDATE
As you requested, here is the code to set the RecordSource:
frm.RecordSource = "select * from TableName where [ID] = " & patID
This is probably cleaner than using a filter given that a user can remove the filter (depending on the type of form).

Query criteria: from public variable or from form

After a few hours searching, I couldn't find any solution to this little problem I'm having.
I have a query that retrieves one of its criteria from a form. I have referenced correctly the value on the form from the query, and it works, but what I wanted to do is a bit more complicated: when the form is closed, I want to launch the query with a "default value".
I tried to do it in 2 different ways:
a) Defining an "IIf" at the query criteria: I would need a function that checks if the form from which I retrieve the values is open.
b) Defining public variables with a default value, which would be changed from the form: I don't know where/when to initialize the value of the variable.
Does anyone have a better idea on how to do this?
TL;DR: Query gets criteria from form when it's open. If form is closed, query uses default value. HELP!
You can create a VBA function in a module to do this :
Function MyCriterion() As Long
MyCriterion = 1234 ' default value
If CurrentProject.AllForms("MyForm").IsLoaded Then
MyCriterion = Forms("MyForm").MyControl.Value
End If
End Function

Add ‘subscription_date’ and ‘user_ip_address’ fields for Newsletter Subscriber and show it in admin panel in Magento

I want to add two custom fields for newsletter subscriber at which date customer subscribed and its ip address:
1) I had added two columns in ‘newsletter_subscriber’ table.
How this can be achieved?
In the file app/code/core/Mage/Newsletter/Model/Subscriber.php
I found the the functions like:
$this->setSubscriberEmail($email);
$this->setStoreId(Mage::app()->getStore()->getId());
$this->setCustomerId(0);
But i did not found its code.
I think I also save data like that but how it will be possible ? where I have to define and declare code for the function like $this->seSubscriptionDate();
And how to display in Admin panel under Newsletter->Newsletter Subscriber ?
I had not find the solution or help from any person.
I bang my head and find the solution its very simple hope in future may some one’s time save by this post:
1) Add columns in the table “newsletter_subscriber” say in my case “subscription_date” and “sub_ip_address”
2) Add following two lines in the file # two places
app\code\core\Mage\Newsletter\Model\Subscriber.php
**$this->setSubscriptionDate(date("d-m-Y"));
**$this->setSubIpAddress($_SERVER['REMOTE_ADDR']);
One place: in the function: public function subscribe($email)
Before: $this->save();
Second place: in the function: public function subscribeCustomer($customer)
Before: $this->save();
Now data will be added in the table
Now to show in admin panel
1) Open the file app\code\core\Mage\Adminhtml\Block\Newsletter\Subscriber\Grid.php
Just add two required columns in it like
$this->addColumn('subscription_date', array(
'header' => Mage::helper('newsletter')->__('Subscription Date'),
'index' => 'subscription_date',
'default' => '----'
));
$this->addColumn('sub_ip_address', array(
'header' => Mage::helper('newsletter')->__('IP Address'),
'index' => 'sub_ip_address',
'default' => '----'
));
Now finish.
** This is the point where I spent my time and at the end I added this function on hit and trieal basis but it works.
Someone from Core magento team plz explain why this {setSubscriptionDate()}function work ? I had not declared anybody of this function.
It seems it shows intellisense to detect table field?
I also had the similar problem. However, in my case I am supposed to add a "country" and "gender" field in the newsletter form. So, on digging into the core for hours I figured this out. Below is the explaination:
In the app/design/frontend//default/template/newsletter/subscribe.phtml,
the form has getFormActionUrl() ?> in its action field. This referes to app/code/core/Mage/Newsletter/Block/Subscribe.php
The $this object used in the subscirbe.phtml file referes to this class (i.e. Mage_Newsletter_Block_Subscribe) and thus $this->getFormActionUrl() in the subscribe.phtml file refers to the function getFormActionUrl() of this class.
Now, this getFormActionUrl() is returning a method $this->getUrl('newsletter/subscriber/new', array('_secure' => true)) which belongs to its parent Mage_Core_Block_Template (I mean the method getURL()). This part is not improtant to us.
See the parameter passed to the getURL() function which is newsletter/subscriber/new
The first part "newsletter" is the module name (app/code/code/Mage/newsletter), the second part "subscriber" is the controller name (app/code/code/Mage/newsletter/controllers/SubscriberController) and the third part "new" is the method newAction in the controller SubscriberController.
Controller names are suffixed with Controller and function names are suffixed by Action. (Thanks to Phalcon framework to help understand this)
Now in the newAction() method you can see that by default, only the email is being posted
as
$email = (string) $this->getRequest()->getPost('email');
What I did was, I cloned the subscribe.phtml template with a name custom-newsletter.phtml and add two fields with name "country" and "gender". Then added the following lines in the newAction():
$country = (string) $this->getRequest()->getPost('country');
$gender = (string) $this->getRequest()->getPost('gender');
Now at line number 67 of the SubscriberController (inside the newAction() method) there is a code :
$status = Mage::getModel('newsletter/subscriber')->subscribe($email);
This line is calling the subscribe method of app/code/code/Mage/newsletter/Model/Subscriber.php and is passing the $email that is posted from newsletter form into it. I modified the line as:
$status = Mage::getModel('newsletter/subscriber')->subscribe($email,$country,$gender);
Now, I have to edit the app/code/code/Mage/newsletter/Model/Subscriber.php
When we talk about Models, we are talking about the database table the Model refers to. The model name is Subscriber and it belongs to module Newsletter so, the database table that this model affects is newsletter_subscriber
From this part on #Hassan Ali Sshahzad's question will be answered.
I added two new columns there with the name: subscriber_country and subscriber_gender.
Now, Magento' system automatically makes available a getter and a setter function to these columns with the following name:
function getSubscriberCountry(){}
function setSubscriberCountry($country_name){}
So, all I had to do in the model was:
Edit the method subscribe($email) to subscribe($email,$country,$gender)
add the following code in the subscribe($email,$country,$gender) function right before the try statement as follows:
$this->setSubscriberCountry($country);
$this->setSubscriberGender($gender);
try{
$this->save()
Hassan's method played a key role in my understanding of the MVC of Magento so its a return from my side.

Hiding end date in Drupal

I'm creating a calendar module in Drupal. Due to the clients' needs, I need to be able to hide the end date using a boolean variable on the node, saved in a CCK field.
My problem is that I am able to hide it, by hooking into the theme_date_display_range() theming function, but not from within my module. As far as I can see, this is only possible from within the theme. This will mean, that I won't be able to hide the end date without using a certain theme enabling this.
If I then say, I'll use a certain theme and live with that, I'm still not able to see the context in which the mytheme_date_display_range() is called, and I thereby have no way of knowing if the current node wants to show or hide the end date. I could pass it along as a variable, but would there be a better way to do this?
Could I maybe overwrite the date's theming function to use my module instead, and how would I do this, if that was the best/correct way?
Okay, so I think I've found a good solution without using the theming functions at all. I took a closer look at theme_date_display_combination(), which themes the dates. If the end date is not set, it will only display the start date. I hook into hook_entity_prepare_view() and check if both the date and hide endtime fields are present. If so I unset the endtime based on the boolean value.
/**
* Implements hook_entity_prepare_view().
*/
function kw_calendar_full_entity_prepare_view($entities, $type, $langcode) {
foreach ($entities as &$entity) {
if (isset($entity->field_event_date) && isset($entity->field_hide_endtime) && $entity->field_hide_endtime[LANGUAGE_NONE][0]['value'] == 0) {
unset($entity->field_event_date[LANGUAGE_NONE][0]['value2']);
}
}
}
Hope this will help somebody one day...

ZEND currency symbol is displaying 1

I'm using zend currency to display the currency based on locale. When I use the following code the symbol gets replaced by 1 instead of simply being removed:
$currency = new Zend_Currency($locale);
$currency->setFormat(array('symbol' => Zend_Currency::NO_SYMBOL));
What normally gets returned is this: € 2.500,01
but after the "setFormat" call I'm getting this: 1 2.500,01
I don't want the "1 " in there.
Any ideas on how to fix this?
Thanks.
Here's the ZF tutorial page for this. It looks like it will set a person in the right direction: zend currency tutorial page
You're setting the wrong option in setFormat. You need to set display to Zend_Currency::NO_SYMBOL. Like this:
$c = new Zend_Currency();
$c->setFormat(array('display' => Zend_Currency::NO_SYMBOL));
echo $c->toCurrency(2500.01);
Which outputs
2,500.01
The way you are currently doing it is literally setting the symbol to 1 because that's what the constant NO_SYMBOL evaluates to.