Hi,
I was trying to re-use the captcha that is being used in the contact form of YII. My new page path is :
[indent][indent] index.php?r=Userprof/create[/indent][/indent]
My controller(relevant functions only) is this :
class UserprofController extends Controller
{
/**
* @var string the default layout for the views. Defaults to '//layouts/column2', meaning
* using two-column layout. See 'protected/views/layouts/column2.php'.
*/
public $layout='//layouts/column2';
/**
* @return array action filters
*/
public function actions()
{
return array(
// captcha action renders the CAPTCHA image displayed on the contact page
'captcha'=>array(
'class'=>'CCaptchaAction',
'backColor'=>0xFFFFFF,
),
// page action renders "static" pages stored under 'protected/views/site/pages'
// They can be accessed via: index.php?r=site/page&view=FileName
'page'=>array(
'class'=>'CViewAction',
),
);
}
public function filters()
{
return array(
'accessControl', // perform access control for CRUD operations
);
}
/**
* Specifies the access control rules.
* This method is used by the 'accessControl' filter.
* @return array access control rules
*/
public function accessRules()
{
$user = User::model()->findByAttributes(array('id'=>Yii::app()->user->id));
return array(
array('allow', // allow all users to perform 'index' and 'view' actions
'actions'=>array('admin','delete','index'),
'users'=>array($user->username),
),
array('deny', // allow all users to perform 'index' and 'view' actions
'actions'=>array('update','delete'),
'users'=>array($user->username),
),
array('allow', // allow all users to perform 'index' and 'view' actions
'actions'=>array('create'),
'users'=>array('?'),
),
array('allow', // allow all users to perform 'index' and 'view' actions
'actions'=>array('create','view','index'),
'users'=>array('@'),
),
array('allow', // allow authenticated user to perform 'create' and 'update' actions
'actions'=>array('update'),
'users'=>array('@'),
),
array('allow', // allow admin user to perform 'admin' and 'delete' actions
'actions'=>array('state','city','Specjob'),
'users'=>array('?'),
),
array('allow', // allow admin user to perform 'admin' and 'delete' actions
'actions'=>array('state','city','Specjob'),
'users'=>array('*'),
),
array('deny', // deny all users
'users'=>array('*'),
),
);
}
/**
* Creates a new model.
* If creation is successful, the browser will be redirected to the 'view' page.
*/
public function actionCreate()
{
$model=new Userprof; // this is my model related to table
// Uncomment the following line if AJAX validation is needed
$this->performAjaxValidation($model);
if(isset($_POST['Userprof']))
{
$model->attributes=$_POST['Userprof'];
$uploadedFile=CUploadedFile::getInstance($model,'propic');
$uploadedFile1=CUploadedFile::getInstance($model,'resumeup');
$uploadedFile2=CUploadedFile::getInstance($model,'coverup');
$model->propic = $uploadedFile;
$model->resumeup = $uploadedFile1;
$model->coverup = $uploadedFile2;
//==============
if($model->specjob != "")
{
$list1= Yii::app()->db->createCommand("select specjob_id from gol_specjob order by specjob_id Desc limit 1")->queryAll();
foreach($list1 as $item1)
{
$specificid ="$item1[specjob_id]";
$specificid =$specificid+1;
}
$model->specjob_id=$specificid;
}
//=============
if($model->save())
{
if($model->propic !==NULL)
{
$model->propic->saveAs('Userprof/'.$model->propic);
}
if( $model->resumeup !==NULL)
{
$model->resumeup->saveAs('Userprof/'.$model->resumeup);
}
if( $model->coverup !==NULL)
{
$model->coverup->saveAs('Userprof/'.$model->coverup);
}
//=================================
$tt=$model->specjob;
$tt1=$model->branchser_id;
if($model->specjob != "")
{
Yii::app()->db->createCommand("insert into gol_specjob (specjob_id,sjob_name,service_id) values ($specificid,'$tt',$tt1)")->execute();
}
//==========================
//.
$session=new CHttpSession;
$session->open();
$session['new_user']=$model->usenam;
// $this->redirect(array('admin'));
//=======================
$currentuserid=$model->usenam;
$connection=Yii::app()->db;
$sql='update gol_userprof set resume_content =:homepage where usenam=:currentuserid';
$command=$connection->createCommand($sql);
$command->bindParam(":homepage",$homepage,PDO::PARAM_STR);
$command->bindParam(":currentuserid",$currentuserid,PDO::PARAM_STR);
//$command->bindValue(':status', $state, PDO::PARAM_STR);
$command->execute();
//
$list= Yii::app()->db->createCommand("select * from gol_userprof where usenam='$currentuserid'")->queryAll();
foreach($list as $item)
{
$activelink ="$item[activelink]";
$usenam="$item[usenam]";
$encodeusenam=base64_encode($usenam);
$uid="$item[id]";
$encodeuid=base64_encode($uid);
if($activelink == 0)
{
$to=$usenam;
//$linkuser=Yii::app()->createAbsoluteUrl(array('id' => $uid,'email' => $usenam));
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Golden Orb < evan.altemus@goldenorb.org>' . "\r\n";
$subject2 = "Golden Orb Activation Link";
// Get contact form template from file
$message2 = file_get_contents($email_template);
// Replace place holders in email template
$message2=str_replace("{title}",'Active link',$message2);
$message2=str_replace("{desc}",'Please click this link below to activate your Profile',$message2);
$message2 = str_replace("{link}", $linkuser, $message2);
$message2 = str_replace("{username}", $usenam, $message2);
$message2 = str_replace("{teammembers}",'Golden Orb team', $message2);
mail($to, $subject2, $message2, $headers);
}
else if($activelink == 1)
{
$this->redirect(array('view','id'=>$model->id));
}
}
//====================================
}
}
$this->render('create',array(
'model'=>$model,
));
}
/**
* Updates a particular model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id the ID of the model to be updated
*/
}
?>
My Model is as follows :
<?php
class Userprof extends CActiveRecord
{
public function beforeSave(){
$pass1 = base64_encode($this->pass);
$this->pass = $pass1;
return true;
}
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'gol_userprof';
}
/**
* @return array validation rules for model attributes.
*/
public $repass;
public $specjob;
public $resume;
public $cover;
public $uploadimg;
[b]public $verifyCode;[/b]
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(array('usenam','email'),
// verifyCode needs to be entered correctly
[b] array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements()),[/b]
array('usenam,pass,fname, lname, gender,country_id, state_id, city_id,branchser_id,rank, jobrate, status, active, willjob', 'required'),
array('careerobj,jobrate,qual, ugcourses,pgcourses,resumecon,dutystat, covercon, interest', 'length', 'max'=>500),
array('zipcode', 'numerical', 'integerOnly'=>true),
array('disabled_veteran,resume,cover,uploadimg,fname, lname, gender, joinyear,ug_gpa,pg_gpa separyear', 'length', 'max'=>15),
array('title, active, willjob, boxemail', 'length', 'max'=>30),
array('pass,repass', 'length', 'max'=>25),
array('addr, branchser_id , service_academy', 'length', 'max'=>150),
array('usenam, country_id, state_id, city_id, status, position, citywor', 'length', 'max'=>50),
array(' curindus, curcomp', 'length', 'max'=>100),
array('propic', 'file','types'=>'jpg, gif, png, jpeg', 'allowEmpty'=>TRUE, 'on'=>'update'),
array('resumeup', 'file','types'=>'docx', 'allowEmpty'=>true, 'on'=>'update'),
array('coverup', 'file','types'=>'doc, txt, docx, odt ', 'allowEmpty'=>true, 'on'=>'update'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id,careerobj, usenam, pass, repass, title, fname, lname, gender, addr, country_id, state_id, city_id, zipcode, propic, branchser_id,rank, disabled_veteran, jobrate, specjob_id, status, service_academy, qual, ugcourses, ug_gpa, pgcourses, pg_gpa, joinyear, separyear, dutystat, curindus, curcomp, position, citywor, active, willjob, boxemail, resumecon, resumeup, covercon, coverup, interest, resume_content', 'safe', 'on'=>'search'),
);
//highran, removed fiels after status field
}
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'careerobj' =>'Career / Job Search Objectives',
'usenam' => 'Username',
'pass' => 'Password',
'repass' => 'Confirm password',
'title' => 'Title',
'fname' => 'First name',
'lname' => 'Last name',
'gender' => 'Gender',
'city_id' => 'City',
'state_id' => 'State',
'country_id' => 'Country',
'propic' => 'Profile Image',
'branchser_id' => 'Branch Of Service',
'rank' => 'Please indicate if you are or were an officer or enlisted at your highest rank achieved :',
'disabled_veteran' => 'Disabled Veteran',
'jobrate' => 'Specific Job',
'status' => 'Status',
'service_academy' => 'If you attended a Service Academy then please indicate which one:',
'qual' => 'Qualifications',
'ugcourses' => 'Undergraduate Program / College Programs ',
'ug_gpa' =>'Undergraduate / College Programs GPA',
'pgcourses' => 'Postgraduate Program ',
'pg_gpa' => 'Postgraduate Program GPA',
'joinyear' => 'Year Joined Military',
'separyear' => 'Year Separated Or Retired',
'dutystat' => 'Duty Stations Past / Present',
'curindus' => 'Current Industry',
'curcomp' => 'Current Company Or Active Duty',
'position' => 'Position',
/*'citywor' => 'City / State/ Country',*/
'active' => 'Actively Seeking Employment',
'willjob' => 'Willing To Be Contacted By Present / Past
Veterans About Career Questions?:',
'resumeup' => 'Resume',
'coverup' => 'Cover Letter',
'interest' => 'Personal Interests',
);
//'boxemail' => 'Email Address',
//'covercon' => 'Paste A Cover Letter',
//'resumecon' => 'Paste The Resume',
//'highran' => 'Highest Rank Achieved / Current Rank', befor qua
}
}
And my view page is at :
/protected/views/[b]userprof[/b]
The form page(/protected/views/userprof/_form.php) is as follows :
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'userprof-form',
'enableAjaxValidation'=>true,
'clientOptions'=>array('validateOnSubmit'=>true, 'validateOnChange'=>true, 'validateOnType'=>true),
'htmlOptions' => array('enctype' => 'multipart/form-data'),
)); ?>
<?php if(CCaptcha::checkRequirements()): ?>
<div class="row">
<?php echo $form->labelEx($model,'verifyCode'); ?>
<div>
<?php $this->widget('CCaptcha'); ?>
<?php echo $form->textField($model,'verifyCode'); ?>
</div>
<div class="hint">Please enter the letters as they are shown in the image above.
<br/>Letters are not case-sensitive.</div>
<?php echo $form->error($model,'verifyCode'); ?>
</div>
<?php endif; ?>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Submit' : 'Save'); ?>
</div>
<br style"clear:both" />
<?php $this->endWidget(); ?>
The image of the captcha is not getting displayed. When i check fire bug, the image ulr is something like this :
<div class="row">
<label for="Userprof_verifyCode">Verify Code</label>
<div>
<img id="yw0" alt="" src="[b]index.php?r=userprof/captcha&v=50cec78f66e78[/b]"> //-> This doesnt get displayed.
<input id="Userprof_verifyCode" type="text" name="Userprof[verifyCode]">
</div>
<div class="hint">
Please enter the letters as they are shown in the image above.
<br>
Letters are not case-sensitive.
</div>
<div id="Userprof_verifyCode_em_" class="errorMessage" style="display:none"></div>
</div>
<img id="yw0" alt="" src="index.php?r=userprof/captcha&v=50cec78f66e78"> //-> This doesnt get displayed.
Any solutions pleaseā¦ It works if the page is in views/site/page.php or so. But not outside this folder. I think i am missing a small configuration thing or so. But i cannot know or find out as im bit new to YII. Any help is appreciated.
Thanks
Roy