[ Index ]

PHP Cross Reference of Xoops v2.4.5 code documentation

title

Body

[close]

/modules/profile/class/ -> profile.php (source)

   1  <?php
   2  /**

   3   * Extended User Profile

   4   *

   5   * You may not change or alter any portion of this comment or credits

   6   * of supporting developers from this source code or any supporting source code

   7   * which is considered copyrighted (c) material of the original comment or credit authors.

   8   * This program is distributed in the hope that it will be useful,

   9   * but WITHOUT ANY WARRANTY; without even the implied warranty of

  10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

  11   *

  12   * @copyright       The XOOPS Project http://sourceforge.net/projects/xoops/

  13   * @license         http://www.fsf.org/copyleft/gpl.html GNU public license

  14   * @package         profile

  15   * @since           2.3.0

  16   * @author          Jan Pedersen

  17   * @author          Taiwen Jiang <phppp@users.sourceforge.net>

  18   * @version         $Id: profile.php 4361 2010-02-09 23:36:33Z trabis $

  19   */
  20  
  21  defined('XOOPS_ROOT_PATH') or die("XOOPS root path not defined");
  22  
  23  /**

  24   * @package kernel

  25   * @copyright copyright &copy; 2000 XOOPS.org

  26   */
  27  class ProfileProfile extends XoopsObject
  28  {
  29      function __construct($fields)
  30      {
  31          $this->initVar('profile_id', XOBJ_DTYPE_INT, null, true);
  32          $this->init($fields);
  33      }
  34  
  35      function ProfileProfile($fields)
  36      {
  37          $this->__construct($fields);
  38      }
  39  
  40      /**

  41      * Initiate variables

  42      * @param array $fields field information array of {@link XoopsProfileField} objects

  43      */
  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  }
  53  /**

  54   * @package kernel

  55   * @copyright copyright &copy; 2000 XOOPS.org

  56   */
  57  class ProfileProfileHandler extends XoopsPersistableObjectHandler
  58  {
  59      /**

  60      * holds reference to {@link profileFieldHandler} object

  61      */
  62      var $_fHandler;
  63  
  64      /**

  65      * Array of {@link XoopsProfileField} objects

  66      * @var array

  67      */
  68      var $_fields = array();
  69  
  70      function ProfileProfileHandler(&$db)
  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  
  81      /**

  82       * create a new {@link ProfileProfile}

  83       *

  84       * @param bool $isNew Flag the new objects as "new"?

  85       *

  86       * @return object {@link ProfileProfile}

  87       */
  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  
  98      /**

  99       * Get a {@link ProfileProfile}

 100       *

 101       * @param   boolean $createOnFailure create a new {@link ProfileProfile} if none is feteched

 102       *

 103       * @return  object {@link ProfileProfile}

 104       */
 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  
 114      /**

 115      * Create new {@link profileField} object

 116      *

 117      * @param bool $isNew

 118      *

 119      * @return object

 120      */
 121      function &createField($isNew = true)
 122      {
 123          $return =& $this->_fHandler->create($isNew);
 124          return $return;
 125      }
 126  
 127      /**

 128      * Load field information

 129      *

 130      * @return array

 131      */
 132      function loadFields()
 133      {
 134          if (count($this->_fields) == 0) {
 135              $this->_fields = $this->_fHandler->loadFields();
 136          }
 137          return $this->_fields;
 138      }
 139  
 140      /**

 141      * Fetch fields

 142      *

 143      * @param object $criteria {@link CriteriaElement} object

 144      * @param bool $id_as_key return array with field IDs as key?

 145      * @param bool $as_object return array of objects?

 146      *

 147      * @return array

 148      **/
 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  
 154      /**

 155      * Insert a field in the database

 156      *

 157      * @param object $field

 158      * @param bool $force

 159      *

 160      * @return bool

 161      */
 162      function insertField(&$field, $force = false)
 163      {
 164          return $this->_fHandler->insert($field, $force);
 165      }
 166  
 167      /**

 168      * Delete a field from the database

 169      *

 170      * @param object $field

 171      * @param bool $force

 172      *

 173      * @return bool

 174      */
 175      function deleteField(&$field, $force = false)
 176      {
 177          return $this->_fHandler->delete($field, $force);
 178      }
 179  
 180      /**

 181      * Save a new field in the database

 182      *

 183      * @param array $vars array of variables, taken from $module->loadInfo('profile')['field']

 184      * @param int $categoryid ID of the category to add it to

 185      * @param int $type valuetype of the field

 186      * @param int $moduleid ID of the module, this field belongs to

 187      * @param int $weight

 188      *

 189      * @return string

 190      **/
 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 <b>' . $vars['name'] . '</b> added to the database';
 232          } else {
 233              $msg = '&nbsp;&nbsp;<span style="color:#ff0000;">ERROR: Could not insert field <b>' . $vars['name'] . '</b> into the database. '.implode(' ', $field->getErrors()) . $this->db->error() . '</span>';
 234          }
 235          unset($field);
 236          return $msg;
 237      }
 238  
 239      /**

 240       * insert a new object in the database

 241       *

 242       * @param object $obj reference to the object

 243       * @param bool $force whether to force the query execution despite security settings

 244       * @param bool $checkObject check if the object is dirty and clean the attributes

 245       *

 246       * @return bool FALSE if failed, TRUE if already present and unchanged or successful

 247       */
 248  
 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  
 262      /**

 263       * Get array of standard variable names (user table)

 264       *

 265       * @return array

 266       */
 267      function getUserVars()
 268      {
 269          return $this->_fHandler->getUserVars();
 270      }
 271  
 272      /**

 273       * Search profiles and users

 274       *

 275       * @param object    $criteria   CriteriaElement

 276       * @param array     $searchvars Fields to be fetched

 277       * @param array     $groups     for Usergroups is selected (only admin!)

 278       *

 279       * @return array

 280       */
 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          }
 321          $user_handler = xoops_gethandler('user');
 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  ?>


Generated: Sun Aug 1 01:39:09 2010
Open Source related documentation for developers.