Default element value using Zend_Form with Zend_Config_Ini - zend-framework

I have been looking for hours and I can't find any documentation anywhere as to how you set the default value of an element using Zend_Config_Ini as the initialisation to a Zend_Form.
I've seen the documentation for how you do it in normal PHP code...
$validators = array(
'month' => array(
'digits',
'default' => '1'
)
);
// no value for 'month' field
$data = array();
$input = new Zend_Filter_Input(null, $validators, $data);
echo $input->month; // echoes 1
But there's no documentation as to how you set it in an .ini file. I've tried everything (obviously not, but it seems so)!!
Does anyone know the "syntax"?
query.elements.rows.type = "text"
query.elements.rows.options.required = true
query.elements.rows.options.validators.rows.validator = "digits"
query.elements.rows.default = 0 # DOESN'T WORK!
If the "rows" value provided isn't of "digits" then I want it set to "0".
Thanks in advance!!

query.elements.rows.type = "text"
query.elements.rows.options.required = true
query.elements.rows.options.validators.rows.validator = "digits"
query.elements.rows.options.value = 0 # This works ;)
If the "rows" value provided isn't of "digits" then I want it set to "0".
This is not achievable by default options. Try the following code:
if( !$form->isValid( $_POST ) ) {
$errorsMessages = $form->getMessages();
if(isset($errorsMessages['rows']['notDigits'])){
$form->getElement( 'rows' )->setValue( '0' ) ;
}
}

Related

How to add multiple custom fields to a wp_query in a shortcode?

In a shortcode I can limit wp_query results by custom field values.
Example:
[my-shortcode meta_key=my-custom-field meta_value=100,200 meta_compare='IN']
And obviously it's possible to use multiple custom fields in a wp_query like WP_Query#Custom_Field_Parameters
But how can I use multiple custom fields in my shortcode? At the moment I do pass all the shortcode parameters with $atts.
On of a few different solutions might be to use a JSON encoded format for the meta values. Note that this isn't exactly user-friendly, but certainly would accomplish what you want.
You would of course need to generate the json encoded values first and ensure they are in the proper format. One way of doing that is just using PHP's built in functions:
// Set up your array of values, then json_encode them with PHP
$values = array(
array('key' => 'my_key',
'value' => 'my_value',
'operator' => 'IN'
),
array('key' => 'other_key',
'value' => 'other_value',
)
);
echo json_encode($values);
// outputs: [{"key":"my_key","value":"my_value","operator":"IN"},{"key":"other_key","value":"other_value"}]
Example usage in the shortcode:
[my-shortcode meta='[{"key":"my_key","value":"my_value","operator":"IN"},{"key":"other_key","value":"other_value"}]']
Which then you would parse out in your shortcode function, something like so:
function my_shortcode($atts ) {
$meta = $atts['meta'];
// decode it "quietly" so no errors thrown
$meta = #json_decode( $meta );
// check if $meta set in case it wasn't set or json encoded proper
if ( $meta && is_array( $meta ) ) {
foreach($meta AS $m) {
$key = $m->key;
$value = $m->value;
$op = ( ! empty($m->operator) ) ? $m->operator : '';
// put key, value, op into your meta query here....
}
}
}
Alternate Method
Another method would be to cause your shortcode to accept an arbitrary number of them, with matching numerical indexes, like so:
[my-shortcode meta-key1="my_key" meta-value1="my_value" meta-op1="IN
meta-key2="other_key" meta-value2="other_value"]
Then, in your shortcode function, "watch" for these values and glue them up yourself:
function my_shortcode( $atts ) {
foreach( $atts AS $name => $value ) {
if ( stripos($name, 'meta-key') === 0 ) {
$id = str_ireplace('meta-key', '', $name);
$key = $value;
$value = (isset($atts['meta-value' . $id])) ? $atts['meta-value' . $id] : '';
$op = (isset($atts['meta-op' . $id])) ? $atts['meta-op' . $id] : '';
// use $key, $value, and $op as needed in your meta query
}
}
}

what is inside $conf in typo3 extension

