run time watermark generate on pdf file Yii framework - pdf

" i have uploaded a file on server i want when a user from my website download this file he should be a watermarked file (watermark text will be 'his email id' in pdf file) "
Controller:
public function actionDownloadFiles()
{
ignore_user_abort(true);
$path = ( Yii::app()->basePath.'/../images/prequlification_form/'.$_GET['path'] );
$fullPath = $path ;
if ($fd = fopen ($fullPath, "r")) {
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
header("Content-type: application/octet-stream");
header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
header("Content-length: $fsize");
header("Cache-control: private");
while(!feof($fd)) {
$buffer = fread($fd, 2048);
echo $buffer;
}
}
fclose ($fd);
exit;
}
View:
<h3><p class="note"><span class="required">form</span> Filling is required.</p></h3>
<?php echo $form->errorSummary($model); ?>
<!--here is the user id-->
<?php //echo $id;?>
<div class="file-dwnload">
<?php
$data=prequalificationForm::model()->findByPk(1);
if(!empty($data)) {
echo '<div class="dwnload-file"><li>'.
CHtml::link( $data->file_path,array("prequalificationForm/downloadFiles","path"=>($data->file_path))).
'</li></div> <div class="dwnload-butn"><li>'.
CHtml::link('Download File',array("prequalificationForm/downloadFiles","path"=>($data->file_path)))
."</li></div> " ;
}?></div>
<div class="row">
<?php echo $form->labelEx($model,'file_path'); ?>
<?php echo $form->fileField($model,'file_path',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'file_path'); ?>
</div>
<div class="row buttons">
<?php //echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
<?php echo CHtml::submitButton($model->isNewRecord ? 'Submit' : 'Save',array('class'=>'submit-btn')); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->

So you want to watermark the pdf with some data from the user.
You would have some options to do that.
The easier would be if you don't do it on a uploaded file, instead you generate html content and convert html to pdf on the fly.
But if you need to do it on a uploaded file, maybe this could help you:
Writing/Drawing over a PDF template document in PHP

Related

use of email format in yii

I am able to send simple email to receiver.Now i have to be able to select a email format from existing different formats and this template must appear in my message box.i should also be able to change the content of the template that appears in message box before sending to receiver.How to do this?
My code for view form
<div class="form wide">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'mail-form',
'enableAjaxValidation'=>true,
'htmlOptions' => array('enctype' => 'multipart/form-data'), // ADD THIS
)); ?>
<div class="row col2">
<?php echo $form->labelEx($model,'email_from'); ?>
<?php echo $form->textField($model,'email_from',array('size'=>50,'maxlength'=>50,'readonly'=>'readonly')); ?>
<?php echo $form->error($model,'email_from'); ?>
</div>
<div class="row col2">
<?php echo $form->labelEx($model,'email_to'); ?>
<?php echo $form->textField($model,'email_to',array('size'=>50,'maxlength'=>50)); ?>
<?php echo $form->error($model,'email_to'); ?>
</div>
<div style="clear:both"></div>
<div class="row col2">
<?php echo $form->labelEx($model,'subject'); ?>
<?php echo $form->textField($model,'subject',array('size'=>60,'maxlength'=>250)); ?>
<?php echo $form->error($model,'subject'); ?>
</div>
<div style="clear:both"></div>
<div class="row col2">
<?php echo $form->labelEx($model,'message'); ?>
<?php echo $form->textArea($model,'message',array('style'=>'width: 680px; height: 300px;')); ?>
<?php echo $form->error($model,'message'); ?>
</div>
<div style="clear:both"></div>
<div class="row buttons">
<?php
echo CHtml::submitButton($model->isNewRecord ? 'Send' : 'Send',array('class' => 'btn'));
?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
Try below code:
public function actionContact() {
$model = new ContactForm();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$status = Yii::$app->mailer->compose()
->setTo($model->email_to)
->setFrom([$model->email_from => $model->email_from])
->setSubject($model->subject)
->setTextBody($model->message)
->send();
if ($status) {
Yii::$app->session->setFlash('success', 'Thank you for contacting us. We will respond to you as soon as possible.');
} else {
Yii::$app->session->setFlash('error', 'There was an error sending email.');
}
return $this->refresh();
} else {
return $this->render('contact', [
'model' => $model,
]);
}
}
You can also create a email_template directory under views folder and create template file in which you can set email template content.
you can use js or ajax to do the same.
1: Use jQuery:
I assume that you are using jQuery. So use ajax.
.......JS Cod........
/*I assume that you have your template data in an attribute like data-template-data=""*/
$('#select_template_id').chang(function(){
template=$(this).attr('data-template-data');
$('#template_edit_box').val(template);
});

