00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 include_once( "lib/ezutils/classes/ezdebug.php" );
00040 include_once( "kernel/classes/ezcontentobjecttreenode.php" );
00041
00042
00043
00044 define( "EZ_NODE_ASSIGNMENT_OP_CODE_NOP", 0 );
00045 define( "EZ_NODE_ASSIGNMENT_OP_CODE_EXECUTE", 1 );
00046
00047 define( "EZ_NODE_ASSIGNMENT_OP_CODE_CREATE_NOP", 2 );
00048 define( "EZ_NODE_ASSIGNMENT_OP_CODE_CREATE", 3 );
00049
00050 define( "EZ_NODE_ASSIGNMENT_OP_CODE_MOVE_NOP", 4 );
00051 define( "EZ_NODE_ASSIGNMENT_OP_CODE_MOVE", 5 );
00052
00053 define( "EZ_NODE_ASSIGNMENT_OP_CODE_REMOVE_NOP", 6 );
00054 define( "EZ_NODE_ASSIGNMENT_OP_CODE_REMOVE", 7 );
00055
00056 define( "EZ_NODE_ASSIGNMENT_OP_CODE_SET_NOP", 8 );
00057 define( "EZ_NODE_ASSIGNMENT_OP_CODE_SET", 9 );
00058
00059 class eZNodeAssignment extends eZPersistentObject
00060 {
00061
00062
00063
00064 function eZNodeAssignment( $row )
00065 {
00066 $this->TempNode = null;
00067 $this->Name = false;
00068 $this->eZPersistentObject( $row );
00069 }
00070
00071 function definition()
00072 {
00073 return array( 'fields' => array( 'id' => array( 'name' => 'ID',
00074 'datatype' => 'integer',
00075 'default' => 0,
00076 'required' => true ),
00077 'remote_id' => array( 'name' => 'RemoteID',
00078 'datatype' => 'integer',
00079 'default' => 0,
00080 'required' => true ),
00081 'contentobject_id' => array( 'name' => 'ContentobjectID',
00082 'datatype' => 'integer',
00083 'default' => 0,
00084 'required' => true,
00085 'foreign_class' => 'eZContentObject',
00086 'foreign_attribute' => 'id',
00087 'multiplicity' => '1..*' ),
00088 'contentobject_version' => array( 'name' => 'ContentObjectVersion',
00089 'datatype' => 'integer',
00090 'default' => 0,
00091 'required' => true ),
00092 'parent_node' => array( 'name' => 'ParentNode',
00093 'datatype' => 'integer',
00094 'default' => 0,
00095 'required' => true,
00096 'foreign_class' => 'eZContentObjectTreeNode',
00097 'foreign_attribute' => 'node_id',
00098 'multiplicity' => '1..*' ),
00099 'sort_field' => array( 'name' => 'SortField',
00100 'datatype' => 'integer',
00101 'default' => 1,
00102 'required' => true ),
00103 'sort_order' => array( 'name' => 'SortOrder',
00104 'datatype' => 'integer',
00105 'default' => 1,
00106 'required' => true ),
00107 'is_main' => array( 'name' => 'Main',
00108 'datatype' => 'integer',
00109 'default' => 0,
00110 'required' => true ),
00111 'from_node_id' => array( 'name' => 'FromNodeID',
00112 'datatype' => 'integer',
00113 'default' => 0,
00114 'required' => true,
00115 'foreign_class' => 'eZContentObjectTreeNode',
00116 'foreign_attribute' => 'node_id',
00117 'multiplicity' => '1..*' ),
00118 'parent_remote_id' => array( 'name' => 'ParentRemoteID',
00119 'datatype' => 'string',
00120 'default' => '',
00121 'required' => false ),
00122 'op_code' => array( 'name' => 'OpCode',
00123 'datatype' => 'int',
00124 'default' => 0,
00125 'required' => true ) ),
00126 'keys' => array( 'id' ),
00127 "function_attributes" => array( "parent_node_obj" => "getParentNode",
00128 "parent_contentobject" => "getParentObject",
00129 "node" => "fetchNode",
00130 'is_nop_operation' => 'isNopOperation',
00131 'is_create_operation' => 'isCreateOperation',
00132 'is_move_operation' => 'isMoveOperation',
00133 'is_remove_operation' => 'isRemoveOperation',
00134 'is_set_operation' => 'isSetOperation',
00135 'temp_node' => 'tempNode' ),
00136 "increment_key" => "id",
00137 'class_name' => 'eZNodeAssignment',
00138 'name' => 'eznode_assignment' );
00139 }
00140
00141 function &tempNode()
00142 {
00143 if ( $this->TempNode == null )
00144 {
00145 $this->TempNode = eZContentObjectTreeNode::create( $this->attribute( 'parent_node' ),
00146 $this->attribute( 'contentobject_id' ),
00147 $this->attribute( 'contentobject_version' ),
00148 $this->attribute( 'sort_field' ),
00149 $this->attribute( 'sort_order' ) );
00150 $this->TempNode->setName( $this->Name );
00151 }
00152 return $this->TempNode;
00153 }
00154
00155 function setName( $name )
00156 {
00157 return $this->Name = $name;
00158 }
00159
00160 function name()
00161 {
00162 return $this->Name;
00163 }
00164
00165
00166
00167
00168
00169 function &isNopOperation()
00170 {
00171 $isNopOperation = ( $this->OpCode & 1 ) == EZ_NODE_ASSIGNMENT_OP_CODE_NOP;
00172 return $isNopOperation;
00173 }
00174
00175
00176
00177
00178
00179 function &isCreateOperation()
00180 {
00181 $isCreateOperation = $this->OpCode == EZ_NODE_ASSIGNMENT_OP_CODE_CREATE;
00182 return $isCreateOperation;
00183 }
00184
00185
00186
00187
00188
00189 function &isMoveOperation()
00190 {
00191 $isMoveOperation = $this->OpCode == EZ_NODE_ASSIGNMENT_OP_CODE_MOVE;
00192 return $isMoveOperation;
00193 }
00194
00195
00196
00197
00198
00199 function &isRemoveOperation()
00200 {
00201 $isRemoveOperation = $this->OpCode == EZ_NODE_ASSIGNMENT_OP_CODE_REMOVE;
00202 return $isRemoveOperation;
00203 }
00204
00205
00206
00207
00208
00209 function &isSetOperation()
00210 {
00211 $isSetOperation = $this->OpCode == EZ_NODE_ASSIGNMENT_OP_CODE_SET;
00212 return $isSetOperation;
00213 }
00214
00215 function create( $parameters = array() )
00216 {
00217 if ( !isset( $parameters['contentobject_id'] ) )
00218 {
00219 eZDebug::writeError( $parameters, "Cannot create node assignment without contentobject_id" );
00220 $retValue = null;
00221 return $retValue;
00222 }
00223 if ( !isset( $parameters['contentobject_version'] ) )
00224 {
00225 $parameters['contentobject_version'] = 1;
00226 }
00227 if ( !isset( $parameters['remote_id'] ) )
00228 {
00229 $parameters['remote_id'] = 0;
00230 }
00231 if ( !isset( $parameters['parent_node'] ) )
00232 {
00233 $parameters['parent_node'] = 2;
00234 }
00235 if ( !isset( $parameters['is_main'] ) )
00236 {
00237 $parameters['is_main'] = 0;
00238 }
00239 if ( !isset( $parameters['sort_field'] ) )
00240 {
00241 $parameters['sort_field'] = 2;
00242 }
00243 if ( !isset( $parameters['sort_order'] ) )
00244 {
00245 $parameters['sort_order'] = 0;
00246 }
00247 if ( !isset( $parameters['from_node_id'] ) )
00248 {
00249 $parameters['from_node_id'] = 0;
00250 }
00251 if ( !isset( $parameters['parent_remote_id'] ) )
00252 {
00253 $parameters['parent_remote_id'] = '';
00254 }
00255 if ( !isset( $parameters['op_code'] ) )
00256 {
00257
00258 $parameters['op_code'] = EZ_NODE_ASSIGNMENT_OP_CODE_CREATE;
00259 }
00260
00261 return new eZNodeAssignment( $parameters );
00262 }
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274 function remove( $parentNodeID = false, $contentObjectID = false )
00275 {
00276 $db =& eZDB::instance();
00277 if ( $parentNodeID == false and $contentObjectID == false )
00278 {
00279 $nodeAssignment =& $this;
00280 }
00281 else
00282 {
00283 $parentNodeID =(int) $parentNodeID;
00284 $contentObjectID =(int) $contentObjectID;
00285 $cond = array( 'parent_node' => $parentNodeID,
00286 'contentobject_id' => $contentObjectID );
00287 $nodeAssignment =& eZPersistentObject::fetchObject( eZNodeAssignment::definition(),
00288 null,
00289 $cond,
00290 true );
00291 }
00292 $nodeAssignment->setAttribute( "op_code", EZ_NODE_ASSIGNMENT_OP_CODE_REMOVE );
00293 $nodeAssignment->store();
00294 }
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306 function removeByID( $assignmentID )
00307 {
00308 $db =& eZDB::instance();
00309 if ( is_array( $assignmentID ) )
00310 {
00311 if ( count( $assignmentID ) == 0 )
00312 {
00313 return false;
00314 }
00315 $sql = "UPDATE eznode_assignment SET op_code = " . EZ_NODE_ASSIGNMENT_OP_CODE_REMOVE . ", is_main = 0 WHERE id IN ( ";
00316 $i = 0;
00317 foreach ( $assignmentID as $id )
00318 {
00319 if ( $i > 0 )
00320 $sql .= ", ";
00321 $sql .= (int)$id;
00322 ++$i;
00323 }
00324 $sql .= ' )';
00325 }
00326 else
00327 {
00328 $sql = "UPDATE eznode_assignment SET op_code = " . EZ_NODE_ASSIGNMENT_OP_CODE_REMOVE . ", is_main = 0 WHERE id=" . (int)$assignmentID;
00329 }
00330 $db->query( $sql );
00331 return true;
00332 }
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343 function purge( $parentNodeID = false, $contentObjectID = false )
00344 {
00345 $db =& eZDB::instance();
00346 if ( $parentNodeID == false and $contentObjectID == false )
00347 {
00348 $nodeAssignment =& $this;
00349 $nodeAssignmentID = $nodeAssignment->attribute( 'id' );
00350 $sqlQuery = "DELETE FROM eznode_assignment WHERE id='$nodeAssignmentID'";
00351 $db->query( $sqlQuery );
00352 }
00353 else
00354 {
00355 $parentNodeID =(int) $parentNodeID;
00356 $contentObjectID =(int) $contentObjectID;
00357 $sqlQuery = "DELETE FROM eznode_assignment WHERE parent_node='$parentNodeID' AND contentobject_id='$contentObjectID'";
00358 $db->query( $sqlQuery );
00359 }
00360 }
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372 function purgeByID( $assignmentID )
00373 {
00374 $db =& eZDB::instance();
00375 if ( is_array( $assignmentID ) )
00376 {
00377 if ( count( $assignmentID ) == 0 )
00378 {
00379 return false;
00380 }
00381 $sql = "DELETE FROM eznode_assignment WHERE id IN ( ";
00382 $i = 0;
00383 foreach ( $assignmentID as $id )
00384 {
00385 if ( $i > 0 )
00386 $sql .= ", ";
00387 $sql .= (int)$id;
00388 ++$i;
00389 }
00390 $sql .= ' )';
00391 }
00392 else
00393 {
00394 $sql = "DELETE FROM eznode_assignment WHERE id=" . (int)$assignmentID;
00395 }
00396 $db->query( $sql );
00397 return true;
00398 }
00399
00400 function fetchForObject( $contentObjectID, $version = 1, $main = 0, $asObject = true )
00401 {
00402 $cond = array( 'contentobject_id' => $contentObjectID,
00403 'contentobject_version' => $version );
00404 if( $main > 0 )
00405 {
00406 $cond['is_main'] = 1;
00407 }
00408 $objectList = eZPersistentObject::fetchObjectList( eZNodeAssignment::definition(),
00409 null,
00410 $cond,
00411 null,
00412 null,
00413 $asObject );
00414 return $objectList;
00415 }
00416
00417 function fetch( $contentObjectID, $version = 1, $parentNode = 0 ,$asObject = true )
00418 {
00419 $cond = array( 'contentobject_id' => $contentObjectID,
00420 'contentobject_version' => $version,
00421 'parent_node' => $parentNode );
00422 return eZPersistentObject::fetchObject( eZNodeAssignment::definition(),
00423 null,
00424 $cond,
00425 $asObject );
00426 }
00427
00428
00429
00430
00431
00432
00433 function &fetchNode()
00434 {
00435 $node = eZContentObjectTreeNode::fetchNode( $this->ContentobjectID, $this->ParentNode );
00436 return $node;
00437 }
00438
00439
00440
00441
00442
00443 function fetchByID( $id ,$asObject = true )
00444 {
00445 $cond = array( 'id' => $id );
00446 return eZPersistentObject::fetchObject( eZNodeAssignment::definition(),
00447 null, $cond,
00448 $asObject );
00449 }
00450
00451
00452
00453
00454
00455 function fetchListByID( $idList ,$asObject = true )
00456 {
00457 $cond = array( 'id' => array( $idList ) );
00458 return eZPersistentObject::fetchObjectList( eZNodeAssignment::definition(),
00459 null, $cond, null, null,
00460 $asObject );
00461 }
00462
00463 function clone( $nextVersionNumber = 1, $contentObjectID = false )
00464 {
00465 $assignmentRow = array( 'contentobject_id' => $this->attribute( 'contentobject_id' ),
00466 'contentobject_version' => $nextVersionNumber,
00467 'remote_id' => $this->attribute( 'remote_id' ),
00468 'parent_node' => $this->attribute( 'parent_node' ),
00469 'sort_field' => $this->attribute( 'sort_field' ),
00470 'sort_order' => $this->attribute( 'sort_order' ),
00471 'is_main' => $this->attribute( 'is_main' ),
00472 'parent_remote_id' => $this->attribute( 'parent_remote_id' ) );
00473 if ( $contentObjectID !== false )
00474 $assignmentRow['contentobject_id'] = $contentObjectID;
00475 return eZNodeAssignment::create( $assignmentRow );
00476 }
00477
00478 function &getParentNode()
00479 {
00480 $parent = eZContentObjectTreeNode::fetch( $this->attribute( 'parent_node' ) );
00481 return $parent;
00482 }
00483
00484
00485
00486
00487 function &getParentObject( )
00488 {
00489 $parentObject =& eZContentObject::fetchByNodeID( $this->attribute( 'parent_node' ) );
00490 return $parentObject;
00491 }
00492
00493
00494
00495
00496
00497
00498
00499
00500 function setNewMainAssignment( $objectID, $version )
00501 {
00502
00503 $assignments = eZNodeAssignment::fetchForObject( $objectID, $version );
00504
00505 if ( count( $assignments ) == 0 )
00506 return true;
00507
00508
00509
00510 $newMainAssignment = null;
00511 foreach ( $assignments as $key => $assignment )
00512 {
00513 if ( $assignment->attribute( 'op_code' ) != EZ_NODE_ASSIGNMENT_OP_CODE_REMOVE )
00514 {
00515 if ( $newMainAssignment === null )
00516 {
00517 $newMainAssignment =& $assignment;
00518 }
00519 if ( $assignment->attribute( 'is_main' ) )
00520 {
00521 return false;
00522 }
00523 }
00524 }
00525
00526 $db =& eZDB::instance();
00527
00528 if ( $newMainAssignment === null )
00529 {
00530 $db->query( "UPDATE eznode_assignment SET is_main=0 WHERE contentobject_id=$objectID AND contentobject_version=$version" );
00531 return false;
00532 }
00533
00534 $parentMainNodeID = $newMainAssignment->attribute( 'parent_node' );
00535
00536 $db->begin();
00537 $db->query( "UPDATE eznode_assignment SET is_main=1 WHERE contentobject_id=$objectID AND contentobject_version=$version AND parent_node=$parentMainNodeID" );
00538 $db->query( "UPDATE eznode_assignment SET is_main=0 WHERE contentobject_id=$objectID AND contentobject_version=$version AND parent_node<>$parentMainNodeID" );
00539 $db->commit();
00540
00541 return true;
00542 }
00543
00544
00545 var $ID;
00546
00547
00548 var $RemoteID;
00549 var $ParentRemoteID;
00550 var $ContentobjectID;
00551 var $ContentObjectVersion;
00552 var $ParentNode;
00553 var $SortField;
00554 var $SortOrder;
00555 var $Main;
00556 var $FromNodeID;
00557 }
00558
00559 ?>