Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.DocEnum.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 /**
7  * Document attribute enumerate
8  * @class DocEnum
9  */
10 
11 class DocEnum extends DbObj
12 {
13 
14  public $fields = array(
15  "famid",
16  "attrid",
17  "key",
18  "label",
19  "parentkey",
20  "disabled",
21  "eorder"
22  );
23  /**
24  * identifier of family of enum
25  * @public int
26  */
27  public $famid;
28  /**
29  * identifier of family attribute which used enum
30  * @public text
31  */
32  public $attrid;
33  /**
34  * enum value
35  * @public string
36  */
37  public $key;
38  /**
39  * default label key
40  * @public string
41  */
42  public $label;
43  /**
44  * order to display list enum items
45  * @public string
46  */
47  public $eorder;
48  /**
49  * key of parent enum
50  * @public int
51  */
52  public $parentkey;
53 
54  public $id_fields = array(
55  "famid",
56  "attrid",
57  "key"
58  );
59  protected $needChangeOrder = false;
60  /**
61  * @var bool
62  */
63  public $disabled;
64  public $dbtable = "docenum";
65 
66  public $sqlcreate = '
67 create table docenum (
68  famid int not null,
69  attrid text not null,
70  key text,
71  label text,
72  parentkey text,
73  disabled bool,
74  eorder int);
75 create index if_docenum on docenum(famid, attrid);
76 create unique index i_docenum on docenum(famid, attrid, key);
77 ';
78 
79  public function postUpdate()
80  {
81  if ($this->needChangeOrder) {
82  $this->shiftOrder($this->eorder);
83  }
84  }
85 
86  public function postInsert()
87  {
88  if ($this->needChangeOrder) {
89  $this->shiftOrder($this->eorder);
90  }
91  }
92 
93  function preUpdate()
94  {
95  $this->consolidateOrder();
96  return '';
97  }
98  function preInsert()
99  {
100  $this->consolidateOrder();
101  return '';
102  }
103  /**
104  * get last order
105  */
106  protected function consolidateOrder()
107  {
108  if (empty($this->eorder) || $this->eorder < 0) {
109  $sql = sprintf("select max(eorder) from docenum where famid = '%s' and attrid='%s'", pg_escape_string($this->famid) , pg_escape_string($this->attrid));
110  simpleQuery($this->dbaccess, $sql, $newOrder, true, true);
111  if ($newOrder > 0) {
112  $this->eorder = intval($newOrder) + 1;
113  }
114  }
115  }
116  public function shiftOrder($n)
117  {
118 
119  if ($n > 0) {
120  $sql = sprintf("update docenum set eorder=eorder + 1 where famid = '%s' and attrid='%s' and key != '%s' and eorder >= %d", pg_escape_string($this->famid) , pg_escape_string($this->attrid) , pg_escape_string($this->key) , $n);
121  simpleQuery($this->dbaccess, $sql);
122  $seqName = uniqid("tmpseqenum");
123  $sql = sprintf("create temporary sequence %s;", $seqName);
124 
125  $sql.= sprintf("UPDATE docenum SET eorder = neworder from (SELECT *, nextval('%s') as neworder from (select * from docenum where famid='%s' and attrid = '%s' order by eorder) as tmpz) as w where w.famid=docenum.famid and w.attrid=docenum.attrid and docenum.key=w.key;", $seqName, pg_escape_string($this->famid) , pg_escape_string($this->attrid));
126 
127  simpleQuery($this->dbaccess, $sql);
128  }
129  }
130  public function exists()
131  {
132  if ($this->famid && $this->attrid && $this->key !== null) {
133  simpleQuery($this->dbaccess, sprintf("select true from docenum where famid=%d and attrid='%s' and key='%s'", ($this->famid) , pg_escape_string($this->attrid) , pg_escape_string($this->key)) , $r, true, true);
134  return $r;
135  }
136  return false;
137  }
138 
139  public static function getFamilyEnums($famId, $attrid)
140  {
141  if (!is_numeric($famId)) {
143  }
144  $attrid = strtolower($attrid);
145  $sql = sprintf("select * from docenum where famid=%d and attrid='%s' order by eorder", $famId, pg_escape_string($attrid));
146  simpleQuery(getDbAccess() , $sql, $enums);
147  return $enums;
148  }
149  public static function getDisabledKeys($famId, $attrid)
150  {
151  if (!is_numeric($famId)) {
153  }
154  $attrid = strtolower($attrid);
155  $sql = sprintf("select key from docenum where famid=%d and attrid='%s' and disabled", $famId, pg_escape_string($attrid));
156 
157  simpleQuery(getDbAccess() , $sql, $dKeys, true);
158  return $dKeys;
159  }
160 
161  protected function setOrder($beforeThan)
162  {
163  $sql = sprintf("SELECT count(*) FROM docenum WHERE famid = %d AND attrid = '%s'", $this->famid, pg_escape_string($this->attrid));
164  simpleQuery($this->dbaccess, $sql, $count, true, true);
165  if ($beforeThan !== null) {
166  $sql = sprintf("select eorder from docenum where famid=%d and attrid='%s' and key='%s'", $this->famid, pg_escape_string($this->attrid) , pg_escape_string($beforeThan));
167  simpleQuery($this->dbaccess, $sql, $beforeOrder, true, true);
168  if ($beforeOrder) {
169  $this->eorder = $beforeOrder;
170  } else {
171  /* If the next key does not exists, then set order to count + 1 */
172  $this->eorder = $count + 1;
173  }
174  } else if (empty($this->eorder)) {
175  /*
176  * If item has no beforeThan and eorder is not set, then we assume it's the last one
177  * (there is nothing after him). So, the order is the number of items + 1
178  */
179  $this->eorder = $count + 1;
180  }
181  }
182  public static function addEnum($famId, $attrid, EnumStructure $enumStruct)
183  {
184  if (!is_numeric($famId)) {
186  }
187  $attrid = strtolower($attrid);
188  $enum = new DocEnum("", array(
189  $famId,
190  $attrid,
191  $enumStruct->key
192  ));
193  if ($enum->isAffected()) {
194  throw new \Dcp\Exception(sprintf("Enum %s#%s#%s already exists", $famId, $attrid, $enumStruct->key));
195  }
196 
197  $enum->famid = $famId;
198  $enum->attrid = $attrid;
199  $enum->key = $enumStruct->key;
200  $enum->label = $enumStruct->label;
201  $enum->disabled = ($enumStruct->disabled === true);
202  $enum->needChangeOrder = true;
203  $enum->eorder = $enumStruct->absoluteOrder;
204  if ($enumStruct->orderBeforeThan === null) {
205  $enum->setOrder(null);
206  } else {
207  $enum->setOrder($enumStruct->orderBeforeThan);
208  }
209  $err = $enum->add();
210  if ($err) {
211  throw new \Dcp\Exception(sprintf("Cannot add enum %s#%s#%s : %s", $famId, $attrid, $enumStruct->key, $err));
212  }
213 
214  if ($enumStruct->localeLabel) {
215  foreach ($enumStruct->localeLabel as $lLabel) {
216  self::changeLocale($famId, $attrid, $enumStruct->key, $lLabel->lang, $lLabel->label);
217  }
218  }
219  }
220 
221  public static function modifyEnum($famId, $attrid, EnumStructure $enumStruct)
222  {
223  if (!is_numeric($famId)) {
225  }
226  $attrid = strtolower($attrid);
227  $enum = new DocEnum("", array(
228  $famId,
229  $attrid,
230  $enumStruct->key
231  ));
232  if (!$enum->isAffected()) {
233  throw new \Dcp\Exception(sprintf("Enum %s#%s#%s not found", $famId, $attrid, $enumStruct->key));
234  }
235 
236  $enum->label = $enumStruct->label;
237  $enum->disabled = ($enumStruct->disabled === true);
238  if ($enum->eorder != $enumStruct->absoluteOrder) {
239  $enum->needChangeOrder = true;
240  $enum->eorder = $enumStruct->absoluteOrder;
241  }
242  if ($enumStruct->orderBeforeThan) {
243  $enum->setOrder($enumStruct->orderBeforeThan);
244  }
245 
246  $err = $enum->modify();
247  if ($err) {
248  throw new \Dcp\Exception(sprintf("Cannot modify enum %s#%s#%s : %s", $famId, $attrid, $enumStruct->key, $err));
249  }
250  if ($enumStruct->localeLabel) {
251  foreach ($enumStruct->localeLabel as $lLabel) {
252  self::changeLocale($famId, $attrid, $enumStruct->key, $lLabel->lang, $lLabel->label);
253  }
254  }
255  }
256 
257  public static function getMoFilename($famId, $lang)
258  {
259  $fam = new_Doc("", $famId);
260 
261  $moFile = sprintf("%s/locale/%s/LC_MESSAGES/customFamily_%s.mo", DEFAULT_PUBDIR, substr($lang, 0, 2) , $fam->name);
262  return $moFile;
263  }
264  /**
265  * @param $famId
266  * @param $attrid
267  * @param $enumId
268  * @param $lang
269  * @param $label
270  * @throws Dcp\Exception
271  */
272  public static function changeLocale($famId, $attrid, $enumId, $lang, $label)
273  {
275  $fam = new_Doc("", $famId);
276  $oa = $fam->getAttribute($attrid);
277  if (!$oa) {
278  throw new \Dcp\Exception(sprintf("Locale : Enum %s#%s#%s not found", $famId, $attrid, $enumId));
279  }
280  /*
281  * @var NormalAttribute $oa
282  */
283  $oa->resetEnum();
284  //$curLabel = $oa->getEnumLabel($enumId);
285  if ($label !== null) {
286 
287  $moFile = self::getMoFilename($fam->name, $lang);
288  $poFile = sprintf("%s.po", (substr($moFile, 0, -3)));
289  $msgInit = '';
290  $msgInit = sprintf('msgid ""
291 msgstr ""
292 "Project-Id-Version: Custom enum for %s\n"
293 "Language: %s\n"
294 "PO-Revision-Date: %s"
295 "MIME-Version: 1.0\n"
296 "Content-Type: text/plain; charset=UTF-8\n"
297 "Content-Transfer-Encoding: 8bit\n"', $fam->name, substr($lang, 0, 2) , date('Y-m-d H:i:s'));
298  if (file_exists($moFile)) {
299  // Just test mo validity
300  $cmd = sprintf("(msgunfmt %s > %s) 2>&1", escapeshellarg($moFile) , escapeshellarg($poFile));
301 
302  exec($cmd, $output, $ret);
303  if ($ret) {
304  throw new \Dcp\Exception(sprintf("Locale : Enum %s#%s#%s error : %s", $famId, $attrid, $enumId, implode(',', $output)));
305  }
306  } else {
307  file_put_contents($poFile, $msgInit);
308  }
309  // add new entry
310  $localeKey = sprintf("%s#%s#%s", $fam->name, $oa->id, $enumId);
311  $msgEntry = sprintf('msgid "%s"' . "\n" . 'msgstr "%s"', str_replace('"', '\\"', $localeKey) , str_replace('"', '\\"', $label));
312  $content = file_get_contents($poFile);
313  // fuzzy old entry
314  $match = sprintf('msgid "%s"', $localeKey);
315  $content = str_replace($match, "#, fuzzy\n$match", $content);
316  // delete previous header
317  $content = str_replace('msgid ""', "#, fuzzy\nmsgid \"- HEADER DELETION -\"", $content);
318 
319  file_put_contents($poFile, $msgInit . $msgEntry . "\n\n" . $content);
320  $cmd = sprintf("(msguniq --use-first %s | msgfmt - -o %s; rm -f %s) 2>&1", escapeshellarg($poFile) , escapeshellarg($moFile) , escapeshellarg($poFile));
321  exec($cmd, $output, $ret);
322  if ($ret) {
323  print $cmd;
324  throw new \Dcp\Exception(sprintf("Locale : Enum %s#%s#%s error : %s", $famId, $attrid, $enumId, implode(',', $output)));
325  }
326  }
327  }
328 }
330 {
331  /**
332  * @var string enum key
333  */
334  public $key;
335  public $label;
336  /**
337  * @var bool
338  */
339  public $disabled;
340  /**
341  * @var int enum order
342  * first order is 1
343  * last order is -1 (or 0)
344  */
347  /**
348  * @var EnumLocale[]
349  */
350  public $localeLabel;
351  public function affect(array $o)
352  {
353  $this->key = null;
354  $this->label = null;
355  $this->disabled = false;
356  $this->relativeOrder = null;
357  $this->orderBeforeThan = null;
358  $this->localeLabel = array();
359  foreach ($o as $k => $v) {
360  if ($k != "localeLabel") {
361  $this->$k = $v;
362  } else {
363  foreach ($v as $locale) {
364  $this->localeLabel[] = new EnumLocale($locale["lang"], $locale["label"]);
365  }
366  }
367  }
368  }
369 }
371 {
372  public $lang;
373  public $label;
374  public function __construct($lang, $label)
375  {
376  $this->lang = $lang;
377  $this->label = $label;
378  }
379 }
380 
setOrder($beforeThan)
static modifyEnum($famId, $attrid, EnumStructure $enumStruct)
$ret
consolidateOrder()
shiftOrder($n)
$locale
static addEnum($famId, $attrid, EnumStructure $enumStruct)
static getDisabledKeys($famId, $attrid)
$lang
Definition: lang.php:18
const DEFAULT_PUBDIR
Definition: Lib.Prefix.php:28
foreach($argv as $arg) $cmd
__construct($lang, $label)
affect(array $o)
setLanguage($lang)
Definition: Lib.Common.php:886
getFamIdFromName($dbaccess, $name)
print
Definition: checklist.php:49
if($dbaccess=="") $o
getDbAccess()
Definition: Lib.Common.php:368
new_Doc($dbaccess, $id= '', $latest=false)
static getFamilyEnums($famId, $attrid)
simpleQuery($dbaccess, $query, &$result=array(), $singlecolumn=false, $singleresult=false, $useStrict=null)
Definition: Lib.Common.php:484
if($file) if($subject==""&&$file) if($subject=="") $err
static changeLocale($famId, $attrid, $enumId, $lang, $label)
static getMoFilename($famId, $lang)
← centre documentaire © anakeen