Duplicate insert when ajaxvalidation is true

Im having this weird problem that causes my model to double insert into the database when ajaxvalidation is on for my form. I dont understand why this happens, but this keeps on conflicting with my custom validation and I cannot move on. Can someone enlighten me on the problem.
Model:
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('category, ppmp_id', 'numerical', 'integerOnly'=>true),
array('category','required'),
array('q1, q2, q3, q4', 'numerical'),
array('category', 'myTestUniqueMethod'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('item_id, category, q1, q2, q3, q4, ppmp_id', 'safe', 'on'=>'search'),
);
}
public function myTestUniqueMethod($attribute,$params)
{
$sql = 'SELECT *
FROM ppmp_item
WHERE ppmp_id='.$this->ppmp_id.'and category='.$this->category;
$unique = Yii::app()->db->createCommand($sql)->queryRow();
if ($unique)
{
$this->addError('category', "Category already added to PPMP");
}
}
View:
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'ppmp-item-form',
'enableAjaxValidation'=>true,
)); ?>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'category'); ?>
<?php
$data=PhilgepsCategory::model()->findAll();
$data=CHtml::listData($data,'id','class_name');
echo $form->dropDownList($model,'category',$data,array('options' => array('1'=>array('selected'=>true))));
?>
<?php echo $form->error($model,'category'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'q1'); ?>
<?php echo $form->textField($model,'q1'); ?>
<?php echo $form->error($model,'q1'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'q2'); ?>
<?php echo $form->textField($model,'q2'); ?>
<?php echo $form->error($model,'q2'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'q3'); ?>
<?php echo $form->textField($model,'q3'); ?>
<?php echo $form->error($model,'q3'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'q4'); ?>
<?php echo $form->textField($model,'q4'); ?>
<?php echo $form->error($model,'q4'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
Controller:
public function actionCreate($ppmp,$bal)
{
$model=new PpmpItem;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['PpmpItem']))
{
$model->attributes=$_POST['PpmpItem'];
$model->ppmp_id = $ppmp;
$valid=$model->validate();
if($valid)
if($model->save())
$this->redirect(array('ppmp/view','id'=>$ppmp));
}
$this->render('create',array(
'model'=>$model,
'ppmp'=>$ppmp,
'bal'=>$bal,
));
}
The problem is that you shouldn't execute save() method when validating, because it will save when doing the ajax validation and when sending the data trough the form, so, you should do something like this:
...
...
if(isset($_POST['ajax']) && $_POST['ajax']==='ppmp-item-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
$valid=$model->validate();
if($valid)
if($model->save())
...
...
This is the same that gii uses to validate using ajax.

Yii CJuiDialog isn't being recognized

I've went through this tutorial http://www.yiiframework.com/wiki/561/ajax-login-form-with-validation-errors-inside-jquery-modal-dialog/ It appears to be functioning properly, but instead of the form being in the modal dialog, it just renders it right on the page like zii.widgets.jui.CJuiDialog isn't even there.
<?php $this->beginWidget('zii.widgets.jui.CJuiDialog',array(
'id'=>'login-dialog',
'options'=>array(
'title'=>'Login',
'autoOpen'=>false,
),
));?>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'user_login_form',
'enableAjaxValidation'=>false,
'enableClientValidation'=>true,
'method' => 'POST',
'clientOptions'=>array(
'validateOnSubmit'=>true,
'validateOnChange'=>true,
'validateOnType'=>false,
),
)); ?>
<h1>
Login</h1>
<p>
Please fill out the following form with your login credentials:</p>
<p class="note">
Fields with <span class="required">*</span> are required.</p>
<div id="login-error-div" class="errorMessage" style="display: none;">
</div>
<div class="row">
<?php echo $form->labelEx($model,'username'); ?>
<?php echo $form->textField($model,'username',array("onfocus"=>"$('#login-error- div').hide();")); ?>
<?php //echo $form->error($model,'username'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'password'); ?>
<?php echo $form->passwordField($model,'password',array("onfocus"=>"$('#login-error- div').hide();")); ?>
<?php //echo $form->error($model,'password'); ?>
<p class="hint">
Hint: You may login with <tt>demo/demo</tt>.
</p>
</div>
<div class="row rememberMe">
<?php echo $form->checkBox($model,'rememberMe'); ?>
<?php echo $form->label($model,'rememberMe'); ?>
<?php echo $form->error($model,'rememberMe'); ?>
</div>
<div class="row submit">
<?php echo CHtml::ajaxSubmitButton(
'Sign In',
array('/site/login.GetLogin'),
array(
'beforeSend' => 'function(){
$("#login").attr("disabled",true);
}',
'complete' => 'function(){
$("#user_login_form").each(function(){ this.reset();});
$("#login").attr("disabled",false);
}',
'success'=>'function(data){
var obj = jQuery.parseJSON(data);
// View login errors!
// alert(data);
if(obj.login == "success"){
$("#user_login_form").html("<h4>
Login Successful! Please Wait...</h4>
");
parent.location.href = "/";
}
else{
$("#login-error-div").show();
$("#login-error-div").html("Login failed! Try again.");$("#login-error-div").append("
");
}
}'
),
array("id"=>"login","class" => "btn btn-primary")
); ?>
</div>
<?php $this->endWidget(); ?>
</div>
<!-- form -->
<?php $this->endWidget('zii.widgets.jui.CJuiDialog'); ?>
Its suppose to render like this above so when i click the link below it opens
echo CHtml::link('Login', array('/site/login.GetLogin'), array('onclick'=>'$("#login-dialog").dialog("open"); return false;'));
Its built in a widget, if you have a look at the tutorial. Here's the complete widget
<?php
class loginProvider extends CWidget{
public static function actions(){
return array(
'GetLogin'=>'application.components.actions.getLogin',
);
}
public function run(){
$this->renderContent();
}
protected function renderContent(){
echo '<span style="float:right;">';
if(Yii::app()->user->isGuest){
echo CHtml::link('Login', array('/site/login.GetLogin'), array('onclick'=>'$("#login-dialog").dialog("open"); return false;'));
echo '</span>';
$this->getController()- >renderPartial('application.components.views.login',array('model'=>new LoginForm));
}
else
echo CHtml::link('Logout ('.Yii::app()->user->name.')', array('site/logout'), array('visible'=>!Yii::app()->user->isGuest));
echo '</span>';
}
}