I am reading typo3 extension file, and saw below codes:
function main($content, $conf) {
$this->conf = $conf;
$this->pi_setPiVarDefaults();
$this->pi_loadLL();
$this->template = $this->cObj->fileResource($this->conf['templateFile']);
$GLOBALS['TSFE']->set_no_cache();
I used var_dump($conf); to output $conf, it shows:
array(53) { ["includeLibs"]=> string(47) "typo3conf/ext/jc_job/pi1/class.tx_jcjob_pi1.php" ["userFunc"]=> string(18) "tx_jcjob_pi1->main" ["templateFile"]=> string(28) "EXT:jc_job/pi1/template.html" ["pidList"]=> string(1) "7" ["code"]=> string(4)...
From the result, I can tell some configurations are from this file: ext_typoscript_setup.txt, but some not.
SO my question is:
what is inside $conf? or what files compose $conf?
The end result of $conf is essentially Typoscript from plugin.tx_yourextension_pi1.
This will be what is in ext_typoscript_setup.txt of your extension, plus any changes made along the way. For example in the main template, you can also make changes to the plugin's configuration.
Your Plugin is configured via:
plugin.tx_yourextension_pi1 {
# these values will you have in $conf
someValue = well where it goes?
wrap = wrap|me
something {
different = 1
}
}
So $conf will look like this:
$conf = array(
'someValue' => 'well where it goes?',
'wrap' => 'wrap|me',
'something.' => array(
'different' => '1',
)
)

Zend_Form_Element::clearValidators() doesn't seem to be working

I'm using Zend_Form. Why is the clearValidators() line not working in this example?
$element = $form->getElement('field1');
$element->clearValidators();
var_dump($element->isValid(''));
print_r($element->getErrors());
Outputs
bool(false)
Array
(
[0] => isEmpty
)
There is the possibility of having an allowEmpty flag on an element. Try:
$element = $form->getElement('field1');
$element->clearValidators();
$element->setAllowEmpty(true);
var_dump($element->isValid(''));
print_r($element->getErrors());

Incorrect `update statement` using IN operator with Zend

I have a function which is wanted to execute a statement like below:
UPDATE coupon_users SET status = status | '1' WHERE id IN ('3','4')
And in coupon_users model, I wrote a method like below do to:
/**
* #param array $ids #array(3,4)
* #param array $status #1
*/
public function updateStatus(array $ids, $status)
{
$result = $this->_db->query(
"UPDATE {$this->_name} SET status = status | ? WHERE id IN (?)",
array(
$status,
$ids
)
)->execute();
return $result;
}
But the query is always:
UPDATE coupon_users SET status = status | '1' WHERE id IN ('Array')
I don't know what am I wrong here, please help me, many thanks.
According to the PDO documentation (Zend_Db uses PDO as its DB access backend):
You cannot bind multiple values to a single named parameter in, for
example, the IN() clause of an SQL statement.
So, you'll probably need to prepare a bit further your query, so that it contains as many markers as elements in the array. A possible solution could be the following:
// Compose the query
$queryToExecute = "UPDATE {$this->_name} SET status = status | ? WHERE id IN (";
$questionMarks = array();
for ($id in $ids) {
$questionMarks[] = '?';
}
$queryToExecute .= implode(',', $questionMarks);
$queryToExecute .= ')';
// $queryToExecute should have the format "UPDATE ... WHERE id IN (?,?,?,...?)"
// Execute it
$result = $this->_db->query(
$queryToExecute,
array($status, $ids)
)->execute();
Hope that helps,
try:
public function updateStatus(array $ids, $status)
{
$result = $this->_db->query(
"UPDATE {$this->_name} SET status = ? WHERE id IN (?)",
array(
$status,
implode(',',$ids)
)
)->execute();
return $result;
}
Update:
Have you tried?:
$this->_db->update($this->_name, array('status'=>$status), array('id IN (?)'=>$ids));
I haven't tested it, it also depends on what $this->_db is an instance of
http://framework.zend.com/manual/en/zend.db.adapter.html#zend.db.adapter.write.update
Try this..
public function updateStatus(array $ids, $status)
{
$inarray= implode(',',$ids);
$result = $this->_db->query(
"UPDATE {$this->_name} SET status = status | ? WHERE id IN (?)",
array(
$status,
$inarray
)
)->execute();
return $result;
}
Its working fine for me.
$existingImagesIds = array(1, 2, 3, 7);
$where = $pImgModel->getAdapter()->quoteInto("id in (?) ", $existingImagesIds);
$pImgModel->update(array('status' => '0'), $where);

SugarCRM: Create NOTE with Attachment without SOAP?

I've got this custom button on Lead Editview that when clicked on generates (via AJAX) an invoice number and a PDF bearing the same number.
In the next step, the routine uses SOAP to loopback to Sugar and creates a Note (along with the PDF as attachment).
My question is can I avoid this SOAP call and use some other internal mechanism / classes to do the same? Something along the lines of
$invoice = new Note();
$invoice->create(....);
...
Is this possible? I couldn't find any documentation anywhere... all roads seem to point to SOAP.
If your Ajax call is performing a db update/save operation, then you could look into using a after_save logic hook.
EDIT: for eg: you could try out this code, have a look at the code in <sugar_root>/modules/Notes/Note.php
$note = new Note();
$note->modified_user_id = $current_user->id;
$note->created_by = $current_user->id;
$note->name = 'New';
$note->parent_type = "Accounts";
$note->parent_id = $bean->parent_id;
$note->description = $bean->description;
$note->save();
As far as attachment goes, it's a bit tricky. Sugar expects the attachment to be a upload_file object. Have a look at the code in <sugar_root>/modules/Notes/controller.php the function action_save() and <sugar_root>/include/upload_file.php
HACK: this is not the correct way but it works. With a slight modification to the code above and cunning use of the move function , you could make the attachment work. Sugar stores the attachments in cache/upload folder with the ID of the note created.
$note->filename = "Yourfilename.txt" //your file name goes here
$note->file_mime_type = "text/plain" // your file's mime type goes here
$new_note_id = $note->save();
move(your_file_location, cache/upload/$new_note_id)
//don't add a extension to cache/upload/$new_note_id
HTH
P.S: untested code
Do this on controller.php
foreach ( $_FILES as $file ) {
for ( $i = 0 ; $i < count( $file[ 'name' ] ) ; $i++ ) {
$fileData = file_get_contents( $file[ 'tmp_name' ][ $i ] );
$fileTmpLocation = $file[ 'tmp_name' ][ $i ];
$fileMimeType = mime_content_type( $file[$i] );
$fileInfo = array( 'name' => $file[ 'name' ][ $i ], 'data' => $fileData, 'tmpLocation' =>$fileTmpLocation, 'mimeType' => $fileMimeType );
array_push( $files, $fileInfo );
}
}
$this->guardarNotas($this->bean->id,$files);
}
And this is the function to save Notes with attachment:
private function guardarNotas($case_id,$files){
foreach($files as $file){
$noteBean = BeanFactory::newBean('Notes');
$noteBean->name = $file['name'];
$noteBean->parent_type = "Cases";
$noteBean->parent_id = $case_id;
$noteBean->filename = $file["name"];
$noteBean->file_mime_type = $file["mimeType"];
$noteBean->save();
move_uploaded_file($file["tmpLocation"], "upload/".$noteBean->id);
}
}