dolibarr  7.0.0-beta
canvas.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2010-2011 Regis Houssin <regis.houssin@capnetworks.com>
3  * Copyright (C) 2011 Laurent Destailleur <eldy@users.sourceforge.net>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
29 class Canvas
30 {
31  var $db;
32  var $error;
33  var $errors=array();
34 
35  var $actiontype;
36 
37  var $dirmodule; // Module directory
38  var $targetmodule; // Module concerned by canvas (ex: thirdparty, contact, ...)
39  var $canvas; // Name of canvas (ex: company, individual, product, service, ...)
40  var $card; // Tab (sub-canvas)
41 
42  var $template_dir; // Initialized by getCanvas with templates directory
43  var $control; // Initialized by getCanvas with controller instance
44 
45 
52  function __construct($db, $actiontype='view')
53  {
54  $this->db = $db;
55 
56  $this->actiontype = $this->_cleanaction($actiontype);
57  }
58 
65  private function _cleanaction($action)
66  {
67  $newaction = $action;
68  if ($newaction == 'add') $newaction='create';
69  if ($newaction == 'update') $newaction='edit';
70  if (empty($newaction) || $newaction == 'delete' || $newaction == 'create_user' || $newaction == 'presend' || $newaction == 'send') $newaction='view';
71  return $newaction;
72  }
73 
74 
83  function getCanvas($module, $card, $canvas)
84  {
85  global $conf, $langs;
86 
87  // Set properties with value specific to dolibarr core: this->targetmodule, this->card, this->canvas
88  $this->targetmodule = $module;
89  $this->canvas = $canvas;
90  $this->card = $card;
91  $this->dirmodule = $module;
92  // Correct values if canvas is into an external module
93  if (preg_match('/^([^@]+)@([^@]+)$/i',$canvas,$regs))
94  {
95  $this->canvas = $regs[1];
96  $this->dirmodule = $regs[2];
97  }
98  // For compatibility
99  if ($this->dirmodule == 'thirdparty') { $this->dirmodule = 'societe'; }
100 
101  // Control file
102  $controlclassfile = dol_buildpath('/'.$this->dirmodule.'/canvas/'.$this->canvas.'/actions_'.$this->card.'_'.$this->canvas.'.class.php');
103  if (file_exists($controlclassfile))
104  {
105  // Include actions class (controller)
106  $this->control_file=$controlclassfile;
107  require_once $controlclassfile;
108 
109  // Instantiate actions class (controller)
110  $controlclassname = 'Actions'.ucfirst($this->card).ucfirst($this->canvas);
111  $this->control = new $controlclassname($this->db, $this->dirmodule, $this->targetmodule, $this->canvas, $this->card);
112  }
113 
114  // Template dir
115  $this->template_dir = dol_buildpath('/'.$this->dirmodule.'/canvas/'.$this->canvas.'/tpl/');
116  if (! is_dir($this->template_dir))
117  {
118  $this->template_dir='';
119  }
120 
121  //print 'dimodule='.$dirmodule.' canvas='.$this->canvas.'<br>';
122  //print ' => template_dir='.$this->template_dir.'<br>';
123  }
124 
133  function assign_values(&$action='view', $id=0, $ref='')
134  {
135  if (method_exists($this->control,'assign_values')) $this->control->assign_values($action, $id, $ref);
136  }
137 
144  function displayCanvasExists($action)
145  {
146  if (empty($this->template_dir)) return 0;
147 
148  if (file_exists($this->template_dir.($this->card?$this->card.'_':'').$this->_cleanaction($action).'.tpl.php')) return 1;
149  else return 0;
150  }
151 
159  function display_canvas($action)
160  {
161  global $db, $conf, $langs, $user, $canvas;
162  global $form, $formfile;
163 
164  include $this->template_dir.($this->card?$this->card.'_':'').$this->_cleanaction($action).'.tpl.php'; // Include native PHP template
165  }
166 
167 
168  // This functions should not be used anymore because canvas should contains only templates.
169  // http://wiki.dolibarr.org/index.php/Canvas_development
170 
176  function hasActions()
177  {
178  return (is_object($this->control));
179  }
180 
192  function doActions(&$action='view', $id=0)
193  {
194  if (method_exists($this->control,'doActions'))
195  {
196  $ret = $this->control->doActions($action, $id);
197  return $ret;
198  }
199  }
200 
201 }
display_canvas($action)
Display a canvas page.
Class to manage canvas.
__construct($db, $actiontype='view')
Constructor.
if(empty($reshook)) $form
View.
Definition: perms.php:103
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
displayCanvasExists($action)
Return the template to display canvas (if it exists)
assign_values(&$action='view', $id=0, $ref='')
Shared method for canvas to assign values for templates.
doActions(&$action='view', $id=0)
Shared method for canvas to execute actions.
_cleanaction($action)
Return action code cleaned.
hasActions()
Return if a canvas contains an action controller.
getCanvas($module, $card, $canvas)
Initialize properties: ->targetmodule, ->canvas, ->card, ->dirmodule, ->template_dir.