I need to get each of the checkboxes to be pre-selected in ValidForm Builder based on their default values if they have were selected when the record initially got written to the DB Table.
Here is my code:
$objType = $objGroup->addField('locationType', 'Location Type', VFORM_CHECK_LIST,
array('required' => true),
array('required' => 'Location Type is required'),
array(
'fieldclass' => 'vf__inlineButtons',
'tip' => (($_SESSION['auth']['tips'] && $_POST['action'] != 'delete') ? VFB_TIP_LOCATIONS_LOCATIONTYPE : NULL),
(($_POST['action'] == 'delete') ? 'fieldDisabled' : 'fieldEnabled') => (($_POST['action'] == 'delete') ? 'disabled' : 'enabled'),
'default' => $default['locationType']
)
);
$objType->addField('Destination', 'D');
$objType->addField('Sales', 'S');
$objType->addField('Pickup/Dropoff', 'P');
$objType->addField('Both, Sales & Pickup/Dropoff', 'B');
Here's my trial & error attempts to get it to work:
(1) I removed the 'default' => $default['locationType'] and added 'checked' to each $objType->addField(). So In the code above, that is all 4 of the $objType->addField()'s have the 'checked' element added to them. Result is only the last checkbox gets checked -- no success.
(2) I removed the 'default' => $default['locationType'] and added 'selected' to each $objType->addField(). So In the code above, that is all 4 of the $objType->addField()'s have the 'selected' element added to them. Result is only the last checkbox gets checked -- no success.
(3) I removed the 'default' => $default['locationType'] and added 'checked' => 'checked' to each $objType->addField(). So In the code above, that is all 4 of the $objType->addField()'s have the 'checked' => 'checked' element added to them. Result is the script generates nothing -- no success.
(4) I tried this too -- 'default' => array("D", "", "P", "") -- no success.
(5) I tried this -- 'default' => array(true, false, true, false) -- all boxes get checked -- no success.
(6) I tried this -- 'default' => array("1", "0", "1", "0") -- only the first box gets checked -- no success.
(7) I tried this -- 'default' => array(1, 0, 1, 0) -- only the first box gets checked -- no success.
I am thinking that ValidForm Builder is not ready for checkboxes.
Here is the relevant VFB code (if it helps you see where the issue is). NOTE on 2/22/2014 through my extended T&E troubleshooting I do not believe this class file is even loaded or used in the production of the HTML. Continuing to dig...:
class VF_Checkbox extends VF_Element {
public function toHtml($submitted = FALSE, $blnSimpleLayout = FALSE, $blnLabel = true, $blnDisplayErrors = true) {
$blnError = ($submitted && !$this->__validator->validate() && $blnDisplayErrors) ? TRUE : FALSE;
if (!$blnSimpleLayout) {
//*** We asume that all dynamic fields greater than 0 are never required.
if ($this->__validator->getRequired()) {
$this->setMeta("class", "vf__required");
} else {
$this->setMeta("class", "vf__optional");
}
if ($blnError) $this->setMeta("class", "vf__error");
if (!$blnLabel) $this->setMeta("class", "vf__nolabel");
// Call this right before __getMetaString();
$this->setConditionalMeta();
$strOutput = "<div{$this->__getMetaString()}>\n";
if ($blnError) $strOutput .= "<p class=\"vf__error\">{$this->__validator->getError()}</p>";
if ($this->__getValue($submitted)) {
//*** Add the "checked" attribute to the input field.
$this->setFieldMeta("checked", "checked");
} else {
//*** Remove the "checked" attribute from the input field. Just to be sure it wasn't set before.
$this->setFieldMeta("checked", null, TRUE);
}
if ($blnLabel) {
$strLabel = (!empty($this->__requiredstyle) && $this->__validator->getRequired()) ? sprintf($this->__requiredstyle, $this->__label) : $this->__label;
if (!empty($this->__label)) $strOutput .= "<label for=\"{$this->__id}\"{$this->__getLabelMetaString()}>{$strLabel}</label>\n";
}
} else {
if ($blnError) $this->setMeta("class", "vf__error");
$this->setMeta("class", "vf__multifielditem");
// Call this right before __getMetaString();
$this->setConditionalMeta();
$strOutput = "<div{$this->__getMetaString()}>\n";
if ($this->__getValue($submitted)) {
//*** Add the "checked" attribute to the input field.
$this->setFieldMeta("checked", "checked");
} else {
//*** Remove the "checked" attribute from the input field. Just to be sure it wasn't set before.
$this->setFieldMeta("checked", null, TRUE);
}
}
$strOutput .= "<input type=\"checkbox\" name=\"{$this->__name}\" id=\"{$this->__id}\"{$this->__getFieldMetaString()}/>\n";
if (!empty($this->__tip)) $strOutput .= "<small class=\"vf__tip\">{$this->__tip}</small>\n";
$strOutput .= "</div>\n";
return $strOutput;
}
public function toJS() {
$strOutput = "";
$strCheck = $this->__validator->getCheck();
$strCheck = (empty($strCheck)) ? "''" : str_replace("'", "\\'", $strCheck);
$strRequired = ($this->__validator->getRequired()) ? "true" : "false";;
$intMaxLength = ($this->__validator->getMaxLength() > 0) ? $this->__validator->getMaxLength() : "null";
$intMinLength = ($this->__validator->getMinLength() > 0) ? $this->__validator->getMinLength() : "null";
$strOutput .= "objForm.addElement('{$this->__id}', '{$this->__name}', {$strCheck}, {$strRequired}, {$intMaxLength}, {$intMinLength}, '" . addslashes($this->__validator->getFieldHint()) . "', '" . addslashes($this->__validator->getTypeError()) . "', '" . addslashes($this->__validator->getRequiredError()) . "', '" . addslashes($this->__validator->getHintError()) . "', '" . addslashes($this->__validator->getMinLengthError()) . "', '" . addslashes($this->__validator->getMaxLengthError()) . "');\n";
//*** Condition logic.
$strOutput .= $this->conditionsToJs();
return $strOutput;
}
public function getValue($intDynamicPosition = 0) {
$varValue = parent::getValue($intDynamicPosition);
return (strlen($varValue) > 0 && $varValue !== 0) ? TRUE : FALSE;
}
public function getDefault($intDynamicPosition = 0) {
return (strlen($this->__default) > 0 && $this->getValue($intDynamicPosition)) ? "on" : null;
}
}
I see where the developer recently applied a fix for your issue. You can get the revised source file at http://code.google.com/p/validformbuilder/source/list
Related
I have a code:
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->addIdentifier('name')
[..]
This is a property from translation (KNP translatable). I tried use:
translations.name - label is sortable, but values are missing
name or translate.name - label is not sortable, but values are ok
I don't have any idea how I should to do this. Maybe someone here can help me?
Did you try $listMapper->add('name',null, array('sortable'=>true)) ?
Ok, I made it.
1) Create abstract admin class:
use Sonata\AdminBundle\Admin\AbstractAdmin as BaseAbstractAdmin;
abstract class AbstractAdmin extends BaseAbstractAdmin { .. }
2) Use this class in your admin classes:
class UserAdmin extends AbstractAdmin { .. }
3) Add this to your column definition:
->add(
'fieldName',
null,
[
'sortable' => true,
'sort_field_mapping' => ['fieldName' => 'id'],
'sort_parent_association_mappings' => [],
]
)
4) Add this method to your abstract admin class:
protected function prepareQueryForTranslatableColumns($query)
{
$currentAlias = $query->getRootAliases()[0];
$locale = $this->request->getLocale();
$parameters = $this->getFilterParameters();
$sortBy = $parameters['_sort_by'];
$fieldDescription = $this->getListFieldDescription($sortBy);
$mapping = $fieldDescription->getAssociationMapping();
$entityClass = $mapping['targetEntity'] ?: $this->getClass();
if ($mapping) {
$mappings = $fieldDescription->getParentAssociationMappings();
$mappings[] = $mapping;
foreach ($mappings as $parentMapping) {
$fieldName = $parentMapping['fieldName'];
$query->leftJoin($currentAlias . '.' . $fieldName, $fieldName);
$currentAlias = $fieldName;
}
}
$query
->leftJoin(
$currentAlias . '.translations',
'tr',
'with',
'tr.locale = :lang OR
(NOT EXISTS(SELECT t.id FROM ' . $entityClass . 'Translation t WHERE t.translatable = tr.translatable AND t.locale = :lang)
AND tr.locale = :lang_default)'
)
->addOrderBy('tr.name', $parameters['_sort_order'])
->setParameter(':lang', $locale)
->setParameter(':lang_default', 'en');
return $query;
}
I use JOIN to get translations for currently selected locale and, if translation doesn't exist yet for current locale, I add translation for default locale (it is a reason for use NOT EXIST).
5) Add this method to your admin class:
public function createQuery($context = 'list')
{
$query = parent::createQuery($context);
if ('list' === $context) {
$parameters = $this->getFilterParameters();
$sortBy = $parameters['_sort_by'];
if (in_array($sortBy, ['fieldName', 'fieldName.fieldName2', 'fieldName3', ..])) {
$query = parent::prepareQueryForTranslatableColumns($query);
}
}
return $query;
}
Late answer but I was having the same problem.
The easiest solution for me was to set the right property mapping like this:
$listMapper->add(
'translations',
null,
[
'sortable' => true,
'associated_property' => 'name',
'sort_field_mapping' => [
'fieldName' => 'name',
],
'sort_parent_association_mappings' => [
['fieldName' => 'translations'],
],
]
);
I'm trying to get the data in the array that came from another function(that function is extracting the data in the csv file) and when i tried calling the two fields from that array it shows an error that it is unidentified variables.
The $this->csv_process(); as shown on the function action() is the function that extracts the data from the csv file and stores it in an array which is successful since I tried checking it on var_dump();
I also named the two fields as $name and $email as shown below:
Function CSV_process()
public function csv_process()
{
/* variables for openning the csv file */
if (!in_array($extension, $allowed_ext)) {
$this->session->set_flashdata("message", "Sorry, CSV file only.");
} else {
if ($filesize > 0) {
$file = fopen($filename, "r");
$toWrite = array();
$error = false;
$col_size = 2;
$skip = 0;
while ($data = fgetcsv($file, 10000, ","))
{
$skip++;
if ($skip == 1) {
continue;
}
$numofcol = count($data);
if ($numofcol != $col_size ) {
$this->session->set_flashdata("message", "Column count exceeded or missing.");
} else {
$name1 = $data[0];
$name = str_replace("'", "''", $name1);
$email1 = $data[1];
$email = str_replace("'", "''", $email1);
$toWrite[] = [
'name' => $name,
'email' => $email
];
}
}
}
}
return $toWrite;
}
Function Action()
function action(){
$toWrite[] = $this->csv_process();
foreach ($toWrite as $arr) {
list($name, $email) = $arr;
//die(var_dump($arr));
$query = $this->db->query("SELECT * FROM import WHERE name ='$name' AND email = '$email'");
if ($query->num_rows() >= 1) {
} else {
if ($name == "" OR $email == "") {
} else {
if ((filter_var($email, FILTER_VALIDATE_EMAIL)) == FALSE ) {
} else {
$this->db->query("INSERT INTO import(name, email, created_date) VALUES('".$name."', '".$email."', '".date("Y-m-d h-i-s")."')");
$this->session->set_flashdata('message', 'SUCCESS YEAY');
redirect('Clean_csv/index');
}
}
}
$query->free_result();
}
}
Listing arrays doesn't seem to work for here, anyone knows how to extract the data array from $arr?
You don't need to extract the values. You can use each $arr in a bound query. It simplifies the syntax for the select query.
For inserting use CodeIgniter's insert() method. Again, the $arr can be used directly by adding the date to it before the insert is attempted.
I think this will work.
function action()
{
$toWrite[] = $this->csv_process();
foreach($toWrite as $arr)
{
$query = $this->db->query("SELECT * FROM import WHERE name=? AND email=?", $arr);
if($query->num_rows() >= 1)
{}
else
{
if($arr['name'] == "" OR $arr['email'] == "")
{}
else
{
if((filter_var($email, FILTER_VALIDATE_EMAIL)) == FALSE)
{}
else
{
$arr['created_date'] = date("Y-m-d h-i-s");
$this->db->insert("import", $arr);
$this->session->set_flashdata('message', 'SUCCESS YEAY');
//??? redirect('Clean_csv/index');
//Are you sure, you may still have more $arr in $toWrite to process - right?
}
}
}
$query->free_result();
}
}
You need to know what a terrible idea it is to repeatedly run database queries inside a loop. Even though you use free_result() it could be a massive drain on server resources. If your csv file has several thousand items you are severely stressing the database and the server.
How can i keep the selected checkboxes checked, after the form submit on error? Now, i get this error message: Warning: in_array() expects parameter 2 to be array, string given in
Thanks!
function runsql_array($sql)
{
global $kapcs;
$res = mysqli_query($kapcs , $sql) or die(mysqli_error( $kapcs));
if (mysqli_num_rows($res) == 0)
{
return array();
}
else
{
$out = array();
while ($a = mysqli_fetch_assoc($res))
{
$out[] = $a;
}
return $out;
}
}
<td>
<?php
$ertek = isset($_POST["termek_tul_tipusok"]) ? $_POST["termek_tul_tipusok"] : '' ;
$values = runsql_array("SELECT termek_tipus_id, termek_tipus_nev FROM termek_tipusok WHERE termek_tipus_status = 1
ORDER BY termek_tipus_nev ASC");
foreach($values as $val=>$szoveg)
{
$checked = in_array($val, $ertek) ? ' checked ' : '' ;
echo '<div style="margin:4px 0;"><label style="cursor:pointer;" for="tulajdonsag-'.$val.'">';
echo '<input id="tulajdonsag-'.$val.'" type="checkbox" name="termek_tul_tipusok[]" '.$checked.' value="'.$val.'" />';
echo $szoveg['termek_tipus_nev'];
echo '</label></div>';
}
?>
</td>
the line
$ertek = isset($_POST["termek_tul_tipusok"]) ? $_POST["termek_tul_tipusok"] : '' ;
defines $ertek. And in the case that the post dosnt exist its defined as an empty string. You should use an empty Array.
$ertek = isset($_POST["termek_tul_tipusok"]) ? $_POST["termek_tul_tipusok"] : array() ;
I'm writing my own parser log for CakePHP.
I only need one thing: that is not written a log "message" (as a string), but a serialized array with various log information (date, type, line, stack traces, etc.).
But I don't understand what method/class I should rewrite, although I have consulted the APIs. Can you help me?
EDIT:
For now I do the opposite: I read the logs (already written) and I transform them into an array with a regex.
My code:
$logs = array_map(function($log) {
preg_match('/^'.
'([\d\-]+\s[\d:]+)\s(Error: Fatal Error|Error|Notice: Notice|Warning: Warning)(\s\(\d+\))?:\s([^\n]+)\n'.
'(Exception Attributes:\s((.(?!Request|Referer|Stack|Trace))+)\n)?'.
'(Request URL:\s([^\n]+)\n)?'.
'(Referer URL:\s([^\n]+)\n)?'.
'(Stack Trace:\n(.+))?'.
'(Trace:\n(.+))?(.+)?'.
'/si', $log, $matches);
switch($matches[2]) {
case 'Error: Fatal Error':
$type = 'fatal';
break;
case 'Error':
$type = 'error';
break;
case 'Notice: Notice':
$type = 'notice';
break;
case 'Warning: Warning':
$type = 'warning';
break;
default:
$type = 'unknown';
break;
}
return (object) af([
'datetime' => \Cake\I18n\FrozenTime::parse($matches[1]),
'type' => $type,
'error' => $matches[4],
'attributes' => empty($matches[6]) ? NULL : $matches[6],
'url' => empty($matches[9]) ? NULL : $matches[9],
'referer' => empty($matches[11]) ? NULL : $matches[11],
'stack_trace' => empty($matches[13]) ? (empty($matches[16]) ? NULL : $matches[16]) : $matches[13],
'trace' => empty($matches[15]) ? NULL : $matches[15]
]);
}, af(preg_split('/[\r\n]{2,}/', $logs)));
For now I do the opposite: I read the logs (already written) and with a regex I transform them into an array.
The problem is this is terribly expensive. and that it would be better to do the opposite: to write directly to the logs as a serialized array.
I think what you want to do is write your own LogAdapter.
You simply create a class ArrayLog (extends BaseLog) as mentioned in the docs and configure cakePHP to use it. Within the log function you append the information like $level, $message and $context to a file as an array. This will result in a log file with several arrays that then can be split.
That being said, I would suggest to log to the database and read it out instead of parsing.
Ok, that's it!
(note that this code is absolutely experimental, I have yet to test it properly)
One interesting thing that I want to do: for each log, write to the serialized file and also simultaneously in a plan file.
This allows me either to read logs as a plain text file, or they can be manipulated using the serialized file.
use Cake\Log\Engine\FileLog;
class SerializedLog extends FileLog {
protected function _getLogAsArray($level, $message) {
$serialized['level'] = $level;
$serialized['datetime'] = date('Y-m-d H:i:s');
//Sets exception type and message
if(preg_match('/^(\[([^\]]+)\]\s)?(.+)/', $message, $matches)) {
if(!empty($matches[2]))
$serialized['exception'] = $matches[2];
$serialized['message'] = $matches[3];
}
//Sets the exception attributes
if(preg_match('/Exception Attributes:\s((.(?!Request URL|Referer URL|Stack Trace|Trace))+)/is', $message, $matches)) {
$serialized['attributes'] = $matches[1];
}
//Sets the request URL
if(preg_match('/^Request URL:\s(.+)$/mi', $message, $matches)) {
$serialized['request'] = $matches[1];
}
//Sets the referer URL
if(preg_match('/^Referer URL:\s(.+)$/mi', $message, $matches)) {
$serialized['referer'] = $matches[1];
}
//Sets the trace
if(preg_match('/(Stack )?Trace:\n(.+)$/is', $message, $matches)) {
$serialized['trace'] = $matches[2];
}
$serialized['full'] = date('Y-m-d H:i:s').' '.ucfirst($level).': '.$message;
return (object) $serialized;
}
public function log($level, $message, array $context = []) {
$message = $this->_format(trim($message), $context);
$filename = $this->_getFilename($level);
if (!empty($this->_size)) {
$this->_rotateFile($filename);
}
$pathname = $this->_path . $filename;
$mask = $this->_config['mask'];
//Gets the content of the existing logs and unserializes
$logs = #unserialize(#file_get_contents($pathname));
if(empty($logs) || !is_array($logs))
$logs = [];
//Adds the current log
$logs[] = $this->_getLogAsArray($level, $message);
//Serializes logs
$output = serialize($logs);
if (empty($mask)) {
return file_put_contents($pathname, $output);
}
$exists = file_exists($pathname);
$result = file_put_contents($pathname, $output);
static $selfError = false;
if (!$selfError && !$exists && !chmod($pathname, (int)$mask)) {
$selfError = true;
trigger_error(vsprintf(
'Could not apply permission mask "%s" on log file "%s"',
[$mask, $pathname]
), E_USER_WARNING);
$selfError = false;
}
return $result;
}
}
I am using the csvUpload behavior of the Utils plugin by CakeDC, on a CakePHP 2.2.1 install.
I have it working great it's processing a rather large csv successfully. However there are two fields in my table / Model that would be considered fixed, as they are based on ID's from from associated models that are not consistent. So I need to get these fixed values via variables which is easy enough.
So my question is, how do I use the fixed fields aspect of csvUpload? I have tried that following and many little variation, which obviously didn't work.
public function upload_csv($Id = null) {
$unique_add = 69;
if ( $this->request->is('POST') ) {
$records_count = $this->Model->find( 'count' );
try {
$fixed = array('Model' => array('random_id' => $Id, 'unique_add' => $unique_add));
$this->Model->importCSV($this->request->data['Model']['CsvFile']['tmp_name'], $fixed);
} catch (Exception $e) {
$import_errors = $this->Model->getImportErrors();
$this->set( 'import_errors', $import_errors );
$this->Session->setFlash( __('Error Importing') . ' ' . $this->request->data['Model']['CsvFile']['name'] . ', ' . __('column name mismatch.') );
$this->redirect( array('action'=>'import') );
}
$new_records_count = $this->Model->find( 'count' ) - $records_count;
$this->Session->setFlash(__('Successfully imported') . ' ' . $new_records_count . ' records from ' . $this->request->data['Model']['CsvFile']['name'] );
$this->redirect(array('plugin'=>'usermgmt', 'controller'=>'users', 'action'=>'dashboard'));
}
}
Any help would be greatly appreciated as I have only found 1 post concerning this behavior when I searching...
I made my custom method to achieve the same task. Define the following method in app\Plugin\Utils\Model\Behavior
public function getCSVData(Model &$Model, $file, $fixed = array())
{
$settings = array(
'delimiter' => ',',
'enclosure' => '"',
'hasHeader' => true
);
$this->setup($Model, $settings);
$handle = new SplFileObject($file, 'rb');
$header = $this->_getHeader($Model, $handle);
$db = $Model->getDataSource();
$db->begin($Model);
$saved = array();
$data = array();
$i = 0;
while (($row = $this->_getCSVLine($Model, $handle)) !== false)
{
foreach ($header as $k => $col)
{
// get the data field from Model.field
$col = str_replace('.', '-', trim($col));
if (strpos($col, '.') !== false)
{
list($model,$field) = explode('.', $col);
$data[$i][$model][$field] = (isset($row[$k])) ? $row[$k] : '';
}
else
{
$col = str_replace(' ','_', $col);
$data[$i][$Model->alias][$col] = (isset($row[$k])) ? $row[$k] : '';
}
}
$is_valid_row = false;
foreach($data[$i][$Model->alias] as $col => $value )
{
if(!empty($data[$i][$Model->alias][$col]))
{
$is_valid_row = true;
}
}
if($is_valid_row == true)
{
$i++;
$data = Set::merge($data, $fixed);
}
else
{
unset($data[$i]);
}
}
return $data;
}
And you can use it using:
$csv_data = $this->Model->getCSVData($this->request->data['Model']['CsvFile']['tmp_name'], $fixed);
Here $csv_data will contain an array of all of those records from the csv file which are not empty and with the fixed field in each record index.
So as I was telling Arun, I answered my own question and figured it out. I was looking to broad instead of really examining what was in front of me. I started running some debugging and figured it out.
First of all, $unique_add = 69 is seen as an int, duh. In order for it to be added to the csv it need to viewed as a string. So it simply becomes, $unique_add = '69'.
I couldn't enter the value of $Id directly into the fixed array. So I just had to perform a simple find to get the value I needed.
$needed_id = $this->Model->find('first', array(
'condition'=>array('Model.id'=>$Id)
)
);
$random_id = $needed_id['Model']['id'];
Hopefully this won't be needed to help anyone because hopefully no one else will make this silly mistake. But one plus... Now there's actually more than one post on the internet documenting the use of fixed fields in the CakeDC Utils plugin.