Skip to content
This repository has been archived by the owner on Oct 26, 2020. It is now read-only.

Commit

Permalink
v2
Browse files Browse the repository at this point in the history
  • Loading branch information
benmag committed Dec 8, 2016
1 parent 1aefc5a commit 787e3c5
Show file tree
Hide file tree
Showing 9 changed files with 227 additions and 97 deletions.
37 changes: 37 additions & 0 deletions Assets.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/**
* Connected Communities Initiative
* Copyright (C) 2016 Queensland University of Technology
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

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();
}
}
23 changes: 18 additions & 5 deletions BulkImportEvents.php → Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,45 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

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.
*
* @param type $event
*/
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' => '<i class="fa fa-paw"></i>',
'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' => '<i class="fa fa-paw"></i>',
'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,
));

}

}
17 changes: 4 additions & 13 deletions BulkImportModule.php → Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

class BulkImportModule extends HWebModule{

/**
* Inits the Module
*/
public function init()
{
namespace humhub\modules\bulk_import;

$this->setImport(array(
'bulk_import.forms.*',
));
}

}
class Module extends \humhub\components\Module {

}
18 changes: 9 additions & 9 deletions autostart.php → config.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

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']],
],
];
?>
106 changes: 72 additions & 34 deletions controllers/MainController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,48 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

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'];
Expand All @@ -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) {
Expand All @@ -78,15 +107,18 @@ 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;
}

}

public function actionIndex(){
$form = new BulkImportForm;
$this->render('index', array('model' => $form));

return $this->render('index', array(
'model' => $form
));
}

public function actionSetDefaultPass() {
Expand All @@ -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()) {
Expand All @@ -113,51 +145,54 @@ 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'])) {

// Loop through selected users
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();
$invalidImports = array();

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);
Expand Down Expand Up @@ -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,
));

}

Expand All @@ -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);

}
Expand Down
15 changes: 11 additions & 4 deletions forms/BulkImportForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,29 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

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;

/**
* 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'],
];
}

/**
Expand Down
Loading

0 comments on commit 787e3c5

Please sign in to comment.