eZ Publish  [trunk]
ezpackagecreationhandler.php
Go to the documentation of this file.
00001 <?php
00002 /**
00003  * File containing the eZPackageCreationHandler class.
00004  *
00005  * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
00006  * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
00007  * @version //autogentag//
00008  * @package kernel
00009  */
00010 
00011 /*!
00012   \ingroup package
00013   \class eZPackageCreationHandler ezpackagecreationhandler.php
00014   \brief The class eZPackageCreationHandler does
00015 
00016 */
00017 
00018 class eZPackageCreationHandler
00019 {
00020     /*!
00021      Constructor
00022     */
00023     function eZPackageCreationHandler( $id, $name, $steps )
00024     {
00025         $this->Attributes = array( 'id' => $id,
00026                                    'name' => $name,
00027                                    'steps' => $steps,
00028                                    'step_map' => false,
00029                                    'current_steps' => $steps );
00030         $this->InitializeStepMethodMap = array();
00031         $this->ValidateStepMethodMap = array();
00032         $this->CommitStepMethodMap = array();
00033         $this->LoadStepMethodMap = array();
00034     }
00035 
00036     /*!
00037      Will go over the steps and make sure that:
00038      - The next and previous links are correct
00039      - Steps that aren't needed are removed
00040 
00041       It will also make sure that steps can be looked up by their ID.
00042     */
00043     function generateStepMap( $package, &$persistentData )
00044     {
00045         $steps = $this->attribute( 'steps' );
00046         $map = array();
00047         $lastStep = false;
00048         $currentSteps = array();
00049         for ( $i = 0; $i < count( $steps ); ++$i )
00050         {
00051             $step =& $steps[$i];
00052             if ( !isset( $step['previous_step'] ) )
00053             {
00054                 if ( $lastStep )
00055                     $step['previous_step'] = $lastStep['id'];
00056                 else
00057                     $step['previous_step'] = false;
00058             }
00059             if ( !isset( $step['next_step'] ) )
00060             {
00061                 if ( $i + 1 < count( $steps ) )
00062                     $step['next_step'] = $steps[$i+1]['id'];
00063                 else
00064                     $step['next_step'] = false;
00065             }
00066             if ( isset( $step['methods']['initialize'] ) )
00067                 $this->InitializeStepMethodMap[$step['id']] = $step['methods']['initialize'];
00068             if( isset( $step['methods']['load'] ) )
00069                 $this->LoadStepMethodMap[$step['id']] = $step['methods']['load'];
00070             if ( isset( $step['methods']['validate'] ) )
00071                 $this->ValidateStepMethodMap[$step['id']] = $step['methods']['validate'];
00072             if ( isset( $step['methods']['commit'] ) )
00073                 $this->CommitStepMethodMap[$step['id']] = $step['methods']['commit'];
00074             $isStepIncluded = true;
00075             if ( isset( $step['methods']['check'] ) )
00076             {
00077                 $checkMethod = $step['methods']['check'];
00078                 $isStepIncluded = $this->$checkMethod( $package, $persistentData );
00079             }
00080             if ( $isStepIncluded )
00081             {
00082                 $map[$step['id']] =& $step;
00083                 $lastStep =& $step;
00084                 $currentSteps[] =& $step;
00085             }
00086         }
00087         $this->StepMap = array( 'first' => &$steps[0],
00088                                 'map' => &$map,
00089                                 'steps' => &$steps );
00090         $this->Attributes['step_map'] =& $this->StepMap;
00091         $this->Attributes['current_steps'] = $currentSteps;
00092     }
00093 
00094     function attributes()
00095     {
00096         return array_keys( $this->Attributes );
00097     }
00098 
00099     function hasAttribute( $name )
00100     {
00101         return array_key_exists( $name, $this->Attributes );
00102     }
00103 
00104     function attribute( $name )
00105     {
00106         if ( array_key_exists( $name, $this->Attributes ) )
00107         {
00108             return $this->Attributes[$name];
00109         }
00110 
00111         eZDebug::writeError( "Attribute '$name' does not exist", __METHOD__ );
00112         return null;
00113     }
00114 
00115     function initializeStepMethodMap()
00116     {
00117         return $this->InitializeStepMethodMap;
00118     }
00119 
00120     function loadStepMethodMap()
00121     {
00122         return $this->LoadStepMethodMap;
00123     }
00124 
00125     function validateStepMethodMap()
00126     {
00127         return $this->ValidateStepMethodMap;
00128     }
00129 
00130     function commitStepMethodMap()
00131     {
00132         return $this->CommitStepMethodMap;
00133     }
00134 
00135     /*!
00136      \return a process step map which has proper next/previous links,
00137              method maps and allows lookup of steps by ID.
00138     */
00139     function &stepMap()
00140     {
00141         return $this->StepMap;
00142     }
00143 
00144     function stepTemplate( $step )
00145     {
00146         $stepTemplateName = $step['template'];
00147         if ( isset( $step['use_standard_template'] ) and
00148              $step['use_standard_template'] )
00149             $stepTemplateDir = "create";
00150         else
00151             $stepTemplateDir = "creators/" . $this->attribute( 'id' );
00152         return array( 'name' => $stepTemplateName,
00153                       'dir' => $stepTemplateDir );
00154     }
00155 
00156     /*!
00157      This is called the first time the step is entered (ie. not on validations)
00158      and can be used to fill in values in the \a $persistentData variable
00159      for use in the template or later retrieval.
00160     */
00161     function initializeStep( $package, $http, $step, &$persistentData, $tpl )
00162     {
00163         $methodMap = $this->initializeStepMethodMap();
00164         if ( count( $methodMap ) > 0 )
00165         {
00166             if ( isset( $methodMap[$step['id']] ) )
00167             {
00168                 $method = $methodMap[$step['id']];
00169                 return $this->$method( $package, $http, $step, $persistentData, $tpl );
00170             }
00171         }
00172     }
00173 
00174     /*!
00175      Called each time a step is loaded, and can be used to fetch and process input data in each step.
00176     */
00177     function loadStep( $package, $http, $currentStepID, &$persistentData, $tpl, &$module )
00178     {
00179         $methodMap = $this->loadStepMethodMap();
00180         if ( count( $methodMap ) > 0 )
00181         {
00182             if ( isset( $methodMap[$currentStepID] ) )
00183             {
00184                 $method = $methodMap[$currentStepID];
00185                 return $this->$method( $package, $http, $currentStepID, $persistentData, $tpl, $module );
00186             }
00187         }
00188     }
00189 
00190     /*!
00191      This is called after a step is finished. Reimplement this function to validate
00192      the step values and give back errors.
00193      \return \c false if the next step should not be fetched (ie. errors) or
00194              \c true if the all is OK and the next step should be fetched.
00195              It is also possible to return a step identifier, in which case
00196              this will be the next step.
00197     */
00198     function validateStep( $package, $http, $currentStepID, &$stepMap, &$persistentData, &$errorList )
00199     {
00200         $nextStep = $this->validateAndAdvanceStep( $package, $http, $currentStepID, $stepMap, $persistentData, $errorList );
00201         if ( $nextStep === true )
00202         {
00203             if ( !isset( $stepMap['map'][$currentStepID] ) )
00204             {
00205                 return $stepMap['first']['id'];
00206             }
00207             else
00208             {
00209                 return $stepMap['map'][$currentStepID]['next_step'];
00210             }
00211         }
00212         else if ( $nextStep === false )
00213         {
00214             return $currentStepID;
00215         }
00216         return $nextStep;
00217     }
00218 
00219     function validateAndAdvanceStep( $package, $http, $currentStepID, &$stepMap, &$persistentData, &$errorList )
00220     {
00221         $methodMap = $this->validateStepMethodMap();
00222         if ( count( $methodMap ) > 0 )
00223         {
00224             if ( isset( $methodMap[$currentStepID] ) )
00225             {
00226                 $method = $methodMap[$currentStepID];
00227                 return $this->$method( $package, $http, $currentStepID, $stepMap, $persistentData, $errorList );
00228             }
00229         }
00230         return true;
00231     }
00232 
00233     /*!
00234      This is called after a step has validated it's information. It can
00235      be used to put values in the \a $persistentData variable for later retrieval.
00236     */
00237     function commitStep( $package, $http, $step, &$persistentData, $tpl )
00238     {
00239         $methodMap = $this->commitStepMethodMap();
00240         if ( count( $methodMap ) > 0 )
00241         {
00242             if ( isset( $methodMap[$step['id']] ) )
00243             {
00244                 $method = $methodMap[$step['id']];
00245                 return $this->$method( $package, $http, $step, $persistentData, $tpl );
00246             }
00247         }
00248     }
00249 
00250     /*!
00251      Finalizes the creation process with the gathered information.
00252      This is usually the function that creates the package and
00253      adds the proper elements.
00254     */
00255     function finalize( &$package, $http, &$persistentData )
00256     {
00257     }
00258 
00259     /*!
00260      \static
00261      \return a list of the available creators usable as a limitation in the role system.
00262     */
00263     static function creatorLimitationList()
00264     {
00265         $creators =& eZPackageCreationHandler::creatorList();
00266         $list = array();
00267         foreach ( $creators as $creator )
00268         {
00269             $list[] = array( 'name' => $creator->attribute( 'name' ),
00270                              'id' => $creator->attribute( 'id' ) );
00271         }
00272         return $list;
00273     }
00274 
00275     /*!
00276      \static
00277      \return a list of the available creators.
00278     */
00279     static function &creatorList( $checkRoles = false )
00280     {
00281         $allowedCreators = false;
00282 
00283         $currentUser = eZUser::currentUser();
00284         $accessResult = $currentUser->hasAccessTo( 'package', 'create' );
00285         $limitationList = array();
00286         $canCreate = false;
00287         if ( $accessResult['accessWord'] == 'no' )
00288         {
00289             $creators = array();
00290             return $creators;
00291         }
00292 
00293         if ( $accessResult['accessWord'] == 'limited' )
00294         {
00295             $limitationList = $accessResult['policies'];
00296             foreach( $limitationList as $limitationArray ) // TODO : fix this
00297             {
00298                 foreach ( $limitationArray as $key => $limitation )
00299                 {
00300                     if ( $key == 'CreatorType' )
00301                     {
00302                         if ( !is_array( $allowedCreators ) )
00303                             $allowedCreators = array();
00304                         $list = $limitation;
00305                         $allowedCreators = array_merge( $allowedCreators, $list );
00306                     }
00307                 }
00308             }
00309         }
00310 
00311         $creators =& $GLOBALS['eZPackageCreatorList'];
00312         if ( !isset( $creators ) )
00313         {
00314             $creators = array();
00315             $ini = eZINI::instance( 'package.ini' );
00316             $list = $ini->variable( 'CreationSettings', 'HandlerList' );
00317             foreach ( $list as $name )
00318             {
00319                 if ( is_array( $allowedCreators ) and
00320                      !in_array( $name, $allowedCreators ) )
00321                      continue;
00322                 $handler = eZPackageCreationHandler::instance( $name );
00323                 $creators[] = $handler;
00324             }
00325         }
00326         return $creators;
00327     }
00328 
00329     /**
00330      * Returns a shared instance of the eZPackageCreationHandler class
00331      * pr $handlerName as defined in package.ini[CreationSettings]HandlerAlias
00332      *
00333      * @param string $handlerName
00334      * @return eZPackageCreationHandler
00335      */
00336     static function instance( $handlerName )
00337     {
00338         $handlers =& $GLOBALS['eZPackageCreationHandlers'];
00339         if ( !isset( $handlers ) )
00340             $handlers = array();
00341         $handler = false;
00342 
00343         if( isset( $handlers[$handlerName] ) )
00344         {
00345             $handler = $handlers[$handlerName];
00346             $handler->reset();
00347         }
00348         else
00349         {
00350             $optionArray = array( 'iniFile'       => 'package.ini',
00351                                   'iniSection'    => 'CreationSettings',
00352                                   'iniVariable'   => 'HandlerAlias',
00353                                   'handlerIndex'  => $handlerName,
00354                                   'handlerParams' => array( $handlerName ) );
00355 
00356             $options = new ezpExtensionOptions( $optionArray );
00357 
00358             $handler = eZExtension::getHandlerClass( $options );
00359             $handlers[$handlerName] = $handler;
00360         }
00361         return $handler;
00362     }
00363 
00364     /*!
00365      \static
00366      \return A ready to use creation step which takes care of package information.
00367     */
00368     function packageInformationStep()
00369     {
00370         return array( 'id' => 'packageinfo',
00371                       'name' => ezpI18n::tr( 'kernel/package', 'Package information' ),
00372                       'methods' => array( 'initialize' => 'initializePackageInformation',
00373                                           'validate' => 'validatePackageInformation',
00374                                           'commit' => 'commitPackageInformation' ),
00375                       'use_standard_template' => true,
00376                       'template' => 'info.tpl' );
00377     }
00378 
00379     /*!
00380      \static
00381      \return A ready to use creation step which takes care of reading in maintainer information.
00382     */
00383     function packageMaintainerStep()
00384     {
00385         return array( 'id' => 'packagemaintainer',
00386                       'name' => ezpI18n::tr( 'kernel/package', 'Package maintainer' ),
00387                       'methods' => array( 'initialize' => 'initializePackageMaintainer',
00388                                           'validate' => 'validatePackageMaintainer',
00389                                           'commit' => 'commitPackageMaintainer',
00390                                           'check' => 'checkPackageMaintainer' ),
00391                       'use_standard_template' => true,
00392                       'template' => 'maintainer.tpl' );
00393     }
00394 
00395     /*!
00396      \static
00397      \return A ready to use creation step which takes care of reading in a changelog entry.
00398     */
00399     function packageChangelogStep()
00400     {
00401         return array( 'id' => 'packagechangelog',
00402                       'name' => ezpI18n::tr( 'kernel/package', 'Package changelog' ),
00403                       'methods' => array( 'initialize' => 'initializePackageChangelog',
00404                                           'validate' => 'validatePackageChangelog',
00405                                           'commit' => 'commitPackageChangelog' ),
00406                       'use_standard_template' => true,
00407                       'template' => 'changelog.tpl' );
00408     }
00409 
00410     /*!
00411      \static
00412      \return A ready to use creation step which takes care of fetching a thumbnail image.
00413     */
00414     function packageThumbnailStep()
00415     {
00416         return array( 'id' => 'packagethumbnail',
00417                       'name' => ezpI18n::tr( 'kernel/package', 'Package thumbnail' ),
00418                       'methods' => array( 'initialize' => 'initializePackageThumbnail',
00419                                           'validate' => 'validatePackageThumbnail',
00420                                           'commit' => 'commitPackageThumbnail' ),
00421                       'use_standard_template' => true,
00422                       'template' => 'thumbnail.tpl' );
00423     }
00424 
00425     /*!
00426      \return the type installation this package uses.
00427 
00428      This method is called from the createPackage() method and will return \c 'install' by default.
00429      If you want the creator to have a different install type reimplement this function in the creator.
00430     */
00431     function packageInstallType( $package, &$persistentData )
00432     {
00433         return 'install';
00434     }
00435 
00436     /*!
00437      \return the initial state of the package.
00438 
00439      The state of a package generally tells how stable a package is,
00440      see eZPackage::stateList() for more information on possible states.
00441      \note The default returns \c 'alpha'
00442     */
00443     function packageInitialState( $package, &$persistentData )
00444     {
00445         return 'alpha';
00446     }
00447 
00448     /*!
00449      \return The initial changelog entry for a package.
00450      It is possible to get different initial texts by reimplementing this function.
00451 
00452      \note This function is called from initializePackageChangelog()
00453     */
00454     function initialChangelogEntry( $package, $http, $step, &$persistentData, $tpl )
00455     {
00456         return '- Creation of package.';
00457     }
00458 
00459     /*!
00460      \return The package type taken from \a $package if the package exists,
00461              otherwise \c false.
00462      If the creator should have a specific package type this function should be reimplemented.
00463      See eZPackage::typeList() for more information on available types.
00464 
00465      \note This function is called from createPackage and checkPackageMaintainer()
00466     */
00467     function packageType( $package, &$persistentData )
00468     {
00469         if ( $package instanceof eZPackage )
00470         {
00471             return $package->attribute( 'type' );
00472         }
00473         return false;
00474     }
00475 
00476     /*!
00477      Creates a new package in \a $package and initializes it with the
00478      basic data. The information is taken from the \a $persistentData
00479      which must be filled in prior to this function is called.
00480      \return \c true if the package was created or \c false if it was only re-initialized.
00481      \sa packageType, packageInitialState and packageInstallType
00482     */
00483     function createPackage( &$package, $http, &$persistentData, &$cleanupFiles, $storePackage = true )
00484     {
00485         $createdPackage = false;
00486         if ( !( $package instanceof eZPackage ) )
00487         {
00488             $package = eZPackage::create( $persistentData['name'],
00489                                           array( 'summary' => $persistentData['summary'] ) );
00490             $createdPackage = true;
00491         }
00492         else
00493             $package->setAttribute( 'summary', $persistentData['summary'] );
00494 
00495         $package->setAttribute( 'is_active', false );
00496         $package->setAttribute( 'type', $this->packageType( $package, $persistentData ) );
00497 
00498         $package->setRelease( $persistentData['version'], '1', false,
00499                               $persistentData['licence'], $this->packageInitialState( $package, $persistentData ) );
00500 
00501         $package->setAttribute( 'description', $persistentData['description'] );
00502         $package->setAttribute( 'install_type', $this->packageInstallType( $package, $persistentData ) );
00503 
00504         $package->setAttribute( 'packaging-host', $persistentData['host'] );
00505         $package->setAttribute( 'packaging-packager', $persistentData['packager'] );
00506 
00507         $changelogPerson = $persistentData['changelog_person'];
00508         $changelogEmail = $persistentData['changelog_email'];
00509         $changelogEntries = $persistentData['changelog_entries'];
00510 
00511         $maintainerPerson = $persistentData['maintainer_person'];
00512         $maintainerEmail = $persistentData['maintainer_email'];
00513         $maintainerRole = $persistentData['maintainer_role'];
00514 
00515         if ( $maintainerPerson )
00516         {
00517             $package->appendMaintainer( $maintainerPerson, $maintainerEmail, $maintainerRole );
00518         }
00519 
00520         if ( $changelogPerson )
00521         {
00522             $package->appendChange( $changelogPerson, $changelogEmail, $changelogEntries );
00523         }
00524 
00525         if ( $persistentData['licence'] == 'GPL' )
00526         {
00527             eZPackageCreationHandler::appendLicence( $package );
00528         }
00529 
00530 
00531         $collections = array();
00532         $cleanupFiles = array();
00533 
00534         if ( isset( $persistentData['thumbnail'] ) and
00535              $persistentData['thumbnail'] )
00536         {
00537             $thumbnail = $persistentData['thumbnail'];
00538             $fileItem = array( 'file' => $thumbnail['filename'],
00539                                'type' => 'thumbnail',
00540                                'role' => false,
00541                                'design' => false,
00542                                'path' => $thumbnail['url'],
00543                                'collection' => 'default',
00544                                'file-type' => false,
00545                                'role-value' => false,
00546                                'variable-name' => 'thumbnail' );
00547 
00548             $package->appendFile( $fileItem['file'], $fileItem['type'], $fileItem['role'],
00549                                   $fileItem['design'], $fileItem['path'], $fileItem['collection'],
00550                                   null, null, true, null,
00551                                   $fileItem['file-type'], $fileItem['role-value'], $fileItem['variable-name'] );
00552             $cleanupFiles[] = $fileItem['path'];
00553 
00554             if ( !in_array( $fileItem['collection'], $collections ) )
00555                 $collections[] = $fileItem['collection'];
00556         }
00557 
00558         foreach ( $collections as $collection )
00559         {
00560             $installItems = $package->installItemsList( 'ezfile', false, $collection, true );
00561             if ( count( $installItems ) == 0 )
00562                 $package->appendInstall( 'ezfile', false, false, true,
00563                                          false, false,
00564                                          array( 'collection' => $collection ) );
00565             $dependencyItems = $package->dependencyItems( 'provides',
00566                                                           array( 'type'   => 'ezfile',
00567                                                                  'name'   => 'collection',
00568                                                                  'valiue' => $collection ) );
00569             if ( count( $dependencyItems ) == 0 )
00570                 $package->appendDependency( 'provides',
00571                                             array( 'type'  => 'ezfile',
00572                                                    'name'  => 'collection',
00573                                                    'value' => $collection ) );
00574             $installItems = $package->installItemsList( 'ezfile', false, $collection, false );
00575             if ( count( $installItems ) == 0 )
00576                 $package->appendInstall( 'ezfile', false, false, false,
00577                                          false, false,
00578                                          array( 'collection' => $collection ) );
00579         }
00580 
00581         $package->setAttribute( 'is_active', true );
00582         if ( $storePackage )
00583             $package->store();
00584 
00585         return $createdPackage;
00586     }
00587 
00588     /*!
00589      This is called on the package information step to initialize the name, summary and description fields.
00590      Reimplementing this function allows the creator to fill in some default values for the information fields.
00591      \note The default does nothing.
00592     */
00593     function generatePackageInformation( &$packageInformation, $package, $http, $step, &$persistentData )
00594     {
00595     }
00596 
00597     /*!
00598      Initializes the package information step with some default values.
00599      It will call generatePackageInformation() after the values are initialized.
00600     */
00601     function initializePackageInformation( $package, $http, $step, &$persistentData, $tpl )
00602     {
00603         $persistentData['name'] = false;
00604         $persistentData['summary'] = false;
00605         $persistentData['description'] = false;
00606         $persistentData['licence'] = 'GPL';
00607         $persistentData['version'] = '1.0';
00608         if ( isset( $_SERVER['HOSTNAME'] ) )
00609             $host = $_SERVER['HOSTNAME'];
00610         else
00611             $host = $_SERVER['HTTP_HOST'];
00612         $persistentData['host'] = $host;
00613         $user = eZUser::currentUser();
00614         $userObject = $user->attribute( 'contentobject' );
00615         $packager = false;
00616         if ( $userObject )
00617             $packager = $userObject->attribute( 'name' );
00618         $persistentData['packager'] = $packager;
00619         $this->generatePackageInformation( $persistentData, $package, $http, $step, $persistentData );
00620 
00621         // Make sure the package name contains only valid characters
00622         $trans = eZCharTransform::instance();
00623         $persistentData['name'] = $trans->transformByGroup( $persistentData['name'], 'identifier' );
00624     }
00625 
00626     /*!
00627      Reads in the package information values from POST variables and makes sure
00628      that the package name and package summary is filled in, the version is in correct
00629      format and that a package does not already exists with the same name.
00630     */
00631     function validatePackageInformation( $package, $http, $currentStepID, &$stepMap, &$persistentData, &$errorList )
00632     {
00633         $packageName = false;
00634         $packageSummary = false;
00635         $packageVersion = false;
00636         $packageDescription = false;
00637         $packageLicence = 'GPL';
00638         $packageHost = false;
00639         $packagePackager = false;
00640         if ( $http->hasPostVariable( 'PackageName' ) )
00641         {
00642             $packageName = trim( $http->postVariable( 'PackageName' ) );
00643         }
00644         if ( $http->hasPostVariable( 'PackageSummary' ) )
00645             $packageSummary = $http->postVariable( 'PackageSummary' );
00646         if ( $http->hasPostVariable( 'PackageDescription' ) )
00647             $packageDescription = $http->postVariable( 'PackageDescription' );
00648         if ( $http->hasPostVariable( 'PackageVersion' ) )
00649             $packageVersion = trim( $http->postVariable( 'PackageVersion' ) );
00650         if ( $http->hasPostVariable( 'PackageLicence' ) )
00651             $packageLicence = $http->postVariable( 'PackageLicence' );
00652         if ( $http->hasPostVariable( 'PackageHost' ) )
00653             $packageHost = $http->postVariable( 'PackageHost' );
00654         if ( $http->hasPostVariable( 'PackagePackager' ) )
00655             $packagePackager = $http->postVariable( 'PackagePackager' );
00656 
00657         $persistentData['name'] = $packageName;
00658         $persistentData['summary'] = $packageSummary;
00659         $persistentData['description'] = $packageDescription;
00660         $persistentData['version'] = $packageVersion;
00661         $persistentData['licence'] = $packageLicence;
00662         $persistentData['host'] = $packageHost;
00663         $persistentData['packager'] = $packagePackager;
00664 
00665         $result = true;
00666         if ( $packageName == '' )
00667         {
00668             $errorList[] = array( 'field' => ezpI18n::tr( 'kernel/package', 'Package name' ),
00669                                   'description' => ezpI18n::tr( 'kernel/package', 'Package name is missing' ) );
00670             $result = false;
00671         }
00672         else
00673         {
00674             $existingPackage = eZPackage::fetch( $packageName, false, true );
00675             if ( $existingPackage )
00676             {
00677                 $errorList[] = array( 'field' => ezpI18n::tr( 'kernel/package', 'Package name' ),
00678                                       'description' => ezpI18n::tr( 'kernel/package', 'A package named %packagename already exists, please give another name', false, array( '%packagename' => $packageName ) ) );
00679                 $result = false;
00680             }
00681             else
00682             {
00683                 // Make sure the package name contains only valid characters
00684                 $trans = eZCharTransform::instance();
00685                 $validPackageName = $trans->transformByGroup( $packageName, 'identifier' );
00686                 if ( strcmp( $validPackageName, $packageName ) != 0 )
00687                 {
00688                     $errorList[] = array( 'field' => ezpI18n::tr( 'kernel/package', 'Package name' ),
00689                                           'description' => ezpI18n::tr( 'kernel/package', "The package name %packagename is not valid, it can only contain characters in the range a-z, 0-9 and underscore.", false, array( '%packagename' => $packageName ) ) );
00690                     $result = false;
00691                 }
00692             }
00693         }
00694         if ( !$packageSummary )
00695         {
00696             $errorList[] = array( 'field' => ezpI18n::tr( 'kernel/package', 'Summary' ),
00697                                   'description' => ezpI18n::tr( 'kernel/package', 'Summary is missing' ) );
00698             $result = false;
00699         }
00700         if ( !preg_match( "#^[0-9](\.[0-9]([a-zA-Z]+[0-9]*)?)*$#", $packageVersion ) )
00701         {
00702             $errorList[] = array( 'field' => ezpI18n::tr( 'kernel/package', 'Version' ),
00703                                   'description' => ezpI18n::tr( 'kernel/package', 'The version must only contain numbers (optionally followed by text) and must be delimited by dots (.), e.g. 1.0, 3.4.0beta1' ) );
00704             $result = false;
00705         }
00706         return $result;
00707     }
00708 
00709     /*!
00710      Commits package information.
00711     */
00712     function commitPackageInformation( $package, $http, $step, &$persistentData, $tpl )
00713     {
00714     }
00715 
00716     /*!
00717      Initializes the package changelog step with some values taken from the
00718      current users and the funcvtion initialChangelogEntry().
00719     */
00720     function initializePackageChangelog( $package, $http, $step, &$persistentData, $tpl )
00721     {
00722         $user = eZUser::currentUser();
00723         $userObject = $user->attribute( 'contentobject' );
00724         if ( $userObject )
00725             $changelogPerson = $userObject->attribute( 'name' );
00726         $changelogEmail = $user->attribute( 'email' );
00727         $changelogText = '';
00728 
00729         $persistentData['changelog_person'] = $changelogPerson;
00730         $persistentData['changelog_email'] = $changelogEmail;
00731         if ( !( $package instanceof eZPackage ) )
00732         {
00733             $changelogText = $this->initialChangelogEntry( $package, $http, $step, $persistentData, $tpl );
00734         }
00735         $persistentData['changelog_text'] = $changelogText;
00736         $persistentData['changelog_entries'] = array();
00737     }
00738 
00739     /*!
00740      Checks if the POST variables contains a name and email for the changelog person and
00741      the changelog field contains some text.
00742     */
00743     function validatePackageChangelog( $package, $http, $currentStepID, &$stepMap, &$persistentData, &$errorList )
00744     {
00745         $changelogPerson = false;
00746         $changelogEmail = false;
00747         $changelogText = false;
00748         if ( $http->hasPostVariable( 'PackageChangelogPerson' ) )
00749             $changelogPerson = trim( $http->postVariable( 'PackageChangelogPerson' ) );
00750         if ( $http->hasPostVariable( 'PackageChangelogEmail' ) )
00751             $changelogEmail = $http->postVariable( 'PackageChangelogEmail' );
00752         if ( $http->hasPostVariable( 'PackageChangelogText' ) )
00753             $changelogText = $http->postVariable( 'PackageChangelogText' );
00754 
00755         $persistentData['changelog_person'] = $changelogPerson;
00756         $persistentData['changelog_email'] = $changelogEmail;
00757         $persistentData['changelog_text'] = $changelogText;
00758 
00759         $result = true;
00760         if ( trim( $changelogPerson ) == '' )
00761         {
00762             $errorList[] = array( 'field' => ezpI18n::tr( 'kernel/package', 'Name' ),
00763                                   'description' => ezpI18n::tr( 'kernel/package', 'You must enter a name for the changelog' ) );
00764             $result = false;
00765         }
00766         if ( trim( $changelogEmail ) == '' )
00767         {
00768             $errorList[] = array( 'field' => ezpI18n::tr( 'kernel/package', 'Email' ),
00769                                   'description' => ezpI18n::tr( 'kernel/package', 'You must enter an email for the changelog' ) );
00770             $result = false;
00771         }
00772         if ( trim( $changelogText ) == '' )
00773         {
00774             $errorList[] = array( 'field' => ezpI18n::tr( 'kernel/package', 'Changelog' ),
00775                                   'description' => ezpI18n::tr( 'kernel/package', 'You must supply some text for the changelog entry' ) );
00776             $result = false;
00777         }
00778         return $result;
00779     }
00780 
00781     /*!
00782      Parses the changelog entry text and turns into an array with change entries.
00783     */
00784     function commitPackageChangelog( $package, $http, $step, &$persistentData, $tpl )
00785     {
00786         $changelogEntries = array();
00787         $changelogText = $persistentData['changelog_text'];
00788         $lines = preg_split( "#\r\n|\n|\r#", $changelogText );
00789         $currentEntries = false;
00790         foreach ( $lines as $line )
00791         {
00792             if ( strlen( $line ) > 0 and
00793                  ( $line[0] == '-' or $line[0] == '*' ) )
00794             {
00795                 if ( $currentEntries !== false )
00796                 {
00797                     $changelogEntries[] = implode( ' ', $currentEntries );
00798                 }
00799                 $currentEntries = array();
00800                 $currentEntries[] = trim( substr( $line, 1 ) );
00801             }
00802             else
00803             {
00804                 if ( $currentEntries === false )
00805                 {
00806                     $changelogEntries = array();
00807                 }
00808                 $currentEntries[] = trim( $line );
00809             }
00810         }
00811         if ( $currentEntries !== false )
00812         {
00813             $changelogEntries[] = implode( ' ', $currentEntries );
00814         }
00815         $persistentData['changelog_entries'] = $changelogEntries;
00816     }
00817 
00818     /*!
00819      Initializes the package maintainer step with some values taken from the current user.
00820     */
00821     function initializePackageMaintainer( $package, $http, $step, &$persistentData, $tpl )
00822     {
00823         $maintainerPerson = false;
00824         $maintainerEmail = false;
00825         $user = eZUser::currentUser();
00826            $userObject = $user->attribute( 'contentobject' );
00827         if ( $userObject )
00828             $maintainerPerson = $userObject->attribute( 'name' );
00829         $maintainerEmail = $user->attribute( 'email' );
00830         $persistentData['maintainer_person'] = $maintainerPerson;
00831         $persistentData['maintainer_email'] = $maintainerEmail;
00832         $persistentData['maintainer_role'] = false;
00833     }
00834 
00835     /*!
00836      Checks if the POST variables has a name and email for the person.
00837     */
00838     function validatePackageMaintainer( $package, $http, $currentStepID, &$stepMap, &$persistentData, &$errorList )
00839     {
00840         $maintainerPerson = false;
00841         $maintainerEmail = false;
00842         $maintainerRole = false;
00843         if ( $http->hasPostVariable( 'PackageMaintainerPerson' ) )
00844             $maintainerPerson = trim( $http->postVariable( 'PackageMaintainerPerson' ) );
00845         if ( $http->hasPostVariable( 'PackageMaintainerEmail' ) )
00846             $maintainerEmail = $http->postVariable( 'PackageMaintainerEmail' );
00847         if ( $http->hasPostVariable( 'PackageMaintainerRole' ) )
00848             $maintainerRole = $http->postVariable( 'PackageMaintainerRole' );
00849 
00850         $persistentData['maintainer_person'] = $maintainerPerson;
00851         $persistentData['maintainer_email'] = $maintainerEmail;
00852         $persistentData['maintainer_role'] = $maintainerRole;
00853 
00854         $result = true;
00855         if ( trim( $maintainerPerson ) == '' )
00856         {
00857             $errorList[] = array( 'field' => ezpI18n::tr( 'kernel/package', 'Name' ),
00858                                   'description' => ezpI18n::tr( 'kernel/package', 'You must enter a name of the maintainer' ) );
00859             $result = false;
00860         }
00861         if ( trim( $maintainerEmail ) == '' )
00862         {
00863             $errorList[] = array( 'field' => ezpI18n::tr( 'kernel/package', 'Email' ),
00864                                   'description' => ezpI18n::tr( 'kernel/package', 'You must enter an email address of the maintainer' ) );
00865             $result = false;
00866         }
00867         return $result;
00868     }
00869 
00870     /*!
00871      Commits maintainer step data. Does nothing for now.
00872     */
00873     function commitPackageMaintainer( $package, $http, $step, &$persistentData, $tpl )
00874     {
00875     }
00876 
00877     /*!
00878      Checks if the maintainer step is required and return \c true if so,
00879      otherwise \c false.
00880      The maintainer step is not required if the user has no maintainer roles to use
00881      or if the package already has a maintainer with the same name as the current user.
00882     */
00883     function checkPackageMaintainer( $package, &$persistentData )
00884     {
00885         $roleList = eZPackage::fetchMaintainerRoleIDList( $this->packageType( $package, $persistentData ), true );
00886         if ( count( $roleList ) > 0 )
00887         {
00888             if ( $package instanceof eZPackage )
00889             {
00890                 $maintainerPerson = false;
00891                 $user = eZUser::currentUser();
00892                    $userObject = $user->attribute( 'contentobject' );
00893                 if ( $userObject )
00894                     $maintainerPerson = $userObject->attribute( 'name' );
00895 
00896                 $maintainers = $package->attribute( 'maintainers' );
00897                 foreach ( $maintainers as $maintainer )
00898                 {
00899                     if ( $maintainer['person'] == $maintainerPerson )
00900                     {
00901                            return false;
00902                     }
00903                 }
00904             }
00905             return true;
00906         }
00907         return false;
00908     }
00909 
00910     /*!
00911      Initializes the package thumbnail step.
00912     */
00913     function initializePackageThumbnail( $package, $http, $step, &$persistentData, $tpl )
00914     {
00915         $persistentData['thumbnail'] = false;
00916     }
00917 
00918     /*!
00919      Checks if the POST variables has a proper thumbnail image.
00920     */
00921     function validatePackageThumbnail( $package, $http, $currentStepID, &$stepMap, &$persistentData, &$errorList )
00922     {
00923         // If we don't have an image we continue as normal
00924         if ( !eZHTTPFile::canFetch( 'PackageThumbnail' ) )
00925             return true;
00926 
00927         $file = eZHTTPFile::fetch( 'PackageThumbnail' );
00928 
00929         $result = true;
00930         if ( $file )
00931         {
00932             $mimeData = eZMimeType::findByFileContents( $file->attribute( 'original_filename' ) );
00933             $dir = eZSys::storageDirectory() . '/temp';
00934             eZMimeType::changeDirectoryPath( $mimeData, $dir );
00935             $file->store( false, false, $mimeData );
00936             $persistentData['thumbnail'] = $mimeData;
00937         }
00938         return $result;
00939     }
00940 
00941     /*!
00942      Commits thumbnail step data. Does nothing for now.
00943     */
00944     function commitPackageThumbnail( $package, $http, $step, &$persistentData, $tpl )
00945     {
00946     }
00947 
00948     /*!
00949      \static
00950      Appends the GPL licence file to the package object \a $package.
00951     */
00952     static function appendLicence( $package )
00953     {
00954         $package->appendDocument( 'LICENCE', false, false, false, true,
00955                                   "This file is part of the package " . $package->attribute( 'name' ) . ".\n" .
00956                                   "\n" .
00957                                   "This package is free software; you can redistribute it and/or modify\n" .
00958                                   "it under the terms of the GNU General Public License as published by\n" .
00959                                   "the Free Software Foundation; either version 2 of the License, or\n" .
00960                                   "(at your option) any later version.\n" .
00961                                   "\n" .
00962                                   "This package is distributed in the hope that it will be useful,\n" .
00963                                   "but WITHOUT ANY WARRANTY; without even the implied warranty of\n" .
00964                                   "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n" .
00965                                   "GNU General Public License for more details.\n" .
00966                                   "\n" .
00967                                   "You should have received a copy of the GNU General Public License\n" .
00968                                   "along with this package; if not, write to the Free Software\n" .
00969                                   "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\n" );
00970     }
00971 }
00972 
00973 ?>