Fix error management into modulebuilder

This commit is contained in:
Laurent Destailleur 2017-07-12 13:25:18 +02:00
parent 40b3ae2a15
commit 33ceb22b8b
6 changed files with 372 additions and 594 deletions

View File

@ -4726,6 +4726,9 @@ abstract class CommonObject
return $buyPrice; return $buyPrice;
} }
/** /**
* Function test if type is date * Function test if type is date
* *
@ -4734,7 +4737,7 @@ abstract class CommonObject
*/ */
protected function isDate($info) protected function isDate($info)
{ {
if(isset($info['type']) && $info['type']=='date') return true; if(isset($info['type']) && ($info['type']=='date' || $info['type']=='datetime' || $info['type']=='timestamp')) return true;
else return false; else return false;
} }
@ -4880,40 +4883,6 @@ abstract class CommonObject
return $query; return $query;
} }
/**
* Create object into database
*
* @param User $user User that creates
* @param bool $notrigger false=launch triggers after, true=disable triggers
*
* @return int <0 if KO, Id of created object if OK
*/
public function createCommon(User $user, $notrigger = false)
{
$fields = array_merge(array('datec'=>$this->db->idate(dol_now())), $this->set_save_query());
foreach ($fields as $k => $v) {
$keys[] = $k;
$values[] = $this->quote($v);
}
$sql = 'INSERT INTO '.MAIN_DB_PREFIX.$this->table_element.'
( '.implode( ",", $keys ).' )
VALUES ( '.implode( ",", $values ).' ) ';
$res = $this->db->query( $sql );
if($res===false) {
return false;
}
// TODO Add triggers
return true;
}
/** /**
* Function to load data into current object this * Function to load data into current object this
* *
@ -4967,17 +4936,130 @@ abstract class CommonObject
return implode(',', $keys); return implode(',', $keys);
} }
/**
* Add quote to field value if necessary
*
* @param string|int $value value to protect
* @return string|int
*/
protected function quote($value) {
if(is_null($value)) return 'NULL';
else if(is_numeric($value)) return $value;
else return "'".$this->db->escape( $value )."'";
}
/**
* Create object into database
*
* @param User $user User that creates
* @param bool $notrigger false=launch triggers after, true=disable triggers
* @return int <0 if KO, Id of created object if OK
*/
public function createCommon(User $user, $notrigger = false)
{
$error = 0;
$fields = array_merge(array('datec'=>$this->db->idate(dol_now())), $this->set_save_query());
foreach ($fields as $k => $v) {
$keys[] = $k;
$values[] = $this->quote($v);
}
$this->db->begin();
if (! $error)
{
$sql = 'INSERT INTO '.MAIN_DB_PREFIX.$this->table_element.'
( '.implode( ",", $keys ).' )
VALUES ( '.implode( ",", $values ).' ) ';
$res = $this->db->query( $sql );
if ($res===false) {
$error++;
$this->errors[] = $this->db->lasterror();
}
}
if (! $error && ! $notrigger) {
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . $this->table_element);
if (!$notrigger) {
// Call triggers
$result=$this->call_trigger(strtoupper(get_class(self)).'_CREATE',$user);
if ($result < 0) { $error++; }
// End call triggers
}
}
// Commit or rollback
if ($error) {
$this->db->rollback();
return -1;
} else {
$this->db->commit();
return $this->id;
}
}
/**
* Load an object from its id and create a new one in database
*
* @param User $user User that creates
* @param int $fromid Id of object to clone
* @return int New id of clone
*/
public function createFromCloneCommon(User $user, $fromid)
{
global $user;
$error = 0;
dol_syslog(__METHOD__, LOG_DEBUG);
$object = new self($this->db);
$this->db->begin();
// Load source object
$object->fetchCommon($fromid);
// Reset object
$object->id = 0;
// Clear fields
// ...
// Create clone
$result = $object->createCommon($user);
// Other options
if ($result < 0) {
$error ++;
$this->errors = $object->errors;
dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR);
}
// End
if (!$error) {
$this->db->commit();
return $object->id;
} else {
$this->db->rollback();
return -1;
}
}
/** /**
* Load object in memory from the database * Load object in memory from the database
* *
* @param int $id Id object * @param int $id Id object
* @param string $ref Ref * @param string $ref Ref
*
* @return int <0 if KO, 0 if not found, >0 if OK * @return int <0 if KO, 0 if not found, >0 if OK
*/ */
public function fetchCommon($id, $ref = null) public function fetchCommon($id, $ref = null)
{ {
if (empty($id) && empty($ref)) return false; if (empty($id) && empty($ref)) return false;
$sql = 'SELECT '.$this->get_field_list().', datec, tms'; $sql = 'SELECT '.$this->get_field_list().', datec, tms';
@ -4990,6 +5072,8 @@ abstract class CommonObject
if ($res) if ($res)
{ {
if ($obj = $this->db->fetch_object($res)) if ($obj = $this->db->fetch_object($res))
{
if ($obj)
{ {
$this->id = $id; $this->id = $id;
$this->set_vars_by_db($obj); $this->set_vars_by_db($obj);
@ -5000,6 +5084,11 @@ abstract class CommonObject
return $this->id; return $this->id;
} }
else else
{
return 0;
}
}
else
{ {
$this->error = $this->db->lasterror(); $this->error = $this->db->lasterror();
$this->errors[] = $this->error; $this->errors[] = $this->error;
@ -5019,15 +5108,15 @@ abstract class CommonObject
* *
* @param User $user User that modifies * @param User $user User that modifies
* @param bool $notrigger false=launch triggers after, true=disable triggers * @param bool $notrigger false=launch triggers after, true=disable triggers
*
* @return int <0 if KO, >0 if OK * @return int <0 if KO, >0 if OK
*/ */
public function updateCommon(User $user, $notrigger = false) public function updateCommon(User $user, $notrigger = false)
{ {
$error = 0;
$fields = $this->set_save_query(); $fields = $this->set_save_query();
foreach ($fields as $k => $v) { foreach ($fields as $k => $v) {
if (is_array($key)){ if (is_array($key)){
$i=array_search($k, $key); $i=array_search($k, $key);
if ( $i !== false) { if ( $i !== false) {
@ -5040,20 +5129,37 @@ abstract class CommonObject
continue; continue;
} }
} }
$tmp[] = $k.'='.$this->quote($v); $tmp[] = $k.'='.$this->quote($v);
} }
$sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element.' SET '.implode( ',', $tmp ).' WHERE rowid='.$this->id ; $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element.' SET '.implode( ',', $tmp ).' WHERE rowid='.$this->id ;
$res = $this->db->query( $sql );
if($res===false) { $this->db->begin();
//error
return false; if (! $error)
{
$res = $this->db->query($sql);
if ($res===false)
{
$error++;
$this->errors[] = $this->db->lasterror();
}
} }
// TODO Add triggers if (! $error && ! $notrigger) {
// Call triggers
$result=$this->call_trigger(strtoupper(get_class(self)).'_MODIFY',$user);
if ($result < 0) { $error++; } //Do also here what you must do to rollback action if trigger fail
// End call triggers
}
return true; // Commit or rollback
if ($error) {
$this->db->rollback();
return -1;
} else {
$this->db->commit();
return $this->id;
}
} }
/** /**
@ -5061,36 +5167,55 @@ abstract class CommonObject
* *
* @param User $user User that deletes * @param User $user User that deletes
* @param bool $notrigger false=launch triggers after, true=disable triggers * @param bool $notrigger false=launch triggers after, true=disable triggers
*
* @return int <0 if KO, >0 if OK * @return int <0 if KO, >0 if OK
*/ */
public function deleteCommon(User $user, $notrigger = false) public function deleteCommon(User $user, $notrigger = false)
{
$error=0;
$this->db->begin();
if (! $error) {
if (! $notrigger) {
// Call triggers
$result=$this->call_trigger(strtoupper(get_class(self)).'_DELETE', $user);
if ($result < 0) { $error++; } // Do also here what you must do to rollback action if trigger fail
// End call triggers
}
}
if (! $error)
{ {
$sql = 'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element.' WHERE rowid='.$this->id; $sql = 'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element.' WHERE rowid='.$this->id;
$res = $this->db->query($sql); $res = $this->db->query($sql);
if($res===false) { if($res===false) {
return false; $error++;
$this->errors[] = $this->db->lasterror();
}
} }
// TODO Add triggers // Commit or rollback
if ($error) {
return true; $this->db->rollback();
return -1;
} else {
$this->db->commit();
return 1;
}
} }
/** /**
* Add quote to field value if necessary * Initialise object with example values
* Id must be 0 if object instance is a specimen
* *
* @param string|int $value value to protect * @return void
* @return string|int
*/ */
protected function quote($value) { public function initAsSpecimenCommon()
{
if(is_null($value)) return 'NULL'; $this->id = 0;
else if(is_numeric($value)) return $value;
else return "'".$this->db->escape( $value )."'";
// TODO...
} }
} }

View File

@ -55,14 +55,11 @@ function rebuildObjectClass($destdir, $module, $objectname, $newmask)
{ {
include_once $pathoffiletoeditsrc; include_once $pathoffiletoeditsrc;
if (class_exists($objectname)) $object=new $objectname($db); if (class_exists($objectname)) $object=new $objectname($db);
}
catch(Exception $e) // Backup old file
{ dol_copy($pathoffiletoeditsrc, $pathoffiletoeditsrc.'.back', $newmask, 1);
print $e->getMessage();
}
// Edit class files // Edit class files
$contentclass = file_get_contents(dol_osencode($pathoffiletoeditsrc), 'r'); $contentclass = file_get_contents(dol_osencode($pathoffiletoeditsrc), 'r');
$i=0; $i=0;
@ -114,9 +111,14 @@ function rebuildObjectClass($destdir, $module, $objectname, $newmask)
file_put_contents(dol_osencode($pathoffiletoedittarget), $contentclass); file_put_contents(dol_osencode($pathoffiletoedittarget), $contentclass);
@chmod($pathoffiletoedit, octdec($newmask)); @chmod($pathoffiletoedit, octdec($newmask));
return 1; return 1;
} }
catch(Exception $e)
{
print $e->getMessage();
return -1;
}
}
/** /**
* Save data into a memory area shared by all users, all sessions on server * Save data into a memory area shared by all users, all sessions on server

View File

@ -197,6 +197,7 @@ Parameter=Parameter
Parameters=Parameters Parameters=Parameters
Value=Value Value=Value
PersonalValue=Personal value PersonalValue=Personal value
NewObject=New %s
NewValue=New value NewValue=New value
CurrentValue=Current value CurrentValue=Current value
Code=Code Code=Code

View File

@ -109,366 +109,6 @@ class MyObject extends CommonObject
$this->db = $db; $this->db = $db;
} }
/**
* Create object into database
*
* @param User $user User that creates
* @param bool $notrigger false=launch triggers after, true=disable triggers
*
* @return int <0 if KO, Id of created object if OK
*/
public function create(User $user, $notrigger = false)
{
dol_syslog(__METHOD__, LOG_DEBUG);
$error = 0;
// Clean parameters
if (isset($this->prop1)) {
$this->prop1 = trim($this->prop1);
}
if (isset($this->prop2)) {
$this->prop2 = trim($this->prop2);
}
//...
// Check parameters
// Put here code to add control on parameters values
// Insert request
$sql = 'INSERT INTO ' . MAIN_DB_PREFIX . $this->table_element . '(';
$sql .= ' field1,';
$sql .= ' field2';
//...
$sql .= ') VALUES (';
$sql .= ' \'' . $this->prop1 . '\',';
$sql .= ' \'' . $this->prop2 . '\'';
//...
$sql .= ')';
$this->db->begin();
$resql = $this->db->query($sql);
if (!$resql) {
$error ++;
$this->errors[] = 'Error ' . $this->db->lasterror();
dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR);
}
if (!$error) {
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . $this->table_element);
if (!$notrigger) {
// 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
}
}
// Commit or rollback
if ($error) {
$this->db->rollback();
return - 1 * $error;
} else {
$this->db->commit();
return $this->id;
}
}
/**
* Load object in memory from the database
*
* @param int $id Id object
* @param string $ref Ref
* @return int <0 if KO, 0 if not found, >0 if OK
*/
public function fetch($id, $ref = null)
{
dol_syslog(__METHOD__, LOG_DEBUG);
$sql = 'SELECT';
$sql .= ' t.rowid,';
$sql .= ' t.field1,';
$sql .= ' t.field2';
//...
$sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element . ' as t';
$sql.= ' WHERE 1 = 1';
if (! empty($conf->multicompany->enabled)) {
$sql .= " AND entity IN (" . getEntity('mymoduleobject') . ")";
}
if (null !== $ref) {
$sql .= ' AND t.ref = ' . '\'' . $ref . '\'';
} else {
$sql .= ' AND t.rowid = ' . $id;
}
$resql = $this->db->query($sql);
if ($resql) {
$numrows = $this->db->num_rows($resql);
if ($numrows) {
$obj = $this->db->fetch_object($resql);
$this->id = $obj->rowid;
$this->prop1 = $obj->field1;
$this->prop2 = $obj->field2;
//...
}
$this->db->free($resql);
$this->fetch_optionals();
// $this->fetch_lines();
if ($numrows) {
return 1;
} else {
return 0;
}
} else {
$this->errors[] = 'Error ' . $this->db->lasterror();
dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR);
return - 1;
}
}
/**
* Load object in memory from the database
*
* @param string $sortorder Sort Order
* @param string $sortfield Sort field
* @param int $limit offset limit
* @param int $offset offset limit
* @param array $filter filter array
* @param string $filtermode filter mode (AND or OR)
*
* @return int <0 if KO, >0 if OK
*/
public function fetchAll($sortorder='', $sortfield='', $limit=0, $offset=0, array $filter = array(), $filtermode='AND')
{
dol_syslog(__METHOD__, LOG_DEBUG);
$sql = 'SELECT';
$sql .= ' t.rowid,';
$sql .= ' t.field1,';
$sql .= ' t.field2';
//...
$sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element. ' as t';
// Manage filter
$sqlwhere = array();
if (count($filter) > 0) {
foreach ($filter as $key => $value) {
$sqlwhere [] = $key . ' LIKE \'%' . $this->db->escape($value) . '%\'';
}
}
$sql.= ' WHERE 1 = 1';
if (! empty($conf->multicompany->enabled)) {
$sql .= " AND entity IN (" . getEntity('mymoduleobject') . ")";
}
if (count($sqlwhere) > 0) {
$sql .= ' AND ' . implode(' '.$filtermode.' ', $sqlwhere);
}
if (!empty($sortfield)) {
$sql .= $this->db->order($sortfield,$sortorder);
}
if (!empty($limit)) {
$sql .= ' ' . $this->db->plimit($limit, $offset);
}
$resql = $this->db->query($sql);
if ($resql) {
$num = $this->db->num_rows($resql);
while ($obj = $this->db->fetch_object($resql)) {
$line = new self($this->db);
$line->id = $obj->rowid;
$line->prop1 = $obj->field1;
$line->prop2 = $obj->field2;
//...
}
$this->db->free($resql);
return $num;
} else {
$this->errors[] = 'Error ' . $this->db->lasterror();
dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR);
return - 1;
}
}
/**
* Update object into database
*
* @param User $user User that modifies
* @param bool $notrigger false=launch triggers after, true=disable triggers
*
* @return int <0 if KO, >0 if OK
*/
public function update(User $user, $notrigger = false)
{
dol_syslog(__METHOD__, LOG_DEBUG);
$error = 0;
// Clean parameters
if (isset($this->prop1)) {
$this->prop1 = trim($this->prop1);
}
if (isset($this->prop2)) {
$this->prop2 = trim($this->prop2);
}
//...
// Check parameters
// Put here code to add a control on parameters values
// Update request
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this->table_element . ' SET';
$sql .= " field1=".(isset($this->field1)?"'".$this->db->escape($this->field1)."'":"null").",";
$sql .= " field2=".(isset($this->field2)?"'".$this->db->escape($this->field2)."'":"null")."";
//...
$sql .= ' WHERE rowid=' . $this->id;
$this->db->begin();
$resql = $this->db->query($sql);
if (!$resql) {
$error ++;
$this->errors[] = 'Error ' . $this->db->lasterror();
dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR);
}
if (!$error && !$notrigger) {
// 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
}
// Commit or rollback
if ($error) {
$this->db->rollback();
return - 1 * $error;
} else {
$this->db->commit();
return 1;
}
}
/**
* Delete object in database
*
* @param User $user User that deletes
* @param bool $notrigger false=launch triggers after, true=disable triggers
*
* @return int <0 if KO, >0 if OK
*/
public function delete(User $user, $notrigger = false)
{
dol_syslog(__METHOD__, LOG_DEBUG);
$error = 0;
$this->db->begin();
if (!$error) {
if (!$notrigger) {
// Uncomment this and change MYOBJECT to your own tag if you
// want this action calls a trigger.
//// Call triggers
//$result=$this->call_trigger('MYOBJECT_DELETE',$user);
//if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
//// End call triggers
}
}
// If you need to delete child tables to, you can insert them here
if (!$error) {
$sql = 'DELETE FROM ' . MAIN_DB_PREFIX . $this->table_element;
$sql .= ' WHERE rowid=' . $this->id;
$resql = $this->db->query($sql);
if (!$resql) {
$error ++;
$this->errors[] = 'Error ' . $this->db->lasterror();
dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR);
}
}
// Commit or rollback
if ($error) {
$this->db->rollback();
return - 1 * $error;
} else {
$this->db->commit();
return 1;
}
}
/**
* Load an object from its id and create a new one in database
*
* @param int $fromid Id of object to clone
*
* @return int New id of clone
*/
public function createFromClone($fromid)
{
dol_syslog(__METHOD__, LOG_DEBUG);
global $user;
$error = 0;
$object = new self($this->db);
$this->db->begin();
// Load source object
$object->fetch($fromid);
// Reset object
$object->id = 0;
// Clear fields
// ...
// Create clone
$result = $object->create($user);
// Other options
if ($result < 0) {
$error ++;
$this->errors = $object->errors;
dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR);
}
// End
if (!$error) {
$this->db->commit();
return $object->id;
} else {
$this->db->rollback();
return - 1;
}
}
/** /**
* Return a link to the object card (with optionaly the picto) * Return a link to the object card (with optionaly the picto)
@ -592,9 +232,7 @@ class MyObject extends CommonObject
*/ */
public function initAsSpecimen() public function initAsSpecimen()
{ {
$this->id = 0; $this->initAsSpecimenCommon();
$this->prop1 = 'prop1';
$this->prop2 = 'prop2';
} }
} }

