Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
modenumitems.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 /**
7  * Modify Enum items
8  *
9  * @author Anakeen
10  * @version $Id: editchangestate.php,v 1.8 2008/10/02 15:41:45 eric Exp $
11  * @package FDL
12  * @subpackage
13  */
14 /**
15  */
16 
17 include_once "FDL/Class.Doc.php";
18 /**
19  * Modify Enum Items
20  * @param Action &$action current action
21  * @global string $famid Http var : family id
22  * @global string $enumid Http var : enum id
23  * @global string $enumid Http var : modification properties (json encoded)
24  */
26 {
27  $usage = new ActionUsage($action);
28  $usage->setDefinitionText("Return list of items for an enumerate attribute");
29  $famid = $usage->addRequiredParameter("famid", "Family identifier", function ($fid)
30  {
31  $fam = new_doc("", $fid);
32  if (!$fam->isAlive()) {
33  return sprintf("document \"%s\" not found", $fid);
34  } elseif ($fam->doctype != 'C') {
35  return sprintf("document \"%s\" is not a family", $fid);
36  }
37  return '';
38  });
39  $enumId = $usage->addRequiredParameter("enumid", "Enum identifier", function ($eid) use ($famid)
40  {
41  $fam = new_doc("", $famid);
42  $oa = $fam->getAttribute($eid);
43  if (!$oa) {
44  return sprintf("enumerate attribute \"%s\" not found", $eid);
45  } elseif ($oa->type != "enum") {
46  return sprintf("attribute \"%s\" is not an enum. Is is a \"%s\" ", $eid, $oa->type);
47  }
48  return '';
49  });
50 
51  $modifications = $usage->addRequiredParameter("items", "Items modifications", function ($items)
52  {
53  $dit = json_decode($items);
54  if (empty($dit)) {
55  return sprintf("items \"%s\" must be json encoded structure", $items);
56  } elseif (!is_array($dit)) {
57  return sprintf("items \"%s\" must be json array", $items);
58  }
59  return '';
60  });
61  $usage->setStrictMode(false);
62  $out = array();
63  try {
64  $usage->verify(true);
65  }
66  catch(\Dcp\ApiUsage\Exception $e) {
67  $err = $e->getDcpMessage();
68  $out = array(
69  "error" => $err
70  );
71  }
72  if (empty($err)) {
73  $fam = new_doc("", $famid);
74  /*
75  * @var NormalAttribute $oa
76  */
77  $oa = $fam->getAttribute($enumId);
78  $enums = DocEnum::getFamilyEnums($fam->id, $oa->id);
79 
80  $items = json_decode($modifications, true);
81  foreach ($items as $item) {
82  if (empty($item["key"])) {
83  $err = sprintf("all keys must be not empty");
84  }
85  }
86  if (empty($err)) {
87  $enumKeys = array_keys($oa->getEnumLabel());
88  $es = new EnumStructure();
89  $err = '';
90  try {
91  /*
92  * @var EnumStructure $item
93  */
94  foreach ($items as $item) {
95  $es->affect($item);
96  if (in_array($es->key, $enumKeys)) {
97  DocEnum::modifyEnum($fam->id, $oa->id, $es);
98  } else {
99  DocEnum::addEnum($fam->id, $oa->id, $es);
100  }
101  }
102  }
103  catch(\Dcp\Exception $e) {
104  $err = $e->getDcpMessage();
105  }
106  }
107  $out = array(
108  "familyName" => $fam->name,
109  "enumId" => $oa->id,
110  "error" => $err
111  );
112  }
114  $action->lay->template = json_encode($out);
115  $action->lay->noparse = true;
116  header('Content-type: application/json');
117 }
118 ?>
global $action
static reloadLocaleCache()
Definition: Lib.System.php:118
static modifyEnum($famId, $attrid, EnumStructure $enumStruct)
static addEnum($famId, $attrid, EnumStructure $enumStruct)
modenumitems(Action &$action)
Verify arguments for action function.
static getFamilyEnums($famId, $attrid)
if($file) if($subject==""&&$file) if($subject=="") $err
$usage
Verify arguments for wsh programs.
← centre documentaire © anakeen