XOOPS 2.5.6  Final
 All Classes Namespaces Files Functions Variables Pages
profile.php
Go to the documentation of this file.
1 <?php
21 defined('XOOPS_ROOT_PATH') or die("XOOPS root path not defined");
22 
28 {
29  function __construct($fields)
30  {
31  $this->initVar('profile_id', XOBJ_DTYPE_INT, null, true);
32  $this->init($fields);
33  }
34 
36  {
37  $this->__construct($fields);
38  }
39 
44  function init($fields)
45  {
46  if (is_array($fields) && count($fields) > 0) {
47  foreach (array_keys($fields) as $key ) {
48  $this->initVar($key, $fields[$key]->getVar('field_valuetype'), $fields[$key]->getVar('field_default', 'n'), $fields[$key]->getVar('field_required'), $fields[$key]->getVar('field_maxlength'));
49  }
50  }
51  }
52 }
58 {
63 
68  var $_fields = array();
69 
71  {
72  $this->__construct($db);
73  }
74 
75  function __construct(&$db)
76  {
77  parent::__construct($db, "profile_profile", 'profileprofile', "profile_id");
78  $this->_fHandler = xoops_getmodulehandler('field', 'profile');
79  }
80 
88  function &create($isNew = true)
89  {
90  $obj = new $this->className($this->loadFields() );
91  $obj->handler =& $this;
92  if ($isNew === true) {
93  $obj->setNew();
94  }
95  return $obj;
96  }
97 
105  function &get($uid, $createOnFailure = true)
106  {
107  $obj = parent::get($uid);
108  if (!is_object($obj) && $createOnFailure) {
109  $obj = $this->create();
110  }
111  return $obj;
112  }
113 
121  function &createField($isNew = true)
122  {
123  $return =& $this->_fHandler->create($isNew);
124  return $return;
125  }
126 
132  function loadFields()
133  {
134  if (count($this->_fields) == 0) {
135  $this->_fields = $this->_fHandler->loadFields();
136  }
137  return $this->_fields;
138  }
139 
149  function getFields($criteria, $id_as_key = true, $as_object = true)
150  {
151  return $this->_fHandler->getObjects($criteria, $id_as_key, $as_object);
152  }
153 
162  function insertField(&$field, $force = false)
163  {
164  return $this->_fHandler->insert($field, $force);
165  }
166 
175  function deleteField(&$field, $force = false)
176  {
177  return $this->_fHandler->delete($field, $force);
178  }
179 
191  function saveField($vars, $weight = 0)
192  {
193  $field =& $this->createField();
194  $field->setVar('field_name', $vars['name']);
195  $field->setVar('field_valuetype', $vars['valuetype']);
196  $field->setVar('field_type', $vars['type']);
197  $field->setVar('field_weight', $weight);
198  if (isset($vars['title'])) {
199  $field->setVar('field_title', $vars['title']);
200  }
201  if (isset($vars['description'])) {
202  $field->setVar('field_description', $vars['description']);
203  }
204  if (isset($vars['required'])) {
205  $field->setVar('field_required', $vars['required']); //0 = no, 1 = yes
206  }
207  if (isset($vars['maxlength'])) {
208  $field->setVar('field_maxlength', $vars['maxlength']);
209  }
210  if (isset($vars['default'])) {
211  $field->setVar('field_default', $vars['default']);
212  }
213  if (isset($vars['notnull'])) {
214  $field->setVar('field_notnull', $vars['notnull']);
215  }
216  if (isset($vars['show'])) {
217  $field->setVar('field_show', $vars['show']);
218  }
219  if (isset($vars['edit'])) {
220  $field->setVar('field_edit', $vars['edit']);
221  }
222  if (isset($vars['config'])) {
223  $field->setVar('field_config', $vars['config']);
224  }
225  if (isset($vars['options'])) {
226  $field->setVar('field_options', $vars['options']);
227  } else {
228  $field->setVar('field_options', array() );
229  }
230  if ($this->insertField($field)) {
231  $msg = '&nbsp;&nbsp;Field <strong>' . $vars['name'] . '</strong> added to the database';
232  } else {
233  $msg = '&nbsp;&nbsp;<span class="red">ERROR: Could not insert field <strong>' . $vars['name'] . '</strong> into the database. '.implode(' ', $field->getErrors()) . $this->db->error() . '</span>';
234  }
235  unset($field);
236  return $msg;
237  }
238 
249  function insert(&$obj, $force = false, $checkObject = true)
250  {
251  $uservars = $this->getUserVars();
252  foreach ($uservars as $var) {
253  unset($obj->vars[$var]);
254  }
255  if (count($obj->vars) == 0) {
256  return true;
257  }
258  return parent::insert($obj, $force, $checkObject);
259  }
260 
261 
267  function getUserVars()
268  {
269  return $this->_fHandler->getUserVars();
270  }
271 
281  function search($criteria, $searchvars = array(), $groups = null)
282  {
283  $uservars = $this->getUserVars();
284 
285  $searchvars_user = array_intersect($searchvars, $uservars);
286  $searchvars_profile = array_diff($searchvars, $uservars);
287  $sv = array('u.uid, u.uname, u.email, u.user_viewemail');
288  if (!empty($searchvars_user)) {
289  $sv[0] .= ",u." . implode(", u.", $searchvars_user);
290  }
291  if (!empty($searchvars_profile)) {
292  $sv[] = "p." . implode(", p.", $searchvars_profile);
293  }
294 
295  $sql_select = "SELECT " . (empty($searchvars) ? "u.*, p.*" : implode(", ", $sv));
296  $sql_from = " FROM " . $this->db->prefix("users") . " AS u LEFT JOIN " . $this->table . " AS p ON u.uid=p.profile_id" .
297  (empty($groups) ? "" : " LEFT JOIN " . $this->db->prefix("groups_users_link") . " AS g ON u.uid=g.uid");
298  $sql_clause = " WHERE 1=1";
299  $sql_order = "";
300 
301  $limit = $start = 0;
302  if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
303  $sql_clause .= " AND " . $criteria->render();
304  if ($criteria->getSort() != '') {
305  $sql_order = ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder();
306  }
307  $limit = $criteria->getLimit();
308  $start = $criteria->getStart();
309  }
310 
311  if (!empty($groups)) {
312  $sql_clause .= " AND g.groupid IN (" . implode(", ", $groups) . ")";
313  }
314 
315  $sql_users = $sql_select . $sql_from . $sql_clause . $sql_order;
316  $result = $this->db->query($sql_users, $limit, $start);
317 
318  if (!$result) {
319  return array(array(), array(), 0);
320  }
322  $uservars = $this->getUserVars();
323  $users = array();
324  $profiles = array();
325  while ($myrow = $this->db->fetchArray($result)) {
326  $profile = $this->create(false);
327  $user = $user_handler->create(false);
328 
329  foreach ($myrow as $name => $value) {
330  if (in_array($name, $uservars)) {
331  $user->assignVar($name, $value);
332  } else {
333  $profile->assignVar($name, $value);
334  }
335  }
336  $profiles[$myrow['uid']] = $profile;
337  $users[$myrow['uid']] = $user;
338  }
339 
340  $count = count($users);
341  if ((!empty($limit) && $count >= $limit) || !empty($start)) {
342  $sql_count = "SELECT COUNT(*)" . $sql_from . $sql_clause;
343  $result = $this->db->query($sql_count);
344  list($count) = $this->db->fetchRow($result);
345  }
346 
347  return array($users, $profiles, intval($count));
348  }
349 }
350 ?>