NEW Enhance function setValueFrom so we can use it for "edit in form"

feature.
This commit is contained in:
Laurent Destailleur 2016-09-12 20:00:47 +02:00
parent efb05fda6a
commit 5c1127fc54
3 changed files with 71 additions and 79 deletions

View File

@ -1215,24 +1215,30 @@ abstract class CommonObject
}
/**
* Setter generic. Update a specific field into database
* Setter generic. Update a specific field into database.
* Warning: Trigger is run only if param trigkey is provided
*
* @param string $field Field to update
* @param mixed $value New value
* @param string $table To force other table element or element line (should not be used)
* @param int $id To force other object id (should not be used)
* @param string $format Data format ('text', 'date'). 'text' is used if not defined
* @param string $id_field To force rowid field name. 'rowid' is used it not defined
* @param string $id_field To force rowid field name. 'rowid' is used if not defined
* @param User|string $user Update last update fields also if user object provided
* @param string $trigkey Trigger key to run (in most cases something like 'XXX_MODIFY')
* @return int <0 if KO, >0 if OK
*/
function setValueFrom($field, $value, $table='', $id=null, $format='', $id_field='', $user='')
function setValueFrom($field, $value, $table='', $id=null, $format='', $id_field='', $user='', $trigkey='')
{
if (empty($table)) $table=$this->table_element;
if (empty($id)) $id=$this->id;
if (empty($format)) $format='text';
if (empty($id_field)) $id_field='rowid';
global $user,$langs,$conf;
if (empty($table)) $table=$this->table_element;
if (empty($id)) $id=$this->id;
if (empty($format)) $format='text';
if (empty($id_field)) $id_field='rowid';
$error=0;
$this->db->begin();
// Special case
@ -1240,7 +1246,7 @@ abstract class CommonObject
$sql = "UPDATE ".MAIN_DB_PREFIX.$table." SET ";
if ($format == 'text') $sql.= $field." = '".$this->db->escape($value)."'";
else if ($format == 'date') $sql.= $field." = '".$this->db->idate($value)."'";
else if ($format == 'date') $sql.= $field." = ".($value ? "'".$this->db->idate($value)."'" : "null");
if (is_object($user)) $sql.=", fk_user_modif = ".$user->id;
$sql.= " WHERE ".$id_field." = ".$id;
@ -1248,8 +1254,23 @@ abstract class CommonObject
$resql = $this->db->query($sql);
if ($resql)
{
$this->db->commit();
return 1;
if ($trigkey)
{
$result=$this->call_trigger($trigkey, $user); // This may set this->errors
if ($result < 0) $error++;
}
if (! $error)
{
if (property_exists($this, $field)) $this->$field = $value;
$this->db->commit();
return 1;
}
else
{
$this->db->rollback();
return -2;
}
}
else
{

View File

@ -122,7 +122,6 @@ class Productlot extends CommonObject
// Insert request
$sql = 'INSERT INTO ' . MAIN_DB_PREFIX . $this->table_element . '(';
$sql.= 'entity,';
$sql.= 'fk_product,';
$sql.= 'batch,';
@ -132,10 +131,7 @@ class Productlot extends CommonObject
$sql.= 'fk_user_creat,';
$sql.= 'fk_user_modif,';
$sql.= 'import_key';
$sql .= ') VALUES (';
$sql .= ' '.(! isset($this->entity)?'NULL':$this->entity).',';
$sql .= ' '.(! isset($this->fk_product)?'NULL':$this->fk_product).',';
$sql .= ' '.(! isset($this->batch)?'NULL':"'".$this->db->escape($this->batch)."'").',';
@ -145,8 +141,6 @@ class Productlot extends CommonObject
$sql .= ' '.(! isset($this->fk_user_creat)?'NULL':$this->fk_user_creat).',';
$sql .= ' '.(! isset($this->fk_user_modif)?'NULL':$this->fk_user_modif).',';
$sql .= ' '.(! isset($this->import_key)?'NULL':$this->import_key);
$sql .= ')';
$this->db->begin();
@ -165,10 +159,10 @@ class Productlot extends CommonObject
// Uncomment this and change MYOBJECT to your own tag if you
// want this action to call a trigger.
//// Call triggers
//$result=$this->call_trigger('MYOBJECT_CREATE',$user);
//if ($result < 0) $error++;
//// End call triggers
// Call triggers
$result=$this->call_trigger('PRODUCTLOT_CREATE',$user);
if ($result < 0) $error++;
// End call triggers
}
}
@ -333,8 +327,6 @@ class Productlot extends CommonObject
$line->fk_user_modif = $obj->fk_user_modif;
$line->import_key = $obj->import_key;
$this->lines[$line->id] = $line;
}
$this->db->free($resql);
@ -383,14 +375,11 @@ class Productlot extends CommonObject
$this->import_key = trim($this->import_key);
}
// Check parameters
// Put here code to add a control on parameters values
// Update request
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this->table_element . ' SET';
$sql .= ' entity = '.(isset($this->entity)?$this->entity:"null").',';
$sql .= ' fk_product = '.(isset($this->fk_product)?$this->fk_product:"null").',';
$sql .= ' batch = '.(isset($this->batch)?"'".$this->db->escape($this->batch)."'":"null").',';
@ -401,8 +390,6 @@ class Productlot extends CommonObject
$sql .= ' fk_user_creat = '.(isset($this->fk_user_creat)?$this->fk_user_creat:"null").',';
$sql .= ' fk_user_modif = '.(isset($this->fk_user_modif)?$this->fk_user_modif:"null").',';
$sql .= ' import_key = '.(isset($this->import_key)?$this->import_key:"null");
$sql .= ' WHERE rowid=' . $this->id;
$this->db->begin();
@ -418,10 +405,10 @@ class Productlot extends CommonObject
// Uncomment this and change MYOBJECT to your own tag if you
// want this action calls a trigger.
//// Call triggers
//$result=$this->call_trigger('MYOBJECT_MODIFY',$user);
//if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
//// End call triggers
// Call triggers
$result=$this->call_trigger('PRODUCTLOT_MODIFY',$user);
if ($result < 0) { $error++; }
// End call triggers
}
// Commit or rollback

