Listing 66: Class Controller
<?php
c l a s s C l a s s C o n t r o l l e r e x t e n d s C o n t r o l l e r {
/∗∗
∗ @var s t r i n g t h e d e f a u l t l a y o u t f o r t h e v i e w s . D e f a u l t s t o ’ / / l a y o u t s / column2 ’ , meaning
∗ u s i n g two−column l a y o u t . S e e ’ p r o t e c t e d / v i e w s / l a y o u t s / column2 . php ’ .
∗/
p u b l i c $ l a y o u t = ’// l a y o u t s / column2 ’ ; /∗∗
∗ @ r e t u r n a r r a y a c t i o n f i l t e r s
∗/
p u b l i c f u n c t i o n f i l t e r s ( ) {
r e t u r n a r r a y (
’ a c c e s s C o n t r o l ’ , // p e r f o r m a c c e s s c o n t r o l f o r CRUD o p e r a t i o n s
’ p o s t O n l y + d e l e t e ’ , // we o n l y a l l o w d e l e t i o n v i a POST r e q u e s t ) ;
} /∗∗
∗ S p e c i f i e s t h e a c c e s s c o n t r o l r u l e s .
∗ T h i s method i s u s e d by t h e ’ a c c e s s C o n t r o l ’ f i l t e r .
∗ @ r e t u r n a r r a y a c c e s s c o n t r o l r u l e s
∗/
p u b l i c f u n c t i o n a c c e s s R u l e s ( ) {
r e t u r n a r r a y (
a r r a y ( ’ a l l o w ’ , // a l l o w p r o f t o p e r f o r m a l l a c t i o n s
’ a c t i o n s ’=>a r r a y ( ’ i n d e x ’ , ’ view ’ , ’ c r e a t e ’ , ’ u p d a t e ’ , ’ admin ’ , ’ d e l e t e ’ ) ,
’ e x p r e s s i o n ’=>’ Y i i : : app ( )−>u s e r−>i s P r o f ( ) ’ , ) ,
a r r a y ( ’ a l l o w ’ , // a l l o w s t u d e n t t o p e r f o r m ’ l o a d C l a s s ’ and ’ c o n n e c t ’ a c t i o n s
’ a c t i o n s ’=>a r r a y ( ’ l o a d C l a s s ’ , ’ c o n n e c t ’ ) ,
’ e x p r e s s i o n ’=>’ Y i i : : app ( )−>u s e r−>i s S t d n t ( ) ’ ) ,
a r r a y ( ’ deny ’ , // deny a l l u s e r s
’ u s e r s ’=>a r r a y ( ’∗’ ) , ) ,
) ; }
/∗∗
∗ D i s p l a y s a p a r t i c u l a r model .
∗ @param i n t e g e r $ i d t h e ID o f t h e model t o be d i s p l a y e d
∗/
p u b l i c f u n c t i o n a c t i o n V i e w ( $ i d ) {
$ t h i s−>r e n d e r ( ’ view ’ , a r r a y (
’ model ’=>$ t h i s−>l o a d M o d e l ( $ i d ) , ) ) ;
} /∗∗
∗ C r e a t e s a new model .
∗ I f c r e a t i o n i s s u c c e s s f u l , t h e b r o w s e r w i l l be r e d i r e c t e d t o t h e ’ view ’ p a g e .
∗/
p u b l i c f u n c t i o n a c t i o n C r e a t e ( ) {
$model=new C l a s s M o d e l ;
// Uncomment t h e f o l l o w i n g l i n e i f AJAX v a l i d a t i o n i s n e e d e d // $ t h i s−>p e r f o r m A j a x V a l i d a t i o n ( $model ) ;
i f ( i s s e t ( $ POST [ ’ C l a s s M o d e l ’ ] ) ) {
$model−>a t t r i b u t e s=$ POST [ ’ C l a s s M o d e l ’ ] ;
$model−>p r o f=Y i i : : app ( )−>u s e r−>i d ; i f ( $model−>s a v e ( ) )
$ t h i s−>r e d i r e c t ( a r r a y ( ’ view ’ , ’ i d ’=>$model−>i d ) ) ; }
$ t h i s−>r e n d e r ( ’ c r e a t e ’ , a r r a y (
’ model ’=>$model , ) ) ;
} /∗∗
∗ U p d a t e s a p a r t i c u l a r model .
∗ I f u p d a t e i s s u c c e s s f u l , t h e b r o w s e r w i l l be r e d i r e c t e d t o t h e ’ view ’ p a g e .
∗ @param i n t e g e r $ i d t h e ID o f t h e model t o be u p d a t e d
∗/
p u b l i c f u n c t i o n a c t i o n U p d a t e ( $ i d ) {
$model=$ t h i s−>l o a d M o d e l ( $ i d ) ;
// Uncomment t h e f o l l o w i n g l i n e i f AJAX v a l i d a t i o n i s n e e d e d // $ t h i s−>p e r f o r m A j a x V a l i d a t i o n ( $model ) ;
i f ( i s s e t ( $ POST [ ’ C l a s s M o d e l ’ ] ) ) {
$model−>a t t r i b u t e s=$ POST [ ’ C l a s s M o d e l ’ ] ; i f ( $model−>s a v e ( ) )
$ t h i s−>r e d i r e c t ( a r r a y ( ’ view ’ , ’ i d ’=>$model−>i d ) ) ; }
$ t h i s−>r e n d e r ( ’ u p d a t e ’ , a r r a y (
’ model ’=>$model , ) ) ;
} /∗∗
∗ D e l e t e s a p a r t i c u l a r model .
∗ I f d e l e t i o n i s s u c c e s s f u l , t h e b r o w s e r w i l l be r e d i r e c t e d t o t h e ’ admin ’ p a g e .
∗ @param i n t e g e r $ i d t h e ID o f t h e model t o be d e l e t e d
∗/
p u b l i c f u n c t i o n a c t i o n D e l e t e ( $ i d ) {
$ c l a s s = $ t h i s−>l o a d M o d e l ( $ i d ) ; i f ( $ c l a s s−>exp <= d a t e ( ”Y−m−d ” ) )
$ c l a s s−>d e l e t e ( ) ;
// i f AJAX r e q u e s t ( t r i g g e r e d by d e l e t i o n v i a admin g r i d v i e w ) , we s h o u l d n o t r e d i r e c t t h e b r o w s e r
i f ( ! i s s e t ( $ GET [ ’ a j a x ’ ] ) )
$ t h i s−>r e d i r e c t ( i s s e t ( $ POST [ ’ r e t u r n U r l ’ ] ) ? $ POST [ ’ r e t u r n U r l ’ ] : a r r a y ( ’ admin ’ ) ) ;
} /∗∗
∗ L i s t s a l l m o d e l s .
∗/
p u b l i c f u n c t i o n a c t i o n I n d e x ( ) {
$model=new C l a s s M o d e l ( ’ s e a r c h ’ ) ;
$model−>u n s e t A t t r i b u t e s ( ) ; // c l e a r any d e f a u l t v a l u e s i f ( i s s e t ( $ GET [ ’ C l a s s M o d e l ’ ] ) )
$model−>a t t r i b u t e s=$ GET [ ’ C l a s s M o d e l ’ ] ;
$ t h i s−>r e n d e r ( ’ i n d e x ’ , a r r a y (
’ model ’=>$model , ) ) ;
} /∗∗
∗ Manages a l l m o d e l s .
∗/
p u b l i c f u n c t i o n a c t i o n A d m i n ( ) {
$model=new C l a s s M o d e l ( ’ s e a r c h ’ ) ;
$model−>u n s e t A t t r i b u t e s ( ) ; // c l e a r any d e f a u l t v a l u e s i f ( i s s e t ( $ GET [ ’ C l a s s M o d e l ’ ] ) )
$model−>a t t r i b u t e s=$ GET [ ’ C l a s s M o d e l ’ ] ;
$ t h i s−>r e n d e r ( ’ admin ’ , a r r a y (
’ model ’=>$model , ) ) ;
} /∗∗
∗ R e t u r n s t h e d a t a model b a s e d on t h e p r i m a r y k e y g i v e n i n t h e GET v a r i a b l e .
∗ I f t h e d a t a model i s n o t f o u n d , an HTTP e x c e p t i o n w i l l be r a i s e d .
∗ @param i n t e g e r $ i d t h e ID o f t h e model t o be l o a d e d
∗ @ r e t u r n C l a s s M o d e l t h e l o a d e d model
∗ @throws C H t t p E x c e p t i o n
∗/
p u b l i c f u n c t i o n l o a d M o d e l ( $ i d ) {
$model=C l a s s M o d e l : : model ( )−>f i n d B y P k ( $ i d ) ; i f ( $model===n u l l )
t h r o w new C H t t p E x c e p t i o n ( 4 0 4 , ’ The r e q u e s t e d p a g e d o e s n o t e x i s t . ’ ) ; r e t u r n $model ;
} /∗∗
∗ P e r f o r m s t h e AJAX v a l i d a t i o n .
∗ @param C l a s s M o d e l $model t h e model t o be v a l i d a t e d
∗/
p r o t e c t e d f u n c t i o n p e r f o r m A j a x V a l i d a t i o n ( $model ) {
i f ( i s s e t ( $ POST [ ’ a j a x ’ ] ) && $ POST [ ’ a j a x ’]=== ’ c l a s s−model−form ’ ) {
e c h o CActiveForm : : v a l i d a t e ( $model ) ; Y i i : : app ( )−>end ( ) ;
} }
/∗∗
∗ Loads c l a s s f o r S t u d e n t a c c e s s .
∗ @param i n t e g e r $ p o d i d t h e pod i d
∗/
p u b l i c f u n c t i o n a c t i o n L o a d C l a s s ( $ p o d i d ) {
$model = PodModel : : model ( )−>f i n d B y P k ( $ p o d i d ) ;
$ t h i s−>r e n d e r ( $model−>c l a s s−>c o u r s e c o d e , a r r a y (
’ model ’=>$model ) ) ;
} /∗∗
∗ C o n n e c t s t o t h e d e v i c e c l i c k e d .
∗ @param s t r i n g $ i d t h e i d o f t h e d e v i c e
∗/
p u b l i c f u n c t i o n a c t i o n C o n n e c t ( $ i d ) {
$ t o p o l o g y = T o p o l o g y M o d e l : : model ( )−>f i n d B y P k ( $ i d ) ; i f ( i s s e t ( $ t o p o l o g y ) ) {
i f ( $ t o p o l o g y−>t y p e == 3 ) {
$command = ’ m s t s c|/ v : ’ . $ t o p o l o g y−>i p ; } e l s e {
$ c l a s s = C l a s s M o d e l : : model ( )−>f i n d B y P k ( Y i i : : app ( )−>u s e r−>
g e t S t a t e ( ’ c l a s s ’ ) ) ;
$command = ’ p u t t y|−s s h ’ . $ c l a s s−>t s i p . ’ −l ’ . $ c l a s s−>t s u s e r . ’ −pw ’ . $ c l a s s−>t s p a s s ;
$command .= $ t o p o l o g y−>d i s p l a y === ’ TS ’ ? ”” : ’ −m ” ’ . Y i i : : app ( )−>b a s e P a t h . ’\v i e w s\c l a s s\ \’ . $ c l a s s−>c o u r s e c o d e . ’\ \’ .
$ t o p o l o g y−>d i s p l a y . ’ . t x t ” ’ ; }
}
// e x e c ( $command , $ r e s u l t , $ s t a t u s ) ; // e c h o $command . ”\n ” . $ r e s u l t . ”\n ” . $ s t a t u s ; e c h o $command ;
} }
Listing 67: Course Controller
<?php
c l a s s C o u r s e C o n t r o l l e r e x t e n d s C o n t r o l l e r {
/∗∗
∗ @var s t r i n g t h e d e f a u l t l a y o u t f o r t h e v i e w s . D e f a u l t s t o ’ / / l a y o u t s / column2 ’ , meaning
∗ u s i n g two−column l a y o u t . S e e ’ p r o t e c t e d / v i e w s / l a y o u t s / column2 . php ’ .
∗/
p u b l i c $ l a y o u t = ’// l a y o u t s / column2 ’ ; /∗∗
∗ @ r e t u r n a r r a y a c t i o n f i l t e r s
∗/
p u b l i c f u n c t i o n f i l t e r s ( ) {
r e t u r n a r r a y (
’ a c c e s s C o n t r o l ’ , // p e r f o r m a c c e s s c o n t r o l f o r CRUD o p e r a t i o n s
’ p o s t O n l y + d e l e t e ’ , // we o n l y a l l o w d e l e t i o n v i a POST r e q u e s t ) ;
} /∗∗
∗ S p e c i f i e s t h e a c c e s s c o n t r o l r u l e s .
∗ T h i s method i s u s e d by t h e ’ a c c e s s C o n t r o l ’ f i l t e r .
∗ @ r e t u r n a r r a y a c c e s s c o n t r o l r u l e s
∗/
p u b l i c f u n c t i o n a c c e s s R u l e s ( ) {
r e t u r n a r r a y (
a r r a y ( ’ a l l o w ’ , // a l l o w admin t o p e r f o r m a l l a c t i o n s
’ a c t i o n s ’=>a r r a y ( ’ i n d e x ’ , ’ view ’ , ’ c r e a t e ’ , ’ u p d a t e ’ , ’ admin ’ , ’ d e l e t e ’ , ’ t o p o l o g y ’ ) ,
’ e x p r e s s i o n ’=>’ Y i i : : app ( )−>u s e r−>isAdmin ( ) ’ , ) ,
a r r a y ( ’ deny ’ , // deny a l l u s e r s
’ u s e r s ’=>a r r a y ( ’∗’ ) , ) ,
) ; }
/∗∗
∗ D i s p l a y s a p a r t i c u l a r model .
∗ @param i n t e g e r $ i d t h e ID o f t h e model t o be d i s p l a y e d
∗/
p u b l i c f u n c t i o n a c t i o n V i e w ( $ i d ) {
$ t h i s−>r e n d e r ( ’ view ’ , a r r a y (
’ model ’=>$ t h i s−>l o a d M o d e l ( $ i d ) , ) ) ;
} /∗∗
∗ C r e a t e s a new model .
∗ I f c r e a t i o n i s s u c c e s s f u l , t h e b r o w s e r w i l l be r e d i r e c t e d t o t h e ’ view ’ p a g e .
∗/
p u b l i c f u n c t i o n a c t i o n C r e a t e ( ) {
$model=new C o u r s e M o d e l ;
// Uncomment t h e f o l l o w i n g l i n e i f AJAX v a l i d a t i o n i s n e e d e d // $ t h i s−>p e r f o r m A j a x V a l i d a t i o n ( $model ) ;
i f ( i s s e t ( $ POST [ ’ CourseModel ’ ] ) ) {
$model−>a t t r i b u t e s=$ POST [ ’ CourseModel ’ ] ; i f ( $model−>s a v e ( ) )
$ t h i s−>r e d i r e c t ( a r r a y ( ’ view ’ , ’ i d ’=>$model−>c o d e ) ) ; }
$ t h i s−>r e n d e r ( ’ c r e a t e ’ , a r r a y (
’ model ’=>$model , ) ) ;
} /∗∗
∗ U p d a t e s a p a r t i c u l a r model .
∗ I f u p d a t e i s s u c c e s s f u l , t h e b r o w s e r w i l l be r e d i r e c t e d t o t h e ’ view ’ p a g e .
∗ @param i n t e g e r $ i d t h e ID o f t h e model t o be u p d a t e d
∗/
p u b l i c f u n c t i o n a c t i o n U p d a t e ( $ i d ) {
$model=$ t h i s−>l o a d M o d e l ( $ i d ) ;
// Uncomment t h e f o l l o w i n g l i n e i f AJAX v a l i d a t i o n i s n e e d e d // $ t h i s−>p e r f o r m A j a x V a l i d a t i o n ( $model ) ;
i f ( i s s e t ( $ POST [ ’ CourseModel ’ ] ) ) {
$model−>a t t r i b u t e s=$ POST [ ’ CourseModel ’ ] ; i f ( $model−>s a v e ( ) )
$ t h i s−>r e d i r e c t ( a r r a y ( ’ view ’ , ’ i d ’=>$model−>c o d e ) ) ; }
$ t h i s−>r e n d e r ( ’ u p d a t e ’ , a r r a y (
’ model ’=>$model , ) ) ;
} /∗∗
∗ D e l e t e s a p a r t i c u l a r model .
∗ I f d e l e t i o n i s s u c c e s s f u l , t h e b r o w s e r w i l l be r e d i r e c t e d t o t h e ’ admin ’ p a g e .
∗ @param i n t e g e r $ i d t h e ID o f t h e model t o be d e l e t e d
∗/
p u b l i c f u n c t i o n a c t i o n D e l e t e ( $ i d ) {
$model = $ t h i s−>l o a d M o d e l ( $ i d ) ;
$ c l a s s = C l a s s M o d e l : : model ( )−>f i n d A l l B y A t t r i b u t e s ( a r r a y ( ’ c o u r s e c o d e ’=>$model
−>i d ) ) ;
i f ( c o u n t ( $ c l a s s ) == 0 ) {
$ p a t h = Y i i : : app ( )−>b a s e P a t h . ’\v i e w s\c l a s s\ \’ ; i f ( i s d i r ( $ p a t h . $ i d ) )
e x e c ( ’ r d / s / q ’ . $ p a t h . $ i d , $ r e t u r n , $ s t a t u s ) ; i f ( f i l e e x i s t s ( $ p a t h . $ i d . ” . php ” ) )
u n l i n k ( $ p a t h . $ i d . ” . php ” ) ;
$model−>d e l e t e ( ) ; }
// i f AJAX r e q u e s t ( t r i g g e r e d by d e l e t i o n v i a admin g r i d v i e w ) , we s h o u l d n o t r e d i r e c t t h e b r o w s e r
i f ( ! i s s e t ( $ GET [ ’ a j a x ’ ] ) )
$ t h i s−>r e d i r e c t ( i s s e t ( $ POST [ ’ r e t u r n U r l ’ ] ) ? $ POST [ ’ r e t u r n U r l ’ ] : a r r a y ( ’ admin ’ ) ) ;
} /∗∗
∗ L i s t s a l l m o d e l s .
∗/
p u b l i c f u n c t i o n a c t i o n I n d e x ( ) {
$ d a t a P r o v i d e r=new C A c t i v e D a t a P r o v i d e r ( ’ CourseModel ’ ) ;
$ t h i s−>r e n d e r ( ’ i n d e x ’ , a r r a y (
’ d a t a P r o v i d e r ’=>$ d a t a P r o v i d e r , ) ) ;
} /∗∗
∗ Manages a l l m o d e l s .
∗/
p u b l i c f u n c t i o n a c t i o n A d m i n ( ) {
$model=new C o u r s e M o d e l ( ’ s e a r c h ’ ) ;
$model−>u n s e t A t t r i b u t e s ( ) ; // c l e a r any d e f a u l t v a l u e s i f ( i s s e t ( $ GET [ ’ CourseModel ’ ] ) )
$model−>a t t r i b u t e s=$ GET [ ’ CourseModel ’ ] ;
$ t h i s−>r e n d e r ( ’ admin ’ , a r r a y (
’ model ’=>$model , ) ) ;
} /∗∗
∗ R e t u r n s t h e d a t a model b a s e d on t h e p r i m a r y k e y g i v e n i n t h e GET v a r i a b l e .
∗ I f t h e d a t a model i s n o t f o u n d , an HTTP e x c e p t i o n w i l l be r a i s e d .
∗ @param i n t e g e r $ i d t h e ID o f t h e model t o be l o a d e d
∗ @ r e t u r n C o u r s e M o d e l t h e l o a d e d model
∗ @throws C H t t p E x c e p t i o n
∗/
p u b l i c f u n c t i o n l o a d M o d e l ( $ i d ) {
$model=C o u r s e M o d e l : : model ( )−>f i n d B y P k ( $ i d ) ; i f ( $model===n u l l )
t h r o w new C H t t p E x c e p t i o n ( 4 0 4 , ’ The r e q u e s t e d p a g e d o e s n o t e x i s t . ’ ) ; r e t u r n $model ;
} /∗∗
∗ P e r f o r m s t h e AJAX v a l i d a t i o n .
∗ @param C o u r s e M o d e l $model t h e model t o be v a l i d a t e d
∗/
p r o t e c t e d f u n c t i o n p e r f o r m A j a x V a l i d a t i o n ( $model ) {
i f ( i s s e t ( $ POST [ ’ a j a x ’ ] ) && $ POST [ ’ a j a x ’]=== ’ c o u r s e−model−form ’ ) {
e c h o CActiveForm : : v a l i d a t e ( $model ) ; Y i i : : app ( )−>end ( ) ;
} }
/∗∗
∗ B u i l d s T o p o l o g y .
∗ @param s t r i n g $ i d t h e c o d e o f t h e c o u r s e o f t h e t o p o l o g y
∗/
p u b l i c f u n c t i o n a c t i o n T o p o l o g y ( $ i d ) {
$ c o u r s e = $ t h i s−>l o a d M o d e l ( $ i d ) ; i f ( i s s e t ( $ POST [ ’ TopologyModel ’ ] ) ) {
/∗
e c h o ”<p r e>”;
p r i n t r ( $ POST ) ; e c h o ”</ p r e>”;
d i e (”−−−N o t h i n g e l s e f o l l o w s−−−”) ;
∗/
$ p a t h = Y i i : : app ( )−>b a s e P a t h . ’\v i e w s\c l a s s\ \’ ; i f ( i s d i r ( $ p a t h . $ i d ) )
e x e c ( ’ r m d i r / s / q ’ . $ p a t h . $ i d , $ r e t u r n , $ s t a t u s ) ; mkdir ( $ p a t h . $ i d , 0 7 7 7 ) ;
$ e r r o r s = ’ ’ ;
$ f i l e n a m e = $ p a t h . $ i d . ’ . php ’ ;
$ f i l e h a n d l e = f o p e n ( $ f i l e n a m e , ’w ’ ) ;
$ c o n t e n t s = ’<?php ’ . PHP EOL
. ’ /∗ @var $ t h i s C l a s s C o n t r o l l e r ∗/ ’ . PHP EOL . ’ /∗ @var $model PodModel ∗/ ’ . PHP EOL . ’ ?>’ . PHP EOL . PHP EOL
. ’<l i n k r e l =” s t y l e s h e e t ” t y p e =” t e x t / c s s ” h r e f =”<?php e c h o Y i i : : app ( )−>
r e q u e s t−>b a s e U r l ; ?>/ c s s / t o p o l o g y . c s s ”></ l i n k>’ .PHP EOL . PHP EOL . ’<s c r i p t t y p e =” t e x t / j a v a s c r i p t ” s r c =”<?php e c h o Y i i : : app ( )−>r e q u e s t−>
b a s e U r l ; ?>/ j s / j q u e r y−2 . 1 . 1 . j s ”></ s c r i p t>’ .PHP EOL
. ’<s c r i p t t y p e =” t e x t / j a v a s c r i p t ” s r c =”<?php e c h o Y i i : : app ( )−>r e q u e s t−>
b a s e U r l ; ?>/ j s / r a p h a e l . j s ”></ s c r i p t>’ .PHP EOL
. ’<s c r i p t t y p e =” t e x t / j a v a s c r i p t ” s r c =”<?php e c h o Y i i : : app ( )−>r e q u e s t−>
b a s e U r l ; ?>/ j s / main . j s ”></ s c r i p t>’ .PHP EOL
. ’<s c r i p t t y p e =” t e x t / j a v a s c r i p t ” s r c =”<?php e c h o Y i i : : app ( )−>r e q u e s t−>
b a s e U r l ; ?>/ j s / pod . j s ”></ s c r i p t>’ .PHP EOL
. ’<s c r i p t t y p e =” t e x t / j a v a s c r i p t ” s r c =”<?php e c h o Y i i : : app ( )−>r e q u e s t−>
b a s e U r l ; ?>/ j s / c a b l e . j s ”></ s c r i p t>’ .PHP EOL
. ’<s c r i p t t y p e =” t e x t / j a v a s c r i p t ” s r c =”<?php e c h o Y i i : : app ( )−>r e q u e s t−>
b a s e U r l ; ?>/ j s / g r a y s c a l e . j s ”></ s c r i p t>’ .PHP EOL . ’<s c r i p t t y p e =” t e x t / j a v a s c r i p t ”>’ .PHP EOL
. ’ window . a d d E v e n t L i s t e n e r ( ” l o a d ” , f u n c t i o n ( ) {’ . PHP EOL
. ’ d r a w S t a g e ( ) ; ’ . PHP EOL
. ’ c o n f i g u r e ( ) ; ’ . PHP EOL
. ’ }, f a l s e ) ; ’ . PHP EOL
. ’ $ ( window ) . r e s i z e ( f u n c t i o n ( ) {’ . PHP EOL
. ’ c o n f i g u r e ( ) ; ’ . PHP EOL
. ’ }) ’ . PHP EOL
. ’ i f (<?php e c h o Y i i : : app ( )−>u s e r−>i s P r o f ( ) ? \’ t r u e\’ : \’ f a l s e
\’ ; ?>) ’ . PHP EOL
. ’ window . o n b e f o r e u n l o a d = f u n c t i o n ( ) {’ . PHP EOL
. ’ r e t u r n ” E v e r y t h i n g w i l l be l o s t i f you don\’ t
s a v e y o u r work . ” ’ . PHP EOL
. ’ }’ . PHP EOL
. ’ v a r u r l = ”<?php e c h o Y i i : : app ( )−>g e t B a s e U r l ( t r u e ) ; ?>” , ’ . PHP EOL
. ’ c o d e = ” ’ . $ i d . ’ ” , ’ . PHP EOL
. ’ name = ” ’ . $ c o u r s e−>name . ’ ” ; ’ . PHP EOL . ’</ s c r i p t>’ .PHP EOL . PHP EOL
. ’<d i v i d =”main−d i v ”>’ .PHP EOL
. ’ <?php ’ . PHP EOL
. ’ i f ( Y i i : : app ( )−>u s e r−>i s P r o f ( ) ) {’ . PHP EOL
. ’ $ f o r m=$ t h i s−>b e g i n W i d g e t (\’ CActiveForm\’ ,
a r r a y ( ’ . PHP EOL
. ’ \’ i d\’=>\’ pod−model−f o r m\’ , ’ . PHP EOL
. ’ \’ e n a b l e A j a x V a l i d a t i o n\’=>f a l s e ’ .
PHP EOL
. ’ ) ) ; ’ . PHP EOL
. ’ }’ . PHP EOL
. ’ ?>’ .PHP EOL . PHP EOL
. ’ <d i v i d =” t o p o l o g y−d i v ”>’ .PHP EOL . PHP EOL
. ’ <?php i f ( Y i i : : app ( )−>u s e r−>i s P r o f ( ) ) {’ . PHP EOL
. ’ e c h o \’<d i v i d =” c o n t r o l−d i v ”>\’ ; ’ . PHP EOL . PHP EOL . ’ e c h o \’<h1>C l a s s \’ . $model−>c l a s s i d .\’</ h4
>\’ ; ’ . PHP EOL . PHP EOL
. ’ e c h o \’<h4>Pod \’ . $model−>pod num .\’</ h4>\’ ; ’ . PHP EOL . PHP EOL
. ’ e c h o $form−>h i d d e n F i e l d ( $model , \’ i d\’ ) ; ’ .
PHP EOL . PHP EOL
. ’ e c h o $form−>h i d d e n F i e l d ( $model , \’ pod num\’ )
; ’ . PHP EOL . PHP EOL
. ’ e c h o $form−>h i d d e n F i e l d ( $model , \’ c l a s s i d\’ )
; ’ . PHP EOL . PHP EOL
. ’ e c h o $form−>h i d d e n F i e l d ( $model , \’ p a s s\’ ) ; ’ . PHP EOL . PHP EOL
. ’ e c h o CHtml : : s u b m i t B u t t o n (\’ S a v e\’ , a r r a y ( ’ . PHP EOL
. ’ \’ o n c l i c k\’=>\’ r e t u r n s a v e ( e v e n t )\’ ’ .
PHP EOL
. ’ ) ) ; ’ . PHP EOL . PHP EOL
. ’ e c h o CHtml : : b u t t o n (\’ Back\’ , a r r a y ( ’ . PHP EOL
. ’ \’ o n c l i c k\’=>\’ window . l o c a t i o n . a s s i g n
( ”\’ . Y i i : : app ( )−>c r e a t e U r l (\’ / pod / admin\’ , a r r a y (\’ c l a s s i d\’=>
$model−>c l a s s−>i d ) ) .\’ ” )\’ ’ . PHP EOL
. ’ ) ) ; ’ . PHP EOL
. ’ e c h o \’</ d i v>\’ ; ’ . PHP EOL
. ’ } e l s e {’ . PHP EOL
. ’ e c h o \’<img s r c = ” ’ . Y i i : : app ( )−>r e q u e s t−>b a s e U r l . ’ / i m a g e s / c l o s e . png ” i d =” c l o s e−b u t t o n ” w i d t h =”20px ” h e i g h t =”20px ”
/>\’ ; ’ . PHP EOL
. ’ } ?>’ .PHP EOL . PHP EOL
. ’ <d i v i d =”c a n v a s−d i v ”>’ .PHP EOL
. ’ <s v g i d =” t o p o l o g y−c a n v a s ”>’ .PHP EOL ;
e c h o ”<h r />”;
i f ( i s s e t ( $ POST [ ’ l i n e ’ ] ) )
f o r e a c h ( $ POST [ ’ l i n e ’ ] a s $ l i n e ) {
$ c a b l e = ! empty ( $ l i n e [ ’ i d ’ ] ) ?
C a b l e M o d e l : : model ( )−>f i n d B y P k ( $ l i n e [ ’ i d ’ ] ) : new C a b l e M o d e l ;
$ c a b l e−>a t t r i b u t e s = $ l i n e ;
$ c a b l e−>c o u r s e c o d e = $ i d ; i f ( ! $ c a b l e−>s a v e ( ) )
$ e r r o r s .= CHtml : : errorSummary ( $ c a b l e ) .”<h r />”;
l i s t ( $ r a x , $ r a y , $raw , $ r a h ) = e x p l o d e ( ” , ” , $ l i n e [ ’ r e c t a ’ ] ) ;
l i s t ( $rbx , $rby , $rbw , $ r b h ) = e x p l o d e ( ” , ” , $ l i n e [ ’ r e c t b ’ ] ) ;
// $ r e c t A = e x p l o d e ( ” , ” , $ l i n e [ ’ r e c t a ’ ] ) ; // $ r e c t B = e x p l o d e ( ” , ” , $ l i n e [ ’ r e c t b ’ ] ) ; i f ( $ c a b l e−>t y p e < 2 ) {
$d = s t r r e p l a c e ( ” , ” , ” ” , $ c a b l e−>p a t h ) ;
$ p t s = e x p l o d e ( ” ” , $d ) ;
$ c o n t e n t s . = ’ <p a t h
d = ” ’ . $d . ’ ” f i l l =”none ” s t r o k e = ” ’ . $ c a b l e−>
c o l o r . ’ ” s t r o k e−w i d t h =”3” />’ .PHP EOL ; i f ( ! empty ( $ c a b l e−>p o r t a ) && $ c a b l e−>p o r t a !=
” p o r t a ” ) {
$ c o n t e n t s .= ’
<r e c t x = ” ’ . $ r a x . ’ ” y = ” ’ .
$ r a y . ’ ” w i d t h = ” ’ . $raw . ’ ” h e i g h t
= ” ’ . $ r a h . ’ ” s t y l e =” f i l l : # f f f ; s t r o k e : #000” />’ .PHP EOL
. ’ <t e x t
x = ” ’ . $ p t s [ 1 ] . ’ ” y = ” ’ . $ p t s [ 2 ] . ’ ” s t y l e =” t e x t−a n c h o r : m i d d l e ; f o n t−
s i z e : 10 px ; f o n t−f a m i l y : A r i a l
; ”>’ .PHP EOL . ’
<t s p a n dy = ” 3 . 5 ”>’ . $ c a b l e−>
p o r t a . ’</ t s p a n>’ .PHP EOL
. ’ </ t e x t
>’ .PHP EOL ; }
i f ( ! empty ( $ c a b l e−>p o r t b ) && $ c a b l e−>p o r t b !=
” p o r t b ” ) {
i f ( $ c a b l e−>t y p e == 0 )
$ c o n t e n t s .= ’
<r e c t x
= ” ’ . $ r b x . ’ ” y = ” ’ . $ r b y . ’ ” w i d t h = ” ’ . $rbw . ’ ” h e i g h t
= ” ’ . $ r b h . ’ ” s t y l e =” f i l l : # f f f ; s t r o k e : #000” />’ . PHP EOL
. ’
<t e x t x = ” ’ . $ p t s [ 4 ] . ’ ” y = ” ’ . $ p t s [ 5 ] . ’ ” s t y l e =” t e x t−a n c h o r : m i d d l e
; f o n t−s i z e : 10 px ; f o n t−
f a m i l y : A r i a l ; ”>’ .PHP EOL . ’
<t s p a n dy
= ” 3 . 5 ”>’ . $ c a b l e−>p o r t b . ’</ t s p a n>’ .PHP EOL . ’
</ t e x t>’ .PHP EOL ;
e l s e
$ c o n t e n t s .= ’
<r e c t x
= ” ’ . $ r b x . ’ ” y = ” ’ . $ r b y . ’ ” w i d t h = ” ’ . $rbw . ’ ” h e i g h t
= ” ’ . $ r b h . ’ ” s t y l e =” f i l l : # f f f ; s t r o k e : #000” />’ . PHP EOL
. ’
<t e x t x = ” ’ . $ p t s [ 8 ] . ’ ” y = ” ’ . $ p t s [ 9 ] . ’ ” s t y l e =” t e x t−a n c h o r : m i d d l e
; f o n t−s i z e : 10 px ; f o n t−
f a m i l y : A r i a l ; ”>’ .PHP EOL . ’
<t s p a n dy
= ” 3 . 5 ”>’ . $ c a b l e−>p o r t b . ’</ t s p a n>’ .PHP EOL . ’
</ t e x t>’ .PHP EOL ;
} } e l s e {
$dim = e x p l o d e ( ” , ” , $ c a b l e−>p a t h ) ; l i s t ( $cax , $ c a y ) = e x p l o d e ( ” , ” , $ c a b l e−>
p o r t a c o o r ) ;
l i s t ( $cbx , $ c b y ) = e x p l o d e ( ” , ” , $ c a b l e−>
p o r t b c o o r ) ;
// $ c o o r A = e x p l o d e ( ” , ” , $ c a b l e−>p o r t a c o o r ) ; // $ c o o r B = e x p l o d e ( ” , ” , $ c a b l e−>p o r t b c o o r ) ;
$ i m g f i l e = Y i i : : app ( )−>b a s e P a t h . ’ / d e v i c e s / ’ .
$ c a b l e−>d e v i c e M o d e l−>f i l e ; i f ( f i l e e x i s t s ( $ i m g f i l e ) )
$ i m g s r c = Y i i : : app ( )−>a s s e t M a n a g e r−>
p u b l i s h ( $ i m g f i l e ) ;
$ c o n t e n t s . = ’ <i m a g e
x = ” ’ . $dim [ 0 ] . ’ ” y = ” ’ . $dim [ 1 ] . ’ ” w i d t h = ” ’ .
$dim [ 2 ] . ’ ” h e i g h t = ” ’ . $dim [ 3 ] . ’ ”
p r e s e r v e A s p e c t R a t i o =”none ” x l i n k : h r e f = ” ’ .
$ i m g s r c . ’ ” t r a n s f o r m =” r o t a t e ( ’ . $ c a b l e−>
t r a n s f o r m . ’ ’ . ( $dim [ 0 ] + $dim [ 2 ] / 2 ) . ’ , ’ . ( $dim [ 1 ] + $dim [ 3 ] / 2 ) . ’ ) ” />’ . PHP EOL ;
i f ( ! empty ( $ c a b l e−>p o r t a ) && $ c a b l e−>p o r t a !=
” p o r t a ” ) {
$ c o n t e n t s .= ’
<r e c t x = ” ’ . $ r a x . ’ ” y = ” ’ .
$ r a y . ’ ” w i d t h = ” ’ . $raw . ’ ” h e i g h t
= ” ’ . $ r a h . ’ ” s t y l e =” f i l l : # f f f ; s t r o k e : #000” />’ .PHP EOL
. ’ <t e x t
x = ” ’ . $ c a x . ’ ” y = ” ’ . $ c a y . ’ ” s t y l e =”
t e x t−a n c h o r : m i d d l e ; f o n t−s i z e : 10 px ; f o n t−f a m i l y : A r i a l ; ”>’ .PHP EOL
. ’
<t s p a n dy = ” 3 . 5 ”>’ . $ c a b l e−>
p o r t a . ’</ t s p a n>’ .PHP EOL
. ’ </ t e x t
>’ .PHP EOL ; }
i f ( ! empty ( $ c a b l e−>p o r t b ) && $ c a b l e−>p o r t b !=
” p o r t b ” ) {
$ c o n t e n t s .= ’
<r e c t x = ” ’ . $ r b x . ’ ” y = ” ’ .
$ r b y . ’ ” w i d t h = ” ’ . $rbw . ’ ” h e i g h t
= ” ’ . $ r b h . ’ ” s t y l e =” f i l l : # f f f ; s t r o k e : #000” />’ .PHP EOL
. ’ <t e x t
x = ” ’ . $ c b x . ’ ” y = ” ’ . $ c b y . ’ ” s t y l e =”
t e x t−a n c h o r : m i d d l e ; f o n t−s i z e : 10 px ; f o n t−f a m i l y : A r i a l ; ”>’ .PHP EOL . ’
<t s p a n dy = ” 3 . 5 ”>’ . $ c a b l e−>
p o r t b . ’</ t s p a n>’ .PHP EOL
. ’ </ t e x t
>’ .PHP EOL ; }
} }
$ c o n t e n t s .= ’ Your b r o w s e r d o e s n o t s u p p o r t
S c a l a b l e V e c t o r G r a p h i c s ’ . PHP EOL
. ’ </s v g>’ .PHP EOL ;
f o r e a c h ( $ POST [ ’ d r a g g a b l e ’ ] a s $ d r a g g a b l e ) { i f ( empty ( $ d r a g g a b l e [ ’ d e v i c e n a m e ’ ] ) ) {
$ t o p o l o g y = T o p o l o g y M o d e l : : model ( )−>f i n d B y P k (
$ d r a g g a b l e [ ’ i d ’ ] ) ; i f ( i s s e t ( $ t o p o l o g y ) )
$ t o p o l o g y−>d e l e t e ( ) ; } e l s e {
$ t o p o l o g y = ! empty ( $ d r a g g a b l e [ ’ i d ’ ] ) ?
T o p o l o g y M o d e l : : model ( )−>f i n d B y P k ( $ d r a g g a b l e [ ’ i d ’ ] ) :
new T o p o l o g y M o d e l ;
$ t o p o l o g y−>c o u r s e c o d e = $ i d ;
$ t o p o l o g y−>a t t r i b u t e s = $ d r a g g a b l e ;
$ t o p o l o g y−>h o s t = $ t o p o l o g y−>h o s t == ” h o s t name” ?
$ t o p o l o g y−>h o s t :
s t r t o u p p e r ( $ t o p o l o g y−>h o s t ) ;
$ t o p o l o g y−>d i s p l a y = $ t o p o l o g y−>d i s p l a y == ” d i s p l a y name” ?
( $ t o p o l o g y−>t y p e == 1 ? ”TS” : $ t o p o l o g y−>
d i s p l a y ) :
s t r t o u p p e r ( $ t o p o l o g y−>d i s p l a y ) ; i f ( ! i s s e t ( $ t o p o l o g y−>d e v i c e n a m e ) )
T o p o l o g y M o d e l : : model ( )−>f i n d B y P k ( $ t o p o l o g y−>i d )−>d e l e t e ( ) ;
e l s e i f ( ! $ t o p o l o g y−>s a v e ( ) )
$ e r r o r s .= CHtml : : errorSummary ( $ t o p o l o g y ) .”<h r />”;
i f ( $ t o p o l o g y−>t y p e != 1 ) {
$ c o n f i g h a n d l e = f o p e n ( $ p a t h . $ i d . ’ / ’ . $ t o p o l o g y
−>d i s p l a y . ’ . t x t ’ , ’w ’ ) ;
f w r i t e ( $ c o n f i g h a n d l e , ’ c o n n e c t ’ . $ t o p o l o g y−>
h o s t ) ;
f c l o s e ( $ c o n f i g h a n d l e ) ; }
$ f i l e = Y i i : : app ( )−>b a s e P a t h . ’ / d e v i c e s / ’ . $ t o p o l o g y−>
d e v i c e M o d e l−>f i l e ; i f ( f i l e e x i s t s ( $ f i l e ) )
$ s r c = Y i i : : app ( )−>a s s e t M a n a g e r−>p u b l i s h ( $ f i l e ) ;
$ c o n t e n t s .= ’ <?php i f ( Y i i : : app ( )−>
u s e r−>i s P r o f ( ) ) { ?>’ .PHP EOL
. ’ <d i v i d = ” ’ . $ t o p o l o g y−>
i d . ’ ” o n C l i c k =” t o g g l e S e l e c t e d ( ’ . $ t o p o l o g y−>i d . ’ ,<?
php e c h o $model−>i d ; ?>) ” s t y l e =”t o p : ’ . $ t o p o l o g y
−>y . ’ px ; l e f t : ’ . $ t o p o l o g y−>x . ’ px ; p o s i t i o n :
a b s o l u t e ”>’ .PHP EOL
. ’ <?php ’ . PHP EOL
. ’ } e l s e {’ . PHP EOL
. ’ $ p o d D e v i c e =
P o d D e v i c e M o d e l : : model ( )−>f i n d B y A t t r i b u t e s ( a r r a y ( ’ . PHP EOL
. ’ \’
p o d i d\’=>$model−>i d , ’ . PHP EOL
. ’ \’
t o p o l o g y i d\’=>’. $ t o p o l o g y−>i d . PHP EOL
. ’ ) ) ; ’ . PHP EOL
. ’ ?>’ .PHP EOL
. ’ <d i v <?php e c h o i s s e t (
$ p o d D e v i c e ) ? \’ o n d b l c l i c k =” c o n n e c t (\ \ \’ ’ .
$ t o p o l o g y−>i d . ’\ \ \’ ) ”\’ : \’\’ ; ?> s t y l e =”t o p : ’ .
$ t o p o l o g y−>y . ’ px ; l e f t : ’ . $ t o p o l o g y−>x . ’ px ; p o s i t i o n : a b s o l u t e ; ”>’ .PHP EOL
. ’ <?php } ?>’ .PHP EOL
. ’ <u l t y p e =”none
” a l i g n =” c e n t e r ” s t y l e =”m a r g i n : 0 px ; p a d d i n g : 0 px
”>’ .PHP EOL
. ’ <l i><
img s r c = ” ’ . $ s r c . ’ ” <?php e c h o ( ! i s s e t ( $ p o d D e v i c e )
&& Y i i : : app ( )−>u s e r−>i s S t d n t ( ) ) ? \’ c l a s s =”
g r a y s c a l e ”\’ : \’\’ ; ?> s t y l e =”w i d t h : ’ . $ t o p o l o g y
−>w i d t h . ’ px ; h e i g h t : ’ . $ t o p o l o g y−>h e i g h t . ’ px ; ”
/>’ .PHP EOL
. ’ <l i>’ .
$ t o p o l o g y−>d i s p l a y . PHP EOL
. ’ </u l>’ .PHP EOL
. ’ </d i v>’ .PHP EOL .
PHP EOL ; }
}
i f ( $ c o u r s e−>h a s t o p o l o g y == ”No ” ) {
$ c o u r s e−>h a s t o p o l o g y = ” Yes ” ;
i f ( ! $ c o u r s e−>v a l i d a t e ( ) | | ! $ c o u r s e−>s a v e ( ) ) {
$ e r r o r s .= CHtml : : errorSummary ( $ c o u r s e ) .”<h r />”;
} }
$ c o n t e n t s .= ’ </d i v>’ .PHP EOL
. ’ <?php ’ . PHP EOL
. ’ i f ( Y i i : : app ( )−>u s e r−>i s P r o f ( ) ) {’ . PHP EOL
. ’ $ t h i s−>endWidget ( ) ; ’ . PHP EOL . PHP EOL
. ’ i f ( $model−>c l a s s−>h a s p o d s ) {’ . PHP EOL
. ’ $ d e v i c e s = P o d D e v i c e M o d e l : :
model ( )−>f i n d A l l B y A t t r i b u t e s ( a r r a y ( ’ . PHP EOL
. ’ \’ p o d i d\’=>$model−>i d
’ . PHP EOL
. ’ ) ) ; ’ . PHP EOL
. ’ f o r e a c h ( $ d e v i c e s a s $ d e v i c e )
{’ . PHP EOL
. ’ e c h o \’<s c r i p t t y p e =”
t e x t / j a v a s c r i p t ”>\’ ’ .PHP EOL
. ’ .\’
t o g g l e S e l e c t e d (\’ . $ d e v i c e−>t o p o l o g y i d .\’ ,\’ . $model−>i d .\’ )\’ ’ . PHP EOL
. ’ .\’</ s c r i p t>\’ ; ’ .
PHP EOL
. ’ }’ . PHP EOL
. ’ }’ . PHP EOL
. ’ }’ . PHP EOL
. ’ ?>’ .PHP EOL
. ’ </d i v>’ .PHP EOL
. ’</ d i v>’ ;
f w r i t e ( $ f i l e h a n d l e , $ c o n t e n t s ) ; f c l o s e ( $ f i l e h a n d l e ) ;
i f ( i s s e t ( $ POST [ ’ d e l e t e d ’ ] ) )
f o r e a c h ( $ POST [ ’ d e l e t e d ’ ] a s $ d e l e t e d ) { i f ( $ d e l e t e d [ ’ t y p e ’ ] == ” d e v i c e ” )
T o p o l o g y M o d e l : : model ( )−>f i n d B y P k ( $ d e l e t e d [ ’ i d
’ ] )−>d e l e t e ( ) ;
e l s e i f ( $ d e l e t e d [ ’ t y p e ’ ] == ” c a b l e ” )
C a b l e M o d e l : : model ( )−>f i n d B y P k ( $ d e l e t e d [ ’ i d ’ ] )
−>d e l e t e ( ) ; }
// d i e ( $ e r r o r s ) ;
$ t h i s−>r e d i r e c t ( a r r a y ( ’ / c o u r s e / admin ’ ) ) ; }
$ t h i s−>r e n d e r ( ’ t o p o l o g y ’ , a r r a y (
’ c o u r s e ’=>$ c o u r s e , ) ) ;
} }
Listing 68: Device Controller
<?php
c l a s s D e v i c e C o n t r o l l e r e x t e n d s C o n t r o l l e r {
/∗∗
∗ @var s t r i n g t h e d e f a u l t l a y o u t f o r t h e v i e w s . D e f a u l t s t o ’ / / l a y o u t s / column2 ’ , meaning
∗ u s i n g two−column l a y o u t . S e e ’ p r o t e c t e d / v i e w s / l a y o u t s / column2 . php ’ .
∗/
p u b l i c $ l a y o u t = ’// l a y o u t s / column2 ’ ; /∗∗
∗ @ r e t u r n a r r a y a c t i o n f i l t e r s
∗/
p u b l i c f u n c t i o n f i l t e r s ( ) {
r e t u r n a r r a y (
’ a c c e s s C o n t r o l ’ , // p e r f o r m a c c e s s c o n t r o l f o r CRUD o p e r a t i o n s
’ p o s t O n l y + d e l e t e ’ , // we o n l y a l l o w d e l e t i o n v i a POST r e q u e s t ) ;
}
/∗∗
∗ S p e c i f i e s t h e a c c e s s c o n t r o l r u l e s .
∗ T h i s method i s u s e d by t h e ’ a c c e s s C o n t r o l ’ f i l t e r .
∗ @ r e t u r n a r r a y a c c e s s c o n t r o l r u l e s
∗/
p u b l i c f u n c t i o n a c c e s s R u l e s ( ) {
r e t u r n a r r a y (
a r r a y ( ’ a l l o w ’ , // a l l o w admin t o p e r f o r m a l l a c t i o n s
’ a c t i o n s ’=>a r r a y ( ’ i n d e x ’ , ’ view ’ , ’ c r e a t e ’ , ’ u p d a t e ’ , ’ admin ’ , ’ d e l e t e ’ ) ,
’ e x p r e s s i o n ’=>’ Y i i : : app ( )−>u s e r−>isAdmin ( ) ’ , ) ,
a r r a y ( ’ deny ’ , // deny a l l u s e r s
’ u s e r s ’=>a r r a y ( ’∗’ ) , ) ,
) ; }
/∗∗
∗ D i s p l a y s a p a r t i c u l a r model .
∗ @param i n t e g e r $ i d t h e ID o f t h e model t o be d i s p l a y e d
∗/
p u b l i c f u n c t i o n a c t i o n V i e w ( $ i d ) {
$ t h i s−>r e n d e r ( ’ view ’ , a r r a y (
’ model ’=>$ t h i s−>l o a d M o d e l ( $ i d ) , ) ) ;
} /∗∗
∗ C r e a t e s a new model .
∗ I f c r e a t i o n i s s u c c e s s f u l , t h e b r o w s e r w i l l be r e d i r e c t e d t o t h e ’ view ’ p a g e .
∗/
p u b l i c f u n c t i o n a c t i o n C r e a t e ( ) {
$model=new D e v i c e M o d e l ;
// Uncomment t h e f o l l o w i n g l i n e i f AJAX v a l i d a t i o n i s n e e d e d // $ t h i s−>p e r f o r m A j a x V a l i d a t i o n ( $model ) ;
i f ( i s s e t ( $ POST [ ’ D e v i c e M o d e l ’ ] ) ) {
$model−>a t t r i b u t e s=$ POST [ ’ D e v i c e M o d e l ’ ] ;
$ f i l e =C U p l o a d e d F i l e : : g e t I n s t a n c e ( $model , ’ f i l e ’ ) ; i f ( i s s e t ( $ f i l e ) && $ f i l e−>e r r o r == 0 ) {
$model−>f i l e =$model−>name . ’ . ’ . $ f i l e−>e x t e n s i o n N a m e ;
$ f i l e−>s a v e A s ( Y i i : : app ( )−>b a s e P a t h . ” / d e v i c e s / ” . $model−>f i l e ) ; }
i f ( $model−>s a v e ( ) )
$ t h i s−>r e d i r e c t ( a r r a y ( ’ view ’ , ’ i d ’=>$model−>name ) ) ; }
$ t h i s−>r e n d e r ( ’ c r e a t e ’ , a r r a y (
’ model ’=>$model , ) ) ;
} /∗∗
∗ U p d a t e s a p a r t i c u l a r model .
∗ I f u p d a t e i s s u c c e s s f u l , t h e b r o w s e r w i l l be r e d i r e c t e d t o t h e ’ view ’ p a g e .
∗ @param i n t e g e r $ i d t h e ID o f t h e model t o be u p d a t e d
∗/
p u b l i c f u n c t i o n a c t i o n U p d a t e ( $ i d ) {
$model=$ t h i s−>l o a d M o d e l ( $ i d ) ;
// Uncomment t h e f o l l o w i n g l i n e i f AJAX v a l i d a t i o n i s n e e d e d // $ t h i s−>p e r f o r m A j a x V a l i d a t i o n ( $model ) ;
i f ( i s s e t ( $ POST [ ’ D e v i c e M o d e l ’ ] ) ) {
$model−>a t t r i b u t e s=$ POST [ ’ D e v i c e M o d e l ’ ] ;
$ f i l e = Y i i : : app ( )−>b a s e P a t h . ” / d e v i c e s / ” . $model−>f i l e ; l i s t ( $ f i l e n a m e , $ e x t ) = e x p l o d e ( ” . ” , $model−>f i l e ) ;
$model−>f i l e = $model−>name . ” . ” . $ e x t ;
i f ( rename ( $ f i l e , Y i i : : app ( )−>b a s e P a t h . ” / d e v i c e s / ” . $model−>f i l e ) ) i f ( $model−>s a v e ( ) )
$ t h i s−>r e d i r e c t ( a r r a y ( ’ view ’ , ’ i d ’=>$model−>name ) ) ; }
$ t h i s−>r e n d e r ( ’ u p d a t e ’ , a r r a y (
’ model ’=>$model , ) ) ;
} /∗∗
∗ D e l e t e s a p a r t i c u l a r model .
∗ I f d e l e t i o n i s s u c c e s s f u l , t h e b r o w s e r w i l l be r e d i r e c t e d t o t h e ’ admin ’ p a g e .
∗ @param i n t e g e r $ i d t h e ID o f t h e model t o be d e l e t e d
∗/
p u b l i c f u n c t i o n a c t i o n D e l e t e ( $ i d ) {
$model = $ t h i s−>l o a d M o d e l ( $ i d ) ;
$ f i l e = Y i i : : app ( )−>b a s e P a t h . ” / d e v i c e s / ” . $model−>f i l e ; i f ( f i l e e x i s t s ( $ f i l e ) )
u n l i n k ( $ f i l e ) ;
$model−>d e l e t e ( ) ;
// i f AJAX r e q u e s t ( t r i g g e r e d by d e l e t i o n v i a admin g r i d v i e w ) , we s h o u l d n o t r e d i r e c t t h e b r o w s e r
i f ( ! i s s e t ( $ GET [ ’ a j a x ’ ] ) )
$ t h i s−>r e d i r e c t ( i s s e t ( $ POST [ ’ r e t u r n U r l ’ ] ) ? $ POST [ ’ r e t u r n U r l ’ ] : a r r a y ( ’ admin ’ ) ) ;
} /∗∗
∗ L i s t s a l l m o d e l s .
∗/
p u b l i c f u n c t i o n a c t i o n I n d e x ( ) {
$ d a t a P r o v i d e r=new C A c t i v e D a t a P r o v i d e r ( ’ D e v i c e M o d e l ’ ) ;
$ t h i s−>r e n d e r ( ’ i n d e x ’ , a r r a y (
’ d a t a P r o v i d e r ’=>$ d a t a P r o v i d e r , ) ) ;
} /∗∗
∗ Manages a l l m o d e l s .
∗/
p u b l i c f u n c t i o n a c t i o n A d m i n ( ) {
$model=new D e v i c e M o d e l ( ’ s e a r c h ’ ) ;
$model−>u n s e t A t t r i b u t e s ( ) ; // c l e a r any d e f a u l t v a l u e s i f ( i s s e t ( $ GET [ ’ D e v i c e M o d e l ’ ] ) )
$model−>a t t r i b u t e s=$ GET [ ’ D e v i c e M o d e l ’ ] ;
$ t h i s−>r e n d e r ( ’ admin ’ , a r r a y (
’ model ’=>$model , ) ) ;
} /∗∗
∗ R e t u r n s t h e d a t a model b a s e d on t h e p r i m a r y k e y g i v e n i n t h e GET v a r i a b l e .
∗ I f t h e d a t a model i s n o t f o u n d , an HTTP e x c e p t i o n w i l l be r a i s e d .
∗ @param i n t e g e r $ i d t h e ID o f t h e model t o be l o a d e d
∗ @ r e t u r n D e v i c e M o d e l t h e l o a d e d model
∗ @throws C H t t p E x c e p t i o n
∗/
p u b l i c f u n c t i o n l o a d M o d e l ( $ i d ) {
$model=D e v i c e M o d e l : : model ( )−>f i n d B y P k ( $ i d ) ; i f ( $model===n u l l )
t h r o w new C H t t p E x c e p t i o n ( 4 0 4 , ’ The r e q u e s t e d p a g e d o e s n o t e x i s t . ’ ) ; r e t u r n $model ;
} /∗∗
∗ P e r f o r m s t h e AJAX v a l i d a t i o n .
∗ @param D e v i c e M o d e l $model t h e model t o be v a l i d a t e d
∗/
p r o t e c t e d f u n c t i o n p e r f o r m A j a x V a l i d a t i o n ( $model ) {
i f ( i s s e t ( $ POST [ ’ a j a x ’ ] ) && $ POST [ ’ a j a x ’]=== ’ d e v i c e−model−form ’ ) {
e c h o CActiveForm : : v a l i d a t e ( $model ) ; Y i i : : app ( )−>end ( ) ;
} }
}
Listing 69: Pod Controller
<?php
c l a s s P o d C o n t r o l l e r e x t e n d s C o n t r o l l e r {
/∗∗
∗ @var s t r i n g t h e d e f a u l t l a y o u t f o r t h e v i e w s . D e f a u l t s t o ’ / / l a y o u t s / column2 ’ , meaning
∗ u s i n g two−column l a y o u t . S e e ’ p r o t e c t e d / v i e w s / l a y o u t s / column2 . php ’ .
∗/
p u b l i c $ l a y o u t = ’// l a y o u t s / column2 ’ ; /∗∗
∗ @ r e t u r n a r r a y a c t i o n f i l t e r s
∗/
p u b l i c f u n c t i o n f i l t e r s ( ) {
r e t u r n a r r a y (
’ a c c e s s C o n t r o l ’ , // p e r f o r m a c c e s s c o n t r o l f o r CRUD o p e r a t i o n s
’ p o s t O n l y + d e l e t e ’ , // we o n l y a l l o w d e l e t i o n v i a POST r e q u e s t ) ;
} /∗∗
∗ S p e c i f i e s t h e a c c e s s c o n t r o l r u l e s .
∗ T h i s method i s u s e d by t h e ’ a c c e s s C o n t r o l ’ f i l t e r .
∗ @ r e t u r n a r r a y a c c e s s c o n t r o l r u l e s
∗/
p u b l i c f u n c t i o n a c c e s s R u l e s ( ) {
r e t u r n a r r a y (
a r r a y ( ’ a l l o w ’ , // a l l o w p r o f t o p e r f o r m a l l a c t i o n s
’ a c t i o n s ’=>a r r a y ( ’ i n d e x ’ , ’ view ’ , ’ c r e a t e ’ , ’ u p d a t e ’ , ’ admin ’ , ’ d e l e t e ’ , ’ pods ’ ) ,
’ e x p r e s s i o n ’=>’ Y i i : : app ( )−>u s e r−>i s P r o f ( ) ’ , ) ,
a r r a y ( ’ deny ’ , // deny a l l u s e r s
’ u s e r s ’=>a r r a y ( ’∗’ ) , ) ,
) ; }
/∗∗
∗ D i s p l a y s a p a r t i c u l a r model .
∗ @param i n t e g e r $ i d t h e ID o f t h e model t o be d i s p l a y e d
∗/
p u b l i c f u n c t i o n a c t i o n V i e w ( $ i d ) {
$ t h i s−>r e n d e r ( ’ view ’ , a r r a y (
’ model ’=>$ t h i s−>l o a d M o d e l ( $ i d ) , ) ) ;
} /∗∗
∗ C r e a t e s a new model .
∗ I f c r e a t i o n i s s u c c e s s f u l , t h e b r o w s e r w i l l be r e d i r e c t e d t o t h e ’ view ’ p a g e .
∗/
p u b l i c f u n c t i o n a c t i o n C r e a t e ( $ c l a s s i d ) {
$model=new PodModel ;
$model−>c l a s s i d = $ c l a s s i d ;
// Uncomment t h e f o l l o w i n g l i n e i f AJAX v a l i d a t i o n i s n e e d e d // $ t h i s−>p e r f o r m A j a x V a l i d a t i o n ( $model ) ;
i f ( i s s e t ( $ POST [ ’ PodModel ’ ] ) ) {
$model−>a t t r i b u t e s=$ POST [ ’ PodModel ’ ] ; i f ( $model−>s a v e ( ) )
$ t h i s−>r e d i r e c t ( a r r a y ( ’ view ’ , ’ i d ’=>$model−>i d ) ) ; }
$ t h i s−>r e n d e r ( ’ c r e a t e ’ , a r r a y (
’ model ’=>$model , ) ) ;
} /∗∗
∗ U p d a t e s a p a r t i c u l a r model .
∗ I f u p d a t e i s s u c c e s s f u l , t h e b r o w s e r w i l l be r e d i r e c t e d t o t h e ’ view ’ p a g e .
∗ @param i n t e g e r $ i d t h e ID o f t h e model t o be u p d a t e d
∗/
p u b l i c f u n c t i o n a c t i o n U p d a t e ( $ i d ) {
$model=$ t h i s−>l o a d M o d e l ( $ i d ) ;
// Uncomment t h e f o l l o w i n g l i n e i f AJAX v a l i d a t i o n i s n e e d e d // $ t h i s−>p e r f o r m A j a x V a l i d a t i o n ( $model ) ;
i f ( i s s e t ( $ POST [ ’ PodModel ’ ] ) ) {
$model−>a t t r i b u t e s=$ POST [ ’ PodModel ’ ] ; i f ( $model−>s a v e ( ) )
$ t h i s−>r e d i r e c t ( a r r a y ( ’ view ’ , ’ i d ’=>$model−>i d ) ) ; }
$ t h i s−>r e n d e r ( ’ u p d a t e ’ , a r r a y (
’ model ’=>$model , ) ) ;
} /∗∗
∗ D e l e t e s a p a r t i c u l a r model .
∗ I f d e l e t i o n i s s u c c e s s f u l , t h e b r o w s e r w i l l be r e d i r e c t e d t o t h e ’ admin ’ p a g e .
∗ @param i n t e g e r $ i d t h e ID o f t h e model t o be d e l e t e d
∗/
p u b l i c f u n c t i o n a c t i o n D e l e t e ( $ i d ) {
$ t h i s−>l o a d M o d e l ( $ i d )−>d e l e t e ( ) ;
// i f AJAX r e q u e s t ( t r i g g e r e d by d e l e t i o n v i a admin g r i d v i e w ) , we s h o u l d n o t r e d i r e c t t h e b r o w s e r
i f ( ! i s s e t ( $ GET [ ’ a j a x ’ ] ) )
$ t h i s−>r e d i r e c t ( i s s e t ( $ POST [ ’ r e t u r n U r l ’ ] ) ? $ POST [ ’ r e t u r n U r l ’ ] : a r r a y ( ’ admin ’ ) ) ;
} /∗∗
∗ L i s t s a l l m o d e l s .
∗/
p u b l i c f u n c t i o n a c t i o n I n d e x ( $ c l a s s i d ) {
$model=new PodModel ( ’ s e a r c h ’ ) ;
$model−>u n s e t A t t r i b u t e s ( ) ; // c l e a r any d e f a u l t v a l u e s i f ( i s s e t ( $ GET [ ’ PodModel ’ ] ) )
$model−>a t t r i b u t e s=$ GET [ ’ PodModel ’ ] ;
$ t h i s−>r e n d e r ( ’ i n d e x ’ , a r r a y (
’ model ’=>$model ,
’ c l a s s i d ’=>$ c l a s s i d ,
) ) ; }
/∗∗
∗ Manages a l l m o d e l s .
∗/
p u b l i c f u n c t i o n a c t i o n A d m i n ( $ c l a s s i d ) {
$model=new PodModel ( ’ s e a r c h ’ ) ;
$model−>u n s e t A t t r i b u t e s ( ) ; // c l e a r any d e f a u l t v a l u e s i f ( i s s e t ( $ GET [ ’ PodModel ’ ] ) )
$model−>a t t r i b u t e s=$ GET [ ’ PodModel ’ ] ;
$ t h i s−>r e n d e r ( ’ admin ’ , a r r a y (
’ model ’=>$model ,
’ c l a s s i d ’=>$ c l a s s i d , ) ) ;
} /∗∗
∗ R e t u r n s t h e d a t a model b a s e d on t h e p r i m a r y k e y g i v e n i n t h e GET v a r i a b l e .
∗ I f t h e d a t a model i s n o t f o u n d , an HTTP e x c e p t i o n w i l l be r a i s e d .
∗ @param i n t e g e r $ i d t h e ID o f t h e model t o be l o a d e d
∗ @ r e t u r n PodModel t h e l o a d e d model
∗ @throws C H t t p E x c e p t i o n
∗/
p u b l i c f u n c t i o n l o a d M o d e l ( $ i d ) {
$model=PodModel : : model ( )−>f i n d B y P k ( $ i d ) ; i f ( $model===n u l l )
t h r o w new C H t t p E x c e p t i o n ( 4 0 4 , ’ The r e q u e s t e d p a g e d o e s n o t e x i s t . ’ ) ; r e t u r n $model ;
} /∗∗
∗ P e r f o r m s t h e AJAX v a l i d a t i o n .
∗ @param PodModel $model t h e model t o be v a l i d a t e d
∗/
p r o t e c t e d f u n c t i o n p e r f o r m A j a x V a l i d a t i o n ( $model ) {
i f ( i s s e t ( $ POST [ ’ a j a x ’ ] ) && $ POST [ ’ a j a x ’]=== ’ pod−model−form ’ ) {
e c h o CActiveForm : : v a l i d a t e ( $model ) ; Y i i : : app ( )−>end ( ) ;
} }
/∗∗
∗ L e t s t h e P r o f e s s o r s e l e c t p o d s .
∗ @param i n t e g e r $ i d t h e pod i d
∗/
p u b l i c f u n c t i o n a c t i o n P o d s ( $ i d ) {
$model = PodModel : : model ( )−>f i n d B y P k ( $ i d ) ; i f ( i s s e t ( $ POST [ ’ PodModel ’ ] ) ) {
$ e r r o r s = ’ ’ ;
f o r e a c h ( $ POST [ ’ pods ’ ] a s $pod ) {
$ d e v i c e = P o d D e v i c e M o d e l : : model ( )−>f i n d B y A t t r i b u t e s ( a r r a y (
’ t o p o l o g y i d ’=>$pod [ ’ t o p o l o g y i d ’ ] ,
’ p o d i d ’=>$pod [ ’ p o d i d ’ ] ) ) ;
i f ( ! i s s e t ( $ d e v i c e ) ) {
$ d e v i c e = new P o d D e v i c e M o d e l ;
$ d e v i c e−>a t t r i b u t e s = $pod ; i f ( ! $ d e v i c e−>s a v e ( ) )
$ e r r o r s .= CHtml : : errorSummary ( $ d e v i c e ) .”<h r />”;
} }
i f ( $model−>c l a s s−>h a s p o d s == ”No ” ) {
$ c l a s s = C l a s s M o d e l : : model ( )−>f i n d B y P k ( $model−>c l a s s i d ) ;
$ c l a s s−>h a s p o d s = ” Yes ” ; i f ( ! $ c l a s s−>s a v e ( ) )
$ e r r o r s .= CHtml : : errorSummary ( $ c l a s s ) .”<h r />”;
}
i f ( i s s e t ( $ POST [ ’ d e l e t e d ’ ] ) )
f o r e a c h ( $ POST [ ’ d e l e t e d ’ ] a s $ d e l e t e d ) {
$ d e l e t e d P o d D e v i c e = P o d D e v i c e M o d e l : : model ( )−>
f i n d B y A t t r i b u t e s ( a r r a y (
’ t o p o l o g y i d ’=>$ d e l e t e d [ ’ t o p o l o g y i d ’ ] ,
’ p o d i d ’=>$ d e l e t e d [ ’ p o d i d ’ ] ) ) ;
i f ( i s s e t ( $ d e l e t e d P o d D e v i c e ) )
$ d e l e t e d P o d D e v i c e−>d e l e t e ( ) ; }
// d i e ( $ e r r o r s ) ;
$ t h i s−>r e d i r e c t ( a r r a y ( ’ / pod / admin ’ , ’ c l a s s i d ’=>$model−>c l a s s−>i d ) ) ; }
i f ( $model−>c l a s s−>c o u r s e−>h a s t o p o l o g y === ” Yes ” ) {
$ t h i s−>r e n d e r ( ’ / c l a s s / ’ . $model−>c l a s s−>c o u r s e c o d e , a r r a y (
’ model ’=>$model , ) ) ;
} e l s e {
Y i i : : app ( )−>u s e r−>s e t F l a s h ( ’ e r r o r ’ , ’ T h i s c o u r s e h a s no t o p o l o g y y e t . P l e a s e c o n t a c t y o u r s y s t e m a d m i n i s t r a t o r . ’ ) ;
C C o n t r o l l e r : : r e d i r e c t ( a r r a y ( ’ / pod / admin ’ , ’ c l a s s i d ’ => $model−>
c l a s s i d ) ) ; }
} }
Listing 70: Setup Controller
<?php
c l a s s S e t u p C o n t r o l l e r e x t e n d s C o n t r o l l e r {
p u b l i c f u n c t i o n a c t i o n A d m i n ( ) {
$model = new AdminForm ;
i f ( i s s e t ( $ POST [ ’ AdminForm ’ ] ) ) {
$model−>a t t r i b u t e s = $ POST [ ’ AdminForm ’ ] ; i f ( $model−>v a l i d a t e ( ) ) {
$admin = new U s e r M o d e l ;
$admin−>a t t r i b u t e s = $model−>a t t r i b u t e s ;
$admin−>u s e r t y p e = ’ A d m i n i s t r a t o r ’ ; i f ( $admin−>s a v e ( ) )
$ t h i s−>r e d i r e c t ( a r r a y ( ’ / s i t e / l o g i n ’ ) ) ; }
}
$ t h i s−>r e n d e r ( ’ admin ’ , a r r a y (
’ model ’=>$model , ) ) ;
}
p u b l i c f u n c t i o n a c t i o n D a t a b a s e ( ) {
i f ( ! Y i i : : app ( )−>u s e r−>i s G u e s t ) Y i i : : app ( )−>u s e r−>l o g o u t ( ) ;
$model = new DatabaseForm ;
i f ( i s s e t ( $ POST [ ’ DatabaseForm ’ ] ) ) {
$model−>a t t r i b u t e s = $ POST [ ’ DatabaseForm ’ ] ; i f ( $model−>v a l i d a t e ( ) ) {
$ f i l e n a m e = Y i i : : app ( )−>b a s e P a t h . ’\c o n f i g\c o n f i g . t x t ’ ;
$ f i l e h a n d l e = f o p e n ( $ f i l e n a m e , ’w ’ ) ;
$ c o n f i g = $model−>h o s t . PHP EOL . $model−>u s e r . PHP EOL . $model−>
p a s s ;
f w r i t e ( $ f i l e h a n d l e , $ c o n f i g ) ; f c l o s e ( $ f i l e h a n d l e ) ;
$ f i l e = Y i i : : app ( )−>b a s e P a t h . ’\d a t a\t r e n d s r l a . s q l ’ ;
$commands = e x p l o d e ( ’ ; ’ , f i l e g e t c o n t e n t s ( $ f i l e ) ) ; f o r e a c h ( $commands a s $command ) {
i f ( t r i m ( $command ) !== ’ ’ ) {
Y i i : : app ( )−>db−>createCommand ( $command )−>
e x e c u t e ( ) ; }
}
$ t h i s−>r e d i r e c t ( a r r a y ( ’ / s e t u p / admin ’ ) ) ; }
}
$ t h i s−>r e n d e r ( ’ d a t a b a s e ’ , a r r a y (
’ model ’=>$model , ) ) ;
} }
Listing 71: Site Controller
<?php
c l a s s S i t e C o n t r o l l e r e x t e n d s C o n t r o l l e r {
/∗∗
∗ D e c l a r e s c l a s s−b a s e d a c t i o n s .
∗/
p u b l i c f u n c t i o n a c t i o n s ( ) {
r e t u r n a r r a y (
// c a p t c h a a c t i o n r e n d e r s t h e CAPTCHA i m a g e d i s p l a y e d on t h e c o n t a c t p a g e
’ c a p t c h a ’=>a r r a y (
’ c l a s s ’=>’ CCaptchaAction ’ ,
’ b a c k C o l o r ’=>0xFFFFFF , ) ,
// p a g e a c t i o n r e n d e r s ” s t a t i c ” p a g e s s t o r e d u n d e r ’ p r o t e c t e d / v i e w s / s i t e / p a g e s ’
// They c a n be a c c e s s e d v i a : i n d e x . php ? r= s i t e / p a g e&v i e w=FileName
’ page ’=>a r r a y (
’ c l a s s ’=>’ CViewAction ’ , ) ,
) ; }
/∗∗
∗ T h i s i s t h e d e f a u l t ’ i n d e x ’ a c t i o n t h a t i s i n v o k e d
∗ when an a c t i o n i s n o t e x p l i c i t l y r e q u e s t e d by u s e r s .
∗/
p u b l i c f u n c t i o n a c t i o n I n d e x ( ) {
t r y {
Y i i : : app ( )−>db ; } c a t c h ( CDbException $ e ) {
Y i i : : app ( )−>c o n t r o l l e r−>r e d i r e c t ( a r r a y ( ’ / s e t u p / D a t a b a s e ’ ) ) ; }
$admin = U s e r M o d e l : : model ( )−>f i n d B y A t t r i b u t e s ( a r r a y ( ’ u s e r t y p e ’=>’
A d m i n i s t r a t o r ’ ) ) ; i f ( ! i s s e t ( $admin ) )
Y i i : : app ( )−>c o n t r o l l e r−>r e d i r e c t ( a r r a y ( ’ / s e t u p /Admin ’ ) ) ;
$ c l a s s e s = Y i i : : app ( )−>db−>createCommand ( )
−>s e l e c t ( ”∗” )
−>f r o m ( ” c l a s s ” )
−>q u e r y A l l ( ) ;
$ o n g o i n g = a r r a y ( ) ;
f o r e a c h ( $ c l a s s e s a s $ c l a s s )
i f ( $ c l a s s [ ” exp ” ] < d a t e ( ’ Y−m−d ’ ) )
C l a s s M o d e l : : model ( )−>f i n d B y P k ( $ c l a s s [ ” i d ” ] )−>d e l e t e ( ) ; // c o l l e c t u s e r i n p u t d a t a
i f ( Y i i : : app ( )−>u s e r−>i s G u e s t ) {
$model = new StudentForm ;
i f ( i s s e t ( $ POST [ ’ StudentForm ’ ] ) ) {
$model−>a t t r i b u t e s=$ POST [ ’ StudentForm ’ ] ;
// v a l i d a t e u s e r i n p u t and r e d i r e c t t o t h e p r e v i o u s p a g e i f v a l i d
i f ( $model−>v a l i d a t e ( ) && $model−>l o g i n ( ) )
$ t h i s−>r e d i r e c t ( Y i i : : app ( )−>u s e r−>r e t u r n U r l ) ; }
} e l s e i f ( Y i i : : app ( )−>u s e r−>i s S t d n t ( ) ) {
$model = new PodForm ;
i f ( i s s e t ( $ POST [ ’ PodForm ’ ] ) ) {
$model−>a t t r i b u t e s=$ POST [ ’ PodForm ’ ] ;
$model−>c l a s s I D = Y i i : : app ( )−>u s e r−>g e t S t a t e ( ’ c l a s s ’ ) ; // v a l i d a t e u s e r i n p u t and r e d i r e c t t o t h e p r e v i o u s p a g e i f
v a l i d
i f ( $model−>v a l i d a t e ( ) && $model−>l o g i n ( ) )
Y i i : : app ( )−>c o n t r o l l e r−>r e d i r e c t ( a r r a y ( ’ c l a s s / l o a d C l a s s ’ ,
’ p o d i d ’=>$model−>podNum ) ) ;
} }
// r e n d e r s t h e v i e w f i l e ’ p r o t e c t e d / v i e w s / s i t e / i n d e x . php ’ // u s i n g t h e d e f a u l t l a y o u t ’ p r o t e c t e d / v i e w s / l a y o u t s / main . php ’ i f ( i s s e t ( $model ) )
$ t h i s−>r e n d e r ( ’ i n d e x ’ , a r r a y (
’ model ’=>$model ) ) ;
e l s e
$ t h i s−>r e n d e r ( ’ i n d e x ’ ) ; }
/∗∗
∗ T h i s i s t h e a c t i o n t o h a n d l e e x t e r n a l e x c e p t i o n s .
∗/
p u b l i c f u n c t i o n a c t i o n E r r o r ( ) {
i f ( $ e r r o r=Y i i : : app ( )−>e r r o r H a n d l e r−>e r r o r ) {
i f ( Y i i : : app ( )−>r e q u e s t−>i s A j a x R e q u e s t ) e c h o $ e r r o r [ ’ m e s s a g e ’ ] ; e l s e
$ t h i s−>r e n d e r ( ’ e r r o r ’ , $ e r r o r ) ; }
} /∗∗
∗ D i s p l a y s t h e c o n t a c t p a g e
∗/
p u b l i c f u n c t i o n a c t i o n C o n t a c t ( ) {
$model=new ContactForm ;
i f ( i s s e t ( $ POST [ ’ ContactForm ’ ] ) ) {
$model−>a t t r i b u t e s=$ POST [ ’ ContactForm ’ ] ; i f ( $model−>v a l i d a t e ( ) )
{
$name=’=?UTF−8?B ? ’ . b a s e 6 4 e n c o d e ( $model−>name ) . ’ ? = ’ ;
$ s u b j e c t =’=?UTF−8?B ? ’ . b a s e 6 4 e n c o d e ( $model−>s u b j e c t ) . ’ ? = ’ ;
$ h e a d e r s =”From : $name <{$model−>e m a i l}>\r\n ” .
” Reply−To : {$model−>e m a i l}\r\n ” .
”MIME−V e r s i o n : 1 . 0\r\n ” .
” Content−Type : t e x t / p l a i n ; c h a r s e t=UTF−8”;
m a i l ( Y i i : : app ( )−>params [ ’ adminEmail ’ ] , $ s u b j e c t , $model−>body ,
$ h e a d e r s ) ;
Y i i : : app ( )−>u s e r−>s e t F l a s h ( ’ c o n t a c t ’ , ’ Thank you f o r c o n t a c t i n g u s . We w i l l r e s p o n d t o you a s s o o n a s p o s s i b l e . ’ ) ;
$ t h i s−>r e f r e s h ( ) ; }
}
$ t h i s−>r e n d e r ( ’ c o n t a c t ’ , a r r a y ( ’ model ’=>$model ) ) ; }
/∗∗
∗ D i s p l a y s t h e l o g i n p a g e
∗/
p u b l i c f u n c t i o n a c t i o n L o g i n ( ) {
t r y {
Y i i : : app ( )−>db ; } c a t c h ( CDbException $ e ) {
Y i i : : app ( )−>c o n t r o l l e r−>r e d i r e c t ( a r r a y ( ’ / s e t u p / D a t a b a s e ’ ) ) ; }
$admin = U s e r M o d e l : : model ( )−>f i n d B y A t t r i b u t e s ( a r r a y ( ’ u s e r t y p e ’=>’
A d m i n i s t r a t o r ’ ) ) ; i f ( ! i s s e t ( $admin ) )
Y i i : : app ( )−>c o n t r o l l e r−>r e d i r e c t ( a r r a y ( ’ / s e t u p /Admin ’ ) ) ; i f ( ! Y i i : : app ( )−>u s e r−>i s G u e s t )
$ t h i s−>r e d i r e c t ( a r r a y ( ’ i n d e x ’ ) ) ;
$model=new LoginForm ;
// i f i t i s a j a x v a l i d a t i o n r e q u e s t
i f ( i s s e t ( $ POST [ ’ a j a x ’ ] ) && $ POST [ ’ a j a x ’]=== ’ l o g i n−form ’ ) {
e c h o CActiveForm : : v a l i d a t e ( $model ) ; Y i i : : app ( )−>end ( ) ;
}
// c o l l e c t u s e r i n p u t d a t a i f ( i s s e t ( $ POST [ ’ LoginForm ’ ] ) ) {
$model−>a t t r i b u t e s=$ POST [ ’ LoginForm ’ ] ;
// v a l i d a t e u s e r i n p u t and r e d i r e c t t o t h e p r e v i o u s p a g e i f v a l i d i f ( $model−>v a l i d a t e ( ) && $model−>l o g i n ( ) )
$ t h i s−>r e d i r e c t ( Y i i : : app ( )−>u s e r−>r e t u r n U r l ) ; }
// d i s p l a y t h e l o g i n f o r m
$ t h i s−>r e n d e r ( ’ l o g i n ’ , a r r a y ( ’ model ’=>$model ) ) ; }
/∗∗
∗ L o g s o u t t h e c u r r e n t u s e r and r e d i r e c t t o homepage .
∗/
p u b l i c f u n c t i o n a c t i o n L o g o u t ( ) {
Y i i : : app ( )−>u s e r−>l o g o u t ( ) ;
$ t h i s−>r e d i r e c t ( Y i i : : app ( )−>homeUrl ) ; }
/∗∗
∗ G e n e r a t e s o p t i o n s f o r t h e s t u d e n t f o r m .
∗ @param i n t e g e r $ e m p l o y e e i d t h e Employee ID o f t h e P r o f e s s o r h o l d i n g t h e c l a s s
∗/
p u b l i c f u n c t i o n a c t i o n S u b j e c t s ( $ e m p l o y e e i d ) {
$ c l a s s e s = C l a s s M o d e l : : model ( )−>f i n d A l l B y A t t r i b u t e s ( a r r a y ( ’ p r o f ’=>$ e m p l o y e e i d ) ) ;
$ r e t u r n = ’<o p t i o n v a l u e =”0”>S e l e c t S u b j e c t</o p t i o n>’ ; f o r e a c h ( $ c l a s s e s a s $ c l a s s )
$ r e t u r n .= ”<o p t i o n v a l u e =”. $ c l a s s−>i d . ”>” . $ c l a s s−>c o u r s e c o d e . ” C l a s s
” . $ c l a s s−>i d .”</ o p t i o n>”;
e c h o $ r e t u r n ; }
}
Listing 72: User Controller
<?php
c l a s s S i t e C o n t r o l l e r e x t e n d s C o n t r o l l e r {
/∗∗
∗ D e c l a r e s c l a s s−b a s e d a c t i o n s .
∗/
p u b l i c f u n c t i o n a c t i o n s ( ) {
r e t u r n a r r a y (
// c a p t c h a a c t i o n r e n d e r s t h e CAPTCHA i m a g e d i s p l a y e d on t h e c o n t a c t p a g e
’ c a p t c h a ’=>a r r a y (
’ c l a s s ’=>’ CCaptchaAction ’ ,
’ b a c k C o l o r ’=>0xFFFFFF , ) ,
// p a g e a c t i o n r e n d e r s ” s t a t i c ” p a g e s s t o r e d u n d e r ’ p r o t e c t e d / v i e w s / s i t e / p a g e s ’
// They c a n be a c c e s s e d v i a : i n d e x . php ? r= s i t e / p a g e&v i e w=FileName
’ page ’=>a r r a y (
’ c l a s s ’=>’ CViewAction ’ , ) ,
) ; }
/∗∗
∗ T h i s i s t h e d e f a u l t ’ i n d e x ’ a c t i o n t h a t i s i n v o k e d
∗ when an a c t i o n i s n o t e x p l i c i t l y r e q u e s t e d by u s e r s .
∗/
p u b l i c f u n c t i o n a c t i o n I n d e x ( ) {
t r y {
Y i i : : app ( )−>db ; } c a t c h ( CDbException $ e ) {
Y i i : : app ( )−>c o n t r o l l e r−>r e d i r e c t ( a r r a y ( ’ / s e t u p / D a t a b a s e ’ ) ) ; }
$admin = U s e r M o d e l : : model ( )−>f i n d B y A t t r i b u t e s ( a r r a y ( ’ u s e r t y p e ’=>’
A d m i n i s t r a t o r ’ ) ) ; i f ( ! i s s e t ( $admin ) )
Y i i : : app ( )−>c o n t r o l l e r−>r e d i r e c t ( a r r a y ( ’ / s e t u p /Admin ’ ) ) ;
$ c l a s s e s = Y i i : : app ( )−>db−>createCommand ( )
−>s e l e c t ( ”∗” )
−>f r o m ( ” c l a s s ” )
−>q u e r y A l l ( ) ;
$ o n g o i n g = a r r a y ( ) ;
f o r e a c h ( $ c l a s s e s a s $ c l a s s )
i f ( $ c l a s s [ ” exp ” ] < d a t e ( ’ Y−m−d ’ ) )
C l a s s M o d e l : : model ( )−>f i n d B y P k ( $ c l a s s [ ” i d ” ] )−>d e l e t e ( ) ; // c o l l e c t u s e r i n p u t d a t a
i f ( Y i i : : app ( )−>u s e r−>i s G u e s t ) {
$model = new StudentForm ;
i f ( i s s e t ( $ POST [ ’ StudentForm ’ ] ) ) {
$model−>a t t r i b u t e s=$ POST [ ’ StudentForm ’ ] ;
// v a l i d a t e u s e r i n p u t and r e d i r e c t t o t h e p r e v i o u s p a g e i f v a l i d
i f ( $model−>v a l i d a t e ( ) && $model−>l o g i n ( ) )
$ t h i s−>r e d i r e c t ( Y i i : : app ( )−>u s e r−>r e t u r n U r l ) ; }
} e l s e i f ( Y i i : : app ( )−>u s e r−>i s S t d n t ( ) ) {
$model = new PodForm ;
i f ( i s s e t ( $ POST [ ’ PodForm ’ ] ) ) {
$model−>a t t r i b u t e s=$ POST [ ’ PodForm ’ ] ;
$model−>c l a s s I D = Y i i : : app ( )−>u s e r−>g e t S t a t e ( ’ c l a s s ’ ) ; // v a l i d a t e u s e r i n p u t and r e d i r e c t t o t h e p r e v i o u s p a g e i f
v a l i d
i f ( $model−>v a l i d a t e ( ) && $model−>l o g i n ( ) )
Y i i : : app ( )−>c o n t r o l l e r−>r e d i r e c t ( a r r a y ( ’ c l a s s / l o a d C l a s s ’ ,
’ p o d i d ’=>$model−>podNum ) ) ;
} }
// r e n d e r s t h e v i e w f i l e ’ p r o t e c t e d / v i e w s / s i t e / i n d e x . php ’ // u s i n g t h e d e f a u l t l a y o u t ’ p r o t e c t e d / v i e w s / l a y o u t s / main . php ’ i f ( i s s e t ( $model ) )
$ t h i s−>r e n d e r ( ’ i n d e x ’ , a r r a y (
’ model ’=>$model ) ) ;
e l s e
$ t h i s−>r e n d e r ( ’ i n d e x ’ ) ; }
/∗∗
∗ T h i s i s t h e a c t i o n t o h a n d l e e x t e r n a l e x c e p t i o n s .
∗/
p u b l i c f u n c t i o n a c t i o n E r r o r ( ) {
i f ( $ e r r o r=Y i i : : app ( )−>e r r o r H a n d l e r−>e r r o r ) {
i f ( Y i i : : app ( )−>r e q u e s t−>i s A j a x R e q u e s t ) e c h o $ e r r o r [ ’ m e s s a g e ’ ] ; e l s e
$ t h i s−>r e n d e r ( ’ e r r o r ’ , $ e r r o r ) ; }
} /∗∗
∗ D i s p l a y s t h e c o n t a c t p a g e
∗/
p u b l i c f u n c t i o n a c t i o n C o n t a c t ( ) {
$model=new ContactForm ;
i f ( i s s e t ( $ POST [ ’ ContactForm ’ ] ) ) {
$model−>a t t r i b u t e s=$ POST [ ’ ContactForm ’ ] ; i f ( $model−>v a l i d a t e ( ) )
{
$name=’=?UTF−8?B ? ’ . b a s e 6 4 e n c o d e ( $model−>name ) . ’ ? = ’ ;
$ s u b j e c t =’=?UTF−8?B ? ’ . b a s e 6 4 e n c o d e ( $model−>s u b j e c t ) . ’ ? = ’ ;
$ h e a d e r s =”From : $name <{$model−>e m a i l}>\r\n ” .
” Reply−To : {$model−>e m a i l}\r\n ” .
”MIME−V e r s i o n : 1 . 0\r\n ” .
” Content−Type : t e x t / p l a i n ; c h a r s e t=UTF−8”;
m a i l ( Y i i : : app ( )−>params [ ’ adminEmail ’ ] , $ s u b j e c t , $model−>body ,
$ h e a d e r s ) ;
Y i i : : app ( )−>u s e r−>s e t F l a s h ( ’ c o n t a c t ’ , ’ Thank you f o r c o n t a c t i n g u s . We w i l l r e s p o n d t o you a s s o o n a s p o s s i b l e . ’ ) ;
$ t h i s−>r e f r e s h ( ) ; }
}
$ t h i s−>r e n d e r ( ’ c o n t a c t ’ , a r r a y ( ’ model ’=>$model ) ) ; }
/∗∗
∗ D i s p l a y s t h e l o g i n p a g e
∗/
p u b l i c f u n c t i o n a c t i o n L o g i n ( ) {
t r y {
Y i i : : app ( )−>db ; } c a t c h ( CDbException $ e ) {
Y i i : : app ( )−>c o n t r o l l e r−>r e d i r e c t ( a r r a y ( ’ / s e t u p / D a t a b a s e ’ ) ) ; }
$admin = U s e r M o d e l : : model ( )−>f i n d B y A t t r i b u t e s ( a r r a y ( ’ u s e r t y p e ’=>’
A d m i n i s t r a t o r ’ ) ) ; i f ( ! i s s e t ( $admin ) )
Y i i : : app ( )−>c o n t r o l l e r−>r e d i r e c t ( a r r a y ( ’ / s e t u p /Admin ’ ) ) ; i f ( ! Y i i : : app ( )−>u s e r−>i s G u e s t )
$ t h i s−>r e d i r e c t ( a r r a y ( ’ i n d e x ’ ) ) ;
$model=new LoginForm ;
// i f i t i s a j a x v a l i d a t i o n r e q u e s t
i f ( i s s e t ( $ POST [ ’ a j a x ’ ] ) && $ POST [ ’ a j a x ’]=== ’ l o g i n−form ’ ) {
e c h o CActiveForm : : v a l i d a t e ( $model ) ; Y i i : : app ( )−>end ( ) ;
}
// c o l l e c t u s e r i n p u t d a t a i f ( i s s e t ( $ POST [ ’ LoginForm ’ ] ) ) {
$model−>a t t r i b u t e s=$ POST [ ’ LoginForm ’ ] ;
// v a l i d a t e u s e r i n p u t and r e d i r e c t t o t h e p r e v i o u s p a g e i f v a l i d i f ( $model−>v a l i d a t e ( ) && $model−>l o g i n ( ) )
$ t h i s−>r e d i r e c t ( Y i i : : app ( )−>u s e r−>r e t u r n U r l ) ; }
// d i s p l a y t h e l o g i n f o r m
$ t h i s−>r e n d e r ( ’ l o g i n ’ , a r r a y ( ’ model ’=>$model ) ) ; }
/∗∗
∗ L o g s o u t t h e c u r r e n t u s e r and r e d i r e c t t o homepage .
∗/
p u b l i c f u n c t i o n a c t i o n L o g o u t ( ) {
Y i i : : app ( )−>u s e r−>l o g o u t ( ) ;
$ t h i s−>r e d i r e c t ( Y i i : : app ( )−>homeUrl ) ; }
/∗∗
∗ G e n e r a t e s o p t i o n s f o r t h e s t u d e n t f o r m .
∗ @param i n t e g e r $ e m p l o y e e i d t h e Employee ID o f t h e P r o f e s s o r h o l d i n g t h e c l a s s
∗/
p u b l i c f u n c t i o n a c t i o n S u b j e c t s ( $ e m p l o y e e i d ) {
$ c l a s s e s = C l a s s M o d e l : : model ( )−>f i n d A l l B y A t t r i b u t e s ( a r r a y ( ’ p r o f ’=>$ e m p l o y e e i d ) ) ;
$ r e t u r n = ’<o p t i o n v a l u e =”0”>S e l e c t S u b j e c t</o p t i o n>’ ; f o r e a c h ( $ c l a s s e s a s $ c l a s s )
$ r e t u r n .= ”<o p t i o n v a l u e =”. $ c l a s s−>i d . ”>” . $ c l a s s−>c o u r s e c o d e . ” C l a s s
” . $ c l a s s−>i d .”</ o p t i o n>”;
e c h o $ r e t u r n ; }
}