Hi all,
I’m working under Yii 1.1.5. I’ve been given an existing project and asked to make one dropdown dependent on another’s selected value. I’ve read (and been trying for 2 days) the wiki page Creating a dependent dropdown as well as the forum post [Solved] How to create dependent DropDown Lists but still I cannot make my second dropdown to show some values.
I can say I’m a n00b (doing my internship) and I really need help, so thanks in advance to anyone who could help me.
Here is the original code :
- Controller : TicketsController.php
class TicketsController extends CRootController {
public function actionCreate() {
Yii::app()->clientScript->registerScriptFile( Yii::app()->baseUrl .'/javascript/ui.datepicker-fr.js', CClientScript::POS_END);
$priorities = Yii::app()->db->createCommand("SELECT priority_id, label FROM tickets_priorities ORDER BY sorting")->queryAll();
$types = Yii::app()->db->createCommand("SELECT type_id, label FROM tickets_types ORDER BY sorting ASC, label ASC")->queryAll();
$projects = Yii::app()->db->createCommand("SELECT p.project_id, CONCAT_WS(' ', p.site, '(',p.version,')') as site FROM projects p JOIN projects_users pu ON pu.project_id=p.project_id WHERE p.est_valide='1' AND pu.user_id='". Yii::app()->user->id ."' ORDER BY site")->queryAll();
if( Yii::app()->request->isPostRequest ) {
mb_ereg("([0-9]{1,2})/([0-9]{1,2})/([0-9]{4})", $_POST['Ticket']['deadline'], $regs );
$deadline = $regs[3] .'-'. $regs[2] .'-'. $regs[1];
$query = "INSERT INTO tickets (project_id, status_id, user_id, assignUser_id, priority_id, type_id, title, description, deadline,date_creation, date_modified)
VALUES (:project_id, '1','". Yii::app()->user->id ."', :assignUser_id, :priority_id, :type_id, :title, :description, :deadline, NOW(), NOW() )";
$command = Yii::app()->db->createCommand( $query );
$command->bindParam(':project_id' , $_POST['Ticket']['project_id'], PDO::PARAM_INT );
$command->bindParam(':assignUser_id' , $_POST['Ticket']['assignUser_id'], PDO::PARAM_INT );
$command->bindParam(':priority_id' , $_POST['Ticket']['priority_id'], PDO::PARAM_INT );
$command->bindParam(':type_id' , $_POST['Ticket']['type_id'], PDO::PARAM_INT );
$command->bindParam(':title' , $_POST['Ticket']['title'], PDO::PARAM_STR );
$command->bindParam(':description' , $_POST['Ticket']['description'], PDO::PARAM_STR );
$command->bindParam(':deadline' , $deadline, PDO::PARAM_STR );
$command->execute();
$id = Yii::app()->db->lastInsertID;
//
// AssignUsers
//
$query = "SELECT u.user_id, CONCAT(u.lastname, ' ', SUBSTR(u.name, 1, 1), '.') AS user
FROM users u
JOIN AuthAssignment aa ON aa.userid=u.user_id
WHERE (aa.itemname='developpeur'
OR aa.itemname='admin'
OR aa.itemname='rapporteur')
AND u.est_valide=1
ORDER BY user";
//$assignUsers[0] = '';
$assignUsers = Yii::app()->db->CreateCommand( $query )->queryAll();
$this->render('form', array('priorities' => $priorities,
'types' => $types,
'projects' => $projects,
'assignUsers' => $assignUsers ) );
}
- View : form.php
<div id="submenu">
<?php echo CHtml::link('Liste des tickets', array('tickets/index') ); ?>
|
<strong>Créer un ticket</strong>
</div>
<?php echo CHtml::form('', 'post', array('enctype' => 'multipart/form-data', 'class'=>'formulaire') ); ?>
<div class="tickets-grid-header">
<?php echo CHtml::submitButton('Enregistrer'); ?>
</div>
<fieldset>
<p>
<label>Site</label>
<?php echo CHtml::dropDownList('Ticket[project_id]', '', CHtml::listData( $projects, 'project_id', 'site' ) ); ?>
</p>
<p>
<label>Titre</label>
<input type="text" name="Ticket[title]" value="<?php echo $Ticket['title']; ?>" />
</p>
<p>
<label>Priorité</label>
<?php echo CHtml::dropDownList('Ticket[priority_id]', '', CHtml::listData( $priorities, 'priority_id', 'label' ) ); ?>
</p>
<p>
<label>Nature</label>
<?php echo CHtml::dropDownList('Ticket[type_id]', '', CHtml::listData( $types, 'type_id', 'label' ) ); ?>
</p>
<p>
<label>Asssigné à</label>
<?php echo CHtml::dropDownList('Ticket[assignUser_id]', $ticket['assignUser_id'], CHtml::listData( $assignUsers, 'user_id', 'user' ) ); ?>
</p>
<p>
<label>Deadline</label>
<input type="text" name="Ticket[deadline]" id="ticket_deadline" />
</p>
<p>
<label>Description</label>
<textarea name="Ticket[description]" style="width:66%;max-height:300px" class="elastic"><?php echo $Ticket['description']; ?></textarea>
</p>
<p>
<label>Ajouter des pièces jointes</label>
<?php $this->widget('CMultiFileUpload', array('name' => 'TicketAttachments',
'accept' => 'doc|docx|xls|xlsx|jpg|gif|png|psd|zip|rar|html|swf|pdf') );
?>
</p>
</fieldset>
</form>
<div class="tickets-grid-footer">
</div>
<?php Yii::app()->clientScript->registerScriptFile( Yii::app()->theme->baseUrl .'/js/jquery.elastic.js', CClientScript::POS_END ); ?>
<script type="text/javascript">
$( function() {
$('#ticket_deadline').datepicker({ minDate: new Date() });
$('.elastic').elastic();
});
</script>
And here is the code as I modified it :
- Controller : TicketsController.php
class TicketsController extends CRootController {
//Fonction utilisée pour remplir la dropDownList "Assigné à" en fonction des valeurs de la dropDownList "Site"
public function actionDynamicassignusers()
{
$query = "SELECT u.user_id, CONCAT(u.lastname, ' ', SUBSTR(u.name, 1, 1), '.') AS user
FROM users u
JOIN projects_users pu ON u.user_id = pu.user_id
WHERE project_id = :project_id";
$command = Yii::app()->db->createCommand($query);
$command->bindValue(':project_id', $_POST['Tickets']['project_id'], PDO::PARAM_INT );
$data = $command->queryAll();
$data=CHtml::listData($data,'user_id','user');
foreach($data as $value=>$user)
{
echo CHtml::tag('option',array('value' => $value),CHtml::encode($user),true);
}
}
// Création d'un nouveau ticket
public function actionCreate() {
//Script pour affichage datepicker dans le champ "Deadline"
Yii::app()->clientScript->registerScriptFile( Yii::app()->baseUrl .'/javascript/ui.datepicker-fr.js', CClientScript::POS_END);
//alimentation des dropdownlists
$projects = Yii::app()->db->createCommand("SELECT p.project_id, CONCAT_WS(' ', p.site, '(',p.version,')') as site FROM projects p JOIN projects_users pu ON pu.project_id=p.project_id WHERE p.est_valide='1' AND pu.user_id='". Yii::app()->user->id ."' ORDER BY site")->queryAll();
$priorities = Yii::app()->db->createCommand("SELECT priority_id, label FROM tickets_priorities ORDER BY sorting")->queryAll();
$types = Yii::app()->db->createCommand("SELECT type_id, label FROM tickets_types ORDER BY sorting ASC, label ASC")->queryAll();
//insertion en BDD des données postées par l'utilisateur
if( Yii::app()->request->isPostRequest ) {
mb_ereg("([0-9]{1,2})/([0-9]{1,2})/([0-9]{4})", $_POST['Ticket']['deadline'], $regs );
$deadline = $regs[3] .'-'. $regs[2] .'-'. $regs[1];
$query = "INSERT INTO tickets (project_id, status_id, user_id, assignUser_id, priority_id, type_id, title, description, deadline,date_creation, date_modified)
VALUES (:project_id, '1','". Yii::app()->user->id ."', :assignUser_id, :priority_id, :type_id, :title, :description, :deadline, NOW(), NOW() )";
$command = Yii::app()->db->createCommand( $query );
$command->bindParam(':project_id' , $_POST['Ticket']['project_id'], PDO::PARAM_INT );
$command->bindParam(':assignUser_id' , $_POST['Ticket']['assignUser_id'], PDO::PARAM_INT );
$command->bindParam(':priority_id' , $_POST['Ticket']['priority_id'], PDO::PARAM_INT );
$command->bindParam(':type_id' , $_POST['Ticket']['type_id'], PDO::PARAM_INT );
$command->bindParam(':title' , $_POST['Ticket']['title'], PDO::PARAM_STR );
$command->bindParam(':description' , $_POST['Ticket']['description'], PDO::PARAM_STR );
$command->bindParam(':deadline' , $deadline, PDO::PARAM_STR );
$command->execute();
$id = Yii::app()->db->lastInsertID;
//
// AssignUsers
//
/*$query = "SELECT u.user_id, CONCAT(u.lastname, ' ', SUBSTR(u.name, 1, 1), '.') AS user
FROM users u
JOIN authassignment aa ON aa.userid=u.user_id
WHERE (aa.itemname='developpeur'
OR aa.itemname='admin'
OR aa.itemname='client')
AND u.est_valide=1
ORDER BY user";
//$assignUsers[0] = '';
$assignUsers = Yii::app()->db->CreateCommand( $query )->queryAll();*/
$this->render('form', array('projects' => $projects,
'priorities' => $priorities,
'types' => $types,
//'assignUsers' => $assignUsers
) );
}
- View : form.php
<div id="submenu">
<?php echo CHtml::link('Liste des tickets', array('tickets/index') ); ?>
|
<strong>Créer un ticket</strong>
</div>
<?php echo CHtml::form('', 'post', array('enctype' => 'multipart/form-data', 'class'=>'formulaire') ); ?>
<div class="tickets-grid-header">
<?php echo CHtml::submitButton('Enregistrer'); ?>
</div>
<fieldset>
<p>
<label>Site</label>
<?php echo CHtml::dropDownList('Ticket[project_id]', '', CHtml::listData( $projects, 'project_id', 'site' ),
array(
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('tickets/dynamicassignusers'),
'update'=>'#TicketAssignUser_id',
//'data'=>array('Ticket[project_id]'=>'js:this.value'),
))); ?>
</p>
<p>
<label>Titre</label>
<input type="text" name="Ticket[title]" />
</p>
<p>
<label>Priorité</label>
<?php echo CHtml::dropDownList('Ticket[priority_id]', '', CHtml::listData( $priorities, 'priority_id', 'label' ) ); ?>
</p>
<p>
<label>Nature</label>
<?php echo CHtml::dropDownList('Ticket[type_id]', '', CHtml::listData( $types, 'type_id', 'label' ) ); ?>
</p>
<p>
<label>Assigné à</label>
<?php echo CHtml::dropDownList('TicketAssignUser_id', '', array()); ?>
</p>
<p>
<label>Deadline</label>
<input type="text" name="Ticket[deadline]" id="ticket_deadline" />
</p>
<p>
<label>Description</label>
<textarea name="Ticket[description]" style="width:66%;max-height:300px" class="elastic"></textarea>
</p>
<p>
<label>Ajouter des pièces jointes</label>
<?php $this->widget('CMultiFileUpload', array('name' => 'TicketAttachments',
'accept' => 'doc|docx|xls|xlsx|jpg|gif|png|psd|zip|rar|html|swf|pdf') );
?>
</p>
</fieldset>
</form>
<div class="tickets-grid-footer">
</div>
<?php Yii::app()->clientScript->registerScriptFile( Yii::app()->theme->baseUrl .'/js/jquery.elastic.js', CClientScript::POS_END ); ?>
<script type="text/javascript">
$( function() {
$('#ticket_deadline').datepicker({ minDate: new Date() });
$('.elastic').elastic();
});
</script>