예제 #1
0
 /**
  * Save profile data
  *
  * @param   array   $profile
  * @param   array   $access
  * @return  boolean
  */
 public function saveProfile($profile, $access = array())
 {
     $profile = (array) $profile;
     $access = (array) $access;
     $keep = array();
     foreach ($this->profiles as $field) {
         // Remove any entries not in the incoming data
         if (!isset($profile[$field->get('profile_key')])) {
             if (!$field->destroy()) {
                 $this->addError($field->getError());
                 return false;
             }
             continue;
         }
         // Push to the list of fields we want to keep
         if (!isset($keep[$field->get('profile_key')])) {
             $keep[$field->get('profile_key')] = $field;
         } else {
             // Multi-value field
             $values = $keep[$field->get('profile_key')];
             $values = is_array($values) ? $values : array($values->get('profile_value') => $values);
             $values[$field->get('profile_value')] = $field;
             $keep[$field->get('profile_key')] = $values;
         }
     }
     $i = 1;
     foreach ($profile as $key => $data) {
         if ($key == 'tag' || $key == 'tags') {
             $this->tag($data);
             continue;
         }
         // Is it a multi-value field?
         if (is_array($data)) {
             if (empty($data)) {
                 continue;
             }
             foreach ($data as $val) {
                 if (is_array($val) || is_object($val)) {
                     $val = json_encode($val);
                 }
                 $val = trim($val);
                 // Skip empty values
                 if (!$val) {
                     continue;
                 }
                 $field = null;
                 // Try to find an existing entry
                 if (isset($keep[$key])) {
                     if (is_array($keep[$key])) {
                         if (isset($keep[$key][$val])) {
                             $field = $keep[$key][$val];
                             unset($keep[$key][$val]);
                         }
                     } else {
                         $field = $keep[$key];
                         unset($keep[$key]);
                     }
                 }
                 if (!$field instanceof Profile) {
                     $field = Profile::blank();
                 }
                 $field->set(array('user_id' => $this->get('id'), 'profile_key' => $key, 'profile_value' => $val, 'ordering' => $i, 'access' => isset($access[$key]) ? $access[$key] : $field->get('access', 5)));
                 if (!$field->save()) {
                     $this->addError($field->getError());
                     return false;
                 }
             }
             // Remove any values not already found
             if (isset($keep[$key]) && is_array($keep[$key])) {
                 foreach ($keep[$key] as $f) {
                     if (!$f->destroy()) {
                         $this->addError($f->getError());
                         return false;
                     }
                 }
             }
         } else {
             $val = trim($data);
             $field = null;
             if (isset($keep[$key])) {
                 $field = $keep[$key];
             }
             if (!$field instanceof Profile) {
                 $field = Profile::blank();
             }
             // If value is empty
             if (!$val) {
                 // If an existing field, remove it
                 if ($field->get('id')) {
                     if (!$field->destroy()) {
                         $this->addError($field->getError());
                         return false;
                     }
                 }
                 // Move along. Nothing to see here.
                 continue;
             }
             $field->set(array('user_id' => $this->get('id'), 'profile_key' => $key, 'profile_value' => $val, 'ordering' => $i, 'access' => isset($access[$key]) ? $access[$key] : $field->get('access', 5)));
             if (!$field->save()) {
                 $this->addError($field->getError());
                 return false;
             }
         }
         $i++;
     }
     return true;
 }