View File

@ -106,7 +106,21 @@ if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'e
if (empty($reshook))
{
if ($action == 'update_extras')
if ($action == 'seteatby' && $user->rights->stock->creer)
{
$newvalue = dol_mktime(12, 0, 0, $_POST['eatbymonth'], $_POST['eatbyday'], $_POST['eatbyyear']);
$result = $object->setValueFrom('eatby', $newvalue, '', null, 'date', '', $user, 'PRODUCTLOT_MODIFY');
if ($result < 0) dol_print_error($db, $object->error);
}
if ($action == 'setsellby' && $user->rights->stock->creer)
{
$newvalue=dol_mktime(12, 0, 0, $_POST['sellbymonth'], $_POST['sellbyday'], $_POST['sellbyyear']);
$result = $object->setValueFrom('sellby', $newvalue, '', null, 'date', '', $user, 'PRODUCTLOT_MODIFY');
if ($result < 0) dol_print_error($db, $object->error);
}
if ($action == 'update_extras')
{
// Fill array 'array_options' with data from update form
$extralabels = $extrafields->fetch_name_optionals_label($object->table_element);
@ -154,8 +168,6 @@ if (empty($reshook))
$object->fk_user_modif=GETPOST('fk_user_modif','int');
$object->import_key=GETPOST('import_key','int');
if (empty($object->ref))
{
$error++;
@ -307,49 +319,6 @@ if ($action == 'create')
}
// Part to edit record
if (($id || $ref) && $action == 'edit')
{
print load_fiche_titre($langs->trans("Batch"));
print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
print '<input type="hidden" name="action" value="update">';
print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
print '<input type="hidden" name="id" value="'.$object->id.'">';
dol_fiche_head();
print '<table class="border centpercent">'."\n";
// print '<tr><td class="fieldrequired">'.$langs->trans("Label").'</td><td><input class="flat" type="text" size="36" name="label" value="'.$label.'"></td></tr>';
//
print '<tr><td class="titlefield">'.$langs->trans("Batch").'</td><td>'.$object->batch.'</td></tr>';
print '<tr><td>'.$langs->trans("Product").'</td><td>';
$producttmp = new Product($db);
$producttmp->fetch($object->fk_product);
print $producttmp->getNomUrl(1, 'stock');
print '</td></tr>';
print '<tr><td>'.$langs->trans("Eatby").'</td><td>'.$object->eatby.'</td></tr>';
print '<tr><td>'.$langs->trans("Sellby").'</td><td>'.$object->sellby.'</td></tr>';
// Other attributes
$cols = 2;
include DOL_DOCUMENT_ROOT . '/core/tpl/extrafields_view.tpl.php';
print '</table>';
dol_fiche_end();
print '<div class="center"><input type="submit" class="button" name="save" value="'.$langs->trans("Save").'">';
print ' &nbsp; <input type="submit" class="button" name="cancel" value="'.$langs->trans("Cancel").'">';
print '</div>';
print '</form>';
}
// Part to show record
if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'create')))
{
@ -377,14 +346,29 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea
print '</td>';
print '</tr>';
// Product
print '<tr><td>'.$langs->trans("Product").'</td><td>';
$producttmp = new Product($db);
$producttmp->fetch($object->fk_product);
print $producttmp->getNomUrl(1, 'stock');
print '</td></tr>';
print '<tr><td>'.$langs->trans("Eatby").'</td><td>'.$object->eatby.'</td></tr>';
print '<tr><td>'.$langs->trans("Sellby").'</td><td>'.$object->sellby.'</td></tr>';
// Eat by
print '<tr><td>';
print $form->editfieldkey($langs->trans('Eatby'), 'eatby', $object->eatby, $object, $user->rights->stock->creer, 'datepicker');
print '</td><td colspan="5">';
print $form->editfieldval($langs->trans('Eatby'), 'eatby', $object->eatby, $object, $user->rights->stock->creer, 'datepicker');
print '</td>';
print '</tr>';
// Sell by
print '<tr><td>';
print $form->editfieldkey($langs->trans('Sellby'), 'sellby', $object->sellby, $object, $user->rights->stock->creer, 'datepicker');
print '</td><td colspan="5">';
print $form->editfieldval($langs->trans('Sellby'), 'sellby', $object->sellby, $object, $user->rights->stock->creer, 'datepicker');
print '</td>';
print '</tr>';
// Other attributes
$cols = 2;
include DOL_DOCUMENT_ROOT . '/core/tpl/extrafields_view.tpl.php';