eZ Publish  [trunk]
templatecreate.php
Go to the documentation of this file.
00001 <?php
00002 /**
00003  * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
00004  * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
00005  * @version //autogentag//
00006  * @package kernel
00007  */
00008 
00009 $http = eZHTTPTool::instance();
00010 $module = $Params['Module'];
00011 $parameters = $Params["Parameters"];
00012 
00013 $overrideKeys = array( 'nodeID' => $Params['NodeID'],
00014                        'classID' => $Params['ClassID'] );
00015 
00016 
00017 $ini = eZINI::instance();
00018 $tpl = eZTemplate::factory();
00019 
00020 // Todo: read from siteaccess settings
00021 $siteAccess = $Params['SiteAccess'];
00022 if( $siteAccess )
00023     $http->setSessionVariable( 'eZTemplateAdminCurrentSiteAccess', $siteAccess );
00024 else
00025     $siteAccess = $http->sessionVariable( 'eZTemplateAdminCurrentSiteAccess' );
00026 
00027 $siteBase = $siteAccess;
00028 
00029 $siteINI = eZINI::instance( 'site.ini', 'settings', null, null, true );
00030 $siteINI->prependOverrideDir( "siteaccess/$siteAccess", false, 'siteaccess' );
00031 $siteINI->loadCache();
00032 $siteDesign = $siteINI->variable( "DesignSettings", "SiteDesign" );
00033 
00034 $template = "";
00035 foreach ( $parameters as $param )
00036 {
00037     $template .= "/$param";
00038 }
00039 
00040 
00041 $templateType = 'default';
00042 if ( strpos( $template, "node/view" ) )
00043 {
00044     $templateType = 'node_view';
00045 }
00046 else if ( strpos( $template, "content/view" ) )
00047 {
00048     $templateType = 'object_view';
00049 }
00050 else if ( strpos( $template, "content/edit" ) )
00051 {
00052     $templateType = 'object_view';
00053 }
00054 else if ( strpos( $template, "pagelayout.tpl" ) )
00055 {
00056     $templateType = 'pagelayout';
00057 }
00058 
00059 $error = false;
00060 $templateName = false;
00061 $designExtension = '';
00062 
00063 $designINI = eZINI::instance( 'design.ini' );
00064 $designExtensionList = $designINI->variable( 'ExtensionSettings', 'DesignExtensions' );
00065 if ( $designExtensionList !== array() )
00066 {
00067     $designExtension = $designExtensionList[0];
00068 }
00069 
00070 if ( $module->isCurrentAction( 'CreateOverride' ) )
00071 {
00072     $templateName = trim( $http->postVariable( 'TemplateName' ) );
00073     if ( $http->hasPostVariable( 'DesignExtension' ) )
00074     {
00075         $designExtension = trim( $http->postVariable( 'DesignExtension' ) );
00076     }
00077 
00078     if ( preg_match( "#^[0-9a-z_]+$#", $templateName ) )
00079     {
00080         $templateName = trim( $http->postVariable( 'TemplateName' ) );
00081         $filePath = "design/$siteDesign/override/templates";
00082         if ( $designExtension !== '' )
00083         {
00084             $filePath = eZExtension::baseDirectory() . "/" . $designExtension . "/" . $filePath;
00085         }
00086         $fileName = $filePath . "/" . $templateName . ".tpl";
00087 
00088         $templateCode = "";
00089         switch ( $templateType )
00090         {
00091             case "node_view":
00092             {
00093                 $templateCode = generateNodeViewTemplate( $http, $template, $fileName );
00094             }break;
00095 
00096             case "object_view":
00097             {
00098                 $templateCode = generateObjectViewTemplate( $http, $template, $fileName );
00099             }break;
00100 
00101             case "pagelayout":
00102             {
00103                 $templateCode = generatePagelayoutTemplate( $http, $template, $fileName );
00104             }break;
00105 
00106             default:
00107             {
00108                 $templateCode = generateDefaultTemplate( $http, $template, $fileName );
00109             }break;
00110         }
00111 
00112         if ( !file_exists( $filePath ) )
00113         {
00114             eZDir::mkdir( $filePath, false, true );
00115         }
00116 
00117 
00118         $fp = fopen( $fileName, "w+" );
00119         if ( $fp )
00120         {
00121             $filePermission = $ini->variable( 'FileSettings', 'StorageFilePermissions' );
00122             $oldumask = umask( 0 );
00123             fwrite( $fp, $templateCode );
00124             fclose( $fp );
00125             chmod( $fileName, octdec( $filePermission ) );
00126             umask( $oldumask );
00127 
00128             // Store override.ini.append file
00129             $overrideINI = eZINI::instance( 'override.ini', 'settings', null, null, true );
00130             $overrideINI->prependOverrideDir( "siteaccess/$siteAccess", false, 'siteaccess' );
00131             $overrideINI->loadCache();
00132 
00133             $templateFile = preg_replace( "#^/(.*)$#", "\\1", $template );
00134 
00135             $overrideINI->setVariable( $templateName, 'Source', $templateFile );
00136             $overrideINI->setVariable( $templateName, 'MatchFile', $templateName . ".tpl" );
00137             $overrideINI->setVariable( $templateName, 'Subdir', "templates" );
00138 
00139             if ( $http->hasPostVariable( 'Match' ) )
00140             {
00141                 $matchArray = $http->postVariable( 'Match' );
00142 
00143                 foreach ( array_keys( $matchArray ) as $matchKey )
00144                 {
00145                     if ( $matchArray[$matchKey] == -1 or trim( $matchArray[$matchKey] ) == "" )
00146                         unset( $matchArray[$matchKey] );
00147                 }
00148                 $overrideINI->setVariable( $templateName, 'Match', $matchArray );
00149             }
00150 
00151             $oldumask = umask( 0 );
00152             $overrideINI->save( "siteaccess/$siteAccess/override.ini.append" );
00153             $overridePath = "settings/siteaccess/$siteAccess/override.ini.append.php";
00154             if ( file_exists( $overridePath ) )
00155             {
00156                 $s = stat($overridePath);
00157                 $mode = $s["mode"] & 0777; // get only the last 9 bits.
00158                 if ($mode & $filePermission != $filePermission ) // filePermission wrong?
00159                 {
00160                     chmod( $overridePath, octdec( $filePermission ) );
00161                 }
00162             }
00163             umask( $oldumask );
00164 
00165             // Expire content view cache
00166             eZContentCacheManager::clearAllContentCache();
00167 
00168             // Clear override cache
00169             $cachedDir = eZSys::cacheDirectory();
00170             $cachedDir .= "/override/";
00171             eZDir::recursiveDelete( $cachedDir );
00172         }
00173         else
00174         {
00175             $error = "permission_denied";
00176             eZDebug::writeError( "Could not create override template, check permissions on $fileName", "Template override" );
00177         }
00178     }
00179     else
00180     {
00181         $error = "invalid_name";
00182     }
00183 
00184     if ( $error == false )
00185     {
00186         $module->redirectTo( '/visual/templateview'. $template );
00187         return eZModule::HOOK_STATUS_CANCEL_RUN;
00188     }
00189 }
00190 else if( $module->isCurrentAction( 'CancelOverride' ) )
00191 {
00192    $module->redirectTo( '/visual/templateview'. $template );
00193 }
00194 
00195 
00196 function generateNodeViewTemplate( $http, $template, $fileName )
00197 {
00198     $matchArray = $http->postVariable( 'Match' );
00199 
00200     $templateCode = "";
00201     $classIdentifier = $matchArray['class_identifier'];
00202 
00203     $class = eZContentClass::fetchByIdentifier( $classIdentifier );
00204 
00205     // Check what kind of contents we should create in the template
00206     switch ( $http->postVariable( 'TemplateContent' ) )
00207     {
00208         case 'DefaultCopy' :
00209         {
00210             $siteAccess = $http->sessionVariable( 'eZTemplateAdminCurrentSiteAccess' );
00211             $overrideArray = eZTemplateDesignResource::overrideArray( $siteAccess );
00212             $fileName = $overrideArray[$template]['base_dir'] . $overrideArray[$template]['template'];
00213             $fp = fopen( $fileName, 'rb' );
00214             if ( $fp )
00215             {
00216                 $codeFromFile = fread( $fp, filesize( $fileName ) );
00217 
00218                 // Remove the "{* DO NOT EDIT... *}" first line (if exists).
00219                 $templateCode = preg_replace('@^{\*\s*DO\sNOT\sEDIT.*?\*}\n(.*)@s', '$1', $codeFromFile);
00220             }
00221             else
00222             {
00223                 eZDebug::writeError( "Could not open file $fileName, check read permissions" );
00224             }
00225             fclose( $fp );
00226         }break;
00227 
00228         case 'ContainerTemplate' :
00229         {
00230             $templateCode = "<h1>{\$node.name}</h1>\n\n";
00231 
00232             // Append attribute view
00233             if ( $class instanceof eZContentClass )
00234             {
00235                 $attributes = $class->fetchAttributes();
00236                 foreach ( $attributes as $attribute )
00237                 {
00238                     $identifier = $attribute->attribute( 'identifier' );
00239                     $name = $attribute->attribute( 'name' );
00240                     $templateCode .= "<h2>$name</h2>\n";
00241                     $templateCode .= "{attribute_view_gui attribute=\$node.object.data_map.$identifier}\n\n";
00242                 }
00243             }
00244 
00245             $templateCode .= "" .
00246                  "{let page_limit=20\n" .
00247                  "    children=fetch('content','list',hash(parent_node_id,\$node.node_id,sort_by,\$node.sort_array,limit,\$page_limit,offset,\$view_parameters.offset))" .
00248                  "    list_count=fetch('content','list_count',hash(parent_node_id,\$node.node_id))}\n" .
00249                  "\n" .
00250                  "{section name=Child loop=\$children sequence=array(bglight,bgdark)}\n" .
00251                  "{node_view_gui view=line content_node=\$Child:item}\n" .
00252                  "{/section}\n" .
00253 
00254                  "{include name=navigator\n" .
00255                  "    uri='design:navigator/google.tpl'\n" .
00256                  "    page_uri=concat('/content/view','/full/',\$node.node_id)\n" .
00257                  "    item_count=\$list_count\n" .
00258                  "    view_parameters=\$view_parameters\n" .
00259                  "    item_limit=\$page_limit}\n" .
00260             "{/let}\n";
00261         }break;
00262 
00263         case 'ViewTemplate' :
00264         {
00265             $templateCode = "<h1>{\$node.name}</h1>\n\n";
00266 
00267             // Append attribute view
00268             if ( $class instanceof eZContentClass )
00269             {
00270                 $attributes = $class->fetchAttributes();
00271                 foreach ( $attributes as $attribute )
00272                 {
00273                     $identifier = $attribute->attribute( 'identifier' );
00274                     $name = $attribute->attribute( 'name' );
00275                     $templateCode .= "<h2>$name</h2>\n";
00276                     $templateCode .= "{attribute_view_gui attribute=\$node.object.data_map.$identifier}\n\n";
00277                 }
00278             }
00279 
00280         }break;
00281 
00282         default:
00283         case 'EmptyFile' :
00284         {
00285         }break;
00286     }
00287 
00288     return $templateCode;
00289 }
00290 
00291 
00292 function generateObjectViewTemplate( $http, $template, $fileName )
00293 {
00294     $matchArray = $http->postVariable( 'Match' );
00295 
00296     $templateCode = "";
00297     $classID = $matchArray['class'];
00298 
00299     $class = eZContentClass::fetch( $classID );
00300 
00301     // Check what kind of contents we should create in the template
00302     switch ( $http->postVariable( 'TemplateContent' ) )
00303     {
00304         case 'DefaultCopy' :
00305         {
00306             $siteAccess = $http->sessionVariable( 'eZTemplateAdminCurrentSiteAccess' );
00307             $overrideArray = eZTemplateDesignResource::overrideArray( $siteAccess );
00308             $fileName = $overrideArray[$template]['base_dir'] . $overrideArray[$template]['template'];
00309             $fp = fopen( $fileName, 'rb' );
00310             if ( $fp )
00311             {
00312                 $codeFromFile = fread( $fp, filesize( $fileName ) );
00313 
00314                 // Remove the "{* DO NOT EDIT... *}" first line (if exists).
00315                 $templateCode = preg_replace('@^{\*\s*DO\sNOT\sEDIT.*?\*}\n(.*)@s', '$1', $codeFromFile);
00316             }
00317             else
00318             {
00319                 eZDebug::writeError( "Could not open file $fileName, check read permissions" );
00320             }
00321             fclose( $fp );
00322         }break;
00323 
00324         case 'ViewTemplate' :
00325         {
00326             $templateCode = "<h1>{\$object.name}</h1>\n\n";
00327 
00328             // Append attribute view
00329             if ( $class instanceof eZContentClass )
00330             {
00331                 $attributes = $class->fetchAttributes();
00332                 foreach ( $attributes as $attribute )
00333                 {
00334                     $identifier = $attribute->attribute( 'identifier' );
00335                     $name = $attribute->attribute( 'name' );
00336                     $templateCode .= "<h2>$name</h2>\n";
00337                     $templateCode .= "{attribute_view_gui attribute=\$object.data_map.$identifier}\n\n";
00338                 }
00339             }
00340 
00341         }break;
00342 
00343         default:
00344         case 'EmptyFile' :
00345         {
00346         }break;
00347     }
00348     return $templateCode;
00349 }
00350 
00351 function generatePagelayoutTemplate( $http, $template, $fileName )
00352 {
00353     $templateCode = "";
00354     // Check what kind of contents we should create in the template
00355     switch ( $http->postVariable( 'TemplateContent' ) )
00356     {
00357         case 'DefaultCopy' :
00358         {
00359             $siteAccess = $http->sessionVariable( 'eZTemplateAdminCurrentSiteAccess' );
00360             $overrideArray = eZTemplateDesignResource::overrideArray( $siteAccess );
00361             $fileName = $overrideArray[$template]['base_dir'] . $overrideArray[$template]['template'];
00362             $fp = fopen( $fileName, 'rb' );
00363             if ( $fp )
00364             {
00365                 $codeFromFile = fread( $fp, filesize( $fileName ) );
00366 
00367                 // Remove the "{* DO NOT EDIT... *}" first line (if exists).
00368                 $templateCode = preg_replace('@^{\*\s*DO\sNOT\sEDIT.*?\*}\n(.*)@s', '$1', $codeFromFile);
00369             }
00370             else
00371             {
00372                 eZDebug::writeError( "Could not open file $fileName, check read permissions" );
00373             }
00374             fclose( $fp );
00375         }break;
00376 
00377         default:
00378         case 'EmptyFile' :
00379         {
00380             $templateCode = '{*?template charset=latin1?*}' .
00381                  '<!DOCTYPE html>' . "\n" .
00382                  '<html lang="en">' .
00383                  '<head>' . "\n" .
00384                  '    <link rel="stylesheet" type="text/css" href={"stylesheets/core.css"|ezdesign} />' . "\n" .
00385                  '    <link rel="stylesheet" type="text/css" href={"stylesheets/debug.css"|ezdesign} />' . "\n" .
00386                  '    {include uri="design:page_head.tpl"}' . "\n" .
00387                  '</head>' . "\n" .
00388                  '<body>' . "\n" .
00389                  '{$module_result.content}' . "\n" .
00390                  '<!--DEBUG_REPORT-->' . "\n" .
00391                  '</body>' . "\n" .
00392                  '</html>' . "\n";
00393         }break;
00394     }
00395     return $templateCode;
00396 }
00397 
00398 function generateDefaultTemplate( $http, $template, $fileName )
00399 {
00400     $templateCode = "";
00401     // Check what kind of contents we should create in the template
00402     switch ( $http->postVariable( 'TemplateContent' ) )
00403     {
00404         case 'DefaultCopy' :
00405         {
00406             $siteAccess = $http->sessionVariable( 'eZTemplateAdminCurrentSiteAccess' );
00407             $overrideArray = eZTemplateDesignResource::overrideArray( $siteAccess );
00408             $fileName = $overrideArray[$template]['base_dir'] . $overrideArray[$template]['template'];
00409             $fp = fopen( $fileName, 'rb' );
00410             if ( $fp )
00411             {
00412                 $codeFromFile = fread( $fp, filesize( $fileName ) );
00413 
00414                 // Remove the "{* DO NOT EDIT... *}" first line (if exists).
00415                 $templateCode = preg_replace('@^{\*\s*DO\sNOT\sEDIT.*?\*}\n(.*)@s', '$1', $codeFromFile);
00416             }
00417             else
00418             {
00419                 eZDebug::writeError( "Could not open file $fileName, check read permissions" );
00420             }
00421             fclose( $fp );
00422         }break;
00423 
00424         default:
00425         case 'EmptyFile' :
00426         {
00427             $templateCode = '{*?template charset=latin1?*}' .
00428                  '<!DOCTYPE html>' . "\n" .
00429                  '<html lang="en">' .
00430                  '<head>' . "\n" .
00431                  '    <link rel="stylesheet" type="text/css" href={"stylesheets/core.css"|ezdesign} />' . "\n" .
00432                  '    <link rel="stylesheet" type="text/css" href={"stylesheets/debug.css"|ezdesign} />' . "\n" .
00433                  '    {include uri="design:page_head.tpl"}' . "\n" .
00434                  '</head>' . "\n" .
00435                  '<body>' . "\n" .
00436                  '{$module_result.content}' . "\n" .
00437                  '<!--DEBUG_REPORT-->' . "\n" .
00438                  '</body>' . "\n" .
00439                  '</html>' . "\n";
00440         }break;
00441     }
00442     return $templateCode;
00443 }
00444 
00445 
00446 $tpl->setVariable( 'error', $error );
00447 $tpl->setVariable( 'template', $template );
00448 $tpl->setVariable( 'template_type', $templateType );
00449 $tpl->setVariable( 'template_name', $templateName );
00450 $tpl->setVariable( 'site_base', $siteBase );
00451 $tpl->setVariable( 'site_design', $siteDesign );
00452 $tpl->setVariable( 'override_keys', $overrideKeys );
00453 $tpl->setVariable( 'design_extension', $designExtension );
00454 
00455 $Result = array();
00456 $Result['content'] = $tpl->fetch( "design:visual/templatecreate.tpl" );
00457 $Result['path'] = array( array( 'url' => "/visual/templatelist/",
00458                                 'text' => ezpI18n::tr( 'kernel/design', 'Template list' ) ),
00459                          array( 'url' => "/visual/templateview". $template,
00460                                 'text' => ezpI18n::tr( 'kernel/design', 'Template view' ) ),
00461                          array( 'url' => false,
00462                                 'text' => ezpI18n::tr( 'kernel/design', 'Create new template' ) ) );
00463 ?>