AjaxSubmitButton problem

I wanted to show the view page ID of the project created, after i click on my AjaxSubmitButton but only if the model was successfully saved. If it had any kind of error, I need to show the error on the different fields (textfields, textarea, ddl, etc)

I’m still a newbie in the Yii framework, but as you can see I got 4 models into my view, and I will save depending on the radiobutton selected… If i use normal SubmitButton, it will hide all the content that I updated by Ajax and basically it will disappear. So i want to use the AjaxSubmitButton to keep the data there but I can’t do the validate on fields.

_form.php


<div class="wide form">


    


    <?php


    $form = $this->beginWidget('GxActiveForm', array(

        'id' => 'project-form',

        'enableAjaxValidation' => true,

    )); 

    

    

    ?>




    <p class="note">

        <?php echo Yii::t('app', 'Fields with'); ?> <span class="required">*</span> <?php echo Yii::t('app', 'are required'); ?>.

    </p>

    <?php echo $form->errorSummary(array($model, $modelidi, $modelid, $modeld)); ?>


    

    <?php

    //DIALOGS

    $img = GxHtml::image(Yii::app()->request->baseUrl . '/images/ico/add.png');

    

    // PROJECT AREA DIALOG

    $this->beginWidget('zii.widgets.jui.CJuiDialog', array(// the dialog

        'id' => 'dialogProjectarea',

        'options' => array(

            'title' => 'Create project\'s area',

            'autoOpen' => false,

            'resizable' => false,

            'modal' => true,

            'width' => 370,

            'height' => 200,

        ),

    ));

    ?>

    <div id="divForForm" class="divForForm"></div>


    <?php $this->endWidget(); ?>


