Class 'Imagine\Gd\Imagine' not found
I am using cropbox extension in yii2 and it is throughing this error
although namespace yii/imagine is used in model file
Following is model class file
namespace app\models;
use Yii;
use yii\imagine;
use yii\imagine\Image;
// use yii\imagine\Gd;
use yii\helpers\Json;
use Imagine\Image\Box;
use Imagine\Image\Point;
/**
* This is the model class for table "tbl_slider_pics".
*
* #property integer $id
* #property string $path
* #property integer $status
* #property string $ip
* #property string $crt_by
* #property string $crt_time
* #property string $mod_by
* #property string $mod_time
*/
class TblSliderPics extends \yii\db\ActiveRecord
{
/**
* #inheritdoc
*/
public $image;
public $crop_info;
public static function tableName()
{
return 'tbl_slider_pics';
}
/**
* #inheritdoc
*/
public function rules()
{
return [
[['path'], 'required'],
[['status'], 'integer'],
[['path'], 'string', 'max' => 2000],
[['ip'], 'string', 'max' => 30],
[['crt_by', 'mod_by'], 'string', 'max' => 50],
[['crt_time', 'mod_time'], 'string', 'max' => 20],
[
'image',
'image',
'extensions' => ['jpg', 'jpeg', 'png', 'gif'],
'mimeTypes' => ['image/jpeg', 'image/pjpeg', 'image/png', 'image/gif'],
],
['crop_info', 'safe'],
];
}
/**
* #inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'path' => 'Path',
'status' => 'Status',
'ip' => 'Ip',
'crt_by' => 'Crt By',
'crt_time' => 'Crt Time',
'mod_by' => 'Mod By',
'mod_time' => 'Mod Time',
];
}
public function afterSave($insert, $changedAttributes)
{
// open image
$image = Image::getImagine()->open($this->image->tempName);
// rendering information about crop of ONE option
$cropInfo = Json::decode($this->crop_info)[0];
$cropInfo['dWidth'] = (int)$cropInfo['dWidth']; //new width image
$cropInfo['dHeight'] = (int)$cropInfo['dHeight']; //new height image
$cropInfo['x'] = $cropInfo['x']; //begin position of frame crop by X
$cropInfo['y'] = $cropInfo['y']; //begin position of frame crop by Y
// Properties bolow we don't use in this example
//$cropInfo['ratio'] = $cropInfo['ratio'] == 0 ? 1.0 : (float)$cropInfo['ratio']; //ratio image.
//$cropInfo['width'] = (int)$cropInfo['width']; //width of cropped image
//$cropInfo['height'] = (int)$cropInfo['height']; //height of cropped image
//$cropInfo['sWidth'] = (int)$cropInfo['sWidth']; //width of source image
//$cropInfo['sHeight'] = (int)$cropInfo['sHeight']; //height of source image
//delete old images
$oldImages = FileHelper::findFiles(Yii::getAlias('/home/sweet947/public_html/fortaj.ca/backend/uploads/'), [
'only' => [
$this->id . '.*',
'thumb_' . $this->id . '.*',
],
]);
for ($i = 0; $i != count($oldImages); $i++) {
#unlink($oldImages[$i]);
}
//saving thumbnail
$newSizeThumb = new Box($cropInfo['dWidth'], $cropInfo['dHeight']);
$cropSizeThumb = new Box(200, 200); //frame size of crop
$cropPointThumb = new Point($cropInfo['x'], $cropInfo['y']);
$pathThumbImage = Yii::getAlias('/home/sweet947/public_html/fortaj.ca/backend/uploads/')
. '/thumb_'
. $this->id
. '.'
. $this->image->getExtension();
$image->resize($newSizeThumb)
->crop($cropPointThumb, $cropSizeThumb)
->save($pathThumbImage, ['quality' => 100]);
//saving original
$this->path->saveAs(
Yii::getAlias('/home/sweet947/public_html/fortaj.ca/backend/uploads/')
. '/'
. $this->id
. '.'
. $this->image->getExtension()
);
}
}
Please help me!!
Related
I have created a widget to render the form in layouts/main.php footer section.
This is a widget file named common\widget\SubscriberFormWidget.php
<?php
namespace common\widgets;
use Yii;
use yii\base\Widget;
use common\models\Subscriber;
class SubscriberFormWidget extends Widget
{
/**
*#return string
*/
public function run()
{
$model = new Subscriber();
return $this->render('_form', [
'model' => $model
]);
}
}
This is _form file used in above widget located in common\widget\views\_form.php
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* #var $this yii\web\View */
/* #var $model common\models\Subscriber */
/* #var $form yii\widgets\ActiveForm */
?>
<?php $form = ActiveForm::begin(['action' => ['subscriber/subscribe']]); ?>
<div class="row">
<div class="col-md-6">
<?= $form->field($model, 'email')->textInput(['maxlength' => true, 'placeholder' => 'Enter # Email to subscribe!'])->label(false) ?>
</div>
<div class="col-md-6">
<?= Html::submitButton('Subscribe', ['class' => 'btn btn-success']) ?>
</div>
</div>
<?php ActiveForm::end(); ?>
This is controller file frontend\controllers\SubscriberController.php
<?php
namespace frontend\controllers;
use Yii;
use common\models\Subscriber;
use common\models\SubscriberSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
/**
* SubscriberController implements the CRUD actions for Subscriber model.
*/
class SubscriberController extends Controller
{
/**
* Creates a new Subscriber model.
* If creation is successful, the browser will be redirected to the 'view' page.
* #return mixed
*/
public function actionSubscribe()
{
$model = new Subscriber();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
if ($model->sendEmail()) {
Yii::$app->session->setFlash('success', 'You have successfully subscribed My-Blog. You will get notification whenever New post is published');
return $this->redirect(Yii::$app->request->referrer);
} else {
Yii::$app->session->setFlash('error', 'Sorry, we are unable to subscribe for the provided email address.');
}
}
return $this->redirect([Yii::$app->request->referrer, ['model' => $model]]);
}
/**
* Creates a new Subscriber model.
* If unsubscribe is successful, the browser will be redirected to the 'view' page.
* #return mixed
*/
}
I have used widget in layouts\main.php in footer section
<footer class="footer">
<div class="container">
<div class="row">
<div class="col-md-3">
<p class="pull-left">© <?= Html::encode(Yii::$app->name) ?> <?= date('Y') ?></p>
</div>
<div class="col-md-6 text-justify" style="border-left : 2px solid black; border-right: 2px solid black">
<?= SubscriberFormWidget::widget(); ?>
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into elec
</p>
</div>
<div class="col-md-3">
<p class="pull-right"><?= Yii::powered() ?></p>
</div>
</div>
</div>
</footer>
This is model used for controller common\models\Subscriber.php
<?php
namespace common\models;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\db\Expression;
/**
* This is the model class for table "subscriber".
*
* #property int $id
* #property string $email
* #property string $token
* #property int $status
* #property int $created_at
* #property int $updated_at
*/
class Subscriber extends \yii\db\ActiveRecord
{
const STATUS_DEACTIVE = 0;
const STATUS_ACTIVE = 1;
/**
* #inheritdoc
*/
public static function tableName()
{
return 'subscriber';
}
public function behaviors()
{
return [
'timestamp' => [
'class' => TimestampBehavior::className(),
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
],
'value' => new Expression('NOW()'),
],
];
}
/**
* #inheritdoc
*/
public function rules()
{
return [
[['email'], 'required'],
[['status', 'created_at', 'updated_at'], 'safe'],
[['email'], 'string', 'max' => 60],
[['token'], 'string', 'max' => 255],
[['token'], 'unique'],
[['email'], 'unique', 'targetClass' => '\common\models\Subscriber', 'message' => 'This email has already subscribed our blog.','filter' => ['!=','status' ,0]],
];
}
/**
* #inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'email' => 'Email',
'token' => 'Token',
'status' => 'Status',
'created_at' => 'Created At',
'updated_at' => 'Updated At',
];
}
/**
* Generates subscriber token
*/
public function generateSubscriberToken()
{
return $this->token = Yii::$app->security->generateRandomString() . '_' . time();
}
/**
* Send Email when successfully subscribe
*/
public function sendEmail()
{
$subscribers = self::find()->where(['email' => $this->email])->one();
//set flag for sending email
$sendMail = false;
//email subject
$subject = '';
//generate token
$token = $this->generateSubscriberToken();
//if email found in subscribers
if ($subscribers !== null) {
//check if inactive
if ($subscribers->status !== self::STATUS_ACTIVE) {
//assign token
$subscribers->token = $token;
//set status to active
$subscribers->status = self::STATUS_ACTIVE;
print_r($subscribers->errors);
//update the recrod
if (!$subscribers->save()) {
return false;
}
//set subject
$subject = 'Welcome back ' . $this->email . 'Thank you for subscribing ' . Yii::$app->name . '<br /><br /> You will receive notification whenever new trick or post is published to website';
$sendMail = true;
}
} else { //if email does not exist only then insert a new record
$this->status = 1;
if (!$this->save()) {
return false;
}
$subject = 'Thank you ' . $this->email . ' for subscribing ' . Yii::$app->name . '<br /><br /> You will receive notification whenever new trick or post is published to website';
$sendMail = true;
}
//check if send mail flag set
if ($sendMail) {
return Yii::$app->mailer
->compose()
->setFrom(['noreply#my-blog.com' => Yii::$app->name . ' robot'])
->setTo('piyush#localhost')
->setSubject('Subscription : ' . Yii::$app->name)
->setHtmlBody($subject)
->send();
}
}
}
Now I want form to be worked with validation. If user enter any wrong input like already registered then, message for this should return to view file from which submitted form data.
Controlelr::redirect() accepts a url and an optional status code parameter.
You're not calling it correctly in the snippet you posed.
I believe you're trying to only use the url parameter (a single array argument) and skip the status code.
You also cannot rely on referrer to point you back to previous page. You need to save the route to return to in the form page
Url::remember([Yii::$app->requestedRoute]);
and later use that to return back to the form
return $this->redirect([Url::previous(), ['model' => $model]]);
You can set the Yii::$app->user->returnUrl and then use the $this->goBack() to navigate back to the previous page which ever it was.
A good approach is to add the beforeAction function inside your controller SubscribeController like below.
public function beforeAction($action)
{
if (parent::beforeAction($action)) {
if($action->id=='subscribe'){
Yii::$app->user->returnUrl = Yii::$app->request->referrer;
}
}
return true;
}
and replace the line
return $this->redirect([Yii::$app->request->referrer, ['model' => $model]]);
with the following
return $this->goBack();
Now from whichever page you submit the footer form it will navigate back to that page.
Another thing that i have noticed that you are not checking the sessionFlash error variable in your form, you need to get the error message like
if(Yii::$app->session->hasFlash('error')){
echo '<div class="alert alert-danger">
<strong>Danger!</strong>'. Yii::$app->session->getFlash('error').'
</div>';
}
A better way to display sessionFlash messages is provided in my previous answer you can follow that to avoid adding the code manually everywhere it will automatically show you the session mssages once set.
Hope it helps you out
I'm using yii2-advanced. I've several table :
tb_user:(iduser(PK),username),
tb_profile:(id,iduser(FK)),
tb_status:(id,iduser(FK))
My question is how can i insert iduser(PK) from tb_user to iduser(FK) on tb_profile and tb_status after i push the signup button.
For a while i think i must to do some modification of bevahiours() function on User model and i found some error, or adding trigger syntax on the table ? (i think this is not a good ways).
Is there anyone who can help me, how to solve my problem ?
this is the User model before the modification :
<?php
namespace common\models;
use Yii;
use yii\base\NotSupportedException;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;
class User extends ActiveRecord implements IdentityInterface
{
const STATUS_DELETED = 0;
const STATUS_ACTIVE = 10;
/**
* #inheritdoc
*/
public static function tableName()
{
return '{{%user}}';
}
/**
* #inheritdoc
*/
public function behaviors()
{
return [
'timestamp' => [
'class' => TimestampBehavior::className(),
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => 'created_at',
ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_at',
],
'value' => function () {return date('Y-m-d h:m:s');},
],
];
}
/**
* #inheritdoc
*/
public function rules()
{
return [
['status', 'default', 'value' => self::STATUS_ACTIVE],
['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]],
];
}
/**
* #inheritdoc
*/
public static function findIdentity($id)
{
return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]);
}
/**
* #inheritdoc
*/
public function getId()
{
return $this->getPrimaryKey();
}
}
?>
The Controller :
public function actionSignup()
{
$model = new SignupForm();
if ($model->load(Yii::$app->request->post())) {
if ($user = $model->signup()) {
if (Yii::$app->getUser()->login($user)) {
return $this->goHome();
}
}
}
return $this->render('signup', [
'model' => $model,
]);
}
I had similar situation in one of my project where i had 2 tables like user,user_image where user_id was foreign key to add the path.
For those kind of situation you can use either of following approach
1.Insert record in both table on click of signup button. You will have to write update action accordingly.
$user = new User();
$user->name = "John"
$user->email = "John#gmail.com"
//Add if any other fields in table
$user->save(); //save the record
$user_image = new UserImage();
$user_image->user_id = $user->id;
$user_image->image = "image path"
//Add any other images here
$user_image->save();//save the record
2.You can also call create action of UserImage and do the same. If you use this approach than you might also need to use any other unique column to find the id of that user and use it to insert new record,for example in my table email is unique column so i can write following code in UserImage and get the id
$user = User::findOne(['email' => 'john#gmail.com']);//this will return whole row
$user_image->user_id = $user->id;
$user_image->image = "image path"
//Add any other images here
$user_image->save();//save the record
And that way you can use the code as per it suits your need
Thank you
I created src/Model/Behavior/ModernTreeBehavior.php:
<?php
namespace Cake\ORM\Behavior;
use Cake\Datasource\EntityInterface;
use Cake\Datasource\Exception\RecordNotFoundException;
use Cake\Event\Event;
use Cake\ORM\Behavior;
use Cake\ORM\Entity;
use Cake\ORM\Query;
use InvalidArgumentException;
use RuntimeException;
class ModernTreeBehavior extends Behavior
{
/**
* Cached copy of the first column in a table's primary key.
*
* #var string
*/
protected $_primaryKey;
/**
* Default config
*
* These are merged with user-provided configuration when the behavior is used.
*
* #var array
*/
protected $_defaultConfig = [
'implementedFinders' => [
'children' => 'findChildren',
'treeList' => 'findTreeList'
],
'implementedMethods' => [
'childCount' => 'childCount',
'getLevel' => 'getLevel'
],
'parent' => 'parent_id',
'path' => 'path',
'scope' => null,
'level' => null
];
public function getLevel($entity)
{
$primaryKey = $this->_getPrimaryKey();
$id = $entity;
if ($entity instanceof EntityInterface) {
$id = $entity->get($primaryKey);
}
$config = $this->config();
$entity = $this->_table->find('all')
->select([$config['path']])
->where([$primaryKey => $id])
->first();
if ($entity === null) {
return false;
}
return substr_count($entity[$config['path']], '-') + 1;
}
/**
* Returns a single string value representing the primary key of the attached table
*
* #return string
*/
protected function _getPrimaryKey()
{
if (!$this->_primaryKey) {
$this->_primaryKey = (array)$this->_table->primaryKey();
$this->_primaryKey = $this->_primaryKey[0];
}
return $this->_primaryKey;
}
}
I used $this->addBehavior('ModernTree'); in CommentTables.php
and I got error Fatal error: Cannot redeclare class Cake\ORM\Behavior\ModernTreeBehavior in .../src/Model/Behavior/ModernTreeBehavior.php on line 0
But if I paste /src/Model/Behavior/ModernTreeBehavior.php into built-in file TreeBehavior.php and load TreeBehavior.php, everything so goods.
Can you tell me the reason?
The namespace declaration of your class is wrong, it should be:
namespace App\Model\Behavior;
Why is it that when I retrieve a record from model, it returns the variable that I declared instead of the fields in the table.
This is my controller function
public function actionGetsp()
{
//$sp_id=$_POST['sp_id'];
$model = TblSubProject::find()->select('sp_title, brgy_code')->all();
return json_encode($model);
}
and here is a part of my model.
class TblSubProject extends \yii\db\ActiveRecord
{
public $province;
public $region;
public $city_code;
/**
* #inheritdoc
*/
public static function tableName()
{
return 'tbl_sub_project';
}
/**
* #inheritdoc
*/
public function rules()
{
return [
[['city_code','brgy_code', 'sp_id', 'sp_title', 'sp_grant', 'lcc', 'modality'], 'required'],
[['sp_id'], 'integer'],
[['sp_grant', 'lcc'], 'number'],
[['brgy_code'], 'string', 'max' => 9],
[['sp_title'], 'string', 'max' => 500],
[['modality'], 'string', 'max' => 50],
[['sp_id'], 'unique']
];
}
/**
* #inheritdoc
*/
public function attributeLabels()
{
return [
'brgy_code' => 'Brgy Code',
'sp_id' => 'Sp ID',
'sp_title' => 'Sp Title',
'sp_grant' => 'Sp Grant',
'lcc' => 'Lcc',
'modality' => 'Modality',
'city_code' => 'City / Municipality',
'brgy_code' => 'Barangay',
'brgyCode.cityCode.province.prov_name' => 'Province',
'brgyCode.cityCode.city_name' => 'City / Municipality',
'brgyCode.brgy_name' => 'Barangay',
];
}
}
And here is sample output..
As you can say, it returns the province, region, and the city_code which is not an attribute in the table but just a declared variable. I want to retrieve like the si_title.
It's a normal Yii ActiveRecord behaviour - it replaces table attributes with declared ones. You don't have do declare them: all table fields are already available as $model->$field.
I would like / have to manage some settings in ZF1 style and provide the view with the infomation about the current environment.
/config/application.config.php
return array(
...
'module_listener_options' => array(
...
'config_glob_paths' => array('config/autoload/{,*.}{global,local}.php')
)
);
/config/autoload/env.local.php
return array(
// allowed values: development, staging, live
'environment' => 'development'
);
In a common view script I can do it over the controller, since the controllers have access to the Service Manager and so to all configs I need:
class MyController extends AbstractActionController {
public function myAction() {
return new ViewModel(array(
'currentEnvironment' => $this->getServiceLocator()->get('Config')['environment'],
));
}
}
Is it also possible to get the configs in a common view directly?
How can I access the configs in a layout view script (/module/Application/view/layout/layout.phtml)?
(My implementation/interpretation of) Crisp's suggestion:
Config view helper
<?php
namespace MyNamespace\View\Helper;
use Zend\View\Helper\AbstractHelper;
use Zend\View\HelperPluginManager as ServiceManager;
class Config extends AbstractHelper {
protected $serviceManager;
public function __construct(ServiceManager $serviceManager) {
$this->serviceManager = $serviceManager;
}
public function __invoke() {
$config = $this->serviceManager->getServiceLocator()->get('Config');
return $config;
}
}
Application Module class
public function getViewHelperConfig() {
return array(
'factories' => array(
'config' => function($serviceManager) {
$helper = new \MyNamespace\View\Helper\Config($serviceManager);
return $helper;
},
)
);
}
Layout view script
// do whatever you want with $this->config()['environment'], e.g.
<?php
if ($this->config()['environment'] == 'live') {
echo $this->partial('partials/partial-foo.phtml');;
}
?>
My implementation/interpretation of Sam's solution hint for the concret goal (to permit displaying the web analytics JS within the dev/staging environment):
DisplayAnalytics view helper
<?php
namespace MyNamespace\View\Helper;
use Zend\View\Helper\AbstractHelper;
use Zend\View\HelperPluginManager as ServiceManager;
class DisplayAnalytics extends AbstractHelper {
protected $serviceManager;
public function __construct(ServiceManager $serviceManager) {
$this->serviceManager = $serviceManager;
}
public function __invoke() {
if ($this->serviceManager->getServiceLocator()->get('Config')['environment'] == 'development') {
$return = $this->view->render('partials/partial-bar.phtml');
}
return $return;
}
}
Application Module class
public function getViewHelperConfig() {
return array(
'factories' => array(
'displayAnalytics' => function($serviceManager) {
$helper = new \MyNamespace\View\Helper\DisplayAnalytics($serviceManager);
return $helper;
}
)
);
}
Layout view script
<?php echo $this->displayAnalytics(); ?>
So this solution becomes more flexible:
/config/application.config.php
return array(
...
'module_listener_options' => array(
...
'config_glob_paths' => array('config/autoload/{,*.}{global,local}.php')
)
);
/config/autoload/whatever.local.php and /config/autoload/whatever.global.php
return array(
// allowed values: development, staging, live
'environment' => 'development'
);
ContentForEnvironment view helper
<?php
namespace MyNamespace\View\Helper;
use Zend\View\Helper\AbstractHelper;
use Zend\View\HelperPluginManager as ServiceManager;
class ContentForEnvironment extends AbstractHelper {
protected $serviceManager;
public function __construct(ServiceManager $serviceManager) {
$this->serviceManager = $serviceManager;
}
/**
* Returns rendered partial $partial,
* IF the current environment IS IN $whiteList AND NOT IN $blackList,
* ELSE NULL.
* Usage examples:
* Partial for every environment (equivalent to echo $this->view->render($partial)):
* echo $this->contentForEnvironment('path/to/partial.phtml');
* Partial for 'live' environment only:
* echo $this->contentForEnvironment('path/to/partial.phtml', ['live']);
* Partial for every environment except 'development':
* echo $this->contentForEnvironment('path/to/partial.phtml', [], ['development', 'staging']);
* #param string $partial
* #param array $whiteList
* #param array $blackList optional
* #return string rendered partial $partial or NULL.
*/
public function __invoke($partial, array $whiteList, array $blackList = []) {
$currentEnvironment = $this->serviceManager->getServiceLocator()->get('Config')['environment'];
$content = null;
if (!empty($whiteList)) {
$content = in_array($currentEnvironment, $whiteList) && !in_array($currentEnvironment, $blackList)
? $this->view->render($partial)
: null
;
} else {
$content = !in_array($currentEnvironment, $blackList)
? $this->view->render($partial)
: null
;
}
return $content;
}
}
Application Module class
public function getViewHelperConfig() {
return array(
'factories' => array(
'contentForEnvironment' => function($serviceManager) {
$helper = new \MyNamespace\View\Helper\ContentForEnvironment($serviceManager);
return $helper;
}
)
);
}
Layout view script
<?php echo $this->contentForEnvironment('path/to/partial.phtml', [], ['development', 'staging']); ?>