diff --git a/Assets.php b/Assets.php new file mode 100755 index 0000000..43e26d5 --- /dev/null +++ b/Assets.php @@ -0,0 +1,37 @@ +. + */ + +namespace humhub\modules\bulk_import; + +use yii\web\AssetBundle; + +class Assets extends AssetBundle +{ + public $js = [ + '//cdn.jsdelivr.net/jdenticon/1.3.2/jdenticon.min.js', + '//cdnjs.cloudflare.com/ajax/libs/blueimp-md5/2.3.0/js/md5.min.js' + ]; + + public function init() + { + // $this->sourcePath = dirname(__FILE__) . '/assets'; + parent::init(); + } +} \ No newline at end of file diff --git a/BulkImportEvents.php b/Events.php similarity index 69% rename from BulkImportEvents.php rename to Events.php index d9e0d3b..a4bb355 100755 --- a/BulkImportEvents.php +++ b/Events.php @@ -18,8 +18,19 @@ * along with this program. If not, see . */ -class BulkImportEvents { +namespace humhub\modules\bulk_import; +use Yii; +use yii\helpers\Url; + + +class Events extends \yii\base\Object +{ + /** + * Defines what to do if admin menu is initialized. + * + * @param type $event + */ /** * Defines what to do if admin menu is initialized. * @@ -27,23 +38,25 @@ class BulkImportEvents { */ public static function onAdminMenuInit($event) { + $event->sender->addItem(array( 'label' => Yii::t('BulkImportModule.base', 'Bulk Import'), - 'url' => Yii::app()->createUrl('//bulk_import/main/index'), + 'url' => Url::to(['/bulk_import/main/index']), 'group' => 'manage', 'icon' => '', - 'isActive' => (Yii::app()->controller->module && Yii::app()->controller->module->id == 'bulk_import' && Yii::app()->controller->id == 'admin'), + 'isActive' => (Yii::$app->controller->module && Yii::$app->controller->module->id == 'bulk_import' && Yii::$app->controller->id == 'admin'), 'sortOrder' => 700, )); $event->sender->addItem(array( 'label' => Yii::t('BulkImportModule.base', 'Bulk Import Identicons'), - 'url' => Yii::app()->createUrl('//bulk_import/main/identicon'), + 'url' => Url::to(['/bulk_import/main/identicon']), 'group' => 'manage', 'icon' => '', - 'isActive' => (Yii::app()->controller->module && Yii::app()->controller->module->id == 'bulk_import' && Yii::app()->controller->id == 'admin'), + 'isActive' => (Yii::$app->controller->module && Yii::$app->controller->module->id == 'bulk_import' && Yii::$app->controller->id == 'admin'), 'sortOrder' => 700, )); + } } \ No newline at end of file diff --git a/BulkImportModule.php b/Module.php similarity index 78% rename from BulkImportModule.php rename to Module.php index a101ddb..6c1e3ff 100755 --- a/BulkImportModule.php +++ b/Module.php @@ -18,17 +18,8 @@ * along with this program. If not, see . */ -class BulkImportModule extends HWebModule{ - - /** - * Inits the Module - */ - public function init() - { +namespace humhub\modules\bulk_import; - $this->setImport(array( - 'bulk_import.forms.*', - )); - } - -} \ No newline at end of file +class Module extends \humhub\components\Module { + +} diff --git a/autostart.php b/config.php similarity index 70% rename from autostart.php rename to config.php index af39fc3..b787bcd 100755 --- a/autostart.php +++ b/config.php @@ -18,14 +18,14 @@ * along with this program. If not, see . */ -Yii::app()->moduleManager->register(array( +use humhub\modules\admin\widgets\AdminMenu; + +return [ 'id' => 'bulk_import', - 'class' => 'application.modules.bulk_import.BulkImportModule', - 'import' => array( - 'application.modules.bulk_import.*', - ), - 'events' => array( - array('class' => 'AdminMenuWidget', 'event' => 'onInit', 'callback' => array('BulkImportEvents', 'onAdminMenuInit')), - ), -)); + 'class' => 'humhub\modules\bulk_import\Module', + 'namespace' => 'humhub\modules\bulk_import', + 'events' => [ + ['class' => AdminMenu::className(), 'event' => AdminMenu::EVENT_INIT, 'callback' => ['humhub\modules\bulk_import\Events', 'onAdminMenuInit']], + ], +]; ?> \ No newline at end of file diff --git a/controllers/MainController.php b/controllers/MainController.php index fa9af13..7717579 100755 --- a/controllers/MainController.php +++ b/controllers/MainController.php @@ -18,20 +18,48 @@ * along with this program. If not, see . */ -class MainController extends Controller{ +namespace humhub\modules\bulk_import\controllers; - public $subLayout = "application.modules_core.admin.views._layout"; +use humhub\modules\admin\models\UserSearch; +use humhub\modules\karma\models\Karma; +use humhub\modules\karma\models\KarmaSearch; +use Yii; +use humhub\modules\bulk_import\forms\BulkImportForm; +use humhub\modules\user\models\User; +use humhub\modules\user\models\Password; +use humhub\modules\space\models\Space; +use yii\helpers\Html; +use humhub\libs\ProfileImage; + +class MainController extends \humhub\modules\admin\components\Controller +{ + + + /** + * @inheritdoc + */ + public function behaviors() + { + return [ + 'acl' => [ + 'class' => \humhub\components\behaviors\AccessControl::className(), + 'adminOnly' => true + ] + ]; + } /** * Registers a user - * @param array data + * @param $data + * @return Bool */ private function registerUser($data) { - $userModel = new User('register'); - $userPasswordModel = new UserPassword(); + $userModel = new User(); + $userModel->scenario = 'registration'; + $profileModel = $userModel->profile; - $profileModel->scenario = 'register'; + $profileModel->scenario = 'registration'; // User: Set values $userModel->username = $data['username']; @@ -45,23 +73,24 @@ private function registerUser($data) { // Password: Set values - $userPasswordModel->setPassword($data['password']); + $userPasswordModel = new Password(); + $userPasswordModel->setPassword($data['password']); if($userModel->save()) { - + // Save user profile $profileModel->user_id = $userModel->id; $profileModel->save(); // Save user password - $userPasswordModel->user_id = $userModel->id; + $userPasswordModel->user_id = $userModel->id; $userPasswordModel->save(); // Join space / create then join space foreach ($data['space_names'] as $key => $space_name) { // Find space by name attribute - $space = Space::model()->findByAttributes(array('name'=>$space_name)); + $space = Space::findOne(['name'=>$space_name]); // Create the space if not found if($space == null) { @@ -78,7 +107,7 @@ private function registerUser($data) { return true; } else { - Yii::app()->user->setFlash('error', CHtml::errorSummary($userModel)); + Yii::$app->session->setFlash('error', Html::errorSummary($userModel)); return false; } @@ -86,7 +115,10 @@ private function registerUser($data) { public function actionIndex(){ $form = new BulkImportForm; - $this->render('index', array('model' => $form)); + + return $this->render('index', array( + 'model' => $form + )); } public function actionSetDefaultPass() { @@ -95,8 +127,8 @@ public function actionSetDefaultPass() { $user_ids = explode(",", $_GET['user_ids']); foreach($user_ids as $user_id) { - $userPasswordModel = new UserPassword(); - $userPasswordModel->user_id = 1; + $userPasswordModel = new Password(); + $userPasswordModel->user_id = $user_id; $userPasswordModel->setPassword("password"); if($userPasswordModel->save()) { @@ -113,11 +145,9 @@ public function actionSetDefaultPass() { public function actionIdenticon() { - $assetPrefix = Yii::app()->assetManager->publish(dirname(__FILE__) . '/../resources', true, 0, defined('YII_DEBUG')); - Yii::app()->clientScript->registerScriptFile($assetPrefix . '/md5.min.js'); - Yii::app()->clientScript->registerScriptFile($assetPrefix . '/jdenticon-1.3.0.min.js'); - - $model = new User('search'); +// $assetPrefix = Yii::app()->assetManager->publish(dirname(__FILE__) . '/../assets', true, 0, defined('YII_DEBUG')); +// Yii::app()->clientScript->registerScriptFile($assetPrefix . '/md5.min.js'); +// Yii::app()->clientScript->registerScriptFile($assetPrefix . '/jdenticon-1.3.0.min.js'); if(isset($_POST['userids'])) { @@ -125,28 +155,31 @@ public function actionIdenticon() { foreach($_POST['userids'] as $user_id) { // Find User by ID - $user = User::model()->findByPk($user_id); + $user = User::findIdentity($user_id); // Upload new profile picture $this->uploadProfilePicture($user->guid, $_POST['identicon_'.$user_id.'_value']); } } - - $this->render('identicon', array( - 'model' => $model - )); + + $searchModel = new \humhub\modules\admin\models\UserSearch(); + $dataProvider = $searchModel->search(Yii::$app->request->queryParams); + + return $this->render('identicon', array( + 'dataProvider' => $dataProvider, + 'searchModel' => $searchModel + )); + } public function actionUpload() { - $assetPrefix = Yii::app()->assetManager->publish(dirname(__FILE__) . '/../resources', true, 0, defined('YII_DEBUG')); - Yii::app()->clientScript->registerScriptFile($assetPrefix . '/md5.min.js'); - Yii::app()->clientScript->registerScriptFile($assetPrefix . '/jdenticon-1.3.0.min.js'); - - - require_once("lib/parsecsv.lib.php"); - $csv = new parseCSV(); +// $assetPrefix = Yii::$app->assetManager->publish(dirname(__FILE__) . '/../assets', array('forceCopy' => true)); +// Yii::$app->clientScript->registerScriptFile($assetPrefix . '/md5.min.js'); +// Yii::$app->clientScript->registerScriptFile($assetPrefix . '/jdenticon-1.3.0.min.js'); + require_once(dirname(__FILE__) . "/../lib/parsecsv.lib.php"); + $csv = new \parseCSV(); $model = new BulkImportForm; $validImports = array(); @@ -154,10 +187,12 @@ public function actionUpload() { if(isset($_POST['BulkImportForm'])) { + $model->attributes=$_POST['BulkImportForm']; if(!empty($_FILES['BulkImportForm']['tmp_name']['csv_file'])) { - $file = CUploadedFile::getInstance($model,'csv_file'); + + $file = \yii\web\UploadedFile::getInstance($model,'csv_file'); $group_id = 1; $csv->auto($file->tempName); @@ -196,7 +231,10 @@ public function actionUpload() { } - $this->render('import_complete', array('validImports' => $validImports, 'invalidImports' => $invalidImports)); + return $this->render('import_complete', array( + 'validImports' => $validImports, + 'invalidImports' => $invalidImports, + )); } @@ -219,7 +257,7 @@ private function uploadProfilePicture($userId, $data) $profileImage = new ProfileImage($userId); $profileImage->setNew($temp_file_name); - // Remove temporary file + // Remove temporary file unlink($temp_file_name); } diff --git a/forms/BulkImportForm.php b/forms/BulkImportForm.php index 0586310..8ae0355 100755 --- a/forms/BulkImportForm.php +++ b/forms/BulkImportForm.php @@ -18,11 +18,15 @@ * along with this program. If not, see . */ +namespace humhub\modules\bulk_import\forms; + +use Yii; + /** - * @package humhub.modules_core.admin.forms + * @package humhub.modules.bulk_import.forms * @since 0.5 */ -class BulkImportForm extends CFormModel { +class BulkImportForm extends \yii\base\Model { public $csv_file; @@ -30,10 +34,13 @@ class BulkImportForm extends CFormModel { * Declares the validation rules. */ public function rules() { - return array( + /*return array( array('csv_file', 'file', 'types' => 'csv, xls', 'maxSize'=>5242880, 'allowEmpty' => true, 'wrongType'=>'Only .csv, .xls files are allowed.', 'tooLarge'=>'File too large! 5MB is the limit') // array('logo', 'file', 'types' => 'jpg, png, jpeg', 'maxSize' => 3 * 1024 * 1024, 'allowEmpty' => true), - ); + );*/ + return [ + [['cvs_file'], 'file'], + ]; } /** diff --git a/views/main/identicon.php b/views/main/identicon.php index e09aaa7..70465fc 100755 --- a/views/main/identicon.php +++ b/views/main/identicon.php @@ -16,11 +16,17 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -?> - - +use yii\helpers\Url; +use yii\helpers\Html; +use yii\grid\GridView; +use yii\data\ActiveDataProvider; +use yii\widgets\ActiveForm; +use humhub\widgets\DataSaved; + +humhub\modules\bulk_import\Assets::register($this); +?> -beginWidget('CActiveForm', array( + +'registration-form', - 'enableAjaxValidation'=>true, - 'action' => Yii::app()->createUrl('//bulk_import/main/identicon') -)); -?> + 'enableAjaxValidation'=>false, + 'action' => Url::to(['/bulk_import/main/identicon']) +)); ?> +
Identicon uploader'); ?>
@@ -51,7 +58,42 @@ function renderJdenticons() {

widget('zii.widgets.grid.CGridView', array( + echo GridView::widget([ + 'dataProvider' => $dataProvider, + 'filterModel' => $searchModel, + 'columns' => [ + array( + 'class' => 'yii\grid\CheckboxColumn', + 'checkboxOptions' => function ($model, $key, $index, $column) { + return [ + 'value' => $model->id + ]; + }, + 'name' => 'userids' + ), + 'username', + 'email', + array( + 'attribute' => 'Old', + 'format' => 'raw', + 'options' => array('width' => '35px'), + 'value' => function($model) { + return Html::img($model->profileImage->getUrl(), array("style" => "width:35px; height: 35px; background-color:#000;")); + } + ), + array( + 'attribute' => 'New', + 'format' => 'raw', + 'options' => array('width' => '35px'), + 'value' => function($model) { + return ''; + } + ), + + ] + ]); + + /*$this->widget('zii.widgets.grid.CGridView', array( 'id' => 'user-grid', 'dataProvider' => $model->resetScope()->search(), 'filter' => $model, @@ -105,12 +147,13 @@ function renderJdenticons() { ), 'pagerCssClass' => 'pagination-container', )); - + */ ?> - + 'btn btn-primary pull-right')); ?>
-
-"btn btn-primary pull-right")); ?> -
-endWidget(); ?> \ No newline at end of file + +end(); ?> + + + diff --git a/views/main/import_complete.php b/views/main/import_complete.php index 0a28701..3f49db0 100755 --- a/views/main/import_complete.php +++ b/views/main/import_complete.php @@ -21,7 +21,7 @@
Bulk Import
user->getFlashes(); + $flashes = Yii::$app->session->getAllFlashes(); if(!empty($flashes)) { echo "
"; @@ -34,7 +34,6 @@ echo "
"; } - ?>
Failed Imports
diff --git a/views/main/index.php b/views/main/index.php index 09a6a19..d7012ec 100755 --- a/views/main/index.php +++ b/views/main/index.php @@ -16,29 +16,28 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ + + +use yii\helpers\Url; +use yii\helpers\Html; +use yii\widgets\ActiveForm; ?>
Bulk Import

Upload CSV

- beginWidget('CActiveForm', array( + 'registration-form', 'enableAjaxValidation'=>true, - 'htmlOptions' => array('enctype' => 'multipart/form-data'), - 'action' => Yii::app()->createUrl('//bulk_import/main/upload') - - )); - ?> - - fileField($model,'csv_file'); ?> - error($model, 'csv_file'); ?> -
- "")); ?> + 'options' => array('enctype' => 'multipart/form-data'), + 'action' => Url::to(['/bulk_import/main/upload']) + )); ?> errorSummary($model); ?> - - endWidget(); ?> + field($model,'csv_file')->fileInput(); ?> +
+ '')); ?> + end(); ?>

@@ -58,6 +57,7 @@ username email + password firstname lastname space_names @@ -65,6 +65,7 @@ user1 user1@example.com + test123 User Wan Space 1 @@ -72,6 +73,7 @@ user2 user2@example.com + test123 User Two Space 1,Space 2