<script type="text/javascript">

        // here is the magic

        function addProjectarea()

        {

<?php


echo GxHtml::ajax(array(

    'url' => array('projectArea/create'),

    'data' => "js:$(this).serialize()",

    'type' => 'post',

    'dataType' => 'json',

    'success' => "function(data)

                {

                    if (data.status == 'failure')

                    {

                        $('#dialogProjectarea div.divForForm').html(data.div);

                        $('#dialogProjectarea div.divForForm form').submit(addProjectarea);

                    }

                    else

                    {

                        $('#dialogProjectarea div.divForForm').html(data.div);

                        setTimeout(\"$('#dialogProjectarea').dialog('close') \",3000);

                        $('#Project_id_area').html('OLA');

                    }

 

                } ",

))

?>;

        return false; 

 

    }

 

    </script>


    <?php

    // PROJECT CUSTOMER DIALOG

    $this->beginWidget('zii.widgets.jui.CJuiDialog', array(// the dialog

        'id' => 'dialogcustomer',

        'options' => array(

            'title' => 'Create project\'s customer',

            'autoOpen' => false,

            'resizable' => false,

            'modal' => true,

            'width' => 370,

            'height' => 250,

        ),

    ));

    ?>

    <div id ="divForForm" class="divForForm"></div>


    <?php $this->endWidget(); ?>


    <script type="text/javascript">

        // here is the magic

        function addcustomer()

        {

<?php

echo CHtml::ajax(array(

    'url' => array('customer/create'),

    'data' => "js:$(this).serialize()",

    'type' => 'post',

    'dataType' => 'json',

    'success' => "function(data)

                {

                    if (data.status == 'failure')

                    {

                        $('#dialogcustomer div.divForForm').html(data.div);

                              // Here is the trick: on submit-> once again this function!

                        $('#dialogcustomer div.divForForm form').submit(addcustomer);

                    }

                    else

                    {

                        $('#dialogcustomer div.divForForm').html(data.div);

                        setTimeout(\"$('#dialogcustomer').dialog('close') \",3000);

                        

                    }

 

                } ",

))

?>;

        return false; 

 

    }

 

    </script>

    

    <div class="row _form_items">

        <?php echo $form->labelEx($model, 'id_area', array('label' => 'Area')); ?>

        <?php echo $form->dropDownList($model, 'id_area', GxHtml::listDataEx(ProjectArea::model()->findAllAttributes(null, true))); ?>

        <?php

        echo GxHtml::link($img, "", array(

            'style' => 'cursor: pointer;text-decoration:none;',

            'onclick' => "{addProjectarea(); $('#dialogProjectarea').dialog('open');}"));

        ?>

        <?php echo $form->error($model, 'id_area'); ?>

    </div><!-- row -->

    <div class="row _form_items">

        <?php echo $form->labelEx($model, 'id_customer', array('label' => 'Customer')); ?>

        <?php echo $form->dropDownList($model, 'id_customer', GxHtml::listDataEx(Customer::model()->findAllAttributes(null, true))); ?>

        <?php

        echo GxHtml::link($img, "", array(

            'style' => 'cursor: pointer;text-decoration:none;',

            'onclick' => "{addcustomer(); $('#dialogcustomer').dialog('open');}"));

        ?>

        <?php echo $form->error($model, 'id_customer'); ?>

    </div><!-- row -->


    <div class="row last _form_items">

        <?php echo $form->labelEx($model, 'name'); ?>

        <?php echo $form->textField($model, 'name', array('maxlength' => 255, 'size' => 65)); ?>

        <?php echo $form->error($model, 'name'); ?>

    </div><!-- row -->

    <?php

    if ($model->id != '') {


        $model_keywords = Keywords::model()->findAll();

        $keywords = '[';

        foreach ($model_keywords as $row) {

            $keywords .= '"' . $row->name . '",';

        }

        $keywords .= ']';

        $model_project_team = User::model()->findAll();

        $project_team = '[';

        foreach ($model_project_team as $row) {

            $project_team .= '"' . $row->name . '",';

        }

        $project_team .= ']';

        

        if ($model->type == 'IDI') {

            ?>

            <?php

            echo '<script>

	$(function() {

		function split( val ) {

			return val.split( /,\s*/ );

		}

		function extractLast( term ) {

			return split( term ).pop();

		}


		$( "#ProjectIdi_keywords" )

			.bind( "keydown", function( event ) {

				if ( event.keyCode === $.ui.keyCode.TAB &&

						$( this ).data( "autocomplete" ).menu.active ) {

					event.preventDefault();

				}

			})

			.autocomplete({

				minLength: 0,

				source: function( request, response ) {

					response( $.ui.autocomplete.filter(

						' . $keywords . ', extractLast( request.term ) ) );

				},

				focus: function() {

					return false;

				},

				select: function( event, ui ) {

					var terms = split( this.value );

					terms.pop();

					terms.push( ui.item.value );

					terms.push( "" );

					this.value = terms.join( ", " );

					return false;

				}

			});

                        $( "#ProjectIdi_project_team" )

			.bind( "keydown", function( event ) {

				if ( event.keyCode === $.ui.keyCode.TAB &&

						$( this ).data( "autocomplete" ).menu.active ) {

					event.preventDefault();

				}

			})

			.autocomplete({

				minLength: 0,

				source: function( request, response ) {

					response( $.ui.autocomplete.filter(

						' . $project_team . ', extractLast( request.term ) ) );

				},

				focus: function() {

					return false;

				},

				select: function( event, ui ) {

					var terms = split( this.value );

					terms.pop();

					terms.push( ui.item.value );

					terms.push( "" );

					this.value = terms.join( ", " );

					return false;

				}

			});

	});

	</script>';

            ?>

            <div class="newitems _form_items">

                <?php echo $form->labelEx($modelidi, 'project_leader'); ?>

                <?php

                $ddl = GxHtml::listDataEx(User::model()->findAll(), 'id', 'name');

                asort($ddl);

                echo $form->dropDownList($modelidi, 'project_leader', $ddl);

                ?>

                <?php echo $form->error($modelidi, 'project_leader'); ?>

            </div><!-- row -->

            <div class="newitems _form_items">

                <?php echo $form->labelEx($modelidi, 'project_team'); ?>

                <?php echo $form->textField($modelidi, 'project_team', array('style' => 'width:400px;')); ?>

                <?php echo $form->error($modelidi, 'project_team'); ?>

            </div>


            <div class="newitems _form_items">

                <?php echo $form->labelEx($modelidi, 'objectives'); ?>

                <?php echo $form->textArea($modelidi, 'objectives', array('style' => 'width:400px;')); ?>

                <?php echo $form->error($modelidi, 'objectives'); ?>

            </div><!-- row -->

            <div class="newitems _form_items">

                <?php echo $form->labelEx($modelidi, 'description'); ?>

                <?php echo $form->textArea($modelidi, 'description', array('style' => 'width:400px;')); ?>

                <?php echo $form->error($modelidi, 'description'); ?>

            </div><!-- row -->

            <div class="newitems _form_items">

                <?php echo $form->labelEx($modelidi, 'keywords'); ?>

                <?php echo $form->textField($modelidi, 'keywords', array('style' => 'width:400px;')); ?>

                <?php echo $form->error($modelidi, 'keywords'); ?>

            </div><!-- row -->

            <div class="newitems _form_items">

                <?php echo $form->labelEx($modelidi, 'situation_before_or_issue_to_be_addressed'); ?>

                <?php echo $form->textArea($modelidi, 'situation_before_or_issue_to_be_addressed', array('style' => 'width:400px;')); ?>

                <?php echo $form->error($modelidi, 'situation_before_or_issue_to_be_addressed'); ?>

            </div><!-- row -->

            <div class="newitems _form_items">

                <?php echo $form->labelEx($modelidi, 'state_of_the_art'); ?>

                <?php echo $form->textArea($modelidi, 'state_of_the_art', array('style' => 'width:400px;')); ?>

                <?php echo $form->error($modelidi, 'state_of_the_art'); ?>

            </div><!-- row -->

            <div class="newitems _form_items">

                <?php echo $form->labelEx($modelidi, 'innovation_type'); ?>

                <?php

                $ddl2 = GxHtml::listDataEx(ProjectInnovationType::model()->findAllAttributes(null, true));

                asort($ddl2);

                echo $form->dropDownList($modelidi, 'innovation_type', $ddl2);

                ?>

                <?php echo $form->error($modelidi, 'innovation_type'); ?>

            </div><!-- row -->

            <div class="newitems _form_items">

                <?php echo $form->labelEx($modelidi, 'innovation_aspects'); ?>

                <?php echo $form->textArea($modelidi, 'innovation_aspects', array('style' => 'width:400px;')); ?>

                <?php echo $form->error($modelidi, 'innovation_aspects'); ?>

            </div><!-- row -->

            <div class="newitems _form_items">

                <?php echo $form->labelEx($modelidi, 'beneficiaries_and_advantages'); ?>

                <?php echo $form->textArea($modelidi, 'beneficiaries_and_advantages', array('style' => 'width:400px;')); ?>

                <?php echo $form->error($modelidi, 'beneficiaries_and_advantages'); ?>

            </div><!-- row -->

            <div class="newitems _form_items">

                <?php echo $form->labelEx($modelidi, 'intellectual_property'); ?>

                <?php echo $form->textArea($modelidi, 'intellectual_property', array('style' => 'width:400px;')); ?>

                <?php echo $form->error($modelidi, 'intellectual_property'); ?>

            </div><!-- row -->

            <div class="newitems _form_items">

                <?php echo $form->labelEx($modelidi, 'lessons_learned'); ?>

                <?php echo $form->textArea($modelidi, 'lessons_learned', array('style' => 'width:400px;')); ?>

                <?php echo $form->error($modelidi, 'lessons_learned'); ?>

            </div><!-- row -->

            <div class="newitems _form_items">

                <?php echo $form->labelEx($modelidi, 'obs'); ?>

                <?php echo $form->textArea($modelidi, 'obs', array('style' => 'width:400px;')); ?>

                <?php echo $form->error($modelidi, 'obs'); ?>

            </div><!-- row -->

            <div class="newitems _form_items">

                <?php echo $form->labelEx($modelidi, 'status'); ?>

                <?php echo $form->dropDownList($modelidi, 'status', array('0' => 'Cancelled', '1' => 'Active', '2' => 'Finished', '3' => 'Standby')); ?>

                <?php echo $form->error($modelidi, 'status'); ?>

            </div><!-- row -->

        <?php

    }


    if ($model->type == 'ID') {

        ?>

        <?php

        echo '<script>

	$(function() {

		function split( val ) {

			return val.split( /,\s*/ );

		}

		function extractLast( term ) {

			return split( term ).pop();

		}


		$( "#ProjectId_keywords" )

			.bind( "keydown", function( event ) {

				if ( event.keyCode === $.ui.keyCode.TAB &&

						$( this ).data( "autocomplete" ).menu.active ) {

					event.preventDefault();

				}

			})

			.autocomplete({

				minLength: 0,

				source: function( request, response ) {

					response( $.ui.autocomplete.filter(

						' . $keywords . ', extractLast( request.term ) ) );

				},

				focus: function() {

					return false;

				},

				select: function( event, ui ) {

					var terms = split( this.value );

					terms.pop();

					terms.push( ui.item.value );

					terms.push( "" );

					this.value = terms.join( ", " );

					return false;

				}

			});

                        $( "#ProjectId_project_team" )

			.bind( "keydown", function( event ) {

				if ( event.keyCode === $.ui.keyCode.TAB &&

						$( this ).data( "autocomplete" ).menu.active ) {

					event.preventDefault();

				}

			})

			.autocomplete({

				minLength: 0,

				source: function( request, response ) {

					response( $.ui.autocomplete.filter(

						' . $project_team . ', extractLast( request.term ) ) );

				},

				focus: function() {

					return false;

				},

				select: function( event, ui ) {

					var terms = split( this.value );

					terms.pop();

					terms.push( ui.item.value );

					terms.push( "" );

					this.value = terms.join( ", " );

					return false;

				}

			});

	});

	</script>';

        ?>

        <div class="newitems _form_items">

            <?php echo $form->labelEx($model, 'pid', array('label' => 'Project IDI')); ?>

            <?php

            $ddl = GxHtml::listDataEx(Project::model()->findAll("type='IDI'"), 'id', 'name');

            asort($ddl);

            echo $form->dropDownList($model, 'pid', $ddl);

            ?>

            <?php echo $form->error($model, 'pid'); ?>

        </div><!-- row -->

        <div class="newitems _form_items">

            <?php echo $form->labelEx($modelid, 'project_leader'); ?>

            <?php

            $ddl = GxHtml::listDataEx(User::model()->findAll(), 'id', 'name');

            asort($ddl);

            echo $form->dropDownList($modelid, 'project_leader', $ddl);

            ?>

            <?php echo $form->error($modelid, 'project_leader'); ?>

        </div><!-- row -->

        <div class="newitems _form_items">

            <?php echo $form->labelEx($modelid, 'project_team'); ?>

            <?php echo $form->textField($modelid, 'project_team', array('style' => 'width:400px;')); ?>

            <?php echo $form->error($modelid, 'project_team'); ?>

        </div>


        <div class="newitems _form_items">

            <?php echo $form->labelEx($modelid, 'objectives'); ?>

            <?php echo $form->textArea($modelid, 'objectives', array('style' => 'width:400px;')); ?>

            <?php echo $form->error($modelid, 'objectives'); ?>

        </div><!-- row -->

        <div class="newitems _form_items">

            <?php echo $form->labelEx($modelid, 'description'); ?>

            <?php echo $form->textArea($modelid, 'description', array('style' => 'width:400px;')); ?>

            <?php echo $form->error($modelid, 'description'); ?>

        </div><!-- row -->

        <div class="newitems _form_items">

            <?php echo $form->labelEx($modelid, 'keywords'); ?>

            <?php echo $form->textField($modelid, 'keywords', array('style' => 'width:400px;')); ?>

            <?php echo $form->error($modelid, 'keywords'); ?>

        </div><!-- row -->

        <div class="newitems _form_items">

            <?php echo $form->labelEx($modelid, 'intellectual_property'); ?>

            <?php echo $form->textArea($modelid, 'intellectual_property', array('style' => 'width:400px;')); ?>

            <?php echo $form->error($modelid, 'intellectual_property'); ?>

        </div><!-- row -->

        <div class="newitems _form_items">

            <?php echo $form->labelEx($modelid, 'obs'); ?>

            <?php echo $form->textArea($modelid, 'obs', array('style' => 'width:400px;')); ?>

            <?php echo $form->error($modelid, 'obs'); ?>

        </div><!-- row -->

        <div class="newitems _form_items">

            <?php echo $form->labelEx($modelid, 'status'); ?>

            <?php echo $form->dropDownList($modelid, 'status', array('0' => 'Cancelled', '1' => 'Active', '2' => 'Finished', '3' => 'Standby')); ?>

            <?php echo $form->error($modelid, 'status'); ?>

        </div><!-- row -->

        <?php

    }

    if ($model->type == 'D') {

        ?>

        <?php

        echo '<script>

	$(function() {

		function split( val ) {

			return val.split( /,\s*/ );

		}

		function extractLast( term ) {

			return split( term ).pop();

		}


		$( "#ProjectD_keywords" )

			.bind( "keydown", function( event ) {

				if ( event.keyCode === $.ui.keyCode.TAB &&

						$( this ).data( "autocomplete" ).menu.active ) {

					event.preventDefault();

				}

			})

			.autocomplete({

				minLength: 0,

				source: function( request, response ) {

					response( $.ui.autocomplete.filter(

						' . $keywords . ', extractLast( request.term ) ) );

				},

				focus: function() {

					return false;

				},

				select: function( event, ui ) {

					var terms = split( this.value );

					terms.pop();

					terms.push( ui.item.value );

					terms.push( "" );

					this.value = terms.join( ", " );

					return false;

				}

			});

                        $( "#ProjectD_project_team" )

			.bind( "keydown", function( event ) {

				if ( event.keyCode === $.ui.keyCode.TAB &&

						$( this ).data( "autocomplete" ).menu.active ) {

					event.preventDefault();

				}

			})

			.autocomplete({

				minLength: 0,

				source: function( request, response ) {

					response( $.ui.autocomplete.filter(

						' . $project_team . ', extractLast( request.term ) ) );

				},

				focus: function() {

					return false;

				},

				select: function( event, ui ) {

					var terms = split( this.value );

					terms.pop();

					terms.push( ui.item.value );

					terms.push( "" );

					this.value = terms.join( ", " );

					return false;

				}

			});

	});

	</script>';

        ?>

        <div class="newitems _form_items">

            <?php echo $form->labelEx($model, 'pid', array('label' => 'Project ID')); ?>

            <?php

            $ddl = GxHtml::listDataEx(Project::model()->findAll("type='ID'"), 'id', 'name');

            asort($ddl);

            echo $form->dropDownList($model, 'pid', $ddl);

            ?>

            <?php echo $form->error($model, 'pid'); ?>

        </div><!-- row -->

        <div class="newitems _form_items">

            <?php echo $form->labelEx($modeld, 'project_leader'); ?>

            <?php

            $ddl = GxHtml::listDataEx(User::model()->findAll(), 'id', 'name');

            asort($ddl);

            echo $form->dropDownList($modeld, 'project_leader', $ddl);

            ?>

            <?php echo $form->error($modeld, 'project_leader'); ?>

        </div><!-- row -->

        <div class="newitems _form_items">

            <?php echo $form->labelEx($modeld, 'project_team'); ?>

            <?php echo $form->textField($modeld, 'project_team', array('style' => 'width:400px;')); ?>

            <?php echo $form->error($modeld, 'project_team'); ?>

        </div>

        <div class="newitems _form_items">

            <?php echo $form->labelEx($modeld, 'objectives'); ?>

            <?php echo $form->textArea($modeld, 'objectives', array('style' => 'width:400px;')); ?>

            <?php echo $form->error($modeld, 'objectives'); ?>

        </div><!-- row -->

        <div class="newitems _form_items">

            <?php echo $form->labelEx($modeld, 'description'); ?>

            <?php echo $form->textArea($modeld, 'description', array('style' => 'width:400px;')); ?>

            <?php echo $form->error($modeld, 'description'); ?>

        </div><!-- row -->

        <div class="newitems _form_items">

            <?php echo $form->labelEx($modeld, 'keywords'); ?>

            <?php echo $form->textField($modeld, 'keywords', array('style' => 'width:400px;')); ?>

            <?php echo $form->error($modeld, 'keywords'); ?>

        </div><!-- row -->

        <div class="newitems _form_items">

            <?php echo $form->labelEx($modeld, 'obs'); ?>

            <?php echo $form->textArea($modeld, 'obs', array('style' => 'width:400px;')); ?>

            <?php echo $form->error($modeld, 'obs'); ?>

        </div><!-- row -->

        <div class="newitems _form_items">

            <?php echo $form->labelEx($modeld, 'status'); ?>

            <?php echo $form->dropDownList($modeld, 'status', array('0' => 'Cancelled', '1' => 'Active', '2' => 'Finished', '3' => 'Standby')); ?>

            <?php echo $form->error($modeld, 'status'); ?>

        </div><!-- row -->

        <?php

    }

} else {

    ?>

    <div class="row checkboxgroup">

        <?php echo $form->labelEx($model, 'type'); ?>


        <?php


        echo $form->radioButtonList($model, 'type', array('IDI' => 'IDI', 'ID' => 'ID', 'D' => 'D'), array(

            'uncheckValue' => null,

            'template' => '<div>{input}&nbsp;{label}</div>',

            'separator' => '',

            'onchange' => GxHtml::ajax(array(

                'type' => 'POST',

                'data' => array('type' => 'js:this.value'),

                'url' => GxController::createUrl('verificarDDL'),

                'update' => '#ajax',

            ))

                )

        ); 

        $_POST['type'] = $model->type;

        GxHtml::ajax(array(

            'type' => 'POST',

            'data' => array($model->type => 'type'),

            'url' => GxController::createUrl('verificarDDL'),

            'update' => '#ajax',

        ));

        ?>


        <?php echo $form->error($model, 'type'); ?>


    </div><!-- row -->

<?php } ?>


<div id="ajax" class="row"> </div>


<div class="row"></div>


<!--label--><!--/label-->


<?php

//echo GxHtml::submitButton(Yii::t('app', 'Save'), array('class' => 'btn_submit'));

echo GxHtml::ajaxSubmitButton(

        Yii::t('app', 'Save'),

        $this->createUrl('project/create'),

        array(),

        array('class' => 'btn_submit')

        );

echo GxHtml::Button(Yii::t('app', 'Cancel'), array(

    'class' => 'btn_cancel',

    'submit' => array('project/admin')

));


$this->endWidget();

?>

<div id="success" class="row"> </div>

</div><!-- form -->



ProjectController.php




<?php


class ProjectController extends GxController {


    public function actionPDF($id) {

        $dir = 'c:/';

        $file = time() . '.pdf';

        $mpdf = Yii::app()->ePdf->mpdf();

        $mpdf->WriteHTML(GxHtml::image(Yii::app()->request->baseUrl . '/images/logo.png'));

        $mpdf->WriteHTML(file_get_contents(Yii::app()->basePath . '/../css/detailview.css'), 1);

        $mpdf->WriteHTML(file_get_contents(Yii::app()->basePath . '/../css/main.css'), 1);

        $mpdf->WriteHTML(file_get_contents(Yii::app()->basePath . '/../css/style.css'), 1);

        $mpdf->WriteHTML(file_get_contents(Yii::app()->basePath . '/../css/screen.css'), 1);


        $model = Project::model()->findByPk($id);

        $mpdf->WriteHTML($this->renderPartial('/project/view', array('model' => $model), true));

        $mpdf->Output($dir . $file, 'I');

    }


    public function actionVerificarDDL() {

        $model_keywords = Keywords::model()->findAll(array('order' => 'name ASC'));

        $keywords = '[';

        foreach ($model_keywords as $row) {

            $keywords .= '"' . $row->name . '",';

        }

        $keywords .= ']';

        $model_project_team = User::model()->findAll();

        $project_team = '[';

        foreach ($model_project_team as $row) {

            $project_team .= '"' . $row->name . '",';

        }

        $project_team .= ']';


        $model->type = $_POST['type'];

//### IDI

        if ($model->type == 'IDI') {

            //global


            echo '<script>

	$(function() {

		function split( val ) {

			return val.split( /,\s*/ );

		}

		function extractLast( term ) {

			return split( term ).pop();

		}


		$( "#ProjectIdi_keywords" )

			.bind( "keydown", function( event ) {

				if ( event.keyCode === $.ui.keyCode.TAB &&

						$( this ).data( "autocomplete" ).menu.active ) {

					event.preventDefault();

				}

			})

			.autocomplete({

				minLength: 0,

				source: function( request, response ) {

					response( $.ui.autocomplete.filter(

						' . $keywords . ', extractLast( request.term ) ) );

				},

				focus: function() {

					return false;

				},

				select: function( event, ui ) {

					var terms = split( this.value );

					terms.pop();

					terms.push( ui.item.value );

					terms.push( "" );

					this.value = terms.join( ", " );

					return false;

				}

			});

                        $( "#ProjectIdi_project_team" )

			.bind( "keydown", function( event ) {

				if ( event.keyCode === $.ui.keyCode.TAB &&

						$( this ).data( "autocomplete" ).menu.active ) {

					event.preventDefault();

				}

			})

			.autocomplete({

				minLength: 0,

				source: function( request, response ) {

					response( $.ui.autocomplete.filter(

						' . $project_team . ', extractLast( request.term ) ) );

				},

				focus: function() {

					return false;

				},

				select: function( event, ui ) {

					var terms = split( this.value );

					terms.pop();

					terms.push( ui.item.value );

					terms.push( "" );

					this.value = terms.join( ", " );

					return false;

				}

			});

	});

	</script>';


            //PROJECT PID

            $tbl = 'Project';

            $attribute = "pid";

            $id = $tbl . "_" . $attribute;

            $name = $tbl . "[" . $attribute . "]";

            echo GxHtml::tag('input', array('id' => $id, 'name' => $name, 'type' => 'hidden', 'value' => '0'), '', false);


            $tbl = 'ProjectIdi';


            //Start Date

            $attribute = "start_date";

            $id = $tbl . "_" . $attribute;

            $name = $tbl . "[" . $attribute . "]";

            echo GxHtml::tag('input', array('id' => $id, 'name' => $name, 'type' => 'hidden', 'value' => date("Y-m-d G:i:s", time())), '', false);


            //ALL ATTRIBUTES 

            $attributes = array(

                'project_leader',

                'project_team',

                'objectives',

                'description',

                'keywords',

                'situation_before_or_issue_to_be_addressed',

                'state_of_the_art',

                'innovation_type',

                'innovation_aspects',

                'beneficiaries_and_advantages',

                'intellectual_property',

                'lessons_learned',

                'obs',

            );

            $ddls = array(

                'project_leader',

                'innovation_type',

            );

            $txtf = array(

                'keywords',

                'project_team',

            );

            foreach ($attributes as $attrib) {

                $title = ucfirst(str_replace('_', ' ', $attrib));

                $id = $tbl . "_" . $attrib;

                $name = $tbl . "[" . $attrib . "]";

                echo '<div class="_form_items newitems">';

                if ($attrib != 'obs' && $attrib != 'lessons_learned')

                    echo GxHtml::tag('label', array('class' => 'required', 'for' => $id), $title . ' <span class="required">*</span>', true);

                else

                    echo GxHtml::tag('label', array('for' => $id), $title, true);

                if (!in_array($attrib, $txtf)) {

                    if (!in_array($attrib, $ddls))

                        echo GxHtml::tag('textarea', array('rows' => 3, 'style' => 'width:400px', 'id' => $id, 'name' => $name, 'value' => ''), '', true);

                    else {

                        echo GxHtml::tag('select', array('id' => $id, 'name' => $name, 'value' => ''), '', false);

                        if ($attrib == 'project_leader')

                            $ddlmodel = User::model()->findAll(array('order' => 'name'));

                        if ($attrib == 'innovation_type')

                            $ddlmodel = ProjectInnovationType::model()->findAll(array('order' => 'name'));

                        foreach ($ddlmodel as $row) {

                            echo GxHtml::tag('option', array('value' => $row->id), GxHtml::encode($row->name), true);

                        }

                        echo GxHtml::tag('select', array('id' => $id, 'name' => $name, 'value' => ''), '', true);

                    }

                } else {

                    echo GxHtml::tag('input', array('style' => 'width:400px', 'id' => $id, 'name' => $name, 'value' => ''), '', true);

                }

                echo GxHtml::tag('div', array('id' => $id, 'class' => 'errorMessage', 'style' => 'display:none'), '', true);

                echo '</div>';

            }

        }

//### ID

        if ($model->type == 'ID') {


            //geral


            echo '<script>

	$(function() {

		function split( val ) {

			return val.split( /,\s*/ );

		}

		function extractLast( term ) {

			return split( term ).pop();

		}


		$( "#ProjectId_keywords" )

			.bind( "keydown", function( event ) {

				if ( event.keyCode === $.ui.keyCode.TAB &&

						$( this ).data( "autocomplete" ).menu.active ) {

					event.preventDefault();

				}

			})

			.autocomplete({

				minLength: 0,

				source: function( request, response ) {

					response( $.ui.autocomplete.filter(

						' . $keywords . ', extractLast( request.term ) ) );

				},

				focus: function() {

					return false;

				},

				select: function( event, ui ) {

					var terms = split( this.value );

					terms.pop();

					terms.push( ui.item.value );

					terms.push( "" );

					this.value = terms.join( ", " );

					return false;

				}

			});

                        $( "#ProjectId_project_team" )

			.bind( "keydown", function( event ) {

				if ( event.keyCode === $.ui.keyCode.TAB &&

						$( this ).data( "autocomplete" ).menu.active ) {

					event.preventDefault();

				}

			})

			.autocomplete({

				minLength: 0,

				source: function( request, response ) {

					response( $.ui.autocomplete.filter(

						' . $project_team . ', extractLast( request.term ) ) );

				},

				focus: function() {

					return false;

				},

				select: function( event, ui ) {

					var terms = split( this.value );

					terms.pop();

					terms.push( ui.item.value );

					terms.push( "" );

					this.value = terms.join( ", " );

					return false;

				}

			});

	});

	</script>';


            //global

            $tbl = 'ProjectId';


            //Start Date

            $attribute = "start_date";

            $id = $tbl . "_" . $attribute;

            $name = $tbl . "[" . $attribute . "]";

            echo GxHtml::tag('input', array('id' => $id, 'name' => $name, 'type' => 'hidden', 'value' => date("Y-m-d G:i:s", time())), '', false);


            //ALL ATTRIBUTES 

            $attributes = array(

                'pid',

                'project_leader',

                'project_team',

                'objectives',

                'description',

                'keywords',

                'intellectual_property',

                'obs',

            );

            $ddls = array(

                'project_leader',

                'pid',

            );

            $txtf = array(

                'keywords',

                'project_team',

            );

            foreach ($attributes as $attrib) {

                $title = ucfirst(str_replace('_', ' ', $attrib));

                $id = $tbl . "_" . $attrib;

                $name = $tbl . "[" . $attrib . "]";

                echo '<div class="_form_items newitems">';

                if ($attrib != 'obs') {

                    if ($attrib == 'pid')

                        $title = 'Project IDI ';

                    echo GxHtml::tag('label', array('class' => 'required', 'for' => $id), $title . ' <span class="required">*</span>', true);

                } else

                    echo GxHtml::tag('label', array('for' => $id), $title, true);

                if (!in_array($attrib, $txtf)) {

                    if (!in_array($attrib, $ddls))

                        echo GxHtml::tag('textarea', array('rows' => 3, 'style' => 'width:400px', 'id' => $id, 'name' => $name, 'value' => ''), '', true);

                    else {

                        if ($attrib == 'pid')

                            echo GxHtml::tag('select', array('id' => 'Project_pid', 'name' => 'Project[pid]'), '', false);

                        else

                            echo GxHtml::tag('select', array('id' => $id, 'name' => $name, 'value' => ''), '', false);

                        if ($attrib == 'project_leader')

                            $ddlmodel = User::model()->findAll(array('order' => 'name'));

                        if ($attrib == 'innovation_type')

                            $ddlmodel = ProjectInnovationType::model()->findAll(array('order' => 'name'));

                        if ($attrib == 'pid')

                            $ddlmodel = Project::model()->findAll(array('condition' => "type='IDI'", 'order' => 'name'));

                        foreach ($ddlmodel as $row) {

                            echo GxHtml::tag('option', array('value' => $row->id), GxHtml::encode($row->name), true);

                        }

                        echo GxHtml::tag('select', array('id' => $id, 'name' => $name, 'value' => ''), '', true);

                    }

                } else {

                    echo GxHtml::tag('input', array('style' => 'width:400px', 'id' => $id, 'name' => $name, 'value' => ''), '', true);

                }

                echo GxHtml::tag('div', array('id' => $id, 'class' => 'errorMessage', 'style' => 'display:none'), '', true);

                echo '</div>';

            }

        }

//### D

        if ($model->type == 'D') {


            //geral


            echo '<script>

	$(function() {

		function split( val ) {

			return val.split( /,\s*/ );

		}

		function extractLast( term ) {

			return split( term ).pop();

		}


		$( "#ProjectD_keywords" )

			.bind( "keydown", function( event ) {

				if ( event.keyCode === $.ui.keyCode.TAB &&

						$( this ).data( "autocomplete" ).menu.active ) {

					event.preventDefault();

				}

			})

			.autocomplete({

				minLength: 0,

				source: function( request, response ) {

					response( $.ui.autocomplete.filter(

						' . $keywords . ', extractLast( request.term ) ) );

				},

				focus: function() {

					return false;

				},

				select: function( event, ui ) {

					var terms = split( this.value );

					terms.pop();

					terms.push( ui.item.value );

					terms.push( "" );

					this.value = terms.join( ", " );

					return false;

				}

			});

                        $( "#ProjectD_project_team" )

			.bind( "keydown", function( event ) {

				if ( event.keyCode === $.ui.keyCode.TAB &&

						$( this ).data( "autocomplete" ).menu.active ) {

					event.preventDefault();

				}

			})

			.autocomplete({

				minLength: 0,

				source: function( request, response ) {

					response( $.ui.autocomplete.filter(

						' . $project_team . ', extractLast( request.term ) ) );

				},

				focus: function() {

					return false;

				},

				select: function( event, ui ) {

					var terms = split( this.value );

					terms.pop();

					terms.push( ui.item.value );

					terms.push( "" );

					this.value = terms.join( ", " );

					return false;

				}

			});

	});

	</script>';


            //global

            $tbl = 'ProjectD';


            //Start Date

            $attribute = "start_date";

            $id = $tbl . "_" . $attribute;

            $name = $tbl . "[" . $attribute . "]";

            echo GxHtml::tag('input', array('id' => $id, 'name' => $name, 'type' => 'hidden', 'value' => date("Y-m-d G:i:s", time())), '', false);


            //ALL ATTRIBUTES 

            $attributes = array(

                'pid',

                'project_leader',

                'project_team',

                'objectives',

                'description',

                'keywords',

                'obs',

            );

            $ddls = array(

                'pid',

                'project_leader',

            );

            $txtf = array(

                'keywords',

                'project_team',

            );

            foreach ($attributes as $attrib) {

                $title = ucfirst(str_replace('_', ' ', $attrib));

                $id = $tbl . "_" . $attrib;

                $name = $tbl . "[" . $attrib . "]";

                echo '<div class="_form_items newitems">';

                if ($attrib != 'obs') {

                    if ($attrib == 'pid')

                        $title = 'Project ID ';

                    echo GxHtml::tag('label', array('class' => 'required', 'for' => $id), $title . ' <span class="required">*</span>', true);

                } else

                    echo GxHtml::tag('label', array('for' => $id), $title, true);

                if (!in_array($attrib, $txtf)) {

                    if (!in_array($attrib, $ddls))

                        echo GxHtml::tag('textarea', array('rows' => 3, 'style' => 'width:400px', 'id' => $id, 'name' => $name, 'value' => ''), '', true);

                    else {

                        if ($attrib == 'pid')

                            echo GxHtml::tag('select', array('id' => 'Project_pid', 'name' => 'Project[pid]'), '', false);

                        else

                            echo GxHtml::tag('select', array('id' => $id, 'name' => $name, 'value' => ''), '', false);

                        if ($attrib == 'project_leader')

                            $ddlmodel = User::model()->findAll(array('order' => 'name'));

                        if ($attrib == 'innovation_type')

                            $ddlmodel = ProjectInnovationType::model()->findAll(array('order' => 'name'));

                        if ($attrib == 'pid')

                            $ddlmodel = Project::model()->findAll(array('condition' => "type='ID'", 'order' => 'name'));

                        foreach ($ddlmodel as $row) {

                            echo GxHtml::tag('option', array('value' => $row->id), GxHtml::encode($row->name), true);

                        }

                        echo GxHtml::tag('select', array('id' => $id, 'name' => $name, 'value' => ''), '', true);

                    }

                } else {

                    echo GxHtml::tag('input', array('style' => 'width:400px', 'id' => $id, 'name' => $name, 'value' => ''), '', true);

                }

                echo GxHtml::tag('div', array('id' => $id, 'class' => 'errorMessage', 'style' => 'display:none'), '', true);

                echo '</div>';

            }

        }

    }


    public function filters() {

        return array(

            'accessControl',

        );

    }


    public function actionView($id) {

        $this->render('view', array(

            'model' => $this->loadModel($id, 'Project'),

        ));

    }


    public function actionCreate() {

        $model = new Project;

        $modelid = new ProjectID;

        $modeld = new ProjectD;

        $modelidi = new ProjectIdi;

        $modelkeywords = Keywords::model()->findAll();

        $keywords = '';

        $this->performAjaxValidation(array($model, $modelidi, $modelid, $modeld), 'project-form');


        if (isset($_POST['ProjectIdi'])) {

            $model->setAttributes($_POST['Project']);

            if ($model->validate()) {

                $modelidi->setAttributes($_POST['ProjectIdi']);

                $model->save(false);

                $tbl = Project::model()->findAllBySql('SELECT TOP 1 id FROM Project ORDER BY id DESC');

                $modelidi->id_project = $tbl[0]->id;

                if ($modelidi->validate()) {

                    //keywords

                    $keywords = $modelidi->keywords;

                    $keywords_array = split(",", $keywords);

                    foreach ($keywords_array as $key) {

                        $key = str_replace(' ', '', $key);

                        if (($key != ' ') && ($key != '')) {

                            $count = 0;

                            foreach ($modelkeywords as $m) {

                                if ($key == $m) {

                                    $count++;

                                }

                            }

                            if ($count == 0) {

                                $mk = new Keywords();

                                $mk->name = $key;

                                $mk->save();

                            }

                        }

                    }

                    $modelidi->save(false);

                    $this->redirect(array('view', 'id' => $model->id));

                }

                

            }

        }

        if (isset($_POST['ProjectId'])) {

            $model->setAttributes($_POST['Project']);

            if ($model->validate()) {

                $modelid->setAttributes($_POST['ProjectId']);

                $model->save(false);

                $tbl = Project::model()->findAllBySql('SELECT TOP 1 id FROM Project ORDER BY id DESC');

                $modelid->id_project = $tbl[0]->id;

                if ($modelid->validate()) {

                    //keywords

                    $keywords = $modelid->keywords;

                    $keywords_array = split(",", $keywords);

                    foreach ($keywords_array as $key) {

                        $key = str_replace(' ', '', $key);

                        if (($key != ' ') && ($key != '')) {

                            $count = 0;

                            foreach ($modelkeywords as $m) {

                                if ($key == $m) {

                                    $count++;

                                }

                            }

                            if ($count == 0) {

                                $mk = new Keywords();

                                $mk->name = $key;

                                $mk->save();

                            }

                        }

                    }

                    $modelid->save(false);

                    $this->redirect(array('view', 'id' => $model->id));

                }

            }

        }

        if (isset($_POST['ProjectD'])) {

            $model->setAttributes($_POST['Project']);

            if ($model->validate()) {

                $modeld->setAttributes($_POST['ProjectD']);

                $model->save(false);

                $tbl = Project::model()->findAllBySql('SELECT TOP 1 id FROM Project ORDER BY id DESC');

                $modeld->id_project = $tbl[0]->id;

                if ($modeld->validate()) {

                    //keywords

                    $keywords = $modeld->keywords;

                    $keywords_array = split(",", $keywords);

                    foreach ($keywords_array as $key) {

                        $key = str_replace(' ', '', $key);

                        if (($key != ' ') && ($key != '')) {

                            $count = 0;

                            foreach ($modelkeywords as $m) {

                                if ($key == $m) {

                                    $count++;

                                }

                            }

                            if ($count == 0) {

                                $mk = new Keywords();

                                $mk->name = $key;

                                $mk->save();

                            }

                        }

                    }

                    $modeld->save(false);

                    $this->redirect(array('view', 'id' => $model->id));

                }

            }

        }

        $this->render('create', array(

            'model' => $model,

            'modelidi' => $modelidi,

            'modelid' => $modelid,

            'modeld' => $modeld,

        ));

    }


    public function actionUpdate($id) {

        $model = $this->loadModel($id, 'Project');

        $modelidi = new ProjectIdi;

        $modelid = new ProjectId;

        $modeld = new ProjectD;


        if ($model->type == 'IDI') {

            $modelidi = $model->projectIdi;

            $date = $modelidi->start_date;

        }

        if ($model->type == 'ID') {

            $modelid = $model->project;

            $date = $modelid->start_date;

        }

        if ($model->type == 'D') {

            $modeld = $model->projectD;

            $date = $modeld->start_date;

        }

        $this->performAjaxValidation(array($model, $modelidi, $modelid, $modeld), 'project-form');


        if (isset($_POST['Project'])) {

            $model->attributes = $_POST['Project'];

            if (isset($_POST['ProjectIdi']))

                $modelidi->attributes = $_POST['ProjectIdi'];

            if (isset($_POST['ProjectId']))

                $modelid->attributes = $_POST['ProjectId'];

            if (isset($_POST['ProjectD']))

                $modeld->attributes = $_POST['ProjectD'];

            if ($model->validate()) {

                if (($model->type == 'IDI') && $modelidi->validate()) {

                    // STATUS '0'=>'Cancelled', '1'=>'Active', '2'=>'Finished','3'=>'Standby'

                    $model_status = ProjectStatus::model()->findAll('id_project=' . $id);

                    if ($model_status == array()) {

                        $model_status = new ProjectStatus;

                        $model_status->start_date = $modelidi->start_date;

                        $model_status->end_date = date("Y-m-d G:i:s", time());

                    } else {

                        $model_status = ProjectStatus::model()->find(array('select' => '*', 'condition' => 'id_project=' . $id, 'order' => 'id DESC'));

                        if ($modelidi->status == 1) { //active

                            if ($model_status->end_date != null) {

                                $model_status = new ProjectStatus;

                                $model_status->start_date = date("Y-m-d G:i:s", time());

                            }

                            $model_status->end_date = null;

                        } else { //cancelled, finished or standby

                            $model_status->end_date = date("Y-m-d G:i:s", time());

                        }

                    }

                    $model_status->id_project = $id;

                    $model_status->save();

                    // STATUS END

                    $modelidi->id_project = $id;

                    $modelidi->start_date = $date;

                    $modelidi->update();

                    $model->save(false);

                    $this->redirect(array('view', 'id' => $model->id));

                }

                if (($model->type == 'ID') && $modelid->validate()) {

                    // STATUS '0'=>'Cancelled', '1'=>'Active', '2'=>'Finished','3'=>'Standby'

                    $model_status = ProjectStatus::model()->findAll('id_project=' . $id);

                    if ($model_status == array()) {

                        $model_status = new ProjectStatus;

                        $model_status->start_date = $modelid->start_date;

                        $model_status->end_date = date("Y-m-d G:i:s", time());

                    } else {

                        $model_status = ProjectStatus::model()->find(array('select' => '*', 'condition' => 'id_project=' . $id, 'order' => 'id DESC'));

                        if ($modelid->status == 1) { //active

                            if ($model_status->end_date != null) {

                                $model_status = new ProjectStatus;

                                $model_status->start_date = date("Y-m-d G:i:s", time());

                            }

                            $model_status->end_date = null;

                        } else { //cancelled, finished or standby

                            $model_status->end_date = date("Y-m-d G:i:s", time());

                        }

                    }

                    $model_status->id_project = $id;

                    $model_status->save();

                    // STATUS END

                    $modelid->id_project = $id;

                    $modelid->start_date = $date;

                    $modelid->update();

                    $model->save(false);

                    $this->redirect(array('view', 'id' => $model->id));

                }

                if (($model->type == 'D') && $modeld->validate()) {

                    // STATUS '0'=>'Cancelled', '1'=>'Active', '2'=>'Finished','3'=>'Standby'

                    $model_status = ProjectStatus::model()->findAll('id_project=' . $id);

                    if ($model_status == array()) {

                        $model_status = new ProjectStatus;

                        $model_status->start_date = $modeld->start_date;

                        $model_status->end_date = date("Y-m-d G:i:s", time());

                    } else {

                        $model_status = ProjectStatus::model()->find(array('select' => '*', 'condition' => 'id_project=' . $id, 'order' => 'id DESC'));

                        if ($modeld->status == 1) { //active

                            if ($model_status->end_date != null) {

                                $model_status = new ProjectStatus;

                                $model_status->start_date = date("Y-m-d G:i:s", time());

                            }

                            $model_status->end_date = null;

                        } else { //cancelled, finished or standby

                            $model_status->end_date = date("Y-m-d G:i:s", time());

                        }

                    }

                    $model_status->id_project = $id;

                    $model_status->save();

                    // STATUS END

                    $modeld->id_project = $id;

                    $modeld->start_date = $date;

                    $modeld->update();

                    $model->save(false);

                    $this->redirect(array('view', 'id' => $model->id));

                }

            }

        }


        $this->render('update', array(

            'model' => $model,

            'modelidi' => $modelidi,

            'modelid' => $modelid,

            'modeld' => $modeld,

        ));

    }


    public function actionDelete($id) {

        if (Yii::app()->request->isPostRequest) {

            $this->loadModel($id, 'Project')->delete();


            if (!Yii::app()->request->isAjaxRequest)

                $this->redirect(array('admin'));

        } else

            throw new CHttpException(400,

                    Yii::t('app', 'Invalid request. Please do not repeat this request again.'));

    }


    /*

      public function actionAdmin() {

      $dataProvider = new CActiveDataProvider('Project');

      $this->render('index', array(

      'dataProvider' => $dataProvider,

      ));

      } */


    public function actionAdmin() {

        $model = new Project('search');

        $model->unsetAttributes();


        if (isset($_GET['Project']))

            $model->attributes = $_GET['Project'];


        $this->render('admin', array(

            'model' => $model,

        ));

    }


    public function actionIndex() {

        $model = new Project('search');

        $model->unsetAttributes();


        if (isset($_GET['Project']))

            $model->attributes = $_GET['Project'];


        $this->render('admin', array(

            'model' => $model,

        ));

    }


}