Skip to content

Commit

Permalink
finished import via command line command
Browse files Browse the repository at this point in the history
  • Loading branch information
thyseus committed Mar 24, 2014
1 parent 0cc6c05 commit c324595
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 135 deletions.
110 changes: 55 additions & 55 deletions modules/profile/ProfileModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,62 @@
Yii::setPathOfAlias('profile' , dirname(__FILE__));

class ProfileModule extends CWebModule {
public $adminLayout = 'profile.views.layouts.yum';
public $layout = 'profile.views.layouts.yum';

public $requiredProfileFields = array('firstname', 'lastname');

public $profileRules = array(
array('email', 'unique'),
array('email', 'CEmailValidator'),
);

// set this to true to allow all users to access user profiles
public $profilesViewableByGuests = false;

public $enableProfileVisitLogging = true;
public $enablePrivacySetting = true;
public $enableProfileComments = true;

public $profileTable = '{{profile}}';
public $privacySettingTable = '{{privacysetting}}';
public $profileCommentTable = '{{profile_comment}}';
public $profileVisitTable = '{{profile_visit}}';

public $profileView = '/profile/view';
public $profileViewRoute = '//profile/profile/view';

public $publicFieldsView =
'profile.views.profile.public_fields';
public $profileFormView =
'profile.views.profile._form';
public $profileCommentView =
'profile.views.profileComment._view';
public $profileCommentIndexView =
'profile.views.profileComment.index';
public $profileCommentCreateView =
'profile.views.profileComment.create';
public $adminLayout = 'profile.views.layouts.yum';
public $layout = 'profile.views.layouts.yum';

public $requiredProfileFields = array('firstname', 'lastname');

public $profileRules = array(
array('email', 'unique'),
array('email', 'CEmailValidator'),
);

// set this to true to allow all users to access user profiles
public $profilesViewableByGuests = false;

public $enableProfileVisitLogging = true;
public $enablePrivacySetting = true;
public $enableProfileComments = true;

public $profileTable = '{{profile}}';
public $privacySettingTable = '{{privacysetting}}';
public $profileCommentTable = '{{profile_comment}}';
public $profileVisitTable = '{{profile_visit}}';

public $profileView = '/profile/view';
public $profileViewRoute = '//profile/profile/view';

public $publicFieldsView =
'profile.views.profile.public_fields';
public $profileFormView =
'profile.views.profile._form';
public $profileCommentView =
'profile.views.profileComment._view';
public $profileCommentIndexView =
'profile.views.profileComment.index';
public $profileCommentCreateView =
'profile.views.profileComment.create';
public $profileEditView = '/profile/update';
public $privacySettingView= '/privacy/update';

// Which columns should be displayed in the user administration Grid
public $gridColumns = array('email', 'firstname', 'lastname', 'street');

public $controllerMap=array(
'comments'=>array(
'class'=>'ProfileModule.controllers.YumProfileCommentController'),
'privacy'=>array(
'class'=>'ProfileModule.controllers.YumPrivacysettingController'),
'profile'=>array(
'class'=>'ProfileModule.controllers.YumProfileController'),
);

public function init() {
$this->setImport(array(
'user.controllers.*',
'user.models.*',
'profile.components.*',
'profile.models.*',
));
}
// Which columns should be displayed in the user administration Grid
public $gridColumns = array('email', 'firstname', 'lastname', 'street');

public $controllerMap=array(
'comments'=>array(
'class'=>'ProfileModule.controllers.YumProfileCommentController'),
'privacy'=>array(
'class'=>'ProfileModule.controllers.YumPrivacysettingController'),
'profile'=>array(
'class'=>'ProfileModule.controllers.YumProfileController'),
);

public function init() {
$this->setImport(array(
'user.controllers.*',
'user.models.*',
'profile.components.*',
'profile.models.*',
));
}
}
4 changes: 2 additions & 2 deletions modules/profile/models/YumProfile.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ public function attributeLabels()
}

