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