forked from unitfactoryAlumni/cheatera.pp.ua
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP][unitfactoryAlumni#66] All code refactored, created new namespac…
…e: app\helpers\RememberUserInfo; Achievements RememberHelper added; fixed couple of inaccuracies in view/site/login.php, view/layouts/main.php
- Loading branch information
vbrazas
committed
Apr 6, 2019
1 parent
a08599b
commit dfe91e9
Showing
12 changed files
with
285 additions
and
194 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace app\helpers\RememberUserInfo; | ||
|
||
use app\models\Achievements; | ||
|
||
class RememberAchievements extends RememberHelper | ||
{ | ||
|
||
protected function norminateTheResponse() | ||
{ | ||
foreach ($this->response['achievements'] as &$achievement) { | ||
$achievement['xlogin'] = $this->response['login']; | ||
self::swapKeysInArr($achievement, [ 'id' => 'aid' ]); | ||
} | ||
} | ||
|
||
public function rememberToDB() | ||
{ | ||
$achievements = new Achievements(); | ||
|
||
foreach ($this->response['achievements'] as &$achievement) { | ||
self::saveChangesToDB($achievements, $achievement, $achievements->find() | ||
->Where([ 'aid' => $achievement['aid'] ]) | ||
->andWhere([ 'xlogin' => $achievement['xlogin'] ]) | ||
->all()); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace app\helpers\RememberUserInfo; | ||
|
||
use app\models\Curses; | ||
|
||
class RememberCurses extends RememberHelper | ||
{ | ||
|
||
protected function norminateTheResponse() | ||
{ | ||
foreach ($this->response['cursus_users'] as &$curs) { | ||
self::dateToSqlFormat($curs['begin_at']); | ||
self::swapKeysInArr($curs, [ 'id' => 'xid' ]); | ||
self::mergeChildArrByKey($curs, 'cursus'); | ||
unset($curs['id']); | ||
unset($curs['user']); | ||
$curs['has_coalition'] = $curs['has_coalition'] ? 'True' : 'False'; | ||
self::dateToSqlFormat($curs['created_at']); | ||
} | ||
} | ||
|
||
public function rememberToDB() | ||
{ | ||
$curses = new Curses(); | ||
|
||
foreach ($this->response['cursus_users'] as &$curs) { | ||
self::saveChangesToDB($curses, $curs, $curses->find() | ||
->Where([ 'xlogin' => $curs['xlogin'] ]) | ||
->andWhere([ 'cursus_id' => $curs['cursus_id'] ]) | ||
->all()); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
namespace app\helpers\RememberUserInfo; | ||
|
||
abstract class RememberHelper | ||
{ | ||
|
||
abstract public function rememberToDB(); | ||
abstract protected function norminateTheResponse(); | ||
|
||
|
||
protected $response; | ||
|
||
|
||
public function __construct(&$response) | ||
{ | ||
$this->response =& $response; | ||
static::norminateTheResponse(); | ||
static::rememberToDB(); | ||
} | ||
|
||
|
||
protected static function isArraysIdentical($a1, $a2, $arrayKeysToCompare) | ||
{ | ||
foreach ($arrayKeysToCompare as $keyFor_both) { | ||
if ($a1[$keyFor_both] != $a2[$keyFor_both]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
protected static function dateToSqlFormat(&$date) | ||
{ | ||
$date = date('Y-m-d H:i:s', strtotime( $date )); | ||
} | ||
|
||
protected static function swapKeysInArr(&$arrToChangeKeys, $keys) | ||
{ | ||
foreach ($keys as $keyToReplace => $keyToPut) { | ||
$arrToChangeKeys[$keyToPut] = $arrToChangeKeys[$keyToReplace]; | ||
unset($arrToChangeKeys[$keyToReplace]); | ||
} | ||
} | ||
|
||
protected static function mergeChildArrByKey(&$arr, $key) | ||
{ | ||
foreach ($arr[$key] as $key => $val) { | ||
$arr[$key] = $val; | ||
} | ||
unset($arr[$key]); | ||
} | ||
|
||
|
||
protected static function saveChangesToDB($baseActiveRecordModel, $arrToPutIntoDb, $activeRecords) | ||
{ | ||
if ( empty($activeRecords) ) { | ||
$baseActiveRecordModel->attributes = $arrToPutIntoDb; | ||
$baseActiveRecordModel->insert(false); | ||
} | ||
|
||
$identicalNotFound = true; | ||
$len = sizeof($activeRecords) - 1; | ||
|
||
foreach ($activeRecords as $index => $AR) { | ||
if ( static::isArraysIdentical($arrToPutIntoDb, $AR, $AR::attributes()) ) { | ||
$identicalNotFound = false; | ||
continue ; | ||
} | ||
|
||
if ( $identicalNotFound && $index == $len ) { | ||
$AR->attributes = $arrToPutIntoDb; | ||
$AR->update(false); | ||
} else { | ||
$AR->delete(); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace app\helpers\RememberUserInfo; | ||
|
||
use Yii; | ||
|
||
class RememberLevel extends RememberHelper | ||
{ | ||
|
||
protected function norminateTheResponse() { } | ||
|
||
public function rememberToDB() | ||
{ | ||
Yii::$app->session->set('level', $this->response['cursus_users'][0]['level']); | ||
} | ||
|
||
} |
Oops, something went wrong.