Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.CVDoc.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 /**
7  * Control view Class Document
8  *
9  * @author Anakeen
10  * @package FDL
11  */
12 /**
13  */
14 namespace Dcp\Core;
15 /**
16  * Control view Class
17  */
18 use \Dcp\AttributeIdentifiers\Cvdoc as MyAttributes;
19 class CVDoc extends \Dcp\Family\Base
20 {
21  /**
22  * CVDoc has its own special access depend on special views
23  * by default the three access are always set
24  *
25  * @var array
26  */
27  var $acls = array(
28  "view",
29  "edit",
30  "delete"
31  );
32 
33  var $nbId = 0;
34 
35  var $usefor = 'SW';
36  var $defDoctype = 'P';
37  var $attrPrefix = "CVI"; // prefix attribute
38 
39  /**
40  * @var \Doc instance document
41  */
42  public $doc = null;
43  /**
44  * @var CVDoc profil document
45  */
46  private $pdoc = null;
47  // --------------------------------------------------------------------
48  function __construct($dbaccess = '', $id = '', $res = '', $dbid = 0)
49  {
50  // first construct acl array
51  if (isset($this->fromid)) $this->defProfFamId = $this->fromid; // it's a profil itself
52  // don't use Doc constructor because it could call this constructor => infinitive loop
54 
55  $this->setAcls();
56  }
57 
58  public function preConsultation()
59  {
60  $err = parent::preConsultation();
61  $this->injectCss();
62 
63  $ids = $this->getMultipleRawValues(MyAttributes::cv_idview);
64  $menus = $this->getMultipleRawValues(MyAttributes::cv_menu);
65  $vLabel = $this->getMultipleRawValues(MyAttributes::cv_lview);
66  foreach ($menus as $k => $menuId) {
67 
68  if ($menuId) {
69  $menuLabel = $this->getLocaleViewMenu($ids[$k]);
70 
71  if ($menuLabel && $menuLabel !== $menuId) {
72  $this->setValue(MyAttributes::cv_menu, sprintf("%s (%s)", $menuId, $menuLabel) , $k);
73  }
74  }
75  $label = $this->getLocaleViewLabel($ids[$k]);
76  if ($vLabel[$k] && $vLabel[$k] !== $label) {
77  $this->setValue(MyAttributes::cv_lview, sprintf("%s (%s)", $vLabel[$k], $label) , $k);
78  }
79  }
80 
81  return $err;
82  }
83 
84  public function preEdition()
85  {
86  $err = parent::preEdition();
87  $this->injectCss();
88  return $err;
89  }
90 
91  protected function injectCss()
92  {
93  global $action;
94  $action->parent->addCssRef("FDL/Layout/cvdoc_array_view.css");
95  }
96 
97  protected function postAffect(array $data, $more, $reset)
98  {
99  $this->setAcls();
100  }
101  function setAcls()
102  {
103  $this->extendedAcls = array();
104  $ti = $this->getMultipleRawValues("CV_IDVIEW");
105  $tl = $this->getMultipleRawValues("CV_LVIEW");
106  $tk = $this->getMultipleRawValues("CV_KVIEW");
107 
108  foreach ($tk as $k => $v) {
109  if ($ti[$k] == "") $cvk = "CV$k";
110  else $cvk = $ti[$k];
111  $this->extendedAcls[$cvk] = array(
112  "name" => $cvk,
113  "description" => $tl[$k]
114  );
115 
116  $this->acls[$cvk] = $cvk;
117  }
118  }
119 
120  function computeCreationViewLabel($idCreationView)
121  {
122  if ('' !== $idCreationView) {
123  $viewIds = $this->getAttributeValue(MyAttributes::cv_idview);
124  $viewLabels = $this->getAttributeValue(MyAttributes::cv_lview);
125  $viewIndex = array_search($idCreationView, $viewIds);
126  if (false !== $viewIndex) {
127  return sprintf("%s (%s)", $viewLabels[$viewIndex], $idCreationView);
128  } else {
129  return ' ';
130  }
131  } else {
132  return ' ';
133  }
134  }
135 
136  function isIdValid($value)
137  {
138  $err = "";
139  $sug = array();
140  $this->nbId++;
141 
142  $dc = new \DocCtrl($this->dbaccess);
143  $originals = $dc->dacls;
144 
145  if (!preg_match('!^[0-9a-z_-]+$!i', $value)) {
146  $err = sprintf(_("You must use only a-z, 0-9, _, - characters : \"%s\"") , $value);
147  } elseif (array_key_exists($value, $originals)) {
148  $err = _("Impossible to name a view like a control acl");
149  } else {
150  $id_list = $this->getMultipleRawValues('CV_IDVIEW');
151  $id_count = 0;
152  foreach ($id_list as $id) {
153  if ($id == $value) {
154  $id_count++;
155  }
156  }
157  if ($id_count > 1) {
158  $err = _("Impossible to have several identical names");
159  }
160  }
161  return array(
162  "err" => $err,
163  "sug" => $sug
164  );
165  }
166 
168  {
169  $err = '';
170  $sug = array();
171  if (strlen(trim($value)) == 0) {
172  $err = _("Label must not be empty");
173  }
174  return array(
175  'err' => $err,
176  'sug' => $sug
177  );
178  }
179 
180  function isCreationViewValid($idCreationView, $labelCreationView, $idViews)
181  {
182  $err = '';
183  if ('' !== $idCreationView) {
184  if (!is_array($idViews) || !in_array($idCreationView, $idViews)) {
185  $err = sprintf(___("creation view '%s' does not exists", "CVDOC") , $labelCreationView);
186  }
187  }
188  return $err;
189  }
190  /**
191  * Return view properties
192  * @param $vid
193  * @return array|false false if vid not found
194  */
195  function getView($vid)
196  {
197  $tv = $this->getArrayRawValues("cv_t_views");
198  foreach ($tv as $v) {
199  if ($v["cv_idview"] === $vid) {
200  // found it
201  foreach ($v as $k => $av) {
202  $v[strtoupper($k) ] = $av;
203  }
204  return $v;
205  }
206  }
207  return false;
208  }
209  /**
210  * @param string $vid view identifier
211  * @return string the locale label
212  */
213  public function getLocaleViewLabel($vid)
214  {
215  $key = $this->getPropertyValue("name") . "#label#" . $vid;
216  $lkey = _($key);
217  if ($lkey != $key) {
218  return $lkey;
219  }
220  $view = $this->getView($vid);
221  return isset($view["CV_LVIEW"]) ? $view["CV_LVIEW"] : sprintf(_("Unlabeled view (%s)") , $vid);
222  }
223  /**
224  * @param string $vid view identifier
225  * @return string the locale menu label
226  */
227  public function getLocaleViewMenu($vid)
228  {
229  $key = $this->getPropertyValue("name") . "#menu#" . $vid;
230  $lkey = _($key);
231  if ($lkey != $key) {
232  return $lkey;
233  }
234  $view = $this->getView($vid);
235  return isset($view["CV_MENU"]) ? $view["CV_MENU"] : sprintf(_("Unlabeled menu (%s)") , $vid);
236  }
237 
238  function getViews()
239  {
240  $ti = $this->getMultipleRawValues("CV_IDVIEW");
241  $tv = array();
242  foreach ($ti as $k => $v) {
243 
244  $tv[$v] = $this->getView($v);
245  }
246  return $tv;
247  }
248  /**
249  * get Views that can be displayed in a menu by example
250  */
251  public function getDisplayableViews()
252  {
253  $tv = $this->getArrayRawValues("cv_t_views");
254  $cud = ($this->doc->CanEdit() == "");
255  $displayableViews = array();
256  foreach ($tv as $v) {
257  $vid = $v[MyAttributes::cv_idview];
258  $mode = $v[MyAttributes::cv_kview];
259  if ($v[MyAttributes::cv_displayed] !== "no") {
260  switch ($mode) {
261  case "VCONS":
262  if ($this->control($vid) == "") {
263  $displayableViews[] = $v;
264  }
265  break;
266 
267  case "VEDIT":
268  if ($cud && $this->control($vid) == "") {
269  $displayableViews[] = $v;
270  }
271  break;
272  }
273  }
274  }
275  return $displayableViews;
276  }
277 
278  function preImport(array $extra = array())
279  {
280  return $this->verifyAllConstraints();
281  }
282 
283  function postStore()
284  {
285 
286  $ti = $this->getMultipleRawValues("CV_IDVIEW");
287  foreach ($ti as $k => $v) {
288  if ($v == "") $ti[$k] = "CV$k";
289  }
290  $this->setValue("CV_IDVIEW", $ti);
291  }
292 
293  function docControl($aclname, $strict = false)
294  {
295  return \Doc::control($aclname, $strict);
296  }
297  /**
298  * Special control in case of dynamic controlled profil
299  *
300  * @param string $aclname
301  * @param bool $strict
302  *
303  * @return string
304  */
305  function control($aclname, $strict = false)
306  {
307 
308  $err = $this->docControl($aclname, $strict);
309  if ($err == "") return $err; // normal case
310  if ($this->getRawValue("DPDOC_FAMID") > 0) {
311  if ($this->doc) {
312  // special control for dynamic users
313  if ($this->pdoc === null) {
314  $pdoc = createDoc($this->dbaccess, $this->fromid, false);
315  $pdoc->doctype = "T"; // temporary
316  // $pdoc->setValue("DPDOC_FAMID",$this->getRawValue("DPDOC_FAMID"));
317  $err = $pdoc->Add();
318  if ($err != "") return "CVDoc::Control:" . $err; // can't create profil
319  $pdoc->acls = $this->acls;
320  $pdoc->extendedAcls = $this->extendedAcls;
321  $pdoc->setProfil($this->profid, $this->doc);
322 
323  $this->pdoc = & $pdoc;
324  }
325 
326  $err = $this->pdoc->docControl($aclname, $strict);
327  }
328  }
329  return $err;
330  }
331 
332  function Set(&$doc)
333  {
334  if (!isset($this->doc)) {
335  $this->doc = & $doc;
336  }
337  }
338  /**
339  * retrieve first compatible view
340  * @param bool $edition if true edition view else consultation view
341  * @return string view definition "cv_idview", "cv_mskid"
342  */
343  function getPrimaryView($edition = false)
344  {
345  $view = '';
346  if ($this->doc) {
347  if ($edition && (!$this->doc->id)) {
348  $vidcreate = $this->getRawValue("cv_idcview");
349  if ($vidcreate) {
350  // control must be done by the caller
351  $viewU = $this->getView($vidcreate); // use it first if exist
352  $view = array();
353  foreach ($viewU as $k => $v) $view[strtolower($k) ] = $v;
354  }
355  }
356 
357  if (!$view) {
358  $type = ($edition) ? "VEDIT" : "VCONS";
359  // search preferred view
360  $tv = $this->getArrayRawValues("cv_t_views");
361  // sort
362  usort($tv, "cmp_cvorder3");
363  foreach ($tv as $k => $v) {
364  if ($v["cv_order"] > 0) {
365  if ($v["cv_kview"] == $type) {
366  $err = $this->control($v["cv_idview"]); // control special view
367  if ($err == "") {
368  $view = $v;
369  break;
370  }
371  }
372  }
373  }
374  }
375  }
376  return $view;
377  }
378 }
preImport(array $extra=array())
computeCreationViewLabel($idCreationView)
global $action
verifyAllConstraints($stoptofirst=true, &$info=array())
Definition: Class.Doc.php:4645
getLocaleViewMenu($vid)
getPropertyValue($prop)
Definition: Class.Doc.php:2114
postAffect(array $data, $more, $reset)
Definition: Class.CVDoc.php:97
__construct($dbaccess= '', $id= '', $res= '', $dbid=0)
Definition: Class.CVDoc.php:48
isLabelValid($value)
if(!function_exists('pgettext')) ___($message, $context="")
Definition: Lib.Common.php:46
isIdValid($value)
getArrayRawValues($idAttr, $index=-1)
Definition: Class.Doc.php:3292
__construct($dbaccess= '', $id= '', $res= '', $dbid=0)
docControl($aclname, $strict=false)
$fromid
Definition: Class.Doc.php:366
setValue($attrid, $value, $index=-1, &$kvalue=null)
Definition: Class.Doc.php:3528
getMultipleRawValues($idAttr, $def="", $index=-1)
Definition: Class.Doc.php:3240
createDoc($dbaccess, $fromid, $control=true, $defaultvalues=true, $temporary=false)
getPrimaryView($edition=false)
isCreationViewValid($idCreationView, $labelCreationView, $idViews)
getLocaleViewLabel($vid)
if($file) if($subject==""&&$file) if($subject=="") $err
control($aclname, $strict=false)
getRawValue($idAttr, $def="")
Definition: Class.Doc.php:3117
$value
$data
← centre documentaire © anakeen