Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
autocompletion.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 /**
7  * Generated Header (not documented yet)
8  *
9  * @author Anakeen
10  * @version $Id: autocompletion.php,v 1.22 2009/01/13 14:17:36 eric Exp $
11  * @package FDL
12  * @subpackage
13  */
14 /**
15  */
16 
17 include_once ("FDL/enum_choice.php");
18 
20 {
21  // list of choice to be insert in attribute values
22  $docid = GetHttpVars("docid"); // document being edition
23  if (!$docid) {
24  $docid = GetHttpVars("classid", 0);
25  } // in case of docid is null
26  $attrid = GetHttpVars("attrid", 0); // attribute need to enum
27  $sorm = GetHttpVars("sorm", "single"); // single or multiple
28  $index = GetHttpVars("index", ""); // index of the attributes for arrays
29  $domindex = GetHttpVars("domindex", ""); // index in dom of the attributes for arrays
30  $enum = GetHttpVars("enum"); // special case when it is an enum
31  $skey = GetHttpVars("skey"); // use only when enum (filter key)
32  $acId = GetHttpVars("acid", ""); // autocompletion Id
33  $defaultphpfunc = (getHttpVars("defaultphpfunc", "no") === "yes");
34  if (!empty($_SERVER["HTTP_HOST"])) {
35  header('Content-type: text/xml; charset=utf-8');
36  }
37 
38  if ($enum != "") {
39  $attrid = $enum;
40  }
41  $err = '';
42  $canitem = false;
43  $dbaccess = $action->dbaccess;
44 
46  $docid = $doc->id;
47  $action->lay->Set("count", 0);
48  $oattr = false;
49  if ($docid == 0) {
50  try {
51  if ($acId == '') {
52  throw new Exception(sprintf(_("Missing or empty 'acid'.")));
53  }
54  $ac = $action->Read(sprintf('autocompletion.%s', $acId));
55  if ($ac === '' || $ac === '--') {
56  throw new Exception(sprintf(_("Missing value for 'autocompletion.%s'.") , $acId));
57  }
58  if (!isset($ac['phpfile'])) {
59  throw new Exception(sprintf(_("Missing 'phpfile' in 'autocompletion.%s'.") , $acId));
60  }
61  if (!isset($ac['phpfunc'])) {
62  throw new Exception(sprintf(_("Missing 'phpfunc' in 'autocompletion.%s'.") , $acId));
63  }
64  $label = isset($ac['label']) ? $ac['label'] : _("no label");
65  $phpfile = $ac['phpfile'];
66  $phpfunc = $ac['phpfunc'];
67  // specific interface
68  $index = "";
69  $format = "";
70  $repeat = false;
71  $order = 0;
72  $link = "";
73  $visibility = "W";
74  $needed = "N";
75  $isInTitle = false;
76  $isInAbstract = false;
77  $fieldSet = null;
78  $elink = "";
79  $phpconstraint = "";
80  $usefor = "";
81  $eformat = "";
82  $options = "";
83  $oattr = new NormalAttribute($attrid, $doc->id, $label, "text", $format, $repeat, $order, $link, $visibility, $needed, $isInTitle, $isInAbstract, $fieldSet, $phpfile, $phpfunc, $elink, $phpconstraint, $usefor, $eformat, $options);
84  }
85  catch(Exception $e) {
86  $err = $e->getMessage();
87  }
88  } else {
89  $oattr = $doc->GetAttribute($attrid);
90  }
91  if (!$oattr) {
92  $err = sprintf(_("unknown attribute %s") , $attrid);
93  } else {
94  if ($defaultphpfunc) {
96  }
97  }
98 
99  $xmlDocument = new DOMDocument();
100  $xmlRoot = $xmlDocument->createElement("status");
101  $xmlDocument->appendChild($xmlRoot);
102 
103  if ($err == "") {
104  $notalone = "true";
105 
106  if (preg_match("/([a-z]*)-alone/", $sorm, $reg)) {
107  $notalone = "false";
108  }
109  $action->lay->set("notalone", $notalone);
110 
111  $action->parent->AddJsRef($action->GetParam("CORE_STANDURL") . "app=FDL&action=ENUMCHOICEJS");
112  $phpfunc = $oattr->phpfunc;
113  // capture title
114  $ititle = "";
115 
116  if ((!empty($phpfunc)) && $phpfunc[0] == "[") {
117  if (preg_match('/\[(.*)\](.*)/', $phpfunc, $reg)) {
118  $oattr->phpfunc = $reg[2];
119 
120  $ititle = addslashes($reg[1]);
121  }
122  }
123 
124  $linkprefix = "ilink_"; // in coherence with editutil.php
125  $action->lay->set("ititle", $ititle);
126  // Utf8_decode_POST(); // because default is iso8859-1
127  if ($enum != "") {
128  // Correct multiple call error
129  $oattr = clone $oattr;
130  //if (seems_utf8($skey)) $skey=utf8_decode($skey);
131  $canitem = (!$oattr->existEnum(trim($skey)));
132  if (trim($skey) == '' && strpos($oattr->phpfunc, "linkenum") !== false) {
133  $oattr->getEnum();
134  }
135 
136  $oattr->phpfile = "fdl.php";
137  $oattr->phpfunc = sprintf("fdlGetEnumValues('%s,'%s,'%s):li_%s,%s", $oattr->docid, $oattr->id, str_replace(array(
138  ')',
139  '(',
140  ','
141  ) , array(
142  '&rpar;',
143  '&lpar;',
144  '&comma;'
145  ) , $skey) , $oattr->id, $oattr->id);
146  } elseif ($oattr->type == "docid" || $oattr->type == "account") {
147  $aname = $oattr->id;
148  $famid = $oattr->format;
149  $multi = $oattr->getOption("multiple");
150  $cible = ($multi == "yes") ? "mdocid_work" : "";
151  if (!$oattr->phpfile) {
152  $filter = array(); //no filter by default
153  $sfilter = '""';
154  $idid = "initid"; //if there's no docrev option (or it's present but not fixed), use initid to have the latest.
155  $docrev = $oattr->getOption("docrev");
156  if ($docrev == "fixed") {
157  $idid = "id";
158  } elseif ($docrev != "latest") {
159  //if $docrev is neither fixed nor latest it should be state=...
160  //if not, we'll just ignore the option
161  $matches = array();
162  if (preg_match("/^state\(([a-zA-Z0-9_:-]+)\)/", $docrev, $matches)) {
163  $filter[] = "state='" . pg_escape_string($matches[1]) . "'";
164  }
165  }
166  //make $filter safe to pass in a string for getResPhpFunc.
167  if (count($filter) == 0) $sfilter = serialize($filter);
168  $oattr->phpfunc = "lfamily(D,'$famid',${linkprefix}${aname},0,$sfilter,'$idid):${cible}${aname},${linkprefix}${aname}";
169  $oattr->phpfile = "fdl.php";
170  } else {
171  //getDeclaration(D,ACOM_ENT_ID,ILINK_ACOM_DEPOTDECL):ACOM_DEPOTDECL,ILINK_ACOM_DEPOTDECL
172  $phpfunc = preg_replace('/([\s|,|:|\(])CT([\s|,|\)]|$)/', '$1' . $linkprefix . $aname . '$2', $oattr->phpfunc);
173  $phpfunc = str_replace("):$aname,", "):${cible}${aname},", $phpfunc);
174  $phpfunc = str_replace("):" . strtoupper($aname) . ",", "):${cible}${aname},", $phpfunc);
175  $oattr->phpfunc = $phpfunc;
176  }
177  }
178  $oattr->phpfunc = preg_replace_callback('/([\s|,|:|\(])CT\[([^]]+)\]/', function ($matches) use ($linkprefix)
179  {
180  return $matches[1] . $linkprefix . strtolower($matches[2]);
181  }
182  , $oattr->phpfunc);
183 
184  $res = getResPhpFunc($doc, $oattr, $rargids, $tselect, $tval, true, $index);
185 
186  if (!is_array($res)) {
187  if ($res == "") {
188  $res = sprintf(_("error in calling function %s\n%s") , $oattr->phpfunc, $res);
189  }
190  $err = $res;
191  }
192  if ($err == "") {
193  if (count($res) == 0) {
194  $err = sprintf(_("no match for %s") , $oattr->getLabel());
195  if ($enum) {
196  if (!$canitem) {
197  $err = sprintf(_("existing key item %s") , $skey);
198  } else {
199  if ($oattr->getOption("etype") == "free" || $action->getArgument("usefor") == "D") {
200  $res = array(
201  array(
202  sprintf(_("free item %s") , $skey) ,
203  $skey . ' ' . _("(free item)") ,
204  $skey
205  )
206  );
207  $err = "";
208  } elseif ($oattr->getOption("etype") == "open") {
209  $res = array(
210  array(
211  sprintf(_("new item %s") , $skey) ,
212  $skey . ' ' . _("(new item)") ,
213  $skey
214  )
215  );
216  $err = "";
217  } else {
218  $err = sprintf(_("unknow item %s") , $skey);
219  }
220  }
221  }
222  } else {
223  if ($enum && (trim($skey) != "")) {
224  foreach ($res as $kv) { // verify existed key
225  if (($kv[1] == trim($skey)) || ($kv[2] == trim($skey))) $canitem = false;
226  }
227  if ($canitem) {
228  if ($oattr->getOption("etype") == "free") {
229  $res[] = array(
230  sprintf(_("free item %s") , $skey) ,
231  $skey . ' ' . _("(free item)") ,
232  $skey
233  );
234  $err = "";
235  } elseif ($oattr->getOption("etype") == "open") {
236  $res[] = array(
237  sprintf(_("new item %s") , $skey) ,
238  $skey . ' ' . _("(new item)") ,
239  $skey
240  );
241  $err = "";
242  }
243  }
244  }
245  }
246 
247  if ($err == "") {
248  $xmlCibles = $xmlDocument->createElement("cibles");
249  // add index for return args only if the element is not in a array
250  foreach ($rargids as $k => $noUsed) {
251  $linkprefix = "ilink_";
252  $isILink = false;
253  $attrId = $rargids[$k];
254  if (substr($attrId, 0, strlen($linkprefix)) == $linkprefix) {
255  $attrId = substr($attrId, strlen($linkprefix));
256  $isILink = true;
257  }
258  $docAttr = $doc->getAttribute($attrId);
259  if (is_object($docAttr) && !$docAttr->inArray()) {
260  $targid = trim(strtolower($attrId));
261  } else {
262  $targid = trim(strtolower($attrId . $domindex));
263  }
264  if ($isILink) {
265  $targid = $linkprefix . $targid;
266  }
267  $xmlCible = $xmlDocument->createElement("cible", $targid);
268  $xmlCibles->appendChild($xmlCible);
269  }
270  $xmlRoot->appendChild($xmlCibles);
271 
272  foreach ($res as $k => $v) {
273 
274  $xmlOption = $xmlDocument->createElement("option");
275  $xmlOption->setAttribute("value", $k);
276  //title
277  $title = array_shift($v);
278  $xmlOptionTitle = $xmlDocument->createElement("title");
279  $xmlOptionTitleCData = $xmlDocument->createCDATASection($title);
280  $xmlOptionTitle->appendChild($xmlOptionTitleCData);
281 
282  $xmlOption->appendChild($xmlOptionTitle);
283  //values
284  $xmlOptionValues = $xmlDocument->createElement("values");
285  foreach ($v as $value) {
286  $xmlOptionCurrentValue = $xmlDocument->createElement("val");
287  $xmlOptionCurrentValueCData = $xmlDocument->createCDATASection($value);
288  $xmlOptionCurrentValue->appendChild($xmlOptionCurrentValueCData);
289  $xmlOptionValues->appendChild($xmlOptionCurrentValue);
290  }
291 
292  $xmlOption->appendChild($xmlOptionValues);
293 
294  $xmlRoot->appendChild($xmlOption);
295  }
296 
297  $xmlRoot->setAttribute("count", count($tselect));
298  }
299  }
300  }
301 
302  if ($err) {
303  $xmlRoot->setAttribute("warning", $err);
304  }
305 
306  $action->lay->noparse = true;
307  $action->lay->template = $xmlDocument->saveXML();
308 
309  $action->lay->action = null; // don't want parameters - conflict with possible parameters
310 
311 }
312 
314 {
315 
316  global $_POST, $ZONE_ARGS;
317 
318  foreach ($_POST as $k => $v) {
319  if (is_array($v)) {
320  foreach ($v as $kv => $vv) $ZONE_ARGS[$k][$kv] = utf8_decode($vv);
321  } else {
322  $ZONE_ARGS[$k] = utf8_decode($v);
323  }
324  }
325 }
326 
328 {
329  if ($oattr->type == 'account') {
330  /* Setup default fdlGetAccounts() call */
331  $oattr->phpfile = 'fdl.php';
332  $options = $oattr->options;
333  if ($oattr->format) {
334  if ($options) {
335  $options.= '|';
336  }
337  $options.= sprintf("family=%s", $oattr->format);
338  }
339  $oattr->phpfunc = sprintf('fdlGetAccounts(CT,15,"%s"):%s,CT', str_replace('"', '\\"', $options) , $oattr->id);
340  } else if ($oattr->type == 'docid') {
341  /* Let autocompletion() setup a default lfamily() call */
342  $oattr->phpfile = '';
343  $oattr->phpfunc = '';
344  }
345 }
getResPhpFunc(Doc &$doc, NormalAttribute &$oattr, &$rargids, &$tselect, &$tval, $whttpvars=true, $index="")
Definition: enum_choice.php:62
global $action
global $_POST
Definition: chgpasswd.php:17
Set($name, &$parent)
$docid
Definition: cleanFamily.php:13
autocompletion(Action &$action)
Read($k, $d="")
Utf8_decode_POST()
getHttpVars($name, $def="", $scope="all")
Definition: Lib.Http.php:124
global $_SERVER
new_Doc($dbaccess, $id= '', $latest=false)
resetAttrDefaultPhpFunc(NormalAttribute &$oattr)
static getArgument($k, $def= '')
$dbaccess
Definition: checkVault.php:17
if($file) if($subject==""&&$file) if($subject=="") $err
$value
← centre documentaire © anakeen