[ Index ] |
|
Code source de eZ Publish 3.9.0 |
1 <?php 2 // 3 // Definition of eZCsvimport class 4 // 5 // Created on: <27-Sep-2006 22:23:27 sp> 6 // 7 8 // Copyright (C) 1999-2004 eZ systems as. All rights reserved. 9 // 10 // This source file is part of the eZ publish (tm) Open Source Content 11 // Management System. 12 // 13 // This file may be distributed and/or modified under the terms of the 14 // "GNU General Public License" version 2 as published by the Free 15 // Software Foundation and appearing in the file LICENSE included in 16 // the packaging of this file. 17 // 18 // Licencees holding a valid "eZ publish professional licence" version 2 19 // may use this file in accordance with the "eZ publish professional licence" 20 // version 2 Agreement provided with the Software. 21 // 22 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING 23 // THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24 // PURPOSE. 25 // 26 // The "eZ publish professional licence" version 2 is available at 27 // http://ez.no/ez_publish/licences/professional/ and in the file 28 // PROFESSIONAL_LICENCE included in the packaging of this file. 29 // For pricing of this licence please contact us via e-mail to licence@ez.no. 30 // Further contact information is available at http://ez.no/company/contact/. 31 // 32 // The "GNU General Public License" (GPL) is available at 33 // http://www.gnu.org/copyleft/gpl.html. 34 // 35 // Contact licence@ez.no if any conditions of this licencing isn't clear to 36 // you. 37 // 38 39 /*! \file ezcsvimport.php 40 */ 41 42 /*! 43 \class eZCsvimport ezcsvimport.php 44 \brief The class eZCsvimport does 45 46 */ 47 48 49 include_once ( 'lib/ezutils/classes/ezcli.php' ); 50 include_once ( 'kernel/classes/ezscript.php' ); 51 include_once ( 'kernel/classes/ezcontentobjecttreenode.php' ); 52 include_once ( "lib/ezlocale/classes/ezdatetime.php" ); 53 54 55 $cli =& eZCLI::instance(); 56 $script =& eZScript::instance( array( 'description' => ( "eZ publish CSV import script\n\n" . 57 "\n" . 58 "\n" . 59 "\n" . 60 "\n" . 61 "" ), 62 'use-session' => false, 63 'use-modules' => true, 64 'use-extensions' => true ) ); 65 66 $script->startup(); 67 68 $options = $script->getOptions( "[class:][creator:][storage-dir:]", 69 "[node][file]", 70 array( 'node' => 'parent node_id to upload object under', 71 'file' => 'file to read CSV data from', 72 'class' => 'class identifier to create objects', 73 'creator' => 'user id of imported objects creator', 74 'storage-dir' => 'path to directory which will be added to the path of CSV elements' ), 75 false, 76 array( 'user' => true )); 77 $script->initialize(); 78 79 if ( !$options['node'] and count( $options['arguments'] ) < 2 ) 80 { 81 $cli->error( "Need a parenrt node to place object under and file to read data from" ); 82 $script->shutdown( 1 ); 83 } 84 85 86 $nodeID = $options['arguments'][0]; 87 $inputFileName = $options['arguments'][1]; 88 $createClass = $options['class']; 89 $creator = $options['creator']; 90 if ( $options['storage-dir'] ) 91 { 92 $storageDir = $options['storage-dir']; 93 } 94 else 95 { 96 $storageDir = ''; 97 } 98 99 $csvLineLength = 100000; 100 101 102 $cli->output( "Going to import objects of class " . $createClass . " under node " . $nodeID . " from file " . $inputFileName . "\n" ); 103 104 105 $node = eZContentObjectTreeNode::fetch( $nodeID ); 106 if ( !$node ) 107 { 108 $cli->error( "No such node to import objects" ); 109 $script->shutdown( 1 ); 110 } 111 $parentObject = $node->attribute( 'object' ); 112 113 114 115 $class = eZContentClass::fetchByIdentifier( $createClass ); 116 117 if ( !$class ) 118 { 119 $cli->error( "No class with identifier $createClass" ); 120 $script->shutdown( 1 ); 121 } 122 123 $fp = @fopen( $inputFileName, "r" ); 124 if ( !$fp ) 125 { 126 $cli->error( "Can not open file $inputFileName for reading" ); 127 $script->shutdown( 1 ); 128 } 129 130 while ( $objectData = fgetcsv( $fp, $csvLineLength , ';', '"' ) ) 131 { 132 133 $contentObject = $class->instantiate( $creator ); 134 $contentObject->store(); 135 136 $nodeAssignment = eZNodeAssignment::create( array( 137 'contentobject_id' => $contentObject->attribute( 'id' ), 138 'contentobject_version' => $contentObject->attribute( 'current_version' ), 139 'parent_node' => $nodeID, 140 'is_main' => 1 141 ) 142 ); 143 $nodeAssignment->store(); 144 145 $version =& $contentObject->version( 1 ); 146 $version->setAttribute( 'modified', eZDateTime::currentTimeStamp() ); 147 $version->setAttribute( 'status', EZ_VERSION_STATUS_DRAFT ); 148 $version->store(); 149 150 151 $contentObjectID = $contentObject->attribute( 'id' ); 152 153 $attributes =& $contentObject->attribute( 'contentobject_attributes' ); 154 155 156 157 158 while ( list( $key, $attribute ) = each( $attributes ) ) 159 { 160 $dataString = $objectData[$key]; 161 switch ( $datatypeString = $attribute->attribute( 'data_type_string' ) ) 162 { 163 case 'ezimage': 164 case 'ezbinaryfile': 165 case 'ezmedia': 166 { 167 $dataString = $storageDir . $dataString; 168 break; 169 } 170 default: 171 } 172 $attribute->fromString( $dataString ); 173 $attribute->store(); 174 } 175 176 include_once ( 'lib/ezutils/classes/ezoperationhandler.php' ); 177 $operationResult = eZOperationHandler::execute( 'content', 'publish', array( 'object_id' => $contentObjectID, 178 'version' => 1 ) ); 179 } 180 181 fclose( $fp ); 182 183 $script->shutdown(); 184 185 186 187 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sat Feb 24 10:30:04 2007 | par Balluche grâce à PHPXref 0.7 |