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

Language PHP Lines 188
MD5 Hash c34fd70538b3e28ed1404ea16e6f1902 Estimated Cost $3,737 (why?)
Repository https://github.com/asterix14/dolibarr.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
/* Copyright (C) 2010-2011	Regis Houssin		<regis@dolibarr.fr>
 * Copyright (C) 2011 		Laurent Destailleur	<eldy@users.sourceforge.net>
 *
 * 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 2 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			htdocs/core/class/canvas.class.php
 *   \ingroup		core
 *   \brief			File of class to manage canvas
 */


/**
 *    \class      Canvas
 *    \brief      Class to manage canvas
 */
class Canvas
{
	var $db;
	var $error;
	var $errors=array();

	var $actiontype;

    var $dirmodule;			// Module directory
    var $targetmodule;      // Module concerned by canvas (ex: thirdparty, contact, ...)
    var $canvas;            // Name of canvas (ex: company, individual, product, service, ...)
    var $card;              // Tab (sub-canvas)

    var $template_dir;			// Initialized by getCanvas with templates directory
    var $control;           	// Initialized by getCanvas with controller instance


   /**
	*   Constructor
	*
	*   @param     DoliDB	$DB          	Database handler
	*   @param     string   $actiontype		Action type ('create', 'view', 'edit', 'list')
	*/
	function __construct($DB, $actiontype='view')
	{
		$this->db = $DB;

		$this->actiontype = $actiontype;
        if ($this->actiontype == 'add')    $this->actiontype='create';
		if ($this->actiontype == 'update') $this->actiontype='edit';
		if (empty($this->actiontype) || $this->actiontype == 'delete' || $this->actiontype == 'create_user') $this->actiontype='view';
	}

	/**
	 * 	Initialize properties: ->targetmodule, ->canvas, ->card, ->dirmodule, ->template_dir
	 *
	 * 	@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)
	 * 	@return	void
	 */
	function getCanvas($module, $card, $canvas)
	{
		global $conf, $langs;

		// Set properties with value specific to dolibarr core: this->targetmodule, this->card, this->canvas
        $this->targetmodule = $module;
        $this->canvas = $canvas;
        $this->card = $card;
        $this->dirmodule = $module;
        // Correct values if canvas is into an external module
		if (preg_match('/^([^@]+)@([^@]+)$/i',$canvas,$regs))
		{
            $this->canvas = $regs[1];
		    $this->dirmodule = $regs[2];
		}
		// For compatibility
        if ($this->dirmodule == 'thirdparty') { $this->dirmodule = 'societe'; }

        // Control file
		$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);
            $this->control = new $controlclassname($this->db, $this->dirmodule, $this->targetmodule, $this->canvas, $this->card);
		}

		// Template dir
		$this->template_dir = dol_buildpath('/'.$this->dirmodule.'/canvas/'.$this->canvas.'/tpl/');
        if (! is_dir($this->template_dir))
        {
            $this->template_dir='';
        }

        //print 'dimodule='.$dirmodule.' canvas='.$this->canvas.'<br>';
        //print ' => template_dir='.$this->template_dir.'<br>';
	}

    /**
	 * 	Shared method for canvas to assign values for templates
	 *
	 * 	@param		string		&$action	Action string
	 * 	@param		int			$id			Object id (if ref not provided)
	 * 	@param		string		$ref		Object ref (if id not provided)
	 * 	@return		void
	 */
	function assign_values(&$action='view', $id=0, $ref='')
	{
		if (method_exists($this->control,'assign_values')) $this->control->assign_values($action, $id, $ref);
	}

    /**
     *	Return the template to display canvas (if it exists)
	 *
     *	@return		int		0=Canvas template file does not exist, 1=Canvas template file exists
     */
    function displayCanvasExists()
    {
        if (empty($this->template_dir)) return 0;
        //print $this->template_dir.($this->card?$this->card.'_':'').$this->actiontype.'.tpl.php';
        if (file_exists($this->template_dir.($this->card?$this->card.'_':'').$this->actiontype.'.tpl.php')) return 1;
        else return 0;
    }

	/**
	 *	Display a canvas page. This will include the template for output.
	 *	Variables used by templates may have been defined or loaded before into the assign_values function.
	 *
	 *	@return		void
	 */
	function display_canvas()
	{
		global $db, $conf, $langs, $user, $canvas;
		global $form, $formfile;

		include($this->template_dir.($this->card?$this->card.'_':'').$this->actiontype.'.tpl.php');        // Include native PHP template
	}


	// 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
	 *
	 * 	@param		string		&$action	Action string
	 * 	@param		int			$id			Object id
	 * 	@return		mixed					Return return code of doActions of canvas
	 * 	@deprecated	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.
	 * 	@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;
		}
	}

}
?>
Back to Top