dolibarr/htdocs/core/class/canvas.class.php

202 lines
6.7 KiB
PHP
Raw Normal View History

2010-05-05 09:00:11 +02:00
<?php
2012-12-30 15:13:49 +01:00
/* Copyright (C) 2010-2011 Regis Houssin <regis.houssin@capnetworks.com>
2011-06-01 07:48:52 +02:00
* Copyright (C) 2011 Laurent Destailleur <eldy@users.sourceforge.net>
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
* 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
2011-08-01 01:45:11 +02:00
* along with this program. If not, see <http://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
* \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
{
var $db;
2010-11-04 17:04:12 +01:00
var $error;
var $errors=array();
2011-09-24 17:57:52 +02:00
var $actiontype;
2011-09-24 17:57:52 +02:00
var $dirmodule; // Module directory
var $targetmodule; // Module concerned by canvas (ex: thirdparty, contact, ...)
2011-09-24 22:51:36 +02:00
var $canvas; // Name of canvas (ex: company, individual, product, service, ...)
2011-05-29 16:49:40 +02:00
var $card; // Tab (sub-canvas)
2011-09-24 22:51:36 +02:00
var $template_dir; // Initialized by getCanvas with templates directory
var $control; // Initialized by getCanvas with controller instance
2010-05-05 09:00:11 +02:00
/**
* Constructor
*
2012-03-19 17:18:11 +01:00
* @param DoliDB $db Database handler
2011-09-24 22:51:36 +02:00
* @param string $actiontype Action type ('create', 'view', 'edit', 'list')
2010-05-05 09:00:11 +02:00
*/
function __construct($db, $actiontype='view')
2010-05-05 09:00:11 +02: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
}
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)
{
$newaction = $action;
if ($newaction == 'add') $newaction='create';
if ($newaction == 'update') $newaction='edit';
if (empty($newaction) || $newaction == 'delete' || $newaction == 'create_user' || $newaction == 'presend' || $newaction == 'send') $newaction='view';
2012-05-30 03:46:06 +02:00
return $newaction;
}
/**
2011-09-24 22:51:36 +02:00
* Initialize properties: ->targetmodule, ->canvas, ->card, ->dirmodule, ->template_dir
*
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
*/
2011-05-26 00:49:11 +02:00
function getCanvas($module, $card, $canvas)
{
global $conf, $langs;
2011-05-26 00:49:11 +02:00
// Set properties with value specific to dolibarr core: this->targetmodule, this->card, this->canvas
$this->targetmodule = $module;
$this->canvas = $canvas;
2011-05-29 16:49:40 +02:00
$this->card = $card;
2011-09-24 17:57:52 +02:00
$this->dirmodule = $module;
2011-05-26 00:49:11 +02:00
// Correct values if canvas is into an external module
if (preg_match('/^([^@]+)@([^@]+)$/i',$canvas,$regs))
{
$this->canvas = $regs[1];
2011-09-24 17:57:52 +02:00
$this->dirmodule = $regs[2];
}
// For compatibility
2011-09-24 17:57:52 +02:00
if ($this->dirmodule == 'thirdparty') { $this->dirmodule = 'societe'; }
2011-09-24 22:51:36 +02: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');
if (file_exists($controlclassfile))
{
// Include actions class (controller)
$this->control_file=$controlclassfile;
require_once $controlclassfile;
// Instantiate actions class (controller)
$controlclassname = 'Actions'.ucfirst($this->card).ucfirst($this->canvas);
2011-09-24 17:57:52 +02:00
$this->control = new $controlclassname($this->db, $this->dirmodule, $this->targetmodule, $this->canvas, $this->card);
}
// Template dir
2011-09-24 17:57:52 +02:00
$this->template_dir = dol_buildpath('/'.$this->dirmodule.'/canvas/'.$this->canvas.'/tpl/');
if (! is_dir($this->template_dir))
{
$this->template_dir='';
}
2011-05-29 16:49:40 +02: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
/**
* 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
* @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
*/
function assign_values(&$action='view', $id=0, $ref='')
{
if (method_exists($this->control,'assign_values')) $this->control->assign_values($action, $id, $ref);
}
/**
2011-09-24 17:57:52 +02:00
* Return the template to display canvas (if it exists)
*
2012-03-12 15:09:46 +01:00
* @param string $action Action code
2012-05-30 03:46:06 +02:00
* @return int 0=Canvas template file does not exist, 1=Canvas template file exists
*/
function displayCanvasExists($action)
2012-03-19 17:18:11 +01:00
{
if (empty($this->template_dir)) return 0;
2012-05-30 03:46:06 +02:00
if (file_exists($this->template_dir.($this->card?$this->card.'_':'').$this->_cleanaction($action).'.tpl.php')) return 1;
else return 0;
}
/**
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.
*
2012-03-12 15:09:46 +01:00
* @param string $action Action code
* @return void
*/
function display_canvas($action)
{
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;
include $this->template_dir.($this->card?$this->card.'_':'').$this->_cleanaction($action).'.tpl.php'; // Include native PHP template
}
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.
// http://wiki.dolibarr.org/index.php/Canvas_development
/**
* Return if a canvas contains an action controller
*
* @return boolean Return if canvas contains actions (old feature. now actions should be inside hooks)
*/
function hasActions()
{
return (is_object($this->control));
}
/**
* Shared method for canvas to execute actions.
* @deprecated Use the doActions of hooks instead of this.
* 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
* @return mixed Return return code of doActions of canvas
* @see http://wiki.dolibarr.org/index.php/Canvas_development
*/
function doActions(&$action='view', $id=0)
{
if (method_exists($this->control,'doActions'))
{
$ret = $this->control->doActions($action, $id);
return $ret;
}
}
2010-05-05 09:00:11 +02:00
}