View File

@ -60,12 +60,25 @@ $langs->loadLangs(array("mymodule","other"));
// Get parameters // Get parameters
$id = GETPOST('id', 'int'); $id = GETPOST('id', 'int');
$action = GETPOST('action', 'alpha'); $action = GETPOST('action', 'alpha');
$cancel = GETPOST('cancel'); $cancel = GETPOST('cancel', 'aZ09');
$backtopage = GETPOST('backtopage'); $backtopage = GETPOST('backtopage', 'alpha');
$myparam = GETPOST('myparam','alpha');
$search_field1=GETPOST("search_field1"); // Initialize technical objects
$search_field2=GETPOST("search_field2"); $object=new MyObject($db);
$extrafields = new ExtraFields($db);
$diroutputmassaction=$conf->mymodule->dir_output . '/temp/massgeneration/'.$user->id;
$hookmanager->initHooks(array('myobjectcard')); // Note that conf->hooks_modules contains array
// Fetch optionals attributes and labels
$extralabels = $extrafields->fetch_name_optionals_label('myobject');
$search_array_options=$extrafields->getOptionalsFromPost($extralabels,'','search_');
// Initialize array of search criterias
$search_all=trim(GETPOST("search_all",'alpha'));
$search=array();
foreach($object->fields as $key => $val)
{
if (GETPOST('search_'.$key,'alpha')) $search[$key]=GETPOST('search_'.$key,'alpha');
}
if (empty($action) && empty($id) && empty($ref)) $action='view'; if (empty($action) && empty($id) && empty($ref)) $action='view';
@ -76,19 +89,12 @@ if ($user->societe_id > 0)
} }
//$result = restrictedArea($user, 'mymodule', $id); //$result = restrictedArea($user, 'mymodule', $id);
$object = new MyObject_Class($db);
$extrafields = new ExtraFields($db);
// fetch optionals attributes and labels // fetch optionals attributes and labels
$extralabels = $extrafields->fetch_name_optionals_label($object->table_element); $extralabels = $extrafields->fetch_name_optionals_label($object->table_element);
// Load object // Load object
include DOL_DOCUMENT_ROOT.'/core/actions_fetchobject.inc.php'; // Must be include, not include_once // Must be include, not include_once. Include fetch and fetch_thirdparty but not fetch_optionals include DOL_DOCUMENT_ROOT.'/core/actions_fetchobject.inc.php'; // Must be include, not include_once // Must be include, not include_once. Include fetch and fetch_thirdparty but not fetch_optionals
// Initialize technical object to manage hooks of modules. Note that conf->hooks_modules contains array array
$hookmanager->initHooks(array('mymodule'));
/* /*
@ -107,7 +113,7 @@ if (empty($reshook))
{ {
if ($action != 'addlink') if ($action != 'addlink')
{ {
$urltogo=$backtopage?$backtopage:dol_buildpath('/mymodule/list.php',1); $urltogo=$backtopage?$backtopage:dol_buildpath('/mymodule/myobject_list.php',1);
header("Location: ".$urltogo); header("Location: ".$urltogo);
exit; exit;
} }
@ -120,33 +126,35 @@ if (empty($reshook))
{ {
if ($cancel) if ($cancel)
{ {
$urltogo=$backtopage?$backtopage:dol_buildpath('/mymodule/list.php',1); $urltogo=$backtopage?$backtopage:dol_buildpath('/mymodule/myobject_list.php',1);
header("Location: ".$urltogo); header("Location: ".$urltogo);
exit; exit;
} }
$error=0; $error=0;
/* object_prop_getpost_prop */ foreach ($object->fields as $key => $val)
$object->prop1=GETPOST("field1"); {
$object->prop2=GETPOST("field2"); $object->$key=GETPOST($key,'alpha');
if (in_array($key, array('entity', 'datec', 'tms'))) continue;
if (empty($object->ref)) if ($val['notnull'] && $object->$key == '')
{ {
$error++; $error++;
setEventMessages($langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv("Ref")), null, 'errors'); setEventMessages($langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv($val['label'])), null, 'errors');
}
} }
if (! $error) if (! $error)
{ {
$result=$object->create($user); $result=$object->createCommon($user);
if ($result > 0) if ($result > 0)
{ {
// Creation OK // Creation OK
$urltogo=$backtopage?$backtopage:dol_buildpath('/mymodule/list.php',1); $urltogo=$backtopage?$backtopage:dol_buildpath('/mymodule/myobject_list.php',1);
header("Location: ".$urltogo); header("Location: ".$urltogo);
exit; exit;
} }
else
{ {
// Creation KO // Creation KO
if (! empty($object->errors)) setEventMessages(null, $object->errors, 'errors'); if (! empty($object->errors)) setEventMessages(null, $object->errors, 'errors');
@ -203,7 +211,7 @@ if (empty($reshook))
{ {
// Delete OK // Delete OK
setEventMessages("RecordDeleted", null, 'mesgs'); setEventMessages("RecordDeleted", null, 'mesgs');
header("Location: ".dol_buildpath('/mymodule/list.php',1)); header("Location: ".dol_buildpath('/mymodule/myobject_list.php',1));
exit; exit;
} }
else else
@ -225,10 +233,7 @@ if (empty($reshook))
$form=new Form($db); $form=new Form($db);
llxHeader('','MyPageName',''); llxHeader('','MyObject','');
// Put here content of your page
// Example : Adding jquery code // Example : Adding jquery code
print '<script type="text/javascript" language="javascript"> print '<script type="text/javascript" language="javascript">
@ -249,7 +254,7 @@ jQuery(document).ready(function() {
// Part to create // Part to create
if ($action == 'create') if ($action == 'create')
{ {
print load_fiche_titre($langs->trans("NewMyModule")); print load_fiche_titre($langs->trans("NewObject", $langs->transnoentitiesnoconv("MyObject")));
print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">'; print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
print '<input type="hidden" name="action" value="add">'; print '<input type="hidden" name="action" value="add">';
@ -258,8 +263,15 @@ if ($action == 'create')
dol_fiche_head(); dol_fiche_head();
print '<table class="border centpercent">'."\n"; 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>'; foreach($object->fields as $key => $val)
// LIST_OF_TD_LABEL_FIELDS_CREATE {
if (in_array($key, array('rowid', 'entity', 'datec', 'tms'))) continue;
print '<tr><td';
print ' class="titlefieldcreate';
if ($val['notnull']) print ' fieldrequired';
print '"';
print '>'.$langs->trans($val['label']).'</td><td><input class="flat" type="text" name="'.$key.'" value="'.(GETPOST($key,'alpha')?GETPOST($key,'alpha'):'').'"></td></tr>';
}
print '</table>'."\n"; print '</table>'."\n";
dol_fiche_end(); dol_fiche_end();
@ -343,7 +355,7 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea
// Object card // Object card
// ------------------------------------------------------------ // ------------------------------------------------------------
$linkback = '<a href="' . DOL_URL_ROOT . '/mymodule/list.php' . (! empty($socid) ? '?socid=' . $socid : '') . '">' . $langs->trans("BackToList") . '</a>'; $linkback = '<a href="' . DOL_URL_ROOT . '/mymodule/myobject_list.php' . (! empty($socid) ? '?socid=' . $socid : '') . '">' . $langs->trans("BackToList") . '</a>';
$morehtmlref='<div class="refidno">'; $morehtmlref='<div class="refidno">';

View File

@ -81,8 +81,14 @@ $offset = $limit * $page;
$pageprev = $page - 1; $pageprev = $page - 1;
$pagenext = $page + 1; $pagenext = $page + 1;
// Initialize technical objects
$object=new MyObject($db); $object=new MyObject($db);
$extrafields = new ExtraFields($db);
$diroutputmassaction=$conf->mymodule->dir_output . '/temp/massgeneration/'.$user->id; $diroutputmassaction=$conf->mymodule->dir_output . '/temp/massgeneration/'.$user->id;
$hookmanager->initHooks(array('myobjectlist')); // Note that conf->hooks_modules contains array
// Fetch optionals attributes and labels
$extralabels = $extrafields->fetch_name_optionals_label('myobject');
$search_array_options=$extrafields->getOptionalsFromPost($extralabels,'','search_');
// Default sort order (if not yet defined by previous GETPOST) // Default sort order (if not yet defined by previous GETPOST)
if (! $sortfield) $sortfield="t.".key($object->fields); // Set here default search field. By default 1st field in definition. if (! $sortfield) $sortfield="t.".key($object->fields); // Set here default search field. By default 1st field in definition.
@ -104,13 +110,6 @@ foreach($object->fields as $key => $val)
if (GETPOST('search_'.$key,'alpha')) $search[$key]=GETPOST('search_'.$key,'alpha'); if (GETPOST('search_'.$key,'alpha')) $search[$key]=GETPOST('search_'.$key,'alpha');
} }
// Initialize technical object to manage hooks. Note that conf->hooks_modules contains array
$hookmanager->initHooks(array('myobjectlist'));
$extrafields = new ExtraFields($db);
// Fetch optionals attributes and labels
$extralabels = $extrafields->fetch_name_optionals_label('myobject');
$search_array_options=$extrafields->getOptionalsFromPost($extralabels,'','search_');
// List of fields to search into when doing a "search in all" // List of fields to search into when doing a "search in all"
$fieldstosearchall = array(); $fieldstosearchall = array();
foreach($object->fields as $key => $val) foreach($object->fields as $key => $val)
@ -579,7 +578,8 @@ print '</div>'."\n";
print '</form>'."\n"; print '</form>'."\n";
if ($nbtotalofrecords === '' || $nbtotalofrecords)
{
if ($massaction == 'builddoc' || $action == 'remove_file' || $show_files) if ($massaction == 'builddoc' || $action == 'remove_file' || $show_files)
{ {
require_once(DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php'); require_once(DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php');
@ -599,7 +599,7 @@ else
{ {
print '<br><a name="show_files"></a><a href="'.$_SERVER["PHP_SELF"].'?show_files=1'.$param.'#show_files">'.$langs->trans("ShowTempMassFilesArea").'</a>'; print '<br><a name="show_files"></a><a href="'.$_SERVER["PHP_SELF"].'?show_files=1'.$param.'#show_files">'.$langs->trans("ShowTempMassFilesArea").'</a>';
} }
}
// End of page // End of page
llxFooter(); llxFooter();