-
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.
- Loading branch information
1 parent
6dee76f
commit d12df85
Showing
6 changed files
with
720 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
composer.phar | ||
/vendor/ | ||
|
||
.idea/ | ||
composer.lock | ||
# Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control | ||
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file | ||
# composer.lock |
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 |
---|---|---|
@@ -1,2 +1,35 @@ | ||
# yii-lib-cookie | ||
Cookie library for Yii2 framework - very easy and powerfull | ||
## Set Component | ||
In Yii Advanced go to path: | ||
``common\config\main.php`` | ||
And in Yii Basic go to path: | ||
``config\main.php`` | ||
|
||
Add component to `component` array: | ||
```php | ||
[ | ||
'components'=> | ||
[ | ||
'cookie'=>YiiMan\YiiLibCookie\lib\cookie::class | ||
] | ||
] | ||
``` | ||
|
||
Now you can call component like this: | ||
```php | ||
Yii::$app->cookie | ||
``` | ||
|
||
## Set value to cookie | ||
|
||
Just do : | ||
```php | ||
Yii::$app->cookie->newKey='new value'; | ||
``` | ||
|
||
## Get saved value | ||
Just do : | ||
```php | ||
$value=Yii::$app->cookie->newKey | ||
``` |
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 @@ | ||
{ | ||
"name": "yiiman/yii-lib-cookie", | ||
"description": "Cookie library for Yii2 and Yii3 framework - very easy and powerfull", | ||
"type": "library", | ||
"license": "BSD-3-Clause", | ||
"autoload": { | ||
"psr-4": { | ||
"Yiiman\\YiiLibCookie\\": "src/" | ||
} | ||
}, | ||
"authors": [ | ||
{ | ||
"name": "YiiMan", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"minimum-stability": "dev", | ||
"replace": { | ||
"bower-asset/jquery": ">=1.11.0", | ||
"bower-asset/inputmask": ">=3.2.0", | ||
"bower-asset/punycode": ">=1.3.0", | ||
"bower-asset/yii2-pjax": ">=2.0.0" | ||
}, | ||
"require": { | ||
"php": ">=7.0", | ||
"yiisoft/yii2": "2.0.*", | ||
"ext-json": "*", | ||
"yiiman/yii-module-setting": "0.0.2" | ||
} | ||
} |
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,284 @@ | ||
<?php | ||
/** | ||
* Created by YiiMan. | ||
* Programmer: gholamreza beheshtian | ||
* Mobile:09353466620 | ||
* Site:http://yiiman.ir | ||
* Date: 7/30/2018 | ||
* Time: 7:45 PM | ||
*/ | ||
|
||
namespace Yiiman\YiiLibCookie\lib; | ||
|
||
|
||
use Exception; | ||
use Yii; | ||
use yii\base\Component; | ||
use YiiMan\Setting\module\components\Options; | ||
|
||
/** | ||
* Class Object | ||
* @package system\lib | ||
* @property \YiiMan\Setting\module\components\Options $options | ||
*/ | ||
class Object1 extends Component { | ||
|
||
/** | ||
* you should set this parameter equal common\Homes model for give packed user data from this class in global | ||
* if not set this parameterT then this parameter will be set equal common\model\Applicant and find logged in user | ||
* @var $model | ||
*/ | ||
public $model; | ||
|
||
/** | ||
* this attribute will save unpacked other column data in Home table | ||
* @var $object | ||
*/ | ||
public $object; | ||
|
||
/** | ||
* will set upload dir | ||
* @var $uploadDir string | ||
*/ | ||
public $uploadDir; | ||
|
||
/** | ||
* global upload url | ||
* @var $uploadUrl string | ||
*/ | ||
public $uploadUrl; | ||
|
||
public $options; | ||
|
||
public $baseUrl; | ||
|
||
public function init() { | ||
parent::init(); // TODO: Change the autogenerated stub | ||
|
||
|
||
if ( empty( $this->uploadDir ) ) { | ||
$this->uploadDir = realpath( $_ENV['uploadDir'] ); | ||
} | ||
if ( empty( $this->uploadUrl ) ) { | ||
$this->uploadUrl = $_ENV['uploadURL'] ; | ||
} | ||
|
||
if ( empty( $this->baseUrl ) ) { | ||
$this->baseUrl = $_ENV['SiteURL']; | ||
} | ||
|
||
/* < UnpackData > */ | ||
{ | ||
if ( ! empty( $this->model->other ) ) { | ||
$pack = json_decode( $this->model->other ); | ||
$this->object = []; | ||
foreach ( $pack as $property => $value ) { | ||
$this->object[ $property ] = $value; | ||
} | ||
$this->object = (object) $this->object; | ||
} | ||
|
||
} | ||
/* </ UnpackData > */ | ||
|
||
$this->options=new Options(); | ||
|
||
} | ||
|
||
/** | ||
* @param string $name | ||
* @param mixed $value | ||
* | ||
* @throws \yii\base\UnknownPropertyException | ||
*/ | ||
public function __set( $name, $value ) { | ||
if ($this->hasProperty( $name)){ | ||
$this->$name = $value; | ||
}else{ | ||
$this->setAttribute( $name , $value); | ||
} | ||
|
||
|
||
|
||
} | ||
|
||
public function save() { | ||
|
||
|
||
$pack = json_encode( get_object_vars( $this ) ); | ||
|
||
|
||
$this->model->other = $pack; | ||
|
||
|
||
$this->model->save(); | ||
} | ||
|
||
public function __get( $name ) { | ||
if ( empty( $this->$name ) ) { | ||
if ( ! empty( $this->object->$name ) ) { | ||
return $this->object->$name; | ||
} else { | ||
if ( ! empty( $this->model->$name ) ) { | ||
return $this->model->$name; | ||
}else{ | ||
|
||
$getter = 'get' . $name; | ||
if (method_exists($this, $getter)) { | ||
return $this->$getter(); | ||
} | ||
} | ||
} | ||
|
||
return $this->$name = ''; | ||
} else { | ||
if ( is_float( $this->{$name} ) ) { | ||
return round( $this->{$name}, 2 ); | ||
} else { | ||
return $this->$name; | ||
} | ||
} | ||
} | ||
|
||
public function __isset( $name ) { | ||
return $this->__get( $name); | ||
} | ||
|
||
public function setAttribute( $AttributeName, $value ) { | ||
if ( empty( $this->object ) ) { | ||
$this->object = (object) []; | ||
} | ||
$this->object->$AttributeName = $value; | ||
} | ||
|
||
public function update( $user ) { | ||
$post = \Yii::$app->request->post(); | ||
|
||
$this->model->save(); | ||
} | ||
|
||
/** | ||
* @param string $attributeName | ||
* @param $model | ||
* @param string $folderName \ | ||
* @param string $fileName | ||
* @return string fileName | ||
*/ | ||
protected function getFile( $attributeName, $model, $folderName, $fileName = null ) { | ||
$file = UploadedFile::getInstance( $model, $attributeName ); | ||
if ( empty( $fileName ) ) { | ||
$fileName = $this->hash . '-res.' . $file->extension; | ||
} else { | ||
$fileName = $fileName . '.' . $file->extension; | ||
} | ||
|
||
/* < Make Folder if not exist > */ | ||
{ | ||
$folder = $this->uploadDir . '/' . $folderName . "/"; | ||
|
||
if ( ! file_exists( $folder ) ) { | ||
mkdir( $this->uploadDir . '/' . $folderName, 0777 ); | ||
} | ||
} | ||
/* </ Make Folder if not exist > */ | ||
|
||
/** | ||
* @var $model | ||
*/ | ||
$file->saveAs( $this->uploadDir . '/' . $folderName . '/' . $fileName ); | ||
switch ( $file->error ) { | ||
case UPLOAD_ERR_OK: | ||
|
||
$attr = $this->{$attributeName}; | ||
if ( ! empty( $attr ) ) { | ||
if ( file_exists( $this->uploadDir . '/' . $this->{$attributeName} ) ) { | ||
unlink( $this->uploadDir . '/' . $this->{$attributeName} ); | ||
} | ||
} | ||
break; | ||
case UPLOAD_ERR_NO_FILE: | ||
throw new \RuntimeException( 'No file sent.' ); | ||
case UPLOAD_ERR_INI_SIZE: | ||
case UPLOAD_ERR_FORM_SIZE: | ||
throw new \RuntimeException( 'Exceeded filesize limit.' ); | ||
case UPLOAD_ERR_PARTIAL: | ||
throw new \RuntimeException( 'Exceeded maximum upload size.' ); | ||
default: | ||
throw new \RuntimeException( 'Unknown errors.' ); | ||
break; | ||
} | ||
|
||
$this->setAttribute( $attributeName, $fileName ); | ||
$this->update( $model ); | ||
return $fileName; | ||
} | ||
|
||
/** | ||
* @param string $attributeName | ||
* @param $model | ||
* @param string $folderName | ||
*/ | ||
protected function getFiles( $attributeName, $model, $folderName ) { | ||
$file = UploadedFile::getInstances( $model, $attributeName ); | ||
|
||
|
||
/* < Make Folder if not exist > */ | ||
{ | ||
$folder = $this->uploadDir . '/' . $folderName . "/"; | ||
|
||
if ( ! file_exists( $folder ) ) { | ||
mkdir( $this->uploadDir . '/' . $folderName, 0777 ); | ||
} | ||
} | ||
/* </ Make Folder if not exist > */ | ||
|
||
/** | ||
* @var $model | ||
*/ | ||
if ( ! empty( $file ) ) { | ||
$FileArray = []; | ||
foreach ( $file as $key => $f ) { | ||
$f->saveAs( $this->uploadDir . '/' . $folderName . '/' . $f->name ); | ||
switch ( $f->error ) { | ||
case UPLOAD_ERR_OK: | ||
break; | ||
case UPLOAD_ERR_NO_FILE: | ||
throw new \RuntimeException( 'No file sent.' ); | ||
case UPLOAD_ERR_INI_SIZE: | ||
case UPLOAD_ERR_FORM_SIZE: | ||
throw new \RuntimeException( 'Exceeded filesize limit.' ); | ||
case UPLOAD_ERR_PARTIAL: | ||
throw new \RuntimeException( 'Exceeded maximum upload size.' ); | ||
default: | ||
throw new \RuntimeException( 'Unknown errors.' ); | ||
break; | ||
} | ||
$FileArray[] = $f->name; | ||
} | ||
$oldFiles = \Yii::$app->Applicant->{$attributeName}; | ||
|
||
if ( ! empty( $oldFiles ) ) { | ||
$FileArray = ArrayHelper::merge( $FileArray, $oldFiles ); | ||
} | ||
$this->setAttribute( $attributeName, $FileArray ); | ||
$this->update( $model ); | ||
} | ||
|
||
|
||
} | ||
|
||
/** | ||
* this function will pack Applicant->other objects to json and will return json data | ||
* @return null|string | ||
*/ | ||
protected function packObject() { | ||
if ( ! empty( $this->object ) ) { | ||
|
||
|
||
return json_encode( $this->object ); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.