1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11:
12: use Xoops\Core\Database\Connection;
13: use Xoops\Core\Kernel\XoopsObject;
14: use Xoops\Core\Kernel\XoopsPersistableObjectHandler;
15: use Xoops\Core\Kernel\CriteriaElement;
16:
17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27:
28: class ProfileProfile extends XoopsObject
29: {
30: 31: 32:
33: public $handler;
34:
35: 36: 37:
38: public function __construct($fields)
39: {
40: $this->initVar('profile_id', XOBJ_DTYPE_INT, null, true);
41: $this->init($fields);
42: }
43:
44: 45: 46: 47: 48:
49: public function init($fields)
50: {
51: if (is_array($fields) && count($fields) > 0) {
52:
53: foreach ($fields as $key => $field) {
54: $this->initVar($key, $field->getVar('field_valuetype'), $field->getVar('field_default', 'n'), $field->getVar('field_required'), $field->getVar('field_maxlength'));
55: }
56: }
57: }
58: }
59:
60: class ProfileProfileHandler extends XoopsPersistableObjectHandler
61: {
62: 63: 64:
65: private $_fHandler;
66:
67: 68: 69: 70: 71:
72: private $_fields = array();
73:
74: public function __construct(Connection $db)
75: {
76: parent::__construct($db, 'profile_profile', 'ProfileProfile', 'profile_id');
77: $xoops = Xoops::getInstance();
78: $this->_fHandler = \Xoops::getModuleHelper('profile')->getHandler('field');
79: }
80:
81: 82: 83: 84: 85: 86: 87:
88: public function create($isNew = true)
89: {
90:
91: $obj = new $this->className($this->loadFields());
92: $obj->handler = $this;
93: if ($isNew === true) {
94: $obj->setNew();
95: }
96: return $obj;
97: }
98:
99: 100: 101: 102: 103: 104: 105: 106:
107: public function getProfile($uid, $createOnFailure = true)
108: {
109: $obj = parent::get($uid);
110: if (!is_object($obj) && $createOnFailure) {
111: $obj = $this->create();
112: }
113: return $obj;
114: }
115:
116: 117: 118: 119: 120: 121: 122:
123: public function createField($isNew = true)
124: {
125: $return = $this->_fHandler->create($isNew);
126: return $return;
127: }
128:
129: 130: 131: 132: 133:
134: public function loadFields()
135: {
136: if (count($this->_fields) == 0) {
137: $this->_fields = $this->_fHandler->loadFields();
138: }
139: return $this->_fields;
140: }
141:
142: 143: 144: 145: 146: 147: 148: 149: 150:
151: public function getFields(CriteriaElement $criteria, $id_as_key = true, $as_object = true)
152: {
153: return $this->_fHandler->getObjects($criteria, $id_as_key, $as_object);
154: }
155:
156: 157: 158: 159: 160: 161: 162: 163:
164: public function insertField(ProfileField $field, $force = false)
165: {
166: return $this->_fHandler->insert($field, $force);
167: }
168:
169: 170: 171: 172: 173: 174: 175: 176:
177: public function deleteField(ProfileField $field, $force = false)
178: {
179: return $this->_fHandler->delete($field, $force);
180: }
181:
182: 183: 184: 185: 186: 187: 188: 189:
190: public function saveField($vars, $weight = 0)
191: {
192: $field = $this->createField();
193: $field->setVar('field_name', $vars['name']);
194: $field->setVar('field_valuetype', $vars['valuetype']);
195: $field->setVar('field_type', $vars['type']);
196: $field->setVar('field_weight', $weight);
197: if (isset($vars['title'])) {
198: $field->setVar('field_title', $vars['title']);
199: }
200: if (isset($vars['description'])) {
201: $field->setVar('field_description', $vars['description']);
202: }
203: if (isset($vars['required'])) {
204: $field->setVar('field_required', $vars['required']);
205: }
206: if (isset($vars['maxlength'])) {
207: $field->setVar('field_maxlength', $vars['maxlength']);
208: }
209: if (isset($vars['default'])) {
210: $field->setVar('field_default', $vars['default']);
211: }
212: if (isset($vars['notnull'])) {
213: $field->setVar('field_notnull', $vars['notnull']);
214: }
215: if (isset($vars['show'])) {
216: $field->setVar('field_show', $vars['show']);
217: }
218: if (isset($vars['edit'])) {
219: $field->setVar('field_edit', $vars['edit']);
220: }
221: if (isset($vars['config'])) {
222: $field->setVar('field_config', $vars['config']);
223: }
224: if (isset($vars['options'])) {
225: $field->setVar('field_options', $vars['options']);
226: } else {
227: $field->setVar('field_options', array());
228: }
229: if ($this->insertField($field)) {
230: $msg = ' Field <strong>' . $vars['name'] . '</strong> added to the database';
231: } else {
232: $msg = ' <span class="red">ERROR: Could not insert field <strong>' . $vars['name'] . '</strong> into the database. ' . implode(' ', $field->getErrors()) . $this->db2->errorInfo() . '</span>';
233: }
234: unset($field);
235: return $msg;
236: }
237:
238: 239: 240: 241: 242: 243: 244: 245:
246: public function insert(XoopsObject $obj, $force = false)
247: {
248: $uservars = $this->getUserVars();
249: foreach ($uservars as $var) {
250: unset($obj->vars[$var]);
251: }
252: if (count($obj->vars) == 0) {
253: return true;
254: }
255: return parent::insert($obj, $force);
256: }
257:
258: 259: 260: 261: 262:
263: public function getUserVars()
264: {
265: return $this->_fHandler->getUserVars();
266: }
267:
268: 269: 270: 271: 272: 273: 274: 275: 276:
277: public function search(CriteriaElement $criteria, $searchvars = array(), $groups = null)
278: {
279: $xoops = Xoops::getInstance();
280: $uservars = $this->getUserVars();
281:
282: $searchvars_user = array_intersect($searchvars, $uservars);
283: $searchvars_profile = array_diff($searchvars, $uservars);
284: $sv = array('u.uid, u.uname, u.email, u.user_viewemail');
285: if (!empty($searchvars_user)) {
286: $sv[0] .= ",u." . implode(", u.", $searchvars_user);
287: }
288: if (!empty($searchvars_profile)) {
289: $sv[] = "p." . implode(", p.", $searchvars_profile);
290: }
291:
292: $sql_select = "SELECT " . (empty($searchvars) ? "u.*, p.*" : implode(", ", $sv));
293: $sql_from = " FROM " . $this->db2->prefix("users") . " AS u LEFT JOIN " . $this->table . " AS p ON u.uid=p.profile_id" . (empty($groups) ? "" : " LEFT JOIN " . $this->db2->prefix("system_usergroup") . " AS g ON u.uid=g.uid");
294: $sql_clause = " WHERE 1=1";
295: $sql_order = "";
296:
297: $limit = $start = 0;
298: if (isset($criteria) && is_subclass_of($criteria, 'Xoops\Core\Kernel\CriteriaElement')) {
299: $sql_clause .= " AND " . $criteria->render();
300: if ($criteria->getSort() != '') {
301: $sql_order = ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder();
302: }
303: $limit = $criteria->getLimit();
304: $start = $criteria->getStart();
305: }
306:
307: if (!empty($groups)) {
308: $sql_clause .= " AND g.groupid IN (" . implode(", ", $groups) . ")";
309: }
310:
311: $sql_users = $sql_select . $sql_from . $sql_clause . $sql_order;
312: $result = $this->db2->query($sql_users, $limit, $start);
313:
314: if (!$result) {
315: return array(array(), array(), 0);
316: }
317: $user_handler = $xoops->getHandlerUser();
318: $uservars = $this->getUserVars();
319: $users = array();
320: $profiles = array();
321: while ($myrow = $this->db2->fetchArray($result)) {
322: $profile = $this->create(false);
323: $user = $user_handler->create(false);
324:
325: foreach ($myrow as $name => $value) {
326: if (in_array($name, $uservars)) {
327: $user->assignVar($name, $value);
328: } else {
329: $profile->assignVar($name, $value);
330: }
331: }
332: $profiles[$myrow['uid']] = $profile;
333: $users[$myrow['uid']] = $user;
334: }
335:
336: $count = count($users);
337: if ((!empty($limit) && $count >= $limit) || !empty($start)) {
338: $sql_count = "SELECT COUNT(*)" . $sql_from . $sql_clause;
339: $result = $this->db2->query($sql_count);
340: list($count) = $this->db2->fetchRow($result);
341: }
342:
343: return array($users, $profiles, (int)($count));
344: }
345: }
346: