New : can add new resource in database

This commit is contained in:
Jean-François Ferry 2014-03-15 22:31:11 +01:00 committed by jfefe
parent 703572cd5b
commit c42697dd78
3 changed files with 261 additions and 15 deletions

167
htdocs/resource/add.php Executable file
View File

@ -0,0 +1,167 @@
<?php
/* Copyright (C) 2013 Jean-François Ferry <jfefe@aternatik.fr>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file resource/add.php
* \ingroup resource
* \brief Page to manage resource object
* Initialy built by build_class_from_table on 2013-07-24 16:03
*/
// Change this following line to use the correct relative path (../, ../../, etc)
$res=0;
$res=@include("../main.inc.php"); // For root directory
if (! $res) $res=@include("../../main.inc.php"); // For "custom" directory
if (! $res) die("Include of main fails");
require_once 'class/resource.class.php';
// Load traductions files requiredby by page
$langs->load("resource");
$langs->load("companies");
$langs->load("other");
// Get parameters
$id = GETPOST('id','int');
$action = GETPOST('action','alpha');
if (empty($sortorder)) $sortorder="DESC";
if (empty($sortfield)) $sortfield="t.rowid";
if (empty($arch)) $arch = 0;
if ($page == -1) {
$page = 0 ;
}
$limit = $conf->global->limit;
$offset = $limit * $page ;
$pageprev = $page - 1;
$pagenext = $page + 1;
// Protection if external user
if ($user->societe_id > 0)
{
accessforbidden();
}
$object = new Resource($db);
if ($action == 'confirm_add_resource')
{
$error='';
$ref=GETPOST('ref','alpha');
$description=GETPOST('description','alpha');
$fk_code_type_resource=GETPOST('fk_code_type_resource','alpha');
if (empty($ref))
{
$mesg=$langs->trans("ErrorFieldRequired",$langs->transnoentities("Ref"));
setEventMessage($mesg, 'errors');
$error++;
}
if (! $error)
{
$object=new Resource($db);
$object->ref=$ref;
$object->description=$description;
$object->fk_code_type_resource=$fk_code_type_resource;
$result=$object->create($user);
if ($result > 0)
{
// Creation OK
$db->commit();
setEventMessage($langs->trans('ResourceCreatedWithSuccess'));
Header("Location: card.php?id=" . $object->id);
return;
}
else
{
// Creation KO
setEventMessage($object->error, 'errors');
$action = '';
}
}
else
{
$action = '';
}
}
/***************************************************
* VIEW
*
* Put here all code to build page
****************************************************/
$form=new Form($db);
if ( !$action )
{
$pagetitle=$langs->trans('AddResource');
llxHeader('',$pagetitle,'');
print_fiche_titre($pagetitle,'','resource_32.png');
print '<form method="post" action="'.$_SERVER['PHP_SELF'].'" name="add_resource">';
print '<input type="hidden" name="action" value="confirm_add_resource" />';
print '<table class="border" width="100%">';
// Ref / label
$field = 'ref';
print '<tr>';
print '<td>';
print '<label for="'.$field.'" class="fieldrequired">';
print $langs->trans('ResourceFormLabel_'.$field);
print '</td>';
print '<td>';
print '<input type="text" name="'.$field.'" value="'.$$field.'" />';
print '</td>';
print '</tr>';
// Description
$field = 'description';
print '<tr>';
print '<td>';
print '<label for="'.$field.'">';
print $langs->trans('ResourceFormLabel_'.$field);
print '</label>';
print '</td>';
print '<td>';
require_once (DOL_DOCUMENT_ROOT . "/core/class/doleditor.class.php");
$doleditor = new DolEditor($field, $$field, 160, '', '', false);
$doleditor->Create();
print '</td>';
print '</tr>';
print '</table>';
echo '<div style="text-align: center">',
' <input type="submit" class="button" name="" value="'.$langs->trans('Save').'" />',
'</div>';
print '</form>';
}
// End of page
llxFooter();
$db->close();
?>

View File

