Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
modfamilyparameter.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 
7 include_once ('FDL/Class.Doc.php');
8 /**
9  * Change value of family parameter
10  * @param Action $action
11  */
13 {
14 
15  $usage = new ActionUsage($action);
16  $famid = $usage->addRequiredParameter("famid", "family id");
17  $attrid = $usage->addRequiredParameter("attrid", "attribute id");
18  $value = $usage->addOptionalParameter("value", "value in field");
19  $usage->setStrictMode();
20  $usage->verify();
21 
22  header('Content-type: text/xml; charset=utf-8');
23 
24  $mb = microtime();
25 
26  $out = array(
27  "errors" => "",
28  "success" => true,
29  "parameterid" => $attrid,
30  "modify" => false
31  );
32  /*
33  * @var DocFam $doc
34  */
35  $doc = new_Doc($action->dbaccess, $famid);
36  if ($doc->isAlive()) {
37  $attr = $doc->getAttribute($attrid);
38  if (!$attr) {
39  $out["success"] = false;
40  $out["errors"] = sprintf(_("Parameter [%s] not found in family [%s]") , $attrid, $famid);
41  } else {
42  if ($attr->type == "array" && $value) {
43  $modify = false;
44  $result = array();
45  $i = 0;
46  /*
47  * @var array $value
48  */
49  foreach ($value as $v) {
50  $key = $v["attrid"];
51  if (!array_attrid_exists($key, $result)) {
52  $result[$i] = array(
53  "attrid" => $key
54  );
55  foreach ($value as $e) {
56  if ($e["attrid"] == $key) {
57  $result[$i]["value"][] = $e["value"][0];
58  }
59  }
60  $i++;
61  }
62  }
63  $err = '';
64  foreach ($result as $v) {
65  $val = "";
66  if (!empty($v["value"])) {
67  $val = $doc->arrayToRawValue($v["value"]);
68  }
69  $val = trim($val);
70  $ownParams = $doc->getOwnParams();
71  $oldValue = $ownParams[$v["attrid"]];
72  if ($oldValue != $val) {
73  $modify = true;
74  }
75  $err = $doc->setParam($v["attrid"], $val);
76  if ($err) break;
77  }
78  if (!$err) $err = $doc->store();
79  if ($err) {
80  $out["success"] = false;
81  $out["errors"] = $err;
82  } elseif ($modify) {
83  $out["modify"] = true;
84  }
85  } else {
86  $oldValue = $doc->getParameterRawValue($attrid);
87  $err = $doc->setParam($attrid, $value);
88  if (!$err) $err = $doc->store();
89  if ($err) {
90  $out["success"] = false;
91  $out["errors"] = $err;
92  } else {
93  if ($oldValue != $value) {
94  $out["modify"] = true;
95  }
96  }
97  }
98  }
99  } else {
100  $out["success"] = false;
101  $out["errors"] = sprintf(_("Doucment [%s] not found") , $famid);
102  }
103  $action->lay->set("success", $out["success"]);
104  $action->lay->set("warning", htmlspecialchars($out["errors"]));
105  $action->lay->set("count", 1);
106  $action->lay->set("parameterid", $out["parameterid"]);
107  $action->lay->set("modify", $out["modify"]);
108  $action->lay->set("delay", microtime_diff(microtime() , $mb));
109 }
110 /**
111  * Return true if key is in attrid of array
112  * @param string $key key to search
113  * @param array $array array to search in
114  * @return bool
115  */
116 function array_attrid_exists($key, array $array)
117 {
118  foreach ($array as $val) {
119  if ($val["attrid"] == $key) {
120  return true;
121  }
122  }
123  return false;
124 }
global $action
modfamilyparameter(Action &$action)
microtime_diff($a, $b)
Definition: Lib.Common.php:302
Verify arguments for action function.
new_Doc($dbaccess, $id= '', $latest=false)
if($file) if($subject==""&&$file) if($subject=="") $err
array_attrid_exists($key, array $array)
$usage
$value
← centre documentaire © anakeen