예제 #2
0
 /**
  * Display a list of members
  *
  * @return  void
  */
 public function browseTask()
 {
     // Incoming
     $filters = Filters::getFilters("{$this->_option}.{$this->_controller}");
     // Build query
     $entries = Member::all();
     $a = $entries->getTableName();
     $b = Profile::blank()->getTableName();
     $entries->select($a . '.*')->including(['profiles', function ($profile) {
         $profile->select('*')->whereIn('access', User::getAuthorisedViewLevels());
     }])->whereEquals($a . '.block', 0)->where($a . '.activation', '>', 0)->where($a . '.approved', '>', 0);
     // Take filters and apply them to the tasks
     /*if ($filters['search'])
     		{
     			foreach ($filters['search'] as $term)
     			{
     				//$entries->where($a . '.name', 'LIKE', "%{$term}%");
     
     				$entries->whereLike($a . '.name', strtolower((string)$term), 1)
     					->orWhereLike($a . '.username', strtolower((string)$term), 1)
     					->orWhereLike($a . '.email', strtolower((string)$term), 1)
     					->resetDepth();
     			}
     		}*/
     if ($filters['tags']) {
         $to = '#__tags_object';
         $t = '#__tags';
         $tags = explode(',', $filters['tags']);
         $tags = array_map('trim', $tags);
         $entries->select($a . '.*, COUNT(DISTINCT ' . $to . '.tagid) AS uniques');
         $entries->join($to, $to . '.objectid', $a . '.id', 'inner');
         $entries->join($t, $t . '.id', $to . '.tagid', 'inner');
         $entries->whereIn($t . '.tag', $tags);
         $entries->whereEquals($to . '.tbl', 'xprofiles');
         $entries->having('uniques', '=', count($tags));
         $entries->group($a . '.id');
     }
     if ($filters['q']) {
         $db = App::get('db');
         $i = 1;
         foreach ($filters['q'] as $q) {
             if ($q['field'] == 'name') {
                 if ($q['value'] && !is_array($q['value'])) {
                     if ($q['o'] == 'LIKE') {
                         // Explode multiple words into array
                         $search = explode(' ', $q['value']);
                         // Only allow alphabetical characters for search
                         //$search = preg_replace("/[^a-zA-Z]/", '', $search);
                         foreach ($search as $term) {
                             $term = '%' . trim($term) . '%';
                             $entries->where($a . '.name', ' ' . $q['o'] . ' ', strtolower((string) $term));
                         }
                     } else {
                         $entries->where($a . '.name', ' ' . $q['o'] . ' ', (string) $q['value']);
                     }
                 }
                 continue;
             }
             if ($q['o'] == 'LIKE') {
                 $q['value'] = '%' . $q['value'] . '%';
             }
             $entries->joinRaw($b . ' AS t' . $i, 't' . $i . '.user_id=' . $a . '.id AND t' . $i . '.profile_key=' . $db->quote($q['field']) . ' AND t' . $i . '.profile_value ' . $q['o'] . ' ' . $db->quote($q['value']), 'inner');
             $entries->whereIn('t' . $i . '.access', User::getAuthorisedViewLevels());
             $i++;
         }
     }
     $entries->whereIn($a . '.access', User::getAuthorisedViewLevels());
     switch ($filters['sortby']) {
         case 'name':
         default:
             $filters['sort'] = 'surname';
             $filters['sort_Dir'] = 'asc';
             break;
     }
     $rows = $entries->order($filters['sort'], $filters['sort_Dir'])->paginated('limitstart', 'limit')->rows();
     // Set the page title
     $title = Lang::txt('COM_MEMBERS');
     $title .= $this->_task ? ': ' . Lang::txt(strtoupper($this->_task)) : '';
     Document::setTitle($title);
     // Set the document pathway
     if (Pathway::count() <= 0) {
         Pathway::append(Lang::txt(strtoupper($this->_name)), 'index.php?option=' . $this->_option);
     }
     // Add to the pathway
     Pathway::append(Lang::txt(strtoupper($this->_task)), 'index.php?option=' . $this->_option . '&task=' . $this->_task);
     // Get stats
     if (!($stats = Cache::get('members.stats'))) {
         $stats = $this->stats();
         Cache::put('members.stats', $stats, intval($this->config->get('cache_time', 15)));
     }
     // Instantiate the view
     $this->view->set('config', $this->config)->set('filters', $filters)->set('title', $title)->set('rows', $rows)->set('past_day_members', $stats->past_day_members)->set('past_month_members', $stats->past_month_members)->set('total_members', $stats->total_members)->set('total_public_members', $stats->total_public_members)->display();
 }