Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Method.DocIntranet.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 /**
7  * Intranet User & Group manipulation
8  *
9  * @author Anakeen
10  * @version $Id: Method.DocIntranet.php,v 1.23 2008/04/15 07:11:04 eric Exp $
11  * @package FDL
12  * @subpackage USERCARD
13  */
14 /**
15  */
16 /**
17  * @begin-method-ignore
18  * this part will be deleted when construct document class until end-method-ignore
19  */
20 class _IGROUPUSER extends Doc
21 {
22  /*
23  * @end-method-ignore
24  */
25  /**
26  * @var Account
27  */
28  public $wuser;
29  /**
30  * verify if the login syntax is correct and if the login not already exist
31  * @param string $login login to test
32  * @return array 2 items $err & $sug for view result of the constraint
33  */
35  {
36  $sug = array(
37  "-"
38  );
39  $err = '';
40  if ($login == "") {
41  $err = _("the login must not be empty");
42  } else if ($login == "-") {
43  } else if ($login == "-") {
44  } else {
45  if ($err == "") {
46  return $this->ExistsLogin($login);
47  }
48  }
49  return array(
50  "err" => $err,
51  "sug" => $sug
52  );
53  }
54  /**
55  * verify if the login not already exist
56  * @param string $login login to test
57  * @return array 2 items $err & $sug for view result of the constraint
58  */
59  function ExistsLogin($login, $unused = 0)
60  {
61  $sug = array();
62 
63  $id = $this->getRawValue("US_WHATID");
64 
65  $q = new QueryDb("", "Account");
66  $q->AddQuery(sprintf("login='%s'", pg_escape_string(mb_strtolower($login))));
67  if ($id) $q->AddQuery(sprintf("id != %d", $id));
68  $q->Query(0, 0, "TABLE");
69  $err = $q->basic_elem->msg_err;
70  if (($err == "") && ($q->nb > 0)) $err = _("login yet use");
71 
72  return array(
73  "err" => $err,
74  "sug" => $sug
75  );
76  }
77 
78  function preCreated()
79  {
80  if ($this->getRawValue("US_WHATID") != "") {
81  include_once ('FDL/Lib.Dir.php');
82 
83  $filter = array(
84  "us_whatid = '" . intval($this->getRawValue("US_WHATID")) . "'"
85  );
86  $tdoc = internalGetDocCollection($this->dbaccess, 0, 0, "ALL", $filter, 1, "TABLE", $this->fromid);
87  if (count($tdoc) > 0) return _("system id already set in database\nThis kind of document can not be duplicated");
88  }
89  return '';
90  }
91  /**
92  * avoid deletion of system document
93  */
94  function preDocDelete()
95  {
96  $err = parent::preDocDelete();
97  if ($err == "") {
98  $uid = $this->getRawValue("us_whatid");
99  if (($uid > 0) && ($uid < 10)) $err = _("this system user cannot be deleted");
100  }
101  return $err;
102  }
103  /**
104  * get system id account from document id account
105  * @param array $accountIds
106  * @return array
107  */
108  public function getSystemIds(array $accountIds)
109  {
110  $accountIds = array_unique($accountIds);
111  $kr = array_search('', $accountIds);
112  if ($kr !== false) unset($accountIds[$kr]);
113  $sysIds = array();
114  if (count($accountIds) > 0) {
115  $sql = sprintf("select id from users where fid in (%s)", implode(',', $accountIds));
116  simpleQuery($this->dbaccess, $sql, $sysIds, true, false);
117  $sysIds = array_unique($sysIds);
118  }
119  return $sysIds;
120  }
121  /**
122  * interface to affect group for an user
123  * @templateController interface to view group tree and select group
124  * @param string $target window target name for hyperlink destination
125  * @param bool $ulink if false hyperlink are not generated
126  * @param bool $abstract if true only abstract attribute are generated
127  */
128  function ChooseGroup($target = "_self", $ulink = true, $abstract = false)
129  {
130  global $action;
131 
132  $action->parent->AddJsRef($action->GetParam("CORE_PUBURL") . "/FDL/Layout/mktree.js");
133  $action->parent->addCssRef($action->GetParam("CORE_PUBURL") . "/USERCARD/Layout/choosegroup.css");
134 
135  $err = '';
136  $iduser = $this->getRawValue("US_WHATID");
137  if ($iduser > 0) {
138  $user = $this->getAccount();
139  if (!$user->isAffected()) {
140  return sprintf(_("user #%d does not exist") , $iduser);
141  }
142  $ugroup = $user->GetGroupsId();
143  } else {
144  $ugroup = array(
145  "2"
146  ); // default what group
147 
148  }
149 
150  $tgroup = array();
151 
152  $this->lay->set("wid", ($iduser == "") ? "0" : $iduser);
153 
154  $q2 = new queryDb("", "Account");
155  $groups = $q2->Query(0, 0, "TABLE", "select users.*, groups.idgroup from users, groups where users.id = groups.iduser and users.accounttype='G'");
156 
157  $q2 = new queryDb("", "Account");
158  $mgroups = $q2->Query(0, 0, "TABLE", "select users.* from users where accounttype='G' and id not in (select iduser from groups, users u where groups.idgroup = u.id and u.accounttype='G')");
159 
160  if ($groups) {
161  foreach ($groups as $k => $v) {
162  $v["login"] = htmlspecialchars($v["login"]);
163  $v["firstname"] = htmlspecialchars($v["firstname"]);
164  $v["lastname"] = htmlspecialchars($v["lastname"]);
165  $groupuniq[$v["id"]] = $v;
166 
167  if (in_array($v["id"], $ugroup)) {
168  $groupuniq[$v["id"]]["checkbox"] = "checked";
169  } else {
170  $groupuniq[$v["id"]]["checkbox"] = "";
171  }
172  }
173  } else {
174  $groups = array();
175  }
176 
177  $iconGroup = $this->getIcon('', 14);
178 
179  if ($mgroups) {
180  foreach ($mgroups as $k => $v) {
181  $v["login"] = htmlspecialchars($v["login"]);
182  $v["firstname"] = htmlspecialchars($v["firstname"]);
183  $v["lastname"] = htmlspecialchars($v["lastname"]);
184  $cgroup = $this->_getChildsGroup($v["id"], $groups);
185  $tgroup[$k] = $v;
186  $tgroup[$k]["SUBUL"] = $cgroup;
187  $fid = $v["fid"];
188  if ($fid) {
189  $tdoc = getTDoc($this->dbaccess, $fid);
190  $icon = $this->getIcon($tdoc["icon"], 14);
191  $tgroup[$k]["icon"] = $icon;
192  } else {
193  $tgroup[$k]["icon"] = $iconGroup;
194  }
195 
196  $groupuniq[$v["id"]] = $v;
197  if (in_array($v["id"], $ugroup)) {
198  $groupuniq[$v["id"]]["checkbox"] = "checked";
199  } else {
200  $groupuniq[$v["id"]]["checkbox"] = "";
201  }
202  }
203  }
204  $this->lay->setBlockData("LI", $tgroup);
205  uasort($groupuniq, array(
206  get_class($this) ,
207  "_cmpgroup"
208  ));
209  $this->lay->setBlockData("SELECTGROUP", $groupuniq);
210  return $err;
211  }
212  /**
213  * internal function use for choosegroup
214  * use to compute displayed group tree
215  */
216  function _getChildsGroup($id, $groups)
217  {
218 
219  $tlay = array();
220  foreach ($groups as $k => $v) {
221  if ($v["idgroup"] == $id) {
222  $tlay[$k] = $v;
223  $tlay[$k]["SUBUL"] = $this->_getChildsGroup($v["id"], $groups);
224  $fid = $v["fid"];
225  if ($fid) {
226  $tdoc = getTDoc($this->dbaccess, $fid);
227  $icon = $this->getIcon($tdoc["icon"]);
228  $tlay[$k]["icon"] = $icon;
229  } else {
230  $tlay[$k]["icon"] = "Images/igroup.gif";
231  }
232  }
233  }
234 
235  if (count($tlay) == 0) return "";
236  global $action;
237  $lay = new Layout("USERCARD/Layout/ligroup.xml", $action);
238  uasort($tlay, array(
239  get_class($this) ,
240  "_cmpgroup"
241  ));
242  $lay->setBlockData("LI", $tlay);
243  return $lay->gen();
244  }
245  /**
246  * to sort group by name
247  */
248  static function _cmpgroup($a, $b)
249  {
250  return strcasecmp($a['lastname'], $b['lastname']);
251  }
252  /**
253  * affect new groups to the user
254  * @global gidnew string Http var : egual Y to say effectif change (to not suppress group if gid not set)
255  * @global gid string Http var : array of new groups id
256  */
257  function setGroups()
258  {
259  include_once ("FDL/Lib.Usercard.php");
260 
261  global $_POST;
262  $err = '';
263  $gidnew = isset($_POST["gidnew"]) ? $_POST["gidnew"] : '';
264  $tgid = array(); // group ids will be modified
265  if ($gidnew == "Y") {
266  /*
267  * @var int[] $gids
268  */
269  $gids = $_POST["gid"];
270  if ($gids == "") $gids = array();
271 
272  $gAccount = $this->getAccount();
273  $rgid = $gAccount->GetGroupsId();
274  if ((count($rgid) != count($gids)) || (count(array_diff($rgid, $gids)) != 0)) {
275  $gdel = array_diff($rgid, $gids);
276  $gadd = array_diff($gids, $rgid);
277  // add group
278  $g = new Group("", $gAccount->id);
279  foreach ($gadd as $gid) {
280  $g->iduser = $gAccount->id;
281  $g->idgroup = $gid;
282  // insert in folder group
283  $gdoc = $this->getDocUser($gid);
284  // $gdoc->insertMember($this->id);
285  $err.= $gdoc->insertDocument($this->id); // add in group is set here by postInsert
286  $tgid[$gid] = $gid;
287  }
288  foreach ($gdel as $gid) {
289  $g->iduser = $gid;
290  //$aerr.=$g->SuppressUser($user->id,true);
291  // delete in folder group
292  $gdoc = $this->getDocUser($gid);
293  if (!method_exists($gdoc, "deleteMember")) AddWarningMsg("no group $gid/" . $gdoc->id);
294  else {
295  // $gdoc->deleteMember($this->id);
296  $err = $gdoc->removeDocument($this->id);
297  $tgid[$gid] = $gid;
298  }
299  }
300  // $g->FreedomCopyGroup();
301  //if ($user->isgroup=='Y') $tgid[$user->id]=$user->id;
302 
303  }
304  }
305  // it is now set in bacground
306  // refreshGroups($tgid,true);
307  return $err;
308  }
309  /**
310  * return document objet from what id (user or group)
311  * @param int $wid what identifier
312  * @return \Dcp\Family\Iuser|\Dcp\Family\IGROUP the object document (false if not found)
313  */
314  function getDocUser($wid)
315  {
316  $u = new Account("", $wid);
317  if ($u->isAffected()) {
318  if ($u->fid > 0) {
319  $du = new_Doc($this->dbaccess, $u->fid);
320  if ($du->isAlive()) return $du;
321  }
322  }
323  return false;
324  }
325  /**
326  * return system account object conform to whatid
327  * @param bool $nocache set to true if need to reload user object from database
328  * @return Account return false if not found
329  */
330  function getAccount($nocache = false)
331  {
332  if ($nocache) {
333  $this->wuser=null; // needed for reaffect new values
334 
335  } elseif ($this->wuser) {
336  if ($this->wuser->fid != $this->getRawValue("us_whatid")) {
337  $this->wuser=null; // clear cache when reaffect
338 
339  }
340  }
341 
342  if (!isset($this->wuser)) {
343  $wid = $this->getRawValue("us_whatid");
344  if ($wid > 0) {
345  $this->wuser = new Account("", $wid);
346  }
347  }
348  if (!isset($this->wuser)) return false;
349  return $this->wuser;
350  }
351  /**
352  * return what user object conform to whatid
353  * @deprecated use getAccount instead
354  * @return Account return false if not found
355  */
356  function getWuser($nocache = false)
357  {
358  return $this->getAccount($nocache);
359  }
360  /**
361  * reset wuser
362  */
363  protected function postAffect(array $data, $more, $reset)
364  {
365  if (isset($this->wuser)) {
366  $this->wuser=null;
367  }
368  }
369  /**
370  * @begin-method-ignore
371  * this part will be deleted when construct document class until end-method-ignore
372  */
373 }
374 /*
375  * @end-method-ignore
376 */
377 ?>
Layout is a template generator.
$tdoc
getTDoc($dbaccess, $id, $sqlfilters=array(), $result=array())
global $action
global $_POST
Definition: chgpasswd.php:17
_getChildsGroup($id, $groups)
print< H1 > Check Database< i > $dbaccess</i ></H1 > $a
Definition: checklist.php:45
getWuser($nocache=false)
static _cmpgroup($a, $b)
getIcon($idicon="", $size=null, $otherId=null)
Definition: Class.Doc.php:5817
getSystemIds(array $accountIds)
$tgid
getAccount($nocache=false)
$login
Definition: dav.php:40
internalGetDocCollection($dbaccess, $dirid, $start="0", $slice="ALL", $sqlfilters=array(), $userid=1, $qtype="LIST", $fromid="", $distinct=false, $orderby="title", $latest=true, $trash="", &$debug=null, $folderRecursiveLevel=2, $join= '',\SearchDoc &$searchDoc=null)
Definition: Lib.Dir.php:428
new_Doc($dbaccess, $id= '', $latest=false)
ExistsLogin($login, $unused=0)
postAffect(array $data, $more, $reset)
simpleQuery($dbaccess, $query, &$result=array(), $singlecolumn=false, $singleresult=false, $useStrict=null)
Definition: Lib.Common.php:484
if($file) if($subject==""&&$file) if($subject=="") $err
ChooseGroup($target="_self", $ulink=true, $abstract=false)
getRawValue($idAttr, $def="")
Definition: Class.Doc.php:3117
$data
ConstraintLogin($login)
← centre documentaire © anakeen