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
00040 include_once( "lib/ezdb/classes/ezdb.php" );
00041 include_once( "kernel/classes/ezpolicylimitationvalue.php" );
00042 include_once( "kernel/classes/ezpersistentobject.php" );
00043
00044 class eZPolicyLimitation extends eZPersistentObject
00045 {
00046
00047
00048
00049 function eZPolicyLimitation( $row )
00050 {
00051 $this->eZPersistentObject( $row );
00052 $this->NodeID = 0;
00053 }
00054
00055 function definition()
00056 {
00057 return array( "fields" => array( "id" => array( 'name' => 'ID',
00058 'datatype' => 'integer',
00059 'default' => 0,
00060 'required' => true ),
00061 'policy_id' => array( 'name' => 'PolicyID',
00062 'datatype' => 'integer',
00063 'default' => 0,
00064 'required' => true,
00065 'foreign_class' => 'eZPolicy',
00066 'foreign_attribute' => 'id',
00067 'multiplicity' => '1..*' ),
00068 'identifier' => array( 'name' => 'Identifier',
00069 'datatype' => 'string',
00070 'default' => '',
00071 'required' => true ) ),
00072 "keys" => array( "id" ),
00073 "function_attributes" => array( 'policy' => 'policy',
00074 'values' => 'valueList',
00075 'values_as_array' => 'allValues',
00076 'values_as_string' => 'allValuesAsString',
00077 'values_as_array_with_names' => 'allValuesAsArrayWithNames',
00078 'limit_value' => 'limitValue' ),
00079 "increment_key" => "id",
00080 "sort" => array( "id" => "asc" ),
00081 "class_name" => "eZPolicyLimitation",
00082 "name" => "ezpolicy_limitation" );
00083 }
00084
00085 function &limitValue()
00086 {
00087 return $this->LimitValue;
00088 }
00089
00090
00091
00092
00093 function &policy()
00094 {
00095 include_once( 'kernel/classes/ezpolicy.php' );
00096 $policy = eZPolicy::fetch( $this->attribute( 'policy_id' ) );
00097 return $policy;
00098 }
00099
00100
00101
00102
00103 function setAttribute( $attr, $val )
00104 {
00105 switch( $attr )
00106 {
00107 case 'limit_value':
00108 {
00109 $this->LimitValue = $val;
00110 } break;
00111
00112 default:
00113 {
00114 eZPersistentObject::setAttribute( $attr, $val );
00115 } break;
00116 }
00117 }
00118
00119
00120
00121
00122
00123 function createNew( $policyID, $identifier )
00124 {
00125 $policyParameter = new eZPolicyLimitation( array() );
00126 $policyParameter->setAttribute( 'policy_id', $policyID );
00127 $policyParameter->setAttribute( 'identifier', $identifier );
00128 $policyParameter->store();
00129
00130 return $policyParameter;
00131 }
00132
00133
00134
00135
00136
00137
00138 function &create( $policyID, $identifier )
00139 {
00140 $row = array( 'id' => null,
00141 'policy_id' => $policyID,
00142 'identifier' => $identifier );
00143 $limitation = new eZPolicyLimitation( $row );
00144 return $limitation;
00145 }
00146
00147
00148
00149
00150
00151 function removeSelected( $ID )
00152 {
00153 eZPersistentObject::removeObject( eZPolicyLimitation::definition(),
00154 array( "id" => $ID ) );
00155 }
00156
00157 function fetchByIdentifier( $policyID, $identifier, $asObject = true )
00158 {
00159 return eZPersistentObject::fetchObject( eZPolicyLimitation::definition(),
00160 null,
00161 array( "policy_id" => $policyID,
00162 "identifier" => $identifier ),
00163 $asObject );
00164 }
00165
00166 function fetchByPolicyID( $policyID, $asObject = true )
00167 {
00168 return eZPersistentObject::fetchObjectList( eZPolicyLimitation::definition(),
00169 null,
00170 array( "policy_id" => $policyID ),
00171 null,
00172 null,
00173 $asObject );
00174 }
00175
00176
00177
00178
00179
00180 function copy( $policyID )
00181 {
00182 $newParameter = eZPolicyLimitation::createNew( $policyID, $this->attribute( 'identifier' ) );
00183 foreach( $this->attribute( 'values' ) as $value )
00184 {
00185 $value->copy( $newParameter->attribute( 'id' ) );
00186 }
00187 }
00188
00189
00190
00191
00192
00193 function remove( $id = false )
00194 {
00195 if ( is_numeric( $id ) )
00196 {
00197 $delID = $id;
00198
00199 }
00200 else
00201 {
00202
00203 $delID = $this->ID;
00204 }
00205
00206 $db =& eZDB::instance();
00207 $db->begin();
00208
00209 $db->query( "DELETE FROM ezpolicy_limitation_value
00210 WHERE ezpolicy_limitation_value.limitation_id = '$delID'" );
00211
00212 $db->query( "DELETE FROM ezpolicy_limitation
00213 WHERE ezpolicy_limitation.id = '$delID' " );
00214 $db->commit();
00215 }
00216
00217 function &allValuesAsString()
00218 {
00219 $str='';
00220 foreach ( $this->attribute( 'values' ) as $value )
00221 {
00222 if ( $str == '' )
00223 {
00224 $str .= $value->attribute( 'value' );
00225 }else
00226 {
00227 $str .= ',' . $value->attribute( 'value' );
00228 }
00229 }
00230 return $str;
00231 }
00232
00233 function &allValuesAsArrayWithNames()
00234 {
00235 $returnValue = null;
00236 $valueList =& $this->attribute( 'values_as_array' );
00237 $names = array();
00238 $policy =& $this->attribute( 'policy' );
00239 if ( !$policy )
00240 {
00241 return $returnValue;
00242 }
00243
00244 $currentModule = $policy->attribute( 'module_name' );
00245 $mod = & eZModule::exists( $currentModule );
00246 if ( !is_object( $mod ) )
00247 {
00248 eZDebug::writeError( 'Failed to fetch instance for module ' . $currentModule );
00249 return $returnValue;
00250 }
00251 $functions =& $mod->attribute( 'available_functions' );
00252 $functionNames = array_keys( $functions );
00253
00254 $currentFunction = $policy->attribute( 'function_name' );
00255 $limitationValueArray = array();
00256
00257 $limitation =& $functions[ $currentFunction ][$this->attribute( 'identifier' )];
00258
00259 if ( $limitation &&
00260 count( $limitation[ 'values' ] == 0 ) &&
00261 array_key_exists( 'class', $limitation ) )
00262 {
00263 $basePath = 'kernel/';
00264 if( array_key_exists( 'extension', $limitation ) && $limitation['extension'] )
00265 {
00266 $basePath = 'extension/' . $limitation['extension'] . '/';
00267 }
00268 include_once( $basePath . $limitation['path'] . $limitation['file'] );
00269 $obj = new $limitation['class']( array() );
00270 $limitationValueList = call_user_func_array ( array( &$obj , $limitation['function']) , $limitation['parameter'] );
00271 foreach( $limitationValueList as $limitationValue )
00272 {
00273 $limitationValuePair = array();
00274 $limitationValuePair['Name'] = $limitationValue[ 'name' ];
00275 $limitationValuePair['value'] = $limitationValue[ 'id' ];
00276 $limitationValueArray[] = $limitationValuePair;
00277 }
00278 }
00279 else if ( $limitation['name'] == "Node" )
00280 {
00281 include_once( 'kernel/classes/ezcontentobjecttreenode.php' );
00282 foreach ( $valueList as $value )
00283 {
00284 $node = eZContentObjectTreeNode::fetch( $value, false, false );
00285 if ( $node == null )
00286 continue;
00287 $limitationValuePair = array();
00288 $limitationValuePair['Name'] = $node['name'];
00289 $limitationValuePair['value'] = $value;
00290 $limitationValueArray[] = $limitationValuePair;
00291 }
00292 }
00293 else if ( $limitation['name'] == "Subtree" )
00294 {
00295 include_once( 'kernel/classes/ezcontentobjecttreenode.php' );
00296 foreach ( $valueList as $value )
00297 {
00298 $subtreeObject = eZContentObjectTreeNode::fetchByPath( $value, false );
00299 if ( $subtreeObject != null )
00300 {
00301 $limitationValuePair = array();
00302 $limitationValuePair['Name'] = $subtreeObject['name'];
00303 $limitationValuePair['value'] = $value;
00304 $limitationValueArray[] = $limitationValuePair;
00305 }
00306 }
00307 }
00308 else
00309 {
00310 $limitationValueArray = $limitation[ 'values' ];
00311 }
00312 $limitationValuesWithNames = array();
00313 foreach ( array_keys( $valueList ) as $key )
00314 {
00315 $value = $valueList[$key];
00316 if ( isset( $limitationValueArray ) )
00317 {
00318 reset( $limitationValueArray );
00319 foreach ( array_keys( $limitationValueArray ) as $ckey )
00320 {
00321 if ( $value == $limitationValueArray[$ckey]['value'] )
00322 {
00323 $limitationValuesWithNames[] =& $limitationValueArray[$ckey];
00324 }
00325 }
00326 }
00327 }
00328
00329 return $limitationValuesWithNames;
00330 }
00331
00332
00333
00334
00335
00336
00337 function limitArray()
00338 {
00339 $limitValues =& $this->attribute( 'values' );
00340
00341 $valueArray = array();
00342
00343 foreach ( array_keys( $limitValues ) as $valueKey )
00344 {
00345 $valueArray[] = $limitValues[$valueKey]->attribute( 'value' );
00346 }
00347
00348 return array( $this->attribute( 'identifier' ) => $valueArray );
00349 }
00350
00351 function &allValues()
00352 {
00353 $values = array();
00354 foreach ( $this->attribute( 'values' ) as $value )
00355 {
00356 $values[] = $value->attribute( 'value' );
00357 }
00358
00359 return $values;
00360 }
00361
00362 function &valueList()
00363 {
00364 if ( !isset( $this->Values ) )
00365 {
00366 $values = eZPersistentObject::fetchObjectList( eZPolicyLimitationValue::definition(),
00367 null, array( 'limitation_id' => $this->attribute( 'id') ), null, null,
00368 true);
00369
00370 if ( $this->LimitValue )
00371 {
00372 $values[] = new eZPolicyLimitationValue( array ( 'id' => -1,
00373 'value' => $this->LimitValue ) );
00374 }
00375
00376 $this->Values =& $values;
00377 }
00378
00379 return $this->Values;
00380 }
00381
00382 function findByType( $type, $value, $asObject = true, $useLike = true )
00383 {
00384 $cond = '';
00385 $db = eZDB::instance();
00386 $value = $db->escapeString( $value );
00387 $type = $db->escapeString( $type );
00388 if ( $useLike === true )
00389 {
00390 $cond = "ezpolicy_limitation_value.value like '$value%' ";
00391 }
00392 else
00393 {
00394 $cond = "ezpolicy_limitation_value.value = '$value' ";
00395 }
00396
00397 $query = "SELECT DISTINCT ezpolicy_limitation.*
00398 FROM ezpolicy_limitation,
00399 ezpolicy_limitation_value
00400 WHERE
00401 ezpolicy_limitation.identifier = '$type' AND
00402 $cond AND
00403 ezpolicy_limitation_value.limitation_id = ezpolicy_limitation.id";
00404
00405 $dbResult = $db->arrayQuery( $query );
00406 $resultArray = array();
00407 $resultCount = count( $dbResult );
00408 for( $i = 0; $i < $resultCount; $i++ )
00409 {
00410 if ( $asObject )
00411 {
00412 $resultArray[] = new eZPolicyLimitation( $dbResult[$i] );
00413 }
00414 else
00415 {
00416 $resultArray[] = $dbResult[$i]['id'];
00417 }
00418 }
00419 return $resultArray;
00420 }
00421
00422
00423 var $LimitValue;
00424
00425 }
00426
00427 ?>