Platform  3.1
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.Family.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
5  * @package FDL
6 */
7 /**
8  * Family Object Definition
9  *
10  * @author Anakeen 2009
11  * @version $Id: $
12  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
13  * @package FDL
14  */
15 /**
16  */
17 include_once ("DATA/Class.Document.php");
18 /**
19  * Family Class
20  *
21  */
22 class Fdl_Family extends Fdl_Document
23 {
24  /**
25  * Add a new attribute in family
26  */
27  function addAttribute($id, $label, $order, $type, $parent = '', $visibility = 'W', $intitle = false, $inabstract = false, $needed = false, $link = '', $elink = '', $phpfile = "", $phpfunc = '', $constraint = '', $options = null)
28  {
29  if ($this->doc) {
30  $err = $this->doc->lock(true);
31  if ($err) {
32  $this->setError($err);
33  return null;
34  }
35  $id = strtolower($id);
36  if ($id == "") {
37  $this->setError(_("attribute identificator is null"));
38  $err = $this->doc->unlock(true);
39  return null;
40  }
41  if (!preg_match("/^[a-z0-9_]+$/", $id)) {
42  $this->setError(sprintf(_("attribute [%s] identificator syntax is unavalaible. Must have only alphanumeric characters and must not includes spaces") , $id));
43  $err = $this->doc->unlock(true);
44  return null;
45  }
46 
47  if (!preg_match("/^[H|R|W|S|O|I|U]$/", $visibility)) {
48  $this->setError(sprintf(_("attribute [%s] visibility must be one of H,R,W,S,O,I,U") , $visibility));
49  $err = $this->doc->unlock(true);
50  return null;
51  }
52  $oattr = new DocAttr($this->dbaccess, array(
53  $this->doc->initid,
54  $id
55  ));
56  if ($oattr->isAffected()) {
57  $this->setError(sprintf(_("attribute [%s] already exists : cannot add it") , $id));
58  $err = $this->doc->unlock(true);
59  return null;
60  }
61 
62  if ($parent) {
63  $oap = $this->doc->getAttribute($parent);
64  if (!$oap) {
65  $this->setError(sprintf(_("parent attribute [%s] not exists : cannot add it") , $parent));
66  $err = $this->doc->unlock(true);
67  return null;
68  }
69  }
70 
71  if ($this->completeAttributes($oattr, $id, $label, $order, $type, $parent, $visibility, $intitle, $inabstract, $needed, $link, $elink, $phpfile, $phpfunc, $constraint, $options)) {
72  $err = $oattr->add();
73  if ($err) $this->setError($err);
74  else {
75  $wsh = getWshCmd();
76  $cmd = $wsh . " --api=fdl_adoc --docid=" . intval($this->doc->initid);
77  $log = exec($cmd, $out, $ret);
78  if ($err) $this->setError($err);
79  else return true;
80  }
81  }
82  $err = $this->doc->unlock(true);
83  }
84  return null;
85  }
86  function modifyAttribute($id, $label, $order, $type, $parent = '', $visibility = 'W', $intitle = false, $inabstract = false, $needed = false, $link = '', $elink = '', $phpfile = "", $phpfunc = '', $constraint = '', $options = null)
87  {
88  if ($this->doc) {
89  $err = $this->doc->lock(true);
90  if ($err) {
91  $this->setError($err);
92  return null;
93  }
94  $id = strtolower($id);
95  if ($id == "") {
96  $this->setError(_("attribute identificator is null"));
97  $err = $this->doc->unlock(true);
98  return null;
99  }
100 
101  $oa = $this->doc->getAttribute($id);
102  if (!$oa) {
103  $this->setError(sprintf(_("attribute [%s] not exists : cannot modify it") , $id));
104  $err = $this->doc->unlock(true);
105  return null;
106  }
107 
108  $oattr = new DocAttr($this->dbaccess, array(
109  $oa->docid,
110  $id
111  ));
112  if (!$oattr->isAffected()) $oattr = new DocAttr($this->dbaccess, array(
113  $oa->docid,
114  ':' . $id
115  ));
116  if (!$oattr->isAffected()) {
117  $this->setError(sprintf(_("attribute [%s] not exists : cannot modify it") , $id));
118  $err = $this->doc->unlock(true);
119  return null;
120  }
121  if ($this->completeAttributes($oattr, $oattr->id, $label, $order, $type, $parent, $visibility, $intitle, $inabstract, $needed, $link, $elink, $phpfile, $phpfunc, $constraint, $options)) {
122  $err = $oattr->modify();
123  if ($err) $this->setError($err);
124  else {
125  $wsh = getWshCmd();
126  $cmd = $wsh . " --api=fdl_adoc --docid=" . intval($oa->docid);
127  $log = exec($cmd, $out, $ret);
128  if ($err) $this->setError($err);
129  else return true;
130  }
131  }
132 
133  $err = $this->doc->unlock(true);
134  }
135  return null;
136  }
137 
139  {
140  if ($this->doc) {
141  $err = $this->doc->lock(true);
142  if ($err) {
143  $this->setError($err);
144  return null;
145  }
146  $id = strtolower($id);
147  if ($id == "") {
148  $this->setError(_("attribute identificator is null"));
149  $err = $this->doc->unlock(true);
150  return null;
151  }
152 
153  $oattr = new DocAttr($this->dbaccess, array(
154  $this->doc->initid,
155  $id
156  ));
157  if (!$oattr->isAffected()) {
158  $this->setError(sprintf(_("attribute [%s] not exists : cannot delete it") , $id));
159  $err = $this->doc->unlock(true);
160  return null;
161  }
162  $err = $oattr->delete();
163  if ($err) $this->setError($err);
164  else {
165  $wsh = getWshCmd();
166  $cmd = $wsh . "--api=fdl_adoc --docid=" . intval($this->doc->initid);
167  $log = exec($cmd, $out, $ret);
168  if ($err) $this->setError($err);
169  else return true;
170  }
171 
172  $err = $this->doc->unlock(true);
173  }
174  return null;
175  }
176 
177  private function completeAttributes(&$oattr, $id, $label, $order, $type, $parent = '', $visibility = 'W', $intitle = false, $inabstract = false, $needed = false, $link = '', $elink = '', $phpfile = "", $phpfunc = '', $constraint = '', $options = null)
178  {
179  if ($this->doc) {
180  $id = strtolower($id);
181  if ($id == "") {
182  $this->setError(_("attribute identificator is null"));
183  return null;
184  }
185  if (!preg_match("/^[a-z0-9_:]+$/", $id)) {
186  $this->setError(sprintf(_("attribute [%s] identificator syntax is unavalaible. Must have only alphanumeric characters and must not includes spaces") , $id));
187  return null;
188  }
189 
190  if ($visibility !== null) {
191  if (!preg_match("/^[H|R|W|S|O|I|U]$/", $visibility)) {
192  $this->setError(sprintf(_("attribute [%s] visibility must be one of H,R,W,S,O,I,U") , $visibility));
193  return null;
194  }
195  }
196 
197  if ($parent) {
198  $oap = $this->doc->getAttribute($parent);
199  if (!$oap) {
200  $this->setError(sprintf(_("parent attribute [%s] not exists : cannot add it") , $parent));
201  $err = $this->doc->unlock(true);
202  return null;
203  }
204  }
205 
206  if ($order && (!is_numeric($order))) {
207  // upto or downto
208  if (preg_match("/^(upto|downto) ([a-z0-9_]+)$/", $order, $reg)) {
209  $oar = $this->doc->getAttribute($reg[2]);
210  if ($oar) {
211  $reforder = $oar->ordered;
212  $updown = $reg[1];
213  if ($updown == 'upto') $order = $reforder - 1;
214  else $order = $reforder + 1;
215  uasort($this->doc->attributes->attr, "tordered");
216  $la = $this->doc->getAttributes();
217  // find the previous
218  $oaprev = null;
219  reset($la);
220  while (list($k, $v) = each($la)) {
221  if ($k == $oar->id) {
222  $oaprev = $_oaprev;
223  break;
224  }
225  }
226  prev($la); // here we are in upto element
227  $needdown = array();
228  if ($updown == 'upto') {
229  while (($v = prev($la)) !== false) {
230  if ($v->ordered < $order) {
231  break;
232  }
233  if ($v->id != $id) $needdown[] = $v;
234  }
235  } else {
236  while (($v = next($la)) !== false) {
237  if ($v->ordered > $order) {
238  break;
239  }
240  if ($v->id != $id) $needdown[] = $v;
241  }
242  }
243  $famtoupdate = array();
244  foreach ($needdown as $k => $v) {
245  $attr = new DocAttr($this->dbaccess, array(
246  $v->docid,
247  $v->id
248  ));
249  if (!$attr->isAffected()) $attr = new DocAttr($this->dbaccess, array(
250  $v->docid,
251  ':' . $v->id
252  ));
253  if ($attr->isAffected()) {
254  if ($updown == 'upto') $attr->ordered = $v->ordered - 1;
255  else $attr->ordered = $v->ordered + 1;
256  $err = $attr->modify();
257  if ($v->docid != $this->doc->id) {
258  $famtoupdate[$v->docid] = $v->docid;
259  }
260  }
261  }
262 
263  foreach ($famtoupdate as $k => $v) {
264  $wsh = getWshCmd();
265  $cmd = $wsh . " --api=fdl_adoc --docid=" . intval($k);
266  $log = exec($cmd, $out, $ret);
267  }
268  } else $order = null;
269  } else {
270  $this->setError(sprintf(_("syntax error for order %s. May be an integer or a relative upto or downto identificator") , $order));
271  return null;
272  }
273  }
274  if (!$oattr->id) {
275  $oattr->id = $id;
276  $oattr->docid = $this->doc->id;
277  }
278  if ($label !== null) $oattr->labeltext = $label;
279  if ($intitle !== null) $oattr->title = $intitle ? 'Y' : 'N';
280  if ($inabtract !== null) $oattr->abstract = $inabtract ? 'Y' : 'N';
281  if ($needed !== null) $oattr->needed = $needed ? 'Y' : 'N';
282  if ($type !== null) $oattr->type = $type;
283  if ($parent !== null) $oattr->frameid = $parent ? $parent : "0";
284  if ($order !== null) $oattr->ordered = intval($order);
285  if ($visibility !== null) $oattr->visibility = $visibility;
286  if ($link !== null) $oattr->link = $link;
287  if ($phpfile !== null) $oattr->phpfile = $phpfile;
288  if ($phpfunc !== null) $oattr->phpfunc = $phpfunc;
289  if ($elink !== null) $oattr->elink = $elink;
290  if ($constraint !== null) $oattr->phpconstraint = $constraint;
291 
292  if ($options !== null) {
293  $opt = array();
294  foreach ($options as $k => $v) {
295  $opt[] = "$k=$v";
296  }
297  $oattr->options = implode("|", $opt);
298  }
299 
300  $oattr->usefor = 'N';
301 
302  return true;
303  }
304  return null;
305  }
306 }
307 ?>
← centre documentaire © anakeen - published under CC License - Dynacase