How can I display a warning message on textfield in Yii

I'm new to Yii framework and I need to display the validation error message as in login form "Username cannot be blank". Now, I have a text field where I updated the fields and the during validation I want a message to be displayed. How can I do this?
Controller
public function actionUpdate($id)
{
$model = $this->loadModel($id);
// set the parameters for the bizRule
$params = array('GroupzSupport'=>$model);
// now check the bizrule for this user
if (!Yii::app()->user->checkAccess('updateSelf', $params) &&
!Yii::app()->user->checkAccess('admin'))
{
throw new CHttpException(403, 'You are not authorized to perform this action');
}
else
{
if(isset($_POST['GroupzSupport']))
{
$password_current=$_POST['GroupzSupport']['password_current'];
$pass=$model->validatePassword($password_current);
$model->attributes=$_POST['GroupzSupport'];
if($pass==1)
{
$model->password = $model->hashPassword($_POST['GroupzSupport']['password_new']);
if($model->save())
$this->redirect(array('/messageTemplate/admin'));
}
else {$errors="Incorrect Current password"; print '<span style="color:red"><b>';
print '</b><b>'.$errors;
print '</b></span>';}
}
$this->render('update',array(
'model'=>$model,
));
}
}
View
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'password-recovery-reset-password-form',
'enableAjaxValidation'=>false,
)); ?>
<div class="row"><?php
echo $form->labelEx($model,'username');
echo $form->textField($model,'username',array('size'=>45,'maxlength'=>150));
echo $form->error($model,'username');
?></div>
<div class="row">
<?php echo $form->labelEx($model,'current password'); ?>
<?php echo $form->passwordField($model,'password_current',array('size'=>30,'maxlength'=>30)); ?>
<?php echo $form->error($model,'password_current'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'new password'); ?>
<?php echo $form->textField($model,'password_new',array('size'=>30,'maxlength'=>30)); ?>
<?php echo $form->error($model,'password_new'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'confirm new password'); ?>
<?php echo $form->passwordField($model,'password_repeat',array('size'=>30,'maxlength'=>30)); ?>
<?php echo $form->error($model,'password_repeat'); ?>
</div>
<div class="row buttons"><?php
echo CHtml::submitButton('Reset Your Password');
?></div><?php
$this->endWidget(); ?>
</div>
Now currently I'm displaying it at the top.
I want to display it right on the textfield as in login page. How can I do this?
Before redirect, add the message to the desired field.
In the model Validator:
$this->addError('field_name', "Message error.");
Or in Controller action:
$model->addError('field_name', "Message error.");

