We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 26503
    • 620 Posts
    Been trying to get Xodus to export all users and break out the extended fields in csv columns, not just group by group.... I have the extended fields working, I just don't see how to get it to do all users. here is the modified code [with the excel stuff removed] - the only real thing I did to the sql was to have it select all groups > 0:


    <?php
    
    /**
     * Xodus
     *
     * Copyright 2012 by Joe Molloy <[email protected]>
     *
     * This file is part of Xodus, a simple component MODx Revolution which allows users from a selected user group to be exported to CSV or MS Excel format.
     *
     * Xouds is free software; you can redistribute it and/or modify it under the
     * terms of the GNU General Public License as published by the Free Software
     * Foundation; either version 2 of the License, or (at your option) any later
     * version.
     *
     * Xodus is distributed in the hope that it will be useful, but WITHOUT ANY
     * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
     *
     * You should have received a copy of the GNU General Public License along with
     * Xodus; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
     * Suite 330, Boston, MA 02111-1307 USA
     *
     * @package xodus
     */
    
    class XodusGetGroupUserListProcessor extends modObjectGetListProcessor {
        public $classKey = 'modUser';
        public $languageTopics = array('modx:default');
        public $defaultSortField = 'fullname';
        public $defaultSortDirection = 'ASC';
        public $objectType = '';
    	private $users = array();
    	private $fext = '';
    	private $timestamp = '';
    
    	public function beforeQuery() {
    		$this->setProperty('limit', 0);
    		return true;
    	}
    	
    	public function prepareQueryBeforeCount(xPDOQuery $c) {
    
            $usergroup = (int)$this->getProperty('group',0);
    		$c->innerJoin ('modUserProfile','Profile');
    		$c->innerJoin ('modUserGroupMember','UserGroupMembers');
    		$c->innerJoin ('modUserGroup','UserGroup','`UserGroupMembers`.`user_group` = 		`UserGroup`.`id`');
    		$c->leftJoin ('modUserGroupRole','UserGroupRole','`UserGroupMembers`.`role` = `UserGroupRole`.`id`');
            $c->where(array(
    			'active' => true,
    			//'UserGroupMembers.user_group' => $usergroup,
    			'UserGroupMembers.user_group:>' => 0,
    		));
    		$c->select(array('modUser.id','modUser.username','modUser.password'));
    		//$c->prepare();
    		//$d = $c->toSQL();
    		//$this->modx->log(modX::LOG_LEVEL_ERROR,'exodus query '.$d);
    		//$this->modx->log(modX::LOG_LEVEL_ERROR,'ending function: prepareQueryBeforeCount');
    		return $c;
        }
    	
    	public function prepareRow(xPDOObject $obj){
    
    		$profile = $obj->getOne('Profile');
    		$obj->set('fullname', $profile->get('fullname'));
    		$obj->set('email', $profile->get('email'));
    		$obj->set('phone', $profile->get('phone'));
    		$obj->set('mobilephone',$profile->get('mobilephone'));
    		$obj->set('blocked',$profile->get('blocked'));
    		$obj->set('blockeduntil',$profile->get('blockeduntil'));
    		$obj->set('blockedafter',$profile->get('blockedafter'));
    		$obj->set('logincount',$profile->get('logincount'));
    		$obj->set('lastlogin',$profile->get('lastlogin'));
    		$obj->set('failedlogincount',$profile->get('failedlogincount'));
    		$obj->set('sessionid',$profile->get('sessionid'));
    		$obj->set('dob',$profile->get('dob'));
    		$obj->set('gender',$profile->get('gender'));
    		$obj->set('address',$profile->get('address'));
    		$obj->set('country',$profile->get('country'));
    		$obj->set('city',$profile->get('city'));
    		$obj->set('state',$profile->get('state'));
    		$obj->set('zip',$profile->get('zip'));
    		$obj->set('fax',$profile->get('fax'));
    		$obj->set('photo',$profile->get('photo'));
    		$obj->set('comment',$profile->get('comment'));
    		$obj->set('website',$profile->get('website'));
    		$obj->set('extended',$profile->get('extended'));
    		$obj->set('uid',$obj->get('id'));
    		$ta = $obj->toArray('',false,true);
    
    		return $ta;
    	}
    	
    	public function afterIteration(array $users) {
    		
    		$this->users = $users;
    			
    		if(count($this->users) > 0){
    		$this->timestamp = time();
    		$this->doExportFile($this->users,$this->timestamp);
    		}
    		return $users;
        }
    	
    	public function outputArray(array $users, $count = false){
    
    		if ($count === false) { $count = count($users);}
    		if($count == 0){
    			return '{"success":true,"message":"'.$this->modx->lexicon('xodus.export_no_users').'","object":{"total":"0"},"data":[]}';	
    		}else{
    			$action_url = $this->getActionURL();
    			if($this->fext == 'csv'){
    				return '{"success":true,"message":"'.count($users).' '.$this->modx->lexicon('xodus.export_users').'","object":{"file":"'.$this->timestamp.'.csv","action_url":"'.$action_url.'","total":"'.count($users).'"},"data":[]}';
    			}else{
    				return '{"success":true,"message":"'.count($users).' '.$this->modx->lexicon('xodus.export_users').'","object":{"file":"'.$this->timestamp.'.'.$this->fext.'","action_url":"'.$action_url.'","total":"'.count($users).'"},"data":[]}';	
    			}
    		}
    	}
    	
    	private function doExportFile($users,$timestamp){
    
    		$format = $this->getProperty('format');
    		if($format == 0){
    			$this->doCSV($users,$timestamp);
    		}else{
    			$this->doExcel($users,$timestamp);	
    		}
    
    	}
    	
    	private function doCSV($users,$timestamp){
    
    		$this->fext = 'csv';
    		$fname = dirname(dirname(dirname(dirname(__file__)))).'/tmp/'.$timestamp;
    		$content = '';
    		$count = sizeof($users);
    		$headings = array($this->modx->lexicon('xodus.full_name'), $this->modx->lexicon('xodus.email'), $this->modx->lexicon('xodus.password'), $this->modx->lexicon('xodus.phone'), $this->modx->lexicon('xodus.mobile'), $this->modx->lexicon('xodus.fax'), $this->modx->lexicon('xodus.address'), $this->modx->lexicon('xodus.city'), $this->modx->lexicon('xodus.state'), $this->modx->lexicon('xodus.zip'), $this->modx->lexicon('xodus.country'), $this->modx->lexicon('xodus.dob'), $this->modx->lexicon('xodus.gender'), $this->modx->lexicon('xodus.website'), $this->modx->lexicon('xodus.comment'), $this->modx->lexicon('xodus.extended_fields'), $this->modx->lexicon('xodus.blocked'), $this->modx->lexicon('xodus.blocked_after'), $this->modx->lexicon('xodus.blocked_until'), $this->modx->lexicon('xodus.last_login'), $this->modx->lexicon('xodus.login_count'), $this->modx->lexicon('xodus.failed_login_count'), $this->modx->lexicon('xodus.session_id'),'epoch','date','referralsource','referralsource_other','salutation','firstname','lastname','excountry','province','provinceother','excity','telephone','extension','institution','specialty','specialty-other','academicinterests','ai-education','other-academic-interest','academic-interest-specifics','ci-breast','ci-lung','ci-gi','ci-gu','ci-head-and-neck','ci-gynecology','ci-sarcoma','ci-melanoma','ci-supportive-care','ci-lymphoma','ci-myeloma','ci-palliative-care','clinical-interest-other','clinical-interest-specifics','eula-agreement','oncologist-database','mailchimp','phone_number','ai-educational-research','ai-basic-science-research','ai-phase-i-ii-clinical-trials','ai-phase-iii-clinical-trials','ai-health-services-research','ai-transitional-research','ai-not-applicable','userid','usergroups');
    		$fp = fopen($fname.'.csv', 'w');
    		fputcsv($fp,$headings);
    		for($i = 0; $i < $count; $i++){
    			 			
    			$user = array();
    			$user[0] = $users[$i]['fullname'];
    			$user[1] = $users[$i]['email'];
    			$user[2] = $users[$i]['password'];
    			$user[3] = $users[$i]['phone'];
    			$user[4] = $users[$i]['mobilephone'];
    			$user[5] = $users[$i]['fax'];
    			$user[6] = $users[$i]['address'];
    			$user[7] = $users[$i]['city'];
    			$user[8] = $users[$i]['state'];
    			$user[9] = $users[$i]['zip'];
    			$user[10] = $users[$i]['country'];
    			if($users[$i]['dob'] != 0){
    				$dob = date('Y-m-d',$users[$i]['dob']);
    			}else{
    				$dob = '';
    			}
    			$user[11] = $dob;
    			$gender = $this->getGender($users[$i]['gender']);
    			$user[12] = $gender;
    			$user[13] = $users[$i]['website'];
    			$user[14] = $users[$i]['comment'];
    			$user[15] = json_encode($users[$i]['extended']);
    			
    			if($users[$i]['blocked'] == '' or $users[$i]['blocked'] == 0){
    				$blocked = 'No';	
    			}else{
    				$blocked = 'Yes';	
    			}
    			$user[16] = $blocked;
    			if($users[$i]['blockedafter'] == 0 or $users[$i]['blockedafter'] == ''){
    				$blockedafter = '';
    			}else{
    				$blockedafter = date('Y-m-d H:i:s',$users[$i]['blockedafter']);
    			}
    			$user[17] = $blockedafter;
    			if($users[$i]['blockeduntil'] == 0 or $users[$i]['blockeduntil'] == ''){
    				$blockeduntil = '';
    			}else{
    				$blockeduntil = date('Y-m-d H:i:s',$users[$i]['blockeduntil']);
    			}
    			$user[18] = $blockeduntil;
    			
    			if($users[$i]['lastlogin'] != 0){
    				$lastlogin = date('Y-m-d H:i:s',$users[$i]['lastlogin']);	
    			}else{
    				$lastlogin = '';	
    			}
    			$user[19] = $lastlogin;
    			$user[20] = $users[$i]['logincount'];
    			$user[21] = $users[$i]['failedlogincount'];
    			$user[22] = $users[$i]['sessionid'];
    
    			$xtnded = $this->modx->fromJSON($user[15], true);
    			
    			$user[23] = $xtnded['epoch'];
    			$user[24] = $xtnded['date'];
    			$user[25] = $xtnded['referralsource'];
    			$user[26] = $xtnded['referralsource_other'];
    			$user[27] = $xtnded['salutation'];
    			$user[28] = $xtnded['firstname'];
    			$user[29] = $xtnded['lastname'];
    			$user[30] = $xtnded['excountry'];
    			$user[31] = $xtnded['province'];
    			$user[32] = $xtnded['provinceother'];
    			$user[33] = $xtnded['excity'];
    			$user[34] = $xtnded['telephone'];
    			$user[35] = $xtnded['extension'];
    			$user[36] = $xtnded['institution'];
    			$user[37] = $xtnded['specialty'];
    			$user[38] = $xtnded['specialty-other'];
    			$user[39] = $xtnded['academicinterests'];
    			$user[40] = $xtnded['ai-education'];
    			$user[41] = $xtnded['other-academic-interest'];
    			$user[42] = $xtnded['academic-interest-specifics'];
    			$user[43] = $xtnded['ci-breast'];
    			$user[44] = $xtnded['ci-lung'];
    			$user[45] = $xtnded['ci-gi'];
    			$user[46] = $xtnded['ci-gu'];
    			$user[47] = $xtnded['ci-head-and-neck'];
    			$user[48] = $xtnded['ci-gynecology'];
    			$user[49] = $xtnded['ci-sarcoma'];
    			$user[50] = $xtnded['ci-melanoma'];
    			$user[51] = $xtnded['ci-supportive-care'];
    			$user[52] = $xtnded['ci-lymphoma'];
    			$user[53] = $xtnded['ci-myeloma'];
    			$user[54] = $xtnded['ci-palliative-care'];
    			$user[55] = $xtnded['clinical-interest-other'];
    			$user[56] = $xtnded['clinical-interest-specifics'];
    			$user[57] = $xtnded['eula-agreement'];
    			$user[58] = $xtnded['oncologist-database'];
    			$user[59] = $xtnded['mailchimp'];
    			$user[60] = $xtnded['phone_number'];
    			$user[61] = $xtnded['ai-educational-research'];
    			$user[62] = $xtnded['ai-basic-science-research'];
    			$user[63] = $xtnded['ai-phase-i-ii-clinical-trials'];
    			$user[64] = $xtnded['ai-phase-iii-clinical-trials'];
    			$user[65] = $xtnded['ai-health-services-research'];
    			$user[67] = $xtnded['ai-transitional-research'];
    			$user[68] = $xtnded['ai-not-applicable'];
    			
    			$uid = $users[$i]['uid'];
    			
    			$myuser = $this->modx->getObject('modUser', $uid);
    			$groups = $myuser->getUserGroupNames();
    			
    			$mygroups = '';
    			
    			foreach ($groups as $group){
      
    				$mygroups .= '('.$group.')';
    			  
    			}
    			
    			$user[69] = $uid;
    			$user[70] = $mygroups;
    
    
        		fputcsv($fp, $user);
    		}
    		fclose($fp);
    
    	}
    	
    
    	
    	function getActionURL(){
    
    		$action_url = '';
    		$manager_url = $this->modx->getOption('manager_url');
    		$version = $this->modx->getOption('settings_version');
    		if($version < 2.3){
    			$action = $this->modx->getObject('modAction',array('controller'=>'getfile','namespace'=>'xodus'));
    			$action_id = $action->get('id');
    			$action_url = '?a='.$action_id;
    		}else{
    			$action_url = '?action=getFile&namespace=xodus';
    		}
    		//$action_url = str_replace('/','\/',$action_url);
    
    		return $action_url;
    	}
    	
    	function getGender($genderid){
    
    		switch($genderid){
    			case 0:
    				$gender = '';
    				break;
    			case 1:
    				$gender = 'Male';
    				break;
    			case 2:
    				$gender = 'Female';
    				break;	
    		}
    
    		return $gender;	
    	}
    }
    
    return 'XodusGetGroupUserListProcessor';





    How can I make this work?
      *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

      Sean Kimball CLP, CLS.
      Technical Director / Sr. Developer | BigBlock Studios
      ._______________________________________________.
      Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
      27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
      phone/fax: 905-426-5525