@ -42,21 +42,20 @@ class ActionsResource
}
/**
* doActions
* doActions for resource module
*
* @param array $parameters parameters
* @param object &$object object
* @param string &$action action
* @return void
* @param array $parameters parameters
* @param Object $object object
* @param string $action action
*/
function doActions($parameters, &$object, &$action) {
function doActions($parameters, &$object, &$action)
{
global $langs,$user;
$langs->load('resource');
if (in_array('element_resource',explode(':',$parameters['context'])))
{
// Efface une ressource
// Delete a resource linked to an element
if ($action == 'confirm_delete_resource' && $user->rights->resource->delete && GETPOST('confirm') == 'yes')
{
$res = $object->fetch(GETPOST('lineid'));

View File

@ -60,13 +60,96 @@ class Resource extends CommonObject
return 1;
}
/**
* Create object into database
*
* @param User $user User that creates
* @param int $notrigger 0=launch triggers after, 1=disable triggers
* @return int <0 if KO, Id of created object if OK
*/
function create($user, $notrigger=0)
{
global $conf, $langs;
$error=0;
// Clean parameters
if (isset($this->ref)) $this->ref=trim($this->ref);
if (isset($this->description)) $this->description=trim($this->description);
if (isset($this->fk_code_type_resource)) $this->fk_code_type_resource=trim($this->fk_code_type_resource);
if (isset($this->note_public)) $this->note_public=trim($this->note_public);
if (isset($this->note_private)) $this->note_private=trim($this->note_private);
// Insert request
$sql = "INSERT INTO ".MAIN_DB_PREFIX."resource(";
$sql.= " entity,";
$sql.= "ref,";
$sql.= "description,";
$sql.= "fk_code_type_resource,";
$sql.= "note_public,";
$sql.= "note_private";
$sql.= ") VALUES (";
$sql.= $conf->entity.", ";
$sql.= " ".(! isset($this->ref)?'NULL':"'".$this->db->escape($this->ref)."'").",";
$sql.= " ".(! isset($this->description)?'NULL':"'".$this->db->escape($this->description)."'").",";
$sql.= " ".(! isset($this->fk_code_type_resource)?'NULL':"'".$this->db->escape($this->fk_code_type_resource)."'").",";
$sql.= " ".(! isset($this->note_public)?'NULL':"'".$this->db->escape($this->note_public)."'").",";
$sql.= " ".(! isset($this->note_private)?'NULL':"'".$this->db->escape($this->note_private)."'");
$sql.= ")";
$this->db->begin();
dol_syslog(get_class($this)."::create sql=".$sql, LOG_DEBUG);
$resql=$this->db->query($sql);
if (! $resql) {
$error++; $this->errors[]="Error ".$this->db->lasterror();
}
if (! $error)
{
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."place");
if (! $notrigger)
{
//// Call triggers
//include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
//$interface=new Interfaces($this->db);
//$result=$interface->run_triggers('RESOURCE_CREATE',$this,$user,$langs,$conf);
//if ($result < 0) { $error++; $this->errors=$interface->errors; }
//// End call triggers
}
}
// Commit or rollback
if ($error)
{
foreach($this->errors as $errmsg)
{
dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
$this->error.=($this->error?', '.$errmsg:$errmsg);
}
$this->db->rollback();
return -1*$error;
}
else
{
$this->db->commit();
return $this->id;
}
}
/**
* Load object in memory from database
*
* @param int $id id object
* @return int <0 if KO, >0 if OK
*/
function fetch($id)
function fetch_element_resource($id)
{
global $langs;
$sql = "SELECT";
@ -299,13 +382,13 @@ class Resource extends CommonObject
/**
* Update object into database
* Update element resource into database
*
* @param User $user User that modifies
* @param int $notrigger 0=launch triggers after, 1=disable triggers
* @return int <0 if KO, >0 if OK
*/
function update($user=0, $notrigger=0)
function update_element_resource($user=0, $notrigger=0)
{
global $conf, $langs;
$error=0;
@ -319,9 +402,6 @@ class Resource extends CommonObject
if (isset($this->mandatory)) $this->mandatory=trim($this->mandatory);
// Check parameters
// Put here code to add a control on parameters values
// Update request
$sql = "UPDATE ".MAIN_DB_PREFIX."element_resources SET";
$sql.= " resource_id=".(isset($this->resource_id)?"'".$this->db->escape($this->resource_id)."'":"null").",";