PageRenderTime 58ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/Action/Fdl/autocompletion.php

https://github.com/CircleCode/dynacase-core
PHP | 314 lines | 275 code | 16 blank | 23 comment | 25 complexity | bd7cb3db12884c41929710cc795ae268 MD5 | raw file
  1. <?php
  2. /*
  3. * @author Anakeen
  4. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License
  5. * @package FDL
  6. */
  7. /**
  8. * Generated Header (not documented yet)
  9. *
  10. * @author Anakeen
  11. * @version $Id: autocompletion.php,v 1.22 2009/01/13 14:17:36 eric Exp $
  12. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License
  13. * @package FDL
  14. * @subpackage
  15. */
  16. /**
  17. */
  18. include_once ("FDL/enum_choice.php");
  19. function autocompletion(Action & $action)
  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. if (!empty($_SERVER["HTTP_HOST"])) {
  33. header('Content-type: text/xml; charset=utf-8');
  34. }
  35. if ($enum != "") {
  36. $attrid = $enum;
  37. }
  38. $err = '';
  39. $canitem = false;
  40. $dbaccess = $action->GetParam("FREEDOM_DB");
  41. $doc = new_Doc($dbaccess, $docid);
  42. $docid = $doc->id;
  43. $action->lay->Set("count", 0);
  44. if ($docid == 0) {
  45. // specific interface
  46. $value = "";
  47. $label = GetHttpVars("label", _("no label"));
  48. $index = "";
  49. $jsevent = "";
  50. $format = "";
  51. $repeat = false;
  52. $order = 0;
  53. $link = "";
  54. $visibility = "W";
  55. $needed = "N";
  56. $isInTitle = false;
  57. $isInAbstract = false;
  58. $phpfile = GetHttpVars("phpfile");
  59. $phpfunc = GetHttpVars("phpfunc");
  60. $fieldSet = null;
  61. $elink = "";
  62. $phpconstraint = "";
  63. $usefor = "";
  64. $eformat = "";
  65. $options = "";
  66. $oattr = new NormalAttribute($attrid, $doc->id, $label, "text", $format, $repeat, $order, $link, $visibility, $needed, $isInTitle, $isInAbstract, $fieldSet, $phpfile, $phpfunc, $elink, $phpconstraint, $usefor, $eformat, $options);
  67. } else {
  68. $oattr = $doc->GetAttribute($attrid);
  69. if (!$oattr) {
  70. $err = sprintf(_("unknown attribute %s") , $attrid);
  71. }
  72. }
  73. $xmlDocument = new DOMDocument();
  74. $xmlRoot = $xmlDocument->createElement("status");
  75. $xmlDocument->appendChild($xmlRoot);
  76. if ($err == "") {
  77. $notalone = "true";
  78. if (preg_match("/([a-z]*)-alone/", $sorm, $reg)) {
  79. $sorm = $reg[1];
  80. $notalone = "false";
  81. }
  82. $action->lay->set("notalone", $notalone);
  83. $action->parent->AddJsRef($action->GetParam("CORE_STANDURL") . "app=FDL&action=ENUMCHOICEJS");
  84. $phpfunc = $oattr->phpfunc;
  85. // capture title
  86. $ititle = "";
  87. if ((!empty($phpfunc)) && $phpfunc[0] == "[") {
  88. if (preg_match('/\[(.*)\](.*)/', $phpfunc, $reg)) {
  89. $oattr->phpfunc = $reg[2];
  90. $ititle = addslashes($reg[1]);
  91. }
  92. }
  93. $linkprefix = "ilink_"; // in coherence with editutil.php
  94. $action->lay->set("ititle", $ititle);
  95. // Utf8_decode_POST(); // because default is iso8859-1
  96. if ($enum != "") {
  97. // Correct multiple call error
  98. $oattr = clone $oattr;
  99. //if (seems_utf8($skey)) $skey=utf8_decode($skey);
  100. $canitem = (!$oattr->existEnum(trim($skey)));
  101. if (trim($skey) == '' && strpos($oattr->phpfunc, "linkenum") !== false) {
  102. $oattr->getEnum();
  103. }
  104. $eval = $oattr->phpfunc;
  105. $oattr->phpfile = "fdl.php";
  106. $eval = str_replace(array(
  107. '\,',
  108. '\.'
  109. ) , array(
  110. '&comma;',
  111. '&point;'
  112. ) , $eval);
  113. $oattr->phpfunc = sprintf("fdlGetEnumValues('%s,'%s,'%s):li_%s,%s", $oattr->docid, $oattr->id, str_replace(array(
  114. ')',
  115. '(',
  116. ','
  117. ) , array(
  118. '&rpar;',
  119. '&lpar;',
  120. '&comma;'
  121. ) , $skey) , $oattr->id, $oattr->id);
  122. } elseif ($oattr->type == "docid" || $oattr->type == "account") {
  123. $aname = $oattr->id;
  124. $famid = $oattr->format;
  125. $multi = $oattr->getOption("multiple");
  126. $cible = ($multi == "yes") ? "mdocid_work" : "";
  127. if (!$oattr->phpfile) {
  128. $filter = array(); //no filter by default
  129. $sfilter = '';
  130. $idid = "initid"; //if there's no docrev option (or it's present but not fixed), use initid to have the latest.
  131. $docrev = $oattr->getOption("docrev");
  132. if ($docrev == "fixed") {
  133. $idid = "id";
  134. } elseif ($docrev != "latest") {
  135. //if $docrev is neither fixed nor latest it should be state=...
  136. //if not, we'll just ignore the option
  137. $matches = array();
  138. if (preg_match("/^state\(([a-zA-Z0-9_:-]+)\)/", $docrev, $matches)) {
  139. $filter[] = "state='" . pg_escape_string($matches[1]) . "'";
  140. }
  141. }
  142. //make $filter safe to pass in a string for getResPhpFunc.
  143. if (count($filter) == 0) $sfilter = serialize($filter);
  144. $oattr->phpfunc = "lfamily(D,'$famid',${linkprefix}${aname},0,$sfilter,'$idid):${cible}${aname},${linkprefix}${aname}";
  145. $oattr->phpfile = "fdl.php";
  146. } else {
  147. //getDeclaration(D,ACOM_ENT_ID,ILINK_ACOM_DEPOTDECL):ACOM_DEPOTDECL,ILINK_ACOM_DEPOTDECL
  148. $phpfunc = preg_replace('/([\s|,|:|\(])CT([\s|,|\)]|$)/', '$1' . $linkprefix . $aname . '$2', $oattr->phpfunc);
  149. $phpfunc = str_replace("):$aname,", "):${cible}${aname},", $phpfunc);
  150. $phpfunc = str_replace("):" . strtoupper($aname) . ",", "):${cible}${aname},", $phpfunc);
  151. $oattr->phpfunc = $phpfunc;
  152. }
  153. }
  154. $oattr->phpfunc = preg_replace_callback('/([\s|,|:|\(])CT\[([^]]+)\]/', function ($matches) use ($linkprefix)
  155. {
  156. return $matches[1] . $linkprefix . strtolower($matches[2]);
  157. }
  158. , $oattr->phpfunc);
  159. $res = getResPhpFunc($doc, $oattr, $rargids, $tselect, $tval, true, $index);
  160. if (!is_array($res)) {
  161. if ($res == "") {
  162. $res = sprintf(_("error in calling function %s\n%s") , $oattr->phpfunc, $res);
  163. }
  164. $err = $res;
  165. }
  166. if ($err == "") {
  167. if (count($res) == 0) {
  168. $err = sprintf(_("no match for %s") , $oattr->getLabel());
  169. if ($enum) {
  170. if (!$canitem) {
  171. $err = sprintf(_("existing key item %s") , $skey);
  172. } else {
  173. if ($oattr->getOption("etype") == "free" || $action->getArgument("usefor") == "D") {
  174. $res = array(
  175. array(
  176. sprintf(_("free item %s") , $skey) ,
  177. $skey . ' ' . _("(free item)") ,
  178. $skey
  179. )
  180. );
  181. $err = "";
  182. } elseif ($oattr->getOption("etype") == "open") {
  183. $res = array(
  184. array(
  185. sprintf(_("new item %s") , $skey) ,
  186. $skey . ' ' . _("(new item)") ,
  187. $skey
  188. )
  189. );
  190. $err = "";
  191. } else {
  192. $err = sprintf(_("unknow item %s") , $skey);
  193. }
  194. }
  195. }
  196. } else {
  197. if ($enum && (trim($skey) != "")) {
  198. foreach ($res as $kr => $kv) { // verify existed key
  199. if (($kv[1] == trim($skey)) || ($kv[2] == trim($skey))) $canitem = false;
  200. }
  201. if ($canitem) {
  202. if ($oattr->getOption("etype") == "free") {
  203. $res[] = array(
  204. sprintf(_("free item %s") , $skey) ,
  205. $skey . ' ' . _("(free item)") ,
  206. $skey
  207. );
  208. $err = "";
  209. } elseif ($oattr->getOption("etype") == "open") {
  210. $res[] = array(
  211. sprintf(_("new item %s") , $skey) ,
  212. $skey . ' ' . _("(new item)") ,
  213. $skey
  214. );
  215. $err = "";
  216. }
  217. }
  218. }
  219. }
  220. if ($err == "") {
  221. $xmlCibles = $xmlDocument->createElement("cibles");
  222. // add index for return args only if the element is not in a array
  223. while (list($k, $v) = each($rargids)) {
  224. $linkprefix = "ilink_";
  225. $isILink = false;
  226. $attrId = $rargids[$k];
  227. if (substr($attrId, 0, strlen($linkprefix)) == $linkprefix) {
  228. $attrId = substr($attrId, strlen($linkprefix));
  229. $isILink = true;
  230. }
  231. $docAttr = $doc->getAttribute($attrId);
  232. if (is_object($docAttr) && !$docAttr->inArray()) {
  233. $targid = trim(strtolower($attrId));
  234. } else {
  235. $targid = trim(strtolower($attrId . $domindex));
  236. }
  237. if ($isILink) {
  238. $targid = $linkprefix . $targid;
  239. }
  240. $xmlCible = $xmlDocument->createElement("cible", $targid);
  241. $xmlCibles->appendChild($xmlCible);
  242. }
  243. $xmlRoot->appendChild($xmlCibles);
  244. foreach ($res as $k => $v) {
  245. $xmlOption = $xmlDocument->createElement("option");
  246. $xmlOption->setAttribute("value", $k);
  247. //title
  248. $title = array_shift($v);
  249. $xmlOptionTitle = $xmlDocument->createElement("title");
  250. $xmlOptionTitleCData = $xmlDocument->createCDATASection($title);
  251. $xmlOptionTitle->appendChild($xmlOptionTitleCData);
  252. $xmlOption->appendChild($xmlOptionTitle);
  253. //values
  254. $xmlOptionValues = $xmlDocument->createElement("values");
  255. foreach ($v as $value) {
  256. $xmlOptionCurrentValue = $xmlDocument->createElement("val");
  257. $xmlOptionCurrentValueCData = $xmlDocument->createCDATASection($value);
  258. $xmlOptionCurrentValue->appendChild($xmlOptionCurrentValueCData);
  259. $xmlOptionValues->appendChild($xmlOptionCurrentValue);
  260. }
  261. $xmlOption->appendChild($xmlOptionValues);
  262. $xmlRoot->appendChild($xmlOption);
  263. }
  264. $xmlRoot->setAttribute("count", count($tselect));
  265. }
  266. }
  267. }
  268. if ($err) {
  269. $xmlRoot->setAttribute("warning", $err);
  270. }
  271. $action->lay->noparse = true;
  272. $action->lay->template = $xmlDocument->saveXML();
  273. $action->lay->action = null; // don't want parameters - conflict with possible parameters
  274. }
  275. function Utf8_decode_POST()
  276. {
  277. global $_POST, $ZONE_ARGS;
  278. foreach ($_POST as $k => $v) {
  279. if (is_array($v)) {
  280. foreach ($v as $kv => $vv) $ZONE_ARGS[$k][$kv] = utf8_decode($vv);
  281. } else {
  282. $ZONE_ARGS[$k] = utf8_decode($v);
  283. }
  284. }
  285. }
  286. ?>