2010-05-05 09:00:11 +02:00
|
|
|
<?php
|
2018-10-27 14:43:12 +02:00
|
|
|
/* Copyright (C) 2010-2018 Regis Houssin <regis.houssin@inodbox.com>
|
2011-06-01 07:48:52 +02:00
|
|
|
* Copyright (C) 2011 Laurent Destailleur <eldy@users.sourceforge.net>
|
2024-09-30 10:05:24 +02:00
|
|
|
* Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
|
2010-05-05 09:00:11 +02:00
|
|
|
*
|
|
|
|
|
* 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
|
2013-01-16 15:36:08 +01:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2010-05-05 09:00:11 +02:00
|
|
|
* (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
|
2019-09-23 21:55:30 +02:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2010-05-05 09:00:11 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
2010-09-07 11:57:11 +02:00
|
|
|
* \file htdocs/core/class/canvas.class.php
|
2010-05-05 09:00:11 +02:00
|
|
|
* \ingroup core
|
2011-04-06 16:26:43 +02:00
|
|
|
* \brief File of class to manage canvas
|
2010-05-05 09:00:11 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2012-03-12 15:09:46 +01:00
|
|
|
* Class to manage canvas
|
2010-05-05 09:00:11 +02:00
|
|
|
*/
|
|
|
|
|
class Canvas
|
|
|
|
|
{
|
2018-08-22 11:06:34 +02:00
|
|
|
/**
|
2020-10-31 14:32:18 +01:00
|
|
|
* @var DoliDB Database handler.
|
|
|
|
|
*/
|
|
|
|
|
public $db;
|
2018-09-02 10:43:34 +02:00
|
|
|
|
2018-08-22 10:37:16 +02:00
|
|
|
/**
|
|
|
|
|
* @var string Error code (or message)
|
|
|
|
|
*/
|
2020-03-12 12:45:44 +01:00
|
|
|
public $error = '';
|
2018-08-21 19:08:11 +02:00
|
|
|
|
2018-08-17 19:31:22 +02:00
|
|
|
/**
|
|
|
|
|
* @var string[] Error codes (or messages)
|
|
|
|
|
*/
|
2018-08-21 19:08:11 +02:00
|
|
|
public $errors = array();
|
2011-09-28 21:07:49 +02:00
|
|
|
|
2024-09-30 10:05:24 +02:00
|
|
|
/**
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2018-09-24 12:03:49 +02:00
|
|
|
public $actiontype;
|
2010-09-05 20:10:23 +02:00
|
|
|
|
2024-09-30 10:05:24 +02:00
|
|
|
/**
|
|
|
|
|
* @var string Module directory
|
|
|
|
|
*/
|
2020-10-31 14:32:18 +01:00
|
|
|
public $dirmodule; // Module directory
|
2024-09-30 10:05:24 +02:00
|
|
|
/**
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2020-10-31 14:32:18 +01:00
|
|
|
public $targetmodule; // Module concerned by canvas (ex: thirdparty, contact, ...)
|
2024-09-30 10:05:24 +02:00
|
|
|
/**
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2020-10-31 14:32:18 +01:00
|
|
|
public $canvas; // Name of canvas (ex: company, individual, product, service, ...)
|
2024-09-30 10:05:24 +02:00
|
|
|
/**
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2020-10-31 14:32:18 +01:00
|
|
|
public $card; // Tab (sub-canvas)
|
2011-05-29 16:49:40 +02:00
|
|
|
|
2024-09-30 10:05:24 +02:00
|
|
|
/**
|
|
|
|
|
* @var string Initialized by getCanvas with templates directory
|
|
|
|
|
*/
|
|
|
|
|
public $template_dir;
|
|
|
|
|
/**
|
|
|
|
|
* @var ActionsContactCardCommon|ActionsAdherentCardCommon|ActionsCardProduct|ActionsCardService|ActionsCardCommon
|
|
|
|
|
*/
|
2020-10-31 14:32:18 +01:00
|
|
|
public $control; // Initialized by getCanvas with controller instance
|
2010-11-11 12:15:06 +01:00
|
|
|
|
2010-05-05 09:00:11 +02:00
|
|
|
|
2020-10-31 14:32:18 +01:00
|
|
|
/**
|
2019-10-27 11:53:20 +01:00
|
|
|
* Constructor
|
|
|
|
|
*
|
|
|
|
|
* @param DoliDB $db Database handler
|
|
|
|
|
* @param string $actiontype Action type ('create', 'view', 'edit', 'list')
|
|
|
|
|
*/
|
2019-02-25 22:27:04 +01:00
|
|
|
public function __construct($db, $actiontype = 'view')
|
2010-05-05 09:00:11 +02:00
|
|
|
{
|
2012-02-28 16:48:18 +01:00
|
|
|
$this->db = $db;
|
2011-09-26 19:50:23 +02:00
|
|
|
|
2012-05-30 03:46:06 +02:00
|
|
|
$this->actiontype = $this->_cleanaction($actiontype);
|
2010-05-05 09:00:11 +02:00
|
|
|
}
|
2010-11-11 12:15:06 +01:00
|
|
|
|
2012-05-30 03:46:06 +02:00
|
|
|
/**
|
|
|
|
|
* Return action code cleaned
|
|
|
|
|
*
|
|
|
|
|
* @param string $action Action type ('create', 'view', 'edit', 'list', 'add', 'update')
|
|
|
|
|
* @return string Cleaned action type ('create', 'view', 'edit', 'list')
|
|
|
|
|
*/
|
|
|
|
|
private function _cleanaction($action)
|
|
|
|
|
{
|
2020-10-31 14:32:18 +01:00
|
|
|
$newaction = $action;
|
2021-02-23 22:03:23 +01:00
|
|
|
if ($newaction == 'add') {
|
|
|
|
|
$newaction = 'create';
|
|
|
|
|
}
|
|
|
|
|
if ($newaction == 'update') {
|
|
|
|
|
$newaction = 'edit';
|
|
|
|
|
}
|
|
|
|
|
if (empty($newaction) || $newaction == 'delete' || $newaction == 'create_user' || $newaction == 'presend' || $newaction == 'send') {
|
|
|
|
|
$newaction = 'view';
|
|
|
|
|
}
|
2020-10-31 14:32:18 +01:00
|
|
|
return $newaction;
|
2012-05-30 03:46:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-09-03 20:28:59 +02:00
|
|
|
/**
|
2011-09-24 22:51:36 +02:00
|
|
|
* Initialize properties: ->targetmodule, ->canvas, ->card, ->dirmodule, ->template_dir
|
2011-08-31 19:44:20 +02:00
|
|
|
*
|
2011-09-24 22:51:36 +02:00
|
|
|
* @param string $module Name of target module (thirdparty, contact, ...)
|
|
|
|
|
* @param string $card Tab name of card (ex: 'card', 'info', 'contactcard', ...) or '' for a list page
|
|
|
|
|
* @param string $canvas Name of canvas (ex: mycanvas, default, or mycanvas@myexternalmodule)
|
2011-09-24 22:56:08 +02:00
|
|
|
* @return void
|
2010-05-05 10:49:17 +02:00
|
|
|
*/
|
2019-02-25 22:27:04 +01:00
|
|
|
public function getCanvas($module, $card, $canvas)
|
2010-05-05 10:49:17 +02:00
|
|
|
{
|
2010-09-05 18:35:46 +02:00
|
|
|
global $conf, $langs;
|
2010-05-05 10:49:17 +02:00
|
|
|
|
2011-05-26 00:49:11 +02:00
|
|
|
// Set properties with value specific to dolibarr core: this->targetmodule, this->card, this->canvas
|
2020-10-31 14:32:18 +01:00
|
|
|
$this->targetmodule = $module;
|
|
|
|
|
$this->canvas = $canvas;
|
|
|
|
|
$this->card = $card;
|
|
|
|
|
$this->dirmodule = $module;
|
|
|
|
|
// Correct values if canvas is into an external module
|
2021-03-30 02:13:57 +02:00
|
|
|
$regs = array();
|
2021-02-23 22:03:23 +01:00
|
|
|
if (preg_match('/^([^@]+)@([^@]+)$/i', $canvas, $regs)) {
|
2020-10-31 14:32:18 +01:00
|
|
|
$this->canvas = $regs[1];
|
|
|
|
|
$this->dirmodule = $regs[2];
|
2010-11-03 12:39:40 +01:00
|
|
|
}
|
|
|
|
|
// For compatibility
|
2021-02-23 22:03:23 +01:00
|
|
|
if ($this->dirmodule == 'thirdparty') {
|
|
|
|
|
$this->dirmodule = 'societe';
|
|
|
|
|
}
|
2010-11-04 10:14:20 +01:00
|
|
|
|
2020-10-31 14:32:18 +01:00
|
|
|
// Control file
|
2011-09-24 17:57:52 +02:00
|
|
|
$controlclassfile = dol_buildpath('/'.$this->dirmodule.'/canvas/'.$this->canvas.'/actions_'.$this->card.'_'.$this->canvas.'.class.php');
|
2021-02-23 22:03:23 +01:00
|
|
|
if (file_exists($controlclassfile)) {
|
2020-10-31 14:32:18 +01:00
|
|
|
// Include actions class (controller)
|
|
|
|
|
require_once $controlclassfile;
|
2010-09-05 20:10:23 +02:00
|
|
|
|
2020-10-31 14:32:18 +01:00
|
|
|
// Instantiate actions class (controller)
|
|
|
|
|
$controlclassname = 'Actions'.ucfirst($this->card).ucfirst($this->canvas);
|
|
|
|
|
$this->control = new $controlclassname($this->db, $this->dirmodule, $this->targetmodule, $this->canvas, $this->card);
|
2011-04-06 16:26:43 +02:00
|
|
|
}
|
2010-11-11 12:15:06 +01:00
|
|
|
|
2011-04-06 16:26:43 +02:00
|
|
|
// Template dir
|
2011-09-24 17:57:52 +02:00
|
|
|
$this->template_dir = dol_buildpath('/'.$this->dirmodule.'/canvas/'.$this->canvas.'/tpl/');
|
2021-02-23 22:03:23 +01:00
|
|
|
if (!is_dir($this->template_dir)) {
|
2020-10-31 14:32:18 +01:00
|
|
|
$this->template_dir = '';
|
|
|
|
|
}
|
2011-05-29 16:49:40 +02:00
|
|
|
|
2020-10-31 14:32:18 +01:00
|
|
|
//print 'dimodule='.$dirmodule.' canvas='.$this->canvas.'<br>';
|
|
|
|
|
//print ' => template_dir='.$this->template_dir.'<br>';
|
2011-09-24 22:51:36 +02:00
|
|
|
}
|
2011-09-24 22:51:36 +02:00
|
|
|
|
2020-10-31 14:32:18 +01:00
|
|
|
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
|
|
|
|
|
/**
|
2011-05-29 19:34:07 +02:00
|
|
|
* Shared method for canvas to assign values for templates
|
2011-09-24 22:51:36 +02:00
|
|
|
*
|
2014-09-27 16:00:11 +02:00
|
|
|
* @param string $action Action string
|
2011-09-28 21:07:49 +02:00
|
|
|
* @param int $id Object id (if ref not provided)
|
|
|
|
|
* @param string $ref Object ref (if id not provided)
|
2011-09-24 17:57:52 +02:00
|
|
|
* @return void
|
2010-05-05 10:49:17 +02:00
|
|
|
*/
|
2019-02-25 22:27:04 +01:00
|
|
|
public function assign_values(&$action = 'view', $id = 0, $ref = '')
|
2010-05-05 10:49:17 +02:00
|
|
|
{
|
2020-10-31 14:32:18 +01:00
|
|
|
// phpcs:enable
|
2022-06-09 10:49:24 +02:00
|
|
|
if (is_object($this->control) && method_exists($this->control, 'assign_values')) {
|
2021-02-23 22:03:23 +01:00
|
|
|
$this->control->assign_values($action, $id, $ref);
|
|
|
|
|
}
|
2010-05-05 10:49:17 +02:00
|
|
|
}
|
2010-05-26 16:52:32 +02:00
|
|
|
|
2020-10-31 14:32:18 +01:00
|
|
|
/**
|
2022-12-30 14:59:32 +01:00
|
|
|
* Return if a template exists to display as canvas (if it exists)
|
2011-08-31 19:44:20 +02:00
|
|
|
*
|
2012-03-12 15:09:46 +01:00
|
|
|
* @param string $action Action code
|
2020-10-31 14:32:18 +01:00
|
|
|
* @return int 0=Canvas template file does not exist, 1=Canvas template file exists
|
|
|
|
|
*/
|
|
|
|
|
public function displayCanvasExists($action)
|
|
|
|
|
{
|
2022-12-30 14:56:55 +01:00
|
|
|
// template_dir should be '/'.$this->dirmodule.'/canvas/'.$this->canvas.'/tpl/', for example '/mymodule/canvas/product/tpl'
|
2021-02-23 22:03:23 +01:00
|
|
|
if (empty($this->template_dir)) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2012-02-28 16:48:18 +01:00
|
|
|
|
2021-02-23 22:03:23 +01:00
|
|
|
if (file_exists($this->template_dir.(!empty($this->card) ? $this->card.'_' : '').$this->_cleanaction($action).'.tpl.php')) {
|
|
|
|
|
return 1;
|
|
|
|
|
} else {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2020-10-31 14:32:18 +01:00
|
|
|
}
|
2011-05-29 19:34:07 +02:00
|
|
|
|
2020-10-31 14:32:18 +01:00
|
|
|
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
|
2010-05-05 10:49:17 +02:00
|
|
|
/**
|
2011-09-24 17:57:52 +02:00
|
|
|
* Display a canvas page. This will include the template for output.
|
2011-09-24 22:51:36 +02:00
|
|
|
* Variables used by templates may have been defined or loaded before into the assign_values function.
|
2011-08-31 19:44:20 +02:00
|
|
|
*
|
2012-03-12 15:09:46 +01:00
|
|
|
* @param string $action Action code
|
|
|
|
|
* @return void
|
2010-05-05 10:49:17 +02:00
|
|
|
*/
|
2019-02-25 22:27:04 +01:00
|
|
|
public function display_canvas($action)
|
2010-05-05 10:49:17 +02:00
|
|
|
{
|
2020-10-31 14:32:18 +01:00
|
|
|
// phpcs:enable
|
2011-06-01 07:48:52 +02:00
|
|
|
global $db, $conf, $langs, $user, $canvas;
|
2011-09-24 17:57:52 +02:00
|
|
|
global $form, $formfile;
|
2010-05-26 16:52:32 +02:00
|
|
|
|
2020-03-10 21:54:50 +01:00
|
|
|
//var_dump($this->card.'-'.$action);
|
2020-03-12 12:45:44 +01:00
|
|
|
include $this->template_dir.(!empty($this->card) ? $this->card.'_' : '').$this->_cleanaction($action).'.tpl.php'; // Include native PHP template
|
2010-05-05 10:49:17 +02:00
|
|
|
}
|
2010-05-05 09:00:11 +02:00
|
|
|
|
2011-09-26 19:50:23 +02:00
|
|
|
|
|
|
|
|
// This functions should not be used anymore because canvas should contains only templates.
|
2021-03-30 18:31:36 +02:00
|
|
|
// https://wiki.dolibarr.org/index.php/Canvas_development
|
2011-09-26 19:50:23 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return if a canvas contains an action controller
|
|
|
|
|
*
|
|
|
|
|
* @return boolean Return if canvas contains actions (old feature. now actions should be inside hooks)
|
|
|
|
|
*/
|
2019-02-25 22:27:04 +01:00
|
|
|
public function hasActions()
|
2011-09-26 19:50:23 +02:00
|
|
|
{
|
2020-10-31 14:32:18 +01:00
|
|
|
return (is_object($this->control));
|
2011-09-26 19:50:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-12-17 14:01:22 +01:00
|
|
|
* Shared method for canvas to execute actions.
|
2020-10-31 14:32:18 +01:00
|
|
|
* @deprecated Use the doActions of hooks instead of this.
|
2015-12-17 14:01:22 +01:00
|
|
|
* This function is called if you add a doActions class inside your canvas. Try to not
|
|
|
|
|
* do that and add action code into a hook instead.
|
2011-09-26 19:50:23 +02:00
|
|
|
*
|
2014-09-27 16:00:11 +02:00
|
|
|
* @param string $action Action string
|
2011-09-26 19:50:23 +02:00
|
|
|
* @param int $id Object id
|
2024-12-04 23:16:16 +01:00
|
|
|
* @return ?mixed Return return code of doActions of canvas
|
2021-03-30 18:31:36 +02:00
|
|
|
* @see https://wiki.dolibarr.org/index.php/Canvas_development
|
2011-09-26 19:50:23 +02:00
|
|
|
*/
|
2019-02-25 22:27:04 +01:00
|
|
|
public function doActions(&$action = 'view', $id = 0)
|
2011-09-26 19:50:23 +02:00
|
|
|
{
|
2024-12-04 23:16:16 +01:00
|
|
|
$control = $this->control;
|
|
|
|
|
if (method_exists($control, 'doActions')) {
|
|
|
|
|
$ret = $control->doActions($action, $id);
|
2011-09-26 19:50:23 +02:00
|
|
|
return $ret;
|
|
|
|
|
}
|
2024-01-31 01:42:15 +01:00
|
|
|
return null;
|
2011-09-26 19:50:23 +02:00
|
|
|
}
|
2010-05-05 09:00:11 +02:00
|
|
|
}
|