public function afterSave() {
if($this->isNewRecord)
Yii::log(Yum::t( 'A profile been created: {profile}', array(
if($this->isNewRecord)
Yii::log(Yum::t( 'A profile has been created: {profile}', array(
'{profile}' => json_encode($this->attributes))));
else
Yii::log(Yum::t( 'A profile has been updated: {profile}', array(
Expand Down
29 changes: 19 additions & 10 deletions modules/user/commands/ImportCommand.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
<?php
Yii::import('user.models.*');
Yii::import('profile.models.*');
Yii::import('role.models.*');

// Usage: yiic.php import csv --file=value [--delimiter=,] [--enclosure="]
// [--escape=\] [--roles=]
class ImportCommand extends CConsoleCommand {
public function actionCsv ($file, $delimiter = ',') {
if(!Yii::app()->db)
throw new CException('No database configured');
public function actionCsv (
$file,
$delimiter = ',
', $enclosure = '"',
$escape = '\\',
$roles = '') {
if(!Yii::app()->db)
throw new CException('No database configured');

if(!in_array('user', Yii::app()->db->schema->tableNames))
throw new CException('No table "user" found; is yum installed?');
if(!in_array('user', Yii::app()->db->schema->tableNames))
throw new CException('No table "user" found; is yum installed?');

if(!$file)
throw new CException('No file given');
if(!$file)
throw new CException('No file given');

$data = readfile($file);
$data = file_get_contents($file);

Yum::import($data, $delimiter);
}
Yum::import($data, $delimiter, $enclosure, $escape, $roles);
}
}
73 changes: 65 additions & 8 deletions modules/user/models/Yum.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
* @package Yum.core
*
*/
class Yum
{
class Yum {
public static function powered() {
return sprintf('Yii User Management version %s.', Yum::module()->version);
return sprintf('Yii User Management version %s.', Yum::module()->version);
}

/** Register an asset file of Yum */
Expand Down Expand Up @@ -223,15 +222,73 @@ public static function generateDemoUser($roles, $password, $status) {
// Import a batch of users. Usually coming from an .csv file.
// First line contains the attributes, which can either be
// a attribute for the user or for the profile table.
// Existing users will be overwritten with the values from the csv.
// set $roles with an comma separated list of role titles for roles
// that should automatically be assigned on creation of new users.
public static function import($data, $delimiter = ',', $enclosure = '"',
$escape = '\\') {
$escape = '\\', $roles = '') {
if(!$data)
throw new CException('No data given');

$rows = str_getcsv($data, $delimiter, $enclosure, $escape);
$attributes = $rows[0];
foreach($attributes as $attribute) {
var_dump($attribute);
$rows = explode("\n", $data);

$firstrow = str_getcsv($rows[0], $delimiter, $enclosure, $escape);
$attributes = array();
$i = 0;
foreach($firstrow as $row) {
$attributes[$i] = $row;
$i++;
}

unset($rows[0]);

foreach($rows as $row) {
$values = str_getcsv($row, $delimiter, $enclosure, $escape);

$user = YumUser::model()->findByPk($values[0]);

// Update existing User
if($user) {
$profile = $user->profile;
foreach($attributes as $key => $attribute) {
if(isset($user->$attribute) && isset($values[$key]))
$user->$attribute = htmlentities($values[$key], ENT_IGNORE, 'utf-8', FALSE);
else if(isset($profile->$attribute) && isset($values[$key]))
$profile->$attribute = htmlentities($values[$key], ENT_IGNORE, 'utf-8', FALSE);
}

$user->save(false);
if($profile instanceof YumProfile)
$profile->save(false);

if($roles)
foreach(explode(',', $roles) as $role)
$user->assignRole(trim($role));

} else if(!$user) { // Create new User
$user = new YumUser;
$profile = new YumProfile;

foreach($attributes as $key => $attribute) {
if(isset($user->$attribute) && isset($values[$key]))
$user->$attribute = htmlentities($values[$key], ENT_IGNORE, 'utf-8', FALSE);
else if(isset($profile->$attribute) && isset($values[$key]))
$profile->$attribute = htmlentities($values[$key], ENT_IGNORE, 'utf-8', FALSE);
}

$user->id = $values[0];
if(!$user->username && $profile->email)
$user->username = $profile->email;
if(!$user->status)
$user->status = 1;
$user->createtime = time();

if($user->username) {
$user->save(false);
$profile->user_id = $user->id;
$profile->save(false);
}
}
}
}
}
Expand Down
113 changes: 53 additions & 60 deletions modules/user/models/YumUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -374,74 +374,67 @@ public function can($action, $subaction = null) {
return false;
}

// possible relations are cached because they depend on the active submodules
// and it takes many expensive milliseconds to evaluate them all the time
public function relations() {
$relations = Yii::app()->cache->get('yum_user_relations');
if($relations === false) {
$relations = array();

if (Yum::hasModule('role')) {
Yii::import('user.role.models.*');
$relations['permissions'] = array(
self::HAS_MANY, 'YumPermission', 'principal_id');

$relations['managed_by'] = array(
self::HAS_MANY, 'YumPermission', 'subordinate_id');

$relations['roles'] = array(
self::MANY_MANY, 'YumRole',
Yum::module('role')->userRoleTable . '(user_id, role_id)');
}
public function relations() {
$relations = array();

if (Yum::hasModule('message')) {
Yii::import('user.message.models.*');
$relations['messages'] = array(
self::HAS_MANY, 'YumMessage', 'to_user_id',
'order' => 'timestamp DESC');
if (Yum::hasModule('role')) {
Yii::import('user.role.models.*');
$relations['permissions'] = array(
self::HAS_MANY, 'YumPermission', 'principal_id');

$relations['unread_messages'] = array(
self::HAS_MANY, 'YumMessage', 'to_user_id',
'condition' => 'message_read = 0',
'order' => 'timestamp DESC');
$relations['managed_by'] = array(
self::HAS_MANY, 'YumPermission', 'subordinate_id');

$relations['sent_messages'] = array(
self::HAS_MANY, 'YumMessage', 'from_user_id');
$relations['roles'] = array(
self::MANY_MANY, 'YumRole',
Yum::module('role')->userRoleTable . '(user_id, role_id)');
}

}
if (Yum::hasModule('profile')) {
Yii::import('user.profile.models.*');
$relations['visits'] = array(
self::HAS_MANY, 'YumProfileVisit', 'visited_id');
$relations['visited'] = array(
self::HAS_MANY, 'YumProfileVisit', 'visitor_id');
$relations['profile'] = array(
self::HAS_ONE, 'YumProfile', 'user_id');
$relations['privacy'] = array(
self::HAS_ONE, 'YumPrivacySetting', 'user_id');
}
if (Yum::hasModule('message')) {
Yii::import('user.message.models.*');
$relations['messages'] = array(
self::HAS_MANY, 'YumMessage', 'to_user_id',
'order' => 'timestamp DESC');

if (Yum::hasModule('friendship')) {
$relations['friendships'] = array(
self::HAS_MANY, 'YumFriendship', 'inviter_id');
$relations['friendships2'] = array(
self::HAS_MANY, 'YumFriendship', 'friend_id');
$relations['friendship_requests'] = array(
self::HAS_MANY, 'YumFriendship', 'friend_id',
'condition' => 'status = 1'); // 1 = FRIENDSHIP_REQUEST
}
$relations['unread_messages'] = array(
self::HAS_MANY, 'YumMessage', 'to_user_id',
'condition' => 'message_read = 0',
'order' => 'timestamp DESC');

if (Yum::hasModule('membership')) {
Yii::import('user.membership.models.*');
$relations['memberships'] = array(
self::HAS_MANY, 'YumMembership', 'user_id');
}
$relations['sent_messages'] = array(
self::HAS_MANY, 'YumMessage', 'from_user_id');

Yii::app()->cache->set('yum_user_relations', $relations, 3600);
}
}
if (Yum::hasModule('profile')) {
Yii::import('user.profile.models.*');
$relations['visits'] = array(
self::HAS_MANY, 'YumProfileVisit', 'visited_id');
$relations['visited'] = array(
self::HAS_MANY, 'YumProfileVisit', 'visitor_id');
$relations['profile'] = array(
self::HAS_ONE, 'YumProfile', 'user_id');
$relations['privacy'] = array(
self::HAS_ONE, 'YumPrivacySetting', 'user_id');
}

return $relations;
}
if (Yum::hasModule('friendship')) {
$relations['friendships'] = array(
self::HAS_MANY, 'YumFriendship', 'inviter_id');
$relations['friendships2'] = array(
self::HAS_MANY, 'YumFriendship', 'friend_id');
$relations['friendship_requests'] = array(
self::HAS_MANY, 'YumFriendship', 'friend_id',
'condition' => 'status = 1'); // 1 = FRIENDSHIP_REQUEST
}

if (Yum::hasModule('membership')) {
Yii::import('user.membership.models.*');
$relations['memberships'] = array(
self::HAS_MANY, 'YumMembership', 'user_id');
}

return $relations;
}

public function isFriendOf($invited_id)
{
Expand Down

0 comments on commit c324595

Please sign in to comment.