00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00101 class TemplateAction extends Action
00102 {
00103 var $defaultSubAction = 'show';
00104 var $template;
00105 var $element;
00106
00107
00108 function TemplateAction()
00109 {
00110 if ( $this->getRequestId() != 0 )
00111 {
00112 $this->template = new Template( $this->getRequestId() );
00113 $this->template->load();
00114 $this->setTemplateVar( 'templateid',$this->template->templateid );
00115 }
00116 else
00117 {
00118 $this->defaultSubAction = 'listing';
00119 }
00120
00121 if ( intval($this->getRequestVar('elementid')) != 0 )
00122 {
00123 $this->element = new Element( $this->getRequestVar('elementid') );
00124 $this->element->load();
00125 $this->setTemplateVar( 'elementid',$this->element->elementid );
00126 }
00127 }
00128
00129
00130 function savesrc()
00131 {
00132
00133
00134 $text = $this->getRequestVar('src');
00135
00136 foreach( $this->template->getElementNames() as $elid=>$elname )
00137 {
00138 $text = str_replace('{{'.$elname.'}}' ,'{{'.$elid.'}}',$text );
00139 $text = str_replace('{{->'.$elname.'}}','{{->'.$elid.'}}',$text );
00140 $text = str_replace('{{'.lang('TEMPLATE_SRC_IFEMPTY' ).':'.$elname.':'.lang('TEMPLATE_SRC_BEGIN').'}}','{{IFEMPTY:' .$elid.':BEGIN}}',$text );
00141 $text = str_replace('{{'.lang('TEMPLATE_SRC_IFEMPTY' ).':'.$elname.':'.lang('TEMPLATE_SRC_END' ).'}}','{{IFEMPTY:' .$elid.':END}}' ,$text );
00142 $text = str_replace('{{'.lang('TEMPLATE_SRC_IFNOTEMPTY').':'.$elname.':'.lang('TEMPLATE_SRC_BEGIN').'}}','{{IFNOTEMPTY:'.$elid.':BEGIN}}',$text );
00143 $text = str_replace('{{'.lang('TEMPLATE_SRC_IFNOTEMPTY').':'.$elname.':'.lang('TEMPLATE_SRC_END' ).'}}','{{IFNOTEMPTY:'.$elid.':END}}' ,$text );
00144 }
00145
00146 $this->template->src = $text;
00147 $this->template->save();
00148 $this->template->load();
00149
00150 $this->addNotice('template',$this->template->name,'SAVED',OR_NOTICE_OK);
00151 }
00152
00153
00154
00155
00156 function srcaddelement()
00157 {
00158 $text = $this->template->src;
00159
00160 switch( $this->getRequestVar('type') )
00161 {
00162 case 'addelement':
00163 $text .= "\n".'{{'.$this->getRequestVar('elementid').'}}';
00164 break;
00165
00166 case 'addicon':
00167 $text .= "\n".'{{->'.$this->getRequestVar('writable_elementid').'}}';
00168 break;
00169
00170 case 'addifempty':
00171 $text .= "\n".'{{IFEMPTY:'.$this->getRequestVar('writable_elementid').':BEGIN}} {{IFEMPTY:'.$this->getRequestVar('writable_elementid').':END}}';
00172 break;
00173
00174 case 'addifnotempty':
00175 $text .= "\n".'{{IFNOTEMPTY:'.$this->getRequestVar('writable_elementid').':BEGIN}} {{IFNOTEMPTY:'.$this->getRequestVar('writable_elementid').':END}}';
00176 break;
00177
00178 default:
00179 $this->addValidationError('type');
00180 $this->callSubAction('srcelement');
00181 return;
00182 }
00183
00184 $this->template->src = $text;
00185
00186 $this->template->save();
00187 $this->template->load();
00188
00189 $this->addNotice('template',$this->template->name,'SAVED',OR_NOTICE_OK);
00190 }
00191
00192
00193
00194
00195 function savename()
00196 {
00197
00198 if ($this->getRequestVar('name') == "")
00199 {
00200 $this->addValidationError('name');
00201 $this->callSubAction('name');
00202 return;
00203 }
00204 else
00205 {
00206 $this->template->name = $this->getRequestVar('name');
00207 $this->template->save();
00208 $this->addNotice('template',$this->template->name,'SAVED',OR_NOTICE_OK);
00209 }
00210 }
00211
00212
00213
00214
00215 function delete()
00216 {
00217 if ( $this->getRequestVar('delete') != '' )
00218 {
00219 $this->template->delete();
00220 $this->addNotice('template',$this->template->name,'DELETED',OR_NOTICE_OK);
00221 }
00222 else
00223 {
00224 $this->addNotice('template',$this->template->name,'CANCELED',OR_NOTICE_WARN);
00225 }
00226 }
00227
00228
00232 function remove()
00233 {
00234 $this->setTemplateVar('name',$this->template->name);
00235 }
00236
00237
00241 function pages()
00242 {
00243 $pages = array();
00244 $pageids = $this->template->getDependentObjectIds();
00245
00246 foreach( $pageids as $pageid )
00247 {
00248 $page = new Page($pageid);
00249 $page->load();
00250
00251 $pages[$pageid] = $page->name;
00252 }
00253
00254 $this->setTemplateVar('pages',$pages);
00255 }
00256
00257
00258
00259
00260 function saveextension()
00261 {
00262 if ( $this->getRequestVar('type') == "list" )
00263 $this->template->extension = $this->getRequestVar('extension');
00264 else
00265 $this->template->extension = $this->getRequestVar('extensiontext');
00266
00267 $this->template->save();
00268 $this->addNotice('template',$this->template->name,'SAVED','ok');
00269 }
00270
00271
00272 function addel()
00273 {
00274
00275 $types = array();
00276
00277 foreach( Element::getAvailableTypes() as $t )
00278 {
00279 $types[ $t ] = lang('EL_'.$t);
00280 }
00281
00282
00283 if ( !$this->userIsAdmin() )
00284 unset( $types['code'] );
00285
00286 $this->setTemplateVar('types',$types);
00287 }
00288
00289
00290
00291
00292
00293 function addelement()
00294 {
00295 if ( $this->getRequestVar('name') != '' )
00296 {
00297 $this->template->addElement( $this->getRequestVar('name'),$this->getRequestVar('description'),$this->getRequestVar('type') );
00298 $this->setTemplateVar('tree_refresh',true);
00299 $this->addNotice('template',$this->template->name,'SAVED','ok');
00300 }
00301 else
00302 {
00303 $this->addValidationError('name');
00304 $this->callSubAction('addel');
00305 }
00306
00307 }
00308
00309
00313 function add()
00314 {
00315 $this->setTemplateVar( 'templates',Template::getAll() );
00316
00317 $examples = array();
00318 $dir = opendir( 'examples/templates');
00319 while( $file = readdir($dir) )
00320 {
00321 if ( substr($file,0,1) != '.')
00322 {
00323 $examples[$file] = $file;
00324 }
00325 }
00326
00327 $this->setTemplateVar( 'examples',$examples );
00328 }
00329
00330
00331
00332 function addtemplate()
00333 {
00334
00335 if ( $this->getRequestVar('name') == '' )
00336 {
00337 $this->addValidationError('name');
00338 $this->callSubAction('add');
00339 return;
00340 }
00341
00342
00343 switch( $this->getRequestVar('type') )
00344 {
00345 case 'empty':
00346
00347 $template = new Template();
00348 $template->add( $this->getRequestVar('name') );
00349 $this->addNotice('template',$template->name,'ADDED','ok');
00350 break;
00351
00352 case 'copy':
00353
00354 $copy_templateid = intval($this->getRequestVar('templateid') );
00355
00356 if ( $copy_templateid == 0 )
00357 {
00358 $this->addValidationError('templateid');
00359 $this->callSubAction('add');
00360 return;
00361 }
00362
00363 $template = new Template();
00364 $template->add( $this->getRequestVar('name') );
00365 $this->addNotice('template',$template->name,'ADDED','ok');
00366
00367 $copy_template = new Template( $copy_templateid );
00368 $copy_template->load();
00369 foreach( $copy_template->getElements() as $element )
00370 {
00371 $element->load();
00372 $element->templateid = $template->templateid;
00373 $element->add();
00374 $element->save();
00375 }
00376
00377 $this->addNotice('template',$copy_template->name,'COPIED','ok');
00378
00379 break;
00380
00381 case 'example':
00382
00383 $template = new Template();
00384
00385 $model = Session::getProjectModel();
00386 $template->modelid = $model->modelid;
00387
00388 $template->add( $this->getRequestVar('name') );
00389
00390 $example = parse_ini_file('examples/templates/'.$this->getRequestVar('example'),true);
00391
00392 foreach( $example as $exampleKey=>$exampleElement )
00393 {
00394 if ( !is_array($exampleElement) )
00395 {
00396 $template->$exampleKey = $exampleElement;
00397 }
00398 else
00399 {
00400 $element = new Element();
00401 $element->templateid = $template->templateid;
00402 $element->name = $exampleKey;
00403 $element->writable = true;
00404 $element->add();
00405
00406 foreach( $exampleElement as $ePropName=>$ePropValue)
00407 $element->$ePropName = $ePropValue;
00408
00409 $element->defaultText = str_replace(';',"\n",$element->defaultText);
00410 $element->save();
00411
00412 }
00413 }
00414
00415 $template->name = $this->getRequestVar('name');
00416 $template->src = str_replace(';',"\n",$template->src);
00417
00418 foreach( $template->getElementNames() as $elid=>$elname )
00419 {
00420 $template->src = str_replace('{{'.$elname.'}}' ,'{{'.$elid.'}}' ,$template->src );
00421 $template->src = str_replace('{{->'.$elname.'}}','{{->'.$elid.'}}',$template->src );
00422 }
00423
00424 $template->save();
00425 $this->addNotice('template',$template->name,'ADDED','ok');
00426
00427 break;
00428 default:
00429 $this->addValidationError('type');
00430 $this->callSubAction('add');
00431 return;
00432 }
00433
00434
00435 $this->setTemplateVar('tree_refresh',true);
00436 }
00437
00438
00439 function prop()
00440 {
00441 }
00442
00443
00444
00448 function name()
00449 {
00450 $this->setTemplateVar('name' ,$this->template->name );
00451 $this->setTemplateVar('extension',$this->template->extension );
00452 $this->setTemplateVar('mime_type',$this->template->mimeType() );
00453 }
00454
00455
00456
00460 function extension()
00461 {
00462
00463 global $conf;
00464 $mime_types = array();
00465 foreach( $conf['mime-types'] as $ext=>$type )
00466 $mime_types[$ext] = $ext.' - '.$type;
00467
00468 $this->setTemplateVar('mime_types',$mime_types);
00469
00470 $this->setTemplateVar('extension' ,$this->template->extension);
00471 $this->setTemplateVar('extensiontext',$this->template->extension);
00472
00473 if ( isset($mime_types[$this->template->extension]) )
00474 $this->setTemplateVar('type','list');
00475 else
00476 $this->setTemplateVar('type','text');
00477 }
00478
00479
00480
00484 function show()
00485 {
00486 $text = $this->template->src;
00487
00488 foreach( $this->template->getElementIds() as $elid )
00489 {
00490 $element = new Element( $elid );
00491 $element->load();
00492 $url = Html::url( 'element','edit',$this->template->templateid,array('elementid'=>$elid));
00493
00494 $text = str_replace('{{'.$elid.'}}',$element->name,
00495 $text );
00496 $text = str_replace('{{->'.$elid.'}}','',
00497 $text );
00498
00499 $text = str_replace('{{IFEMPTY:'.$elid.':BEGIN}}','',
00500 $text );
00501 $text = str_replace('{{IFEMPTY:'.$elid.':END}}','',
00502 $text );
00503
00504 $text = str_replace('{{IFNOTEMPTY:'.$elid.':BEGIN}}','',
00505 $text );
00506 $text = str_replace('{{IFNOTEMPTY:'.$elid.':END}}','',
00507 $text );
00508
00509 unset( $element );
00510 }
00511
00512 echo $text;
00513
00514 exit();
00515 }
00516
00517
00521 function edit()
00522 {
00523 $text = htmlentities( $this->template->src );
00524 $text = str_replace("\n",'<br/>',$text);
00525
00526 foreach( $this->template->getElementIds() as $elid )
00527 {
00528 $element = new Element( $elid );
00529 $element->load();
00530 $url = Html::url( 'element','name',$elid );
00531
00532 $text = str_replace('{{'.$elid.'}}',
00533 '<a href="'.$url.'" class="el_'.
00534 $element->getTypeClass().'" target="cms_main_main" title="'.$element->desc.'">{{'.
00535 $element->name.'}}</a>',
00536 $text );
00537 $text = str_replace('{{->'.$elid.'}}',
00538 '<a href="'.$url.'" class="el_'.
00539 $element->getTypeClass().'" target="cms_main_main" title="'.$element->desc.'">{{->'.
00540 $element->name.'}}</a>',
00541 $text );
00542
00543 $text = str_replace('{{IFEMPTY:'.$elid.':BEGIN}}',
00544 '<a href="'.$url.'" class="el_'.$element->getTypeClass().'" title="'.$element->desc.'">{{'.lang('TEMPLATE_SRC_IFEMPTY').':'.
00545 $element->name.':'.lang('TEMPLATE_SRC_BEGIN').'}}</a>',
00546 $text );
00547 $text = str_replace('{{IFEMPTY:'.$elid.':END}}',
00548 '<a href="'.$url.'" class="el_'.$element->getTypeClass().'" title="'.$element->desc.'">{{'.lang('TEMPLATE_SRC_IFEMPTY').':'.
00549 $element->name.':'.lang('TEMPLATE_SRC_END').'}}</a>',
00550 $text );
00551
00552 $text = str_replace('{{IFNOTEMPTY:'.$elid.':BEGIN}}',
00553 '<a href="'.$url.'" class="el_'.$element->getTypeClass().'" title="'.$element->desc.'">{{'.lang('TEMPLATE_SRC_IFNOTEMPTY').':'.
00554 $element->name.':'.lang('TEMPLATE_SRC_BEGIN').'}}</a>',
00555 $text );
00556 $text = str_replace('{{IFNOTEMPTY:'.$elid.':END}}',
00557 '<a href="'.$url.'" class="el_'.$element->getTypeClass().'" title="'.$element->desc.'">{{'.lang('TEMPLATE_SRC_IFNOTEMPTY').':'.
00558 $element->name.':'.lang('TEMPLATE_SRC_END').'}}</a>',
00559 $text );
00560
00561 unset( $element );
00562 }
00563
00564 $this->setTemplateVar('text',$text);
00565
00566 $this->forward('template_show');
00567 }
00568
00569
00570
00571
00572 function el()
00573 {
00574 global $conf_php;
00575 $list = array();
00576
00577 foreach( $this->template->getElementIds() as $elid )
00578 {
00579 $element = new Element( $elid );
00580 $element->load();
00581
00582 $list[$elid] = array();
00583 $list[$elid]['url' ] = Html::url('element','name',$elid);
00584 $list[$elid]['name'] = $element->name;
00585 $list[$elid]['desc'] = $element->desc;
00586 $list[$elid]['type'] = $element->type;
00587
00588 unset( $element );
00589 }
00590 $this->setTemplateVar('el',$list);
00591 }
00592
00593
00594
00595 function srcelement()
00596 {
00597 $elements = array();
00598 $writable_elements = array();
00599
00600 foreach( $this->template->getElementIds() as $elid )
00601 {
00602 $element = new Element( $elid );
00603 $element->load();
00604
00605 $elements[$elid] = $element->name;
00606
00607 if ( $element->isWritable() )
00608 $writable_elements[$elid] = $element->name;
00609 }
00610
00611 $this->setTemplateVar('elements' ,$elements );
00612 $this->setTemplateVar('writable_elements',$writable_elements);
00613 }
00614
00615
00616
00620 function src()
00621 {
00622 if ( $this->isEditMode() )
00623 {
00624 $text = $this->template->src;
00625
00626 foreach( $this->template->getElementIds() as $elid )
00627 {
00628 $element = new Element( $elid );
00629 $element->load();
00630
00631 $text = str_replace('{{'.$elid.'}}',
00632 '{{'.$element->name.'}}',
00633 $text );
00634 $text = str_replace('{{->'.$elid.'}}',
00635 '{{->'.$element->name.'}}',
00636 $text );
00637 $text = str_replace('{{IFEMPTY:'.$elid.':BEGIN}}',
00638 '{{'.lang('TEMPLATE_SRC_IFEMPTY').':'.$element->name.':'.lang('TEMPLATE_SRC_BEGIN').'}}',
00639 $text );
00640 $text = str_replace('{{IFEMPTY:'.$elid.':END}}',
00641 '{{'.lang('TEMPLATE_SRC_IFEMPTY').':'.$element->name.':'.lang('TEMPLATE_SRC_END').'}}',
00642 $text );
00643 $text = str_replace('{{IFNOTEMPTY:'.$elid.':BEGIN}}',
00644 '{{'.lang('TEMPLATE_SRC_IFNOTEMPTY').':'.$element->name.':'.lang('TEMPLATE_SRC_BEGIN').'}}',
00645 $text );
00646 $text = str_replace('{{IFNOTEMPTY:'.$elid.':END}}',
00647 '{{'.lang('TEMPLATE_SRC_IFNOTEMPTY').':'.$element->name.':'.lang('TEMPLATE_SRC_END').'}}',
00648 $text );
00649 }
00650
00651 $this->setTemplateVar( 'src',$text );
00652 }
00653 else
00654 {
00655 $text = htmlentities( $this->template->src );
00656 $text = str_replace("\n",'<br/>',$text);
00657
00658 foreach( $this->template->getElementIds() as $elid )
00659 {
00660 $element = new Element( $elid );
00661 $element->load();
00662 $url = Html::url( 'element','name',$elid );
00663
00664 $text = str_replace('{{'.$elid.'}}',
00665 '<a href="'.$url.'" class="el_'.
00666 $element->getTypeClass().'" target="cms_main_main" title="'.$element->desc.'">{{'.
00667 $element->name.'}}</a>',
00668 $text );
00669 $text = str_replace('{{->'.$elid.'}}',
00670 '<a href="'.$url.'" class="el_'.
00671 $element->getTypeClass().'" target="cms_main_main" title="'.$element->desc.'">{{->'.
00672 $element->name.'}}</a>',
00673 $text );
00674
00675 $text = str_replace('{{IFEMPTY:'.$elid.':BEGIN}}',
00676 '<a href="'.$url.'" class="el_'.$element->getTypeClass().'" title="'.$element->desc.'">{{'.lang('TEMPLATE_SRC_IFEMPTY').':'.
00677 $element->name.':'.lang('TEMPLATE_SRC_BEGIN').'}}</a>',
00678 $text );
00679 $text = str_replace('{{IFEMPTY:'.$elid.':END}}',
00680 '<a href="'.$url.'" class="el_'.$element->getTypeClass().'" title="'.$element->desc.'">{{'.lang('TEMPLATE_SRC_IFEMPTY').':'.
00681 $element->name.':'.lang('TEMPLATE_SRC_END').'}}</a>',
00682 $text );
00683
00684 $text = str_replace('{{IFNOTEMPTY:'.$elid.':BEGIN}}',
00685 '<a href="'.$url.'" class="el_'.$element->getTypeClass().'" title="'.$element->desc.'">{{'.lang('TEMPLATE_SRC_IFNOTEMPTY').':'.
00686 $element->name.':'.lang('TEMPLATE_SRC_BEGIN').'}}</a>',
00687 $text );
00688 $text = str_replace('{{IFNOTEMPTY:'.$elid.':END}}',
00689 '<a href="'.$url.'" class="el_'.$element->getTypeClass().'" title="'.$element->desc.'">{{'.lang('TEMPLATE_SRC_IFNOTEMPTY').':'.
00690 $element->name.':'.lang('TEMPLATE_SRC_END').'}}</a>',
00691 $text );
00692
00693 unset( $element );
00694 }
00695
00696 $this->setTemplateVar('src',$text);
00697 }
00698
00699 }
00700
00701
00702
00703
00704 function listing()
00705 {
00706 global $conf_php;
00707
00708 $list = array();
00709
00710 foreach( Template::getAll() as $id=>$name )
00711 {
00712 $list[$id] = array();
00713 $list[$id]['name'] = $name;
00714 $list[$id]['url' ] = Html::url('main','template',$id,array(REQ_PARAM_TARGETSUBACTION=>'el'));
00715 }
00716
00717
00718
00719 $this->setTemplateVar('templates',$list);
00720 }
00721
00722
00723 function checkMenu( $menu ) {
00724
00725 switch( $menu)
00726 {
00727 case 'remove':
00728 return (count($this->template->getDependentObjectIds()) == 0);
00729
00730 case 'pages':
00731 return (count($this->template->getDependentObjectIds()) > 0);
00732
00733 default:
00734 return true;
00735
00736 }
00737 }
00738
00739 }