Instagram's geotagging API

I'm working on learning about Instagram's API as a side project. I've followed this tutorial (http://eduvoyage.com/instagram-search-app.html) to implement a search feature by hashtag. It was extremely helpful to me. One of the things I saw in the API was the location ID feature, and I was wondering how to implement that. Through a search on this site, I found that a request can be made which will return the following.
'{
"meta": {
"code": 200
},
"data": {
"attribution": null,
"tags": [],
"type": "image",
"location": {
"latitude": 48.8635,
"longitude": 2.301333333
},
"comments": {
"count": 0,
"data": []
},
..........'
I'm trying to figure out this tutorial to process that information. (http://www.ibm.com/developerworks/xml/library/x-instagram1/index.html#retrievedetails)
<html>
<head>
<style>
</head>
<body>
<h1>Instagram Image Detail</h1>
<?php
// load Zend classes
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Http_Client');
// define consumer key and secret
// available from Instagram API console
$CLIENT_ID = 'YOUR-CLIENT-ID';
$CLIENT_SECRET = 'YOUR-CLIENT-SECRET';
try {
// define image id
$image = '338314508721867526';
// initialize client
$client = new Zend_Http_Client('https://api.instagram.com/v1/media/' . $image);
$client->setParameterGet('client_id', $CLIENT_ID);
// get image metadata
$response = $client->request();
$result = json_decode($response->getBody());
// display image data
?>
<div id="container">
<div id="info">
<h2>Meta</h2>
<strong>Date: </strong>
<?php echo date('d M Y h:i:s', $result->data->created_time); ?>
<br/>
<strong>Creator: </strong>
<?php echo $result->data->user->username; ?>
(<?php echo !empty($result->data->user->full_name) ?
$result->data->user->full_name : 'Not specified'; ?>)
<br/>
<strong>Location: </strong>
<?php echo !is_null($result->data->location) ?
$result->data->location->latitude . ',' .
$result->data->location->longitude : 'Not specified'; ?>
<br/>
<strong>Filter: </strong>
<?php echo $result->data->filter; ?>
<br/>
<strong>Comments: </strong>
<?php echo $result->data->comments->count; ?>
<br/>
<strong>Likes: </strong>
<?php echo $result->data->likes->count; ?>
<br/>
<strong>Resolution: </strong>
<a href="<?php echo $result->data->images
->standard_resolution->url; ?>">Standard</a> |
<a href="<?php echo $result->data->images
->thumbnail->url; ?>">Thumbnail</a>
<br/>
<strong>Tags: </strong>
<?php echo implode(',', $result->data->tags); ?>
<br/>
</div>
<div id="image">
<h2>Image</h2>
<img src="<?php echo $result->data->images
->low_resolution->url; ?>" /></a>
</div>
<div id="comments">
<?php if ($result->data->comments->count > 0): ?>
<h2>Comments</h2>
<ul>
<?php foreach ($result->data->comments->data as $c): ?>
<div class="item"><img src="<?php echo $c
->from->profile_picture; ?>" class="profile" />
<?php echo $c->text; ?> <br/>
By <em> <?php echo $c->from->username; ?></em>
on <?php echo date('d M Y h:i:s', $c->created_time); ?>
</div>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
</div>
<?php
} catch (Exception $e) {
echo 'ERROR: ' . $e->getMessage() . print_r($client);
exit;
}
?>
</body>
</html>
The main problem (I think) that I'm having is here:
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Http_Client');
I downloaded Zend Frame 1.12.13, but I'm not sure what these line of code is asking for. In the Zend Framework folder, It goes "Zend/Loader(Folder)", but nothing called "Loader.php". Does that just load everything in the 'Loader' folder? Same for the Zend_Loader, there is a directory of Zend/Http/Client, but Client is also a folder.
tl;dr
Trying to set up a program that will search instagram (as linked in the tutorial above), be able to click on the picture results, open one tab that shows the picture and the users profile, and another tab that shows all the image metadata. Is there a simpler way to do this? Or a more noob friendly tutorial?
I ran into a similar problem when using this example. You must include the path of the Zend framework at the very top of this example. I did it in just its own php block at the top.
ini_set('include_path', 'path/to/ZendFramework/library');