Skip to content
Snippets Groups Projects
Commit 2e3f1f64 authored by Tibor Katelbach's avatar Tibor Katelbach
Browse files

copied code from modules

parent 3e464a33
No related branches found
No related tags found
No related merge requests found
<?php
/**
* Echolocal Module
*
* @author Tibor Katelbach <oceatoon@mail.com>
* @version 0.0.3
*
*/
class EcholocalModule extends CWebModule
{
public function init()
{
// this method is called when the module is being created
// you may place code here to customize the module or the application
// import the module-level models and components
$this->setImport(array(
'egpc.models.*',
'egpc.components.*',
));
}
public function beforeControllerAction($controller, $action)
{
if(parent::beforeControllerAction($controller, $action))
{
// this method is called before any module controller action is performed
// you may place customized code here
return true;
}
else
return false;
}
}
<?php
/**
* DefaultController.php
*
* API REST pour géré l'application echolocal
*
* @author: Tibor Katelbach <tibor@pixelhumain.com>
* Date: 14/03/2014
*/
class DefaultController extends Controller {
const moduleTitle = "Etat Généraux des pouvoirs citoyens";
public static $moduleKey = "echolocal";
public $sidebar1 = array(
array('label' => "Scenario", "key"=>"scenario","onclick"=>"toggleScenario('scenario')","hide"=>true,
"blocks"=>array(
array("label"=>"Inscription / Creation",
"children"=>array(
)),
array("label"=>"Visualisation",
"children"=>array(
)),
array("label"=>"Communication",
"children"=>array(
)),
)),
array('label' => "User", "key"=>"user",
"children"=> array(
)),
array('label' => "Communication", "key"=>"communications",
"children"=> array(
)),
);
public $percent = 0; //TODO link it to unit test
protected function beforeAction($action)
{
array_push($this->sidebar1, array('label' => "All Modules", "key"=>"modules", "menuOnly"=>true,"children"=>PH::buildMenuChildren("applications") ));
return parent::beforeAction($action);
}
/**
* List all the latest observations
* @return [json Map] list
*/
public function actionIndex()
{
$this->render("index");
}
//********************************************************************************
// USERS
//********************************************************************************
/**
* actionLogin
* Login to open a session
* uses the generic Citoyens login system
* @return [type] [description]
*/
public function actionLogin()
{
$email = $_POST["email"];
$res = Citoyen::login( $email , $_POST["pwd"]);
$res = array_merge($res, Citoyen::applicationRegistered($this::$moduleKey,$email));
Rest::json($res);
Yii::app()->end();
}
/**
* [actionAddWatcher
* create or update a user account
* if the email doesn't exist creates a new citizens with corresponding data
* else simply adds the watcher app the users profile ]
* @return [json]
*/
public function actionSaveUser()
{
$email = $_POST["email"];
if( isset( Yii::app()->session["userId"] ) && Yii::app()->request->isAjaxRequest){
//if exists login else create the new user
$res = Citoyen::register( $email, $_POST["pwd"]);
if(Yii::app()->mongodb->citoyens->findOne( array( "email" => $email ) )){
//udate the new app specific fields
$newInfos = array();
if( isset($_POST['cp']) )
$newInfos['cp'] = $_POST['cp'];
if( isset($_POST['name']) )
$newInfos['name'] = $_POST['name'];
if( isset($_POST['phoneNumber']) )
$newInfos['phoneNumber'] = $_POST['phoneNumber'];
if( isset($_POST['when']) )
$newInfos['when'] = $_POST['when'];
if( isset($_POST['where']) )
$newInfos['where'] = $_POST['where'];
$newInfos['applications'] = array( $this::$moduleKey => array( "usertype"=>$_POST['type'] ,"registrationConfirmed" => false ));
Yii::app()->mongodb->citoyens->update( array("email" => $email),
array('$set' => $newInfos )
);
}
} else
$res = array('result' => false , 'msg'=>'something somewhere went terribly wrong');
Rest::json($res);
Yii::app()->end();
}
/**
* [actionGetWatcher get the user data based on his id]
* @param [string] $email email connected to the citizen account
* @return [type] [description]
*/
public function actionGetUser($email)
{
$user = Yii::app()->mongodb->citoyens->findOne( array( "email" => $email ) );
Rest::json( $user );
Yii::app()->end();
}
public function actionConfirmUserRegistration($email)
{
//TODO : add a test adminUser
//isAppAdminUser
if( isset( Yii::app()->session["userId"] ) ) {
$user = Yii::app()->mongodb->citoyens->findAndModify( array("email" => $email),
array('$set' => array("applications.echolocal.registrationConfirmed"=>true) ) );
$user = Yii::app()->mongodb->citoyens->findOne( array( "email" => $email ) );
Rest::json( $user );
}
Yii::app()->end();
}
public function actionGetPeople()
{
if( isset( $_POST["name"] ) ){
$where["name"] = $_POST["name"];
$group = Yii::app()->mongodb->groups->findOne ( array( "name" => $_POST["name"] ) );
$users = Yii::app()->mongodb->citoyens->find ( array( "associations" => (string)$group['_id']) );
} else {
$users = Yii::app()->mongodb->citoyens->find ( array( "applications.echolocal.usertype" => $this::$moduleKey ) );
}
Rest::json( iterator_to_array($users) );
Yii::app()->end();
}
//********************************************************************************
// ENTITIES
//********************************************************************************
public function actionSaveGroup()
{
$email = $_POST["email"];
if( isset( Yii::app()->session["userId"] ) && Yii::app()->request->isAjaxRequest){
//creating user must exist
if($user = Yii::app()->mongodb->citoyens->findOne( array( "email" => $email ) ))
{
//udate the new app specific fields
$newInfos = array();
if( isset($_POST['email']) )
$newInfos['email'] = $_POST['email'];
if( isset($_POST['cp']) )
$newInfos['cp'] = $_POST['cp'];
if( isset($_POST['name']) )
$newInfos['name'] = $_POST['name'];
if( isset($_POST['phoneNumber']) )
$newInfos['phoneNumber'] = $_POST['phoneNumber'];
if( isset($_POST['type']) )
$newInfos['type'] = $_POST['type'];
$newInfos['applications'] = array( $this::$moduleKey => array( "usertype"=>$_POST['type'],"registrationConfirmed" => false ));
//if exists login else create the new group
if(!Yii::app()->mongodb->groups->findOne( array( "type"=>$_POST['type'],"name"=>$_POST['name'] ) ))
{
Yii::app()->mongodb->groups->insert( $newInfos);
$res = array("result" => true,
"msg" => $_POST['type']." has be created or updated");
} else {
//if there's an email change
Yii::app()->mongodb->groups->update( array("name" => $_POST['name']),
array('$set' => $newInfos )
);
}
} else
$res = array('result' => false, "msg"=>"Connected user must exist");
} else
$res = array('result' => false , 'msg'=>'something somewhere went terribly wrong');
Rest::json($res);
Yii::app()->end();
}
public function actionGetGroup($email)
{
$res = Yii::app()->mongodb->groups->find( array( "email" => $email ) );
Rest::json( iterator_to_array($res) );
Yii::app()->end();
}
public function actionGetGroups()
{
//TODO : other fgroup types
$res = Yii::app()->mongodb->groups->find( array( "applications.echolocal.usertype" => Group::TYPE_ASSOCIATION ));
Rest::json( iterator_to_array($res) );
Yii::app()->end();
}
public function actionLinkUser2Group()
{
if( isset( Yii::app()->session["userId"] ) && Yii::app()->request->isAjaxRequest && isset( $_POST['email'] ) && isset( $_POST['name'] ) )
{
$emails = explode(",",$_POST['email'] );
$res = array();
foreach ($emails as $email) {
$res = array_merge($res, Group::addMember($email , $_POST['name'], Group::TYPE_ASSOCIATION ));
}
} else
$res = array('result' => false , 'msg'=>'something somewhere went terribly wrong');
Rest::json($res);
Yii::app()->end();
}
public function actionUnLinkUser2Group()
{
if( isset( Yii::app()->session["userId"] ) && Yii::app()->request->isAjaxRequest && isset( $_POST['email'] ) && isset( $_POST['name'] ) )
{
$emails = explode(",",$_POST['email'] );
$res = array();
foreach ($emails as $email) {
$res = array_merge($res, Group::removeMember($email , $_POST['name'], Group::TYPE_ASSOCIATION ));
}
} else
$res = array('result' => false , 'msg'=>'something somewhere went terribly wrong');
Rest::json($res);
Yii::app()->end();
}
//********************************************************************************
// COMMUNICATIONS
//********************************************************************************
public function actionSendMessage()
{
if( isset( Yii::app()->session["userId"] ) && Yii::app()->request->isAjaxRequest && isset( $_POST['email'] ) && isset( $_POST['msg'] ) )
{
$res = Message::createMessage($_POST['email'] , $_POST['msg'], self::$moduleKey );
} else
$res = array('result' => false , 'msg'=>'something somewhere went terribly wrong');
Rest::json($res);
Yii::app()->end();
}
}
\ No newline at end of file
<style>
.apiList h2,.apiList h3,.apiList h4 {
font-family: "Homestead";
position:relative;
top:0px;
left:0px;
color: #324553;
background-color: white;
padding : 10px;
border : 3px solid #666;
}
.apiList ul{list-style: none;}
.apiList .block{
border:1px solid #333;
background-color: #ededed;
margin:10px;
padding:10px;
}
.fss{
line-height: 20px;
}
.result{
color: red;
}
.blocki{
background-color: #324553;
color: white;
padding : 10px;
border : 3px solid #666;
}
h4.blocky{
background-color: #fff95e
}
h3.blockp{background-color: #ffd150}
</style>
<div class="containeri apiList">
<div class="hero-uniti">
<h2>Echolocal API : List all URLs</h2>
<ul>
<!-- ////////////////////////////////////////////////////////////////////////////// -->
<li ><h3 class="blockp">Scenario</h3></li>
<li>
<h4 class="blocky">Inscription / Creation</h4>
<ul class="blocki">
<li></li>
</ul>
<h4 class="blocky">Visualisation</h4>
<ul class="blocki">
<li></li>
</ul>
</li>
<!-- ////////////////////////////////////////////////////////////////////////////// -->
<li><h3 class="blockp">User</h3></li>
<li class="block" id="blockLogin">
<a href="/ph/echolocal/default/login">Login</a><br/>
<div class="fss">
Il faut etre loguer par email, cp, et mot de passe<br/>
method type : POST <br/>
</div>
<div class="apiForm login">
email : <input type="text" name="emailLogin" id="emailLogin" value="oceatoon@gmail.com" /><br/>
pwd : <input type="password" name="pwdLogin" id="pwdLogin" value="2210" /><br/>
<a href="javascript:login()">Test it</a><br/>
<div id="loginResult" class="result fss"></div>
<script>
function login(){
$("#loginResult").html("");
params = { "email" : $("#emailLogin").val() ,
"pwd" : $("#pwdLogin").val()};
testitpost("loginResult",'/ph/echolocal/default/login',params);
}
</script>
</div>
</li>
<li class="block"><a href="/ph/echolocal/default/saveUser" id="blockSaveUser">Create/Update user</a><br/>
<div class="fss">
url : /ph/echolocal/default/saveUser<br/>
method type : POST <br/>
Form inputs : email,postalcode,pwd,phoneNumber(is optional)<br/>
return json object {"result":true || false}
</div>
<div class="apiForm createUser">
name : <input type="text" name="nameSaveUser" id="nameSaveUser" value="echolocal User" /><br/>
email* : <input type="text" name="emailSaveUser" id="emailSaveUser" value="echolocal@echolocal.com" /><br/>
cp* : <input type="text" name="postalcodeSaveUser" id="postalcodeSaveUser" value="97421" /><br/>
pwd* : <input type="text" name="pwdSaveUser" id="pwdSaveUser" value="1234" /><br/>
phoneNumber : <input type="text" name="phoneNumberSaveUser" id="phoneNumberSaveUser" value="1234" />(for SMS)<br/>
type : <select name="typeSaveUser" id="typeSaveUser">
<option value="echolocal">Participant</option>
<option value="adminecholocal">adminecholocal</option>
</select><br/>
<a href="javascript:addUser()">Test it</a><br/>
<div id="createUserResult" class="result fss"></div>
<script>
function addUser(){
params = { "email" : $("#emailSaveUser").val() ,
"name" : $("#nameSaveUser").val() ,
"cp" : $("#postalcodeSaveUser").val() ,
"type" : $("#typeSaveUser").val(),
"phoneNumber" : $("#phoneNumberSaveUser").val()};
testitpost("createUserResult",'/ph/echolocal/default/saveUser',params);
}
</script>
</div>
</li>
<li class="block"><a href="/ph/echolocal/default/getUser/email/oceatoon@gmail.com" id="blockGetUser">Get User</a><br/>
<div class="fss">
url : /ph/echolocal/default/getUser/email/oceatoon@gmail.com<br/>
method type : GET <br/>
param : email<br/>
email : <input type="text" name="getUseremail" id="getUseremail" value="oceatoon@gmail.com" /><br/>
<a href="javascript:getUser()">Test it</a><br/>
<a href="javascript:confirmUserRegistration()">Confirm User Registration</a><br/>
<div id="getUserResult" class="result fss"></div>
<script>
function getUser(){
testitget("getUserResult",'/ph/echolocal/default/getUser/email/'+$("#getUseremail").val());
}
function confirmUserRegistration(){
testitget("getUserResult",'/ph/echolocal/default/confirmUserRegistration/email/'+$("#getUseremail").val());
}
</script>
</div>
</li>
<li class="block"><a href="/ph/echolocal/default/getPeople">Get echolocal People</a><br/>
<div class="fss">
url : /ph/echolocal/default/getPeople<br/>
method type : GET <br/>
<a href="javascript:getPeople()">Test it</a><br/>
<div id="getPeopleResult" class="result fss"></div>
<script>
function getPeople(){
testitget("getPeopleResult",'/ph/echolocal/default/getPeople');
}
</script>
</div>
</li>
<!-- ////////////////////////////////////////////////////////////////////////////// -->
<li><h3 class="blockp">Entities</h3></li>
<li class="block">
<a href="javascript:;" class="btn btn-primary" onclick="openModal('groupCreerForm','data',null,'dynamicallyBuild')">Form</a>
<a href="/ph/echolocal/default/saveGroup">Create/Update Entité (Asso. , Entr. , Group )</a>
<div class="fss">
url : /ph/echolocal/default/saveGroup<br/>
method type : POST <br/>
Form inputs : email,postalcode,pwd,phoneNumber(is optional)type<br/>
return json object {"result":true || false}
</div>
<div class="apiForm createUser">
name : <input type="text" name="namesaveGroup" id="namesaveGroup" value="Asso1" /><br/>
email* : <input type="text" name="emailsaveGroup" id="emailsaveGroup" value="echolocal@echolocal.com" /> (personne physique responsable )<br/>
cp* : <input type="text" name="postalcodesaveGroup" id="postalcodesaveGroup" value="97421" /><br/>
phoneNumber : <input type="text" name="phoneNumbersaveGroup" id="phoneNumbersaveGroup" value="1234" />(for SMS)<br/>
type : <select name="typesaveGroup" id="typesaveGroup" onchange="typeChanged()">
<option value="association">Association</option>
<option value="entreprise">Entreprise</option>
<option value="group">Groupe de personne</option>
<option value="event">Evenement</option>
</select><br/>
<span class="hide whensaveGroup">
when : <input type="text" name="whensaveGroup" id="whensaveGroup" value="" />
where : <input type="text" name="wheresaveGroup" id="wheresaveGroup" value="" />
</span><br/>
<a href="javascript:addUser()">Test it</a><br/>
<div id="saveGroupResult" class="result fss"></div>
<script>
function addUser(){
params = { "email" : $("#emailsaveGroup").val() ,
"name" : $("#namesaveGroup").val() ,
"cp" : $("#postalcodesaveGroup").val() ,
"pwd" : $("#pwdsaveGroup").val(),
"type" : $("#typesaveGroup").val(),
"phoneNumber" : $("#phoneNumbersaveGroup").val()};
if( $("#whensaveGroup").val() )
paramas["when"] = $("#whensaveGroup").val();
if( $("#wheresaveGroup").val() )
paramas["where"] = $("#wheresaveGroup").val();
testitpost("saveGroupResult",'/ph/echolocal/default/saveGroup',params);
}
function typeChanged(){
console.log( $("#typesaveGroup").val() );
if ($("#typesaveGroup").val() == "event") {
$(".whensaveGroup").show();
} else {
$(".whensaveGroup").hide();
}
}
</script>
</div>
</li>
<li class="block">
<a href="javascript:;" class="btn btn-primary" onclick="openModal('groupCreerForm','data',null,'dynamicallyBuild')">Get</a>
<a href="/ph/echolocal/default/getGroup">Get une Entité by email </a><br/>
<div class="fss">
url : /ph/echolocal/default/getGroup/email/echolocal@echolocal.com<br/>
method type : GET <br/>
param : email<br/>
email : <input type="text" name="getGroupemail" id="getGroupemail" value="echolocal@echolocal.com" /><br/>
<a href="javascript:getGroup()">Test it</a><br/>
<a href="javascript:confirmUserRegistration()">Confirm User Registration</a><br/>
<div id="getGroupResult" class="result fss"></div>
<script>
function getGroup(){
testitget("getGroupResult",'/ph/echolocal/default/getGroup/email/'+$("#getGroupemail").val());
}
function confirmUserRegistration(){
testitget("getGroupResult",'/ph/echolocal/default/confirmGroupRegistration/email/'+$("#getGroupemail").val());
}
</script>
</div>
</li>
<li class="block">
<a href="javascript:;" class="btn btn-primary" onclick="openModal('groupCreerForm','data',null,'dynamicallyBuild')">Link Form</a>
<a href="/ph/echolocal/default/linkUser2Group">Lié un User a une Entité</a><br/>
<div class="fss">
url : /ph/echolocal/default/linkUser2Group/email/echolocal@echolocal.com<br/>
method type : GET <br/>
param : email<br/>
all echolocal groups : <input type="text" name="linkUser2GroupGroup" id="linkUser2GroupGroup" value="Asso1" />(auto-complete)<br/>
email(s) : <textarea type="text" name="linkUser2Groupemail" id="linkUser2Groupemail">echolocal@echolocal.com</textarea><br/>
séparé par des virgules<br/>
<a href="javascript:linkUser2Group()">Link it</a><br/>
<a href="javascript:unlinkUser2Group()">Unlink it</a><br/>
<div id="linkUser2GroupResult" class="result fss"></div>
<script>
function linkUser2Group(){
params = {
"email" : $("#linkUser2Groupemail").val() ,
"name" : $("#linkUser2GroupGroup").val()
};
testitpost("linkUser2GroupResult",'/ph/echolocal/default/linkUser2Group',params);
}
function unlinkUser2Group(){
params = {
"email" : $("#linkUser2Groupemail").val() ,
"name" : $("#linkUser2GroupGroup").val()
};
testitpost("linkUser2GroupResult",'/ph/echolocal/default/unlinkUser2Group',params);
}
</script>
</div>
</li>
<li class="block">
<a href="javascript:;" class="btn btn-primary" onclick="openModal('groupCreerForm','data',null,'dynamicallyBuild')">Get</a>
<a href="/ph/echolocal/default/getGroups">Get all echolocal Entités</a><br/>
<div class="fss">
url : /ph/echolocal/default/getGroups<br/>
method type : GET <br/>
<a href="javascript:getGroups()">Test it</a><br/>
<div id="getGroupsResult" class="result fss"></div>
<script>
function getGroups(){
testitget("getGroupsResult",'/ph/echolocal/default/getGroups');
}
</script>
</div>
</li>
</ul>
</div>
</div>
div.toto*2>ul>li*4>a
<script type="text/javascript">
function testitpost(id,url,params){
console.log(id,url,params);
$("#"+id).html("");
$.ajax({
url:url,
data:params,
type:"POST",
success:function(data) {
$("#"+id).html(JSON.stringify(data, null, 4));
},
error:function (xhr, ajaxOptions, thrownError){
$("#"+id).html(data);
}
});
}
function testitget(id,url){
$("#"+id).html("");
$.ajax({
url:url,
type:"GET",
success:function(data) {
$("#"+id).html(JSON.stringify(data, null, 4));
},
error:function (xhr, ajaxOptions, thrownError){
$("#"+id).html(data);
}
});
}
</script>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment