idx
int64 0
60.3k
| question
stringlengths 101
6.21k
| target
stringlengths 7
803
|
|---|---|---|
8,200
|
public function limitImageSize ( $ width , $ height ) { if ( $ this -> maxImageSize !== null ) { $ imageSize = $ width * $ height ; if ( $ imageSize > $ this -> maxImageSize ) { $ width = $ width / sqrt ( $ imageSize / $ this -> maxImageSize ) ; $ height = $ height / sqrt ( $ imageSize / $ this -> maxImageSize ) ; } } return [ ( int ) $ width , ( int ) $ height , ] ; }
|
Limit image size to maximum allowed image size .
|
8,201
|
public function runResize ( Image $ image , $ fit , $ width , $ height ) { if ( $ fit === 'contain' ) { return $ this -> runContainResize ( $ image , $ width , $ height ) ; } if ( $ fit === 'fill' ) { return $ this -> runFillResize ( $ image , $ width , $ height ) ; } if ( $ fit === 'max' ) { return $ this -> runMaxResize ( $ image , $ width , $ height ) ; } if ( $ fit === 'stretch' ) { return $ this -> runStretchResize ( $ image , $ width , $ height ) ; } if ( $ fit === 'crop' ) { return $ this -> runCropResize ( $ image , $ width , $ height ) ; } return $ image ; }
|
Perform resize image manipulation .
|
8,202
|
public function runContainResize ( Image $ image , $ width , $ height ) { return $ image -> resize ( $ width , $ height , function ( $ constraint ) { $ constraint -> aspectRatio ( ) ; } ) ; }
|
Perform contain resize image manipulation .
|
8,203
|
public function runMaxResize ( Image $ image , $ width , $ height ) { return $ image -> resize ( $ width , $ height , function ( $ constraint ) { $ constraint -> aspectRatio ( ) ; $ constraint -> upsize ( ) ; } ) ; }
|
Perform max resize image manipulation .
|
8,204
|
public function runFillResize ( $ image , $ width , $ height ) { $ image = $ this -> runMaxResize ( $ image , $ width , $ height ) ; return $ image -> resizeCanvas ( $ width , $ height , 'center' ) ; }
|
Perform fill resize image manipulation .
|
8,205
|
public function runCropResize ( Image $ image , $ width , $ height ) { list ( $ resize_width , $ resize_height ) = $ this -> resolveCropResizeDimensions ( $ image , $ width , $ height ) ; $ zoom = $ this -> getCrop ( ) [ 2 ] ; $ image -> resize ( $ resize_width * $ zoom , $ resize_height * $ zoom , function ( $ constraint ) { $ constraint -> aspectRatio ( ) ; } ) ; list ( $ offset_x , $ offset_y ) = $ this -> resolveCropOffset ( $ image , $ width , $ height ) ; return $ image -> crop ( $ width , $ height , $ offset_x , $ offset_y ) ; }
|
Perform crop resize image manipulation .
|
8,206
|
public function resolveCropResizeDimensions ( Image $ image , $ width , $ height ) { if ( $ height > $ width * ( $ image -> height ( ) / $ image -> width ( ) ) ) { return [ $ height * ( $ image -> width ( ) / $ image -> height ( ) ) , $ height ] ; } return [ $ width , $ width * ( $ image -> height ( ) / $ image -> width ( ) ) ] ; }
|
Resolve the crop resize dimensions .
|
8,207
|
public function resolveCropOffset ( Image $ image , $ width , $ height ) { list ( $ offset_percentage_x , $ offset_percentage_y ) = $ this -> getCrop ( ) ; $ offset_x = ( int ) ( ( $ image -> width ( ) * $ offset_percentage_x / 100 ) - ( $ width / 2 ) ) ; $ offset_y = ( int ) ( ( $ image -> height ( ) * $ offset_percentage_y / 100 ) - ( $ height / 2 ) ) ; $ max_offset_x = $ image -> width ( ) - $ width ; $ max_offset_y = $ image -> height ( ) - $ height ; if ( $ offset_x < 0 ) { $ offset_x = 0 ; } if ( $ offset_y < 0 ) { $ offset_y = 0 ; } if ( $ offset_x > $ max_offset_x ) { $ offset_x = $ max_offset_x ; } if ( $ offset_y > $ max_offset_y ) { $ offset_y = $ max_offset_y ; } return [ $ offset_x , $ offset_y ] ; }
|
Resolve the crop offset .
|
8,208
|
public function getCrop ( ) { $ cropMethods = [ 'crop-top-left' => [ 0 , 0 , 1.0 ] , 'crop-top' => [ 50 , 0 , 1.0 ] , 'crop-top-right' => [ 100 , 0 , 1.0 ] , 'crop-left' => [ 0 , 50 , 1.0 ] , 'crop-center' => [ 50 , 50 , 1.0 ] , 'crop-right' => [ 100 , 50 , 1.0 ] , 'crop-bottom-left' => [ 0 , 100 , 1.0 ] , 'crop-bottom' => [ 50 , 100 , 1.0 ] , 'crop-bottom-right' => [ 100 , 100 , 1.0 ] , ] ; if ( array_key_exists ( $ this -> fit , $ cropMethods ) ) { return $ cropMethods [ $ this -> fit ] ; } if ( preg_match ( '/^crop-([\d]{1,3})-([\d]{1,3})(?:-([\d]{1,3}(?:\.\d+)?))*$/' , $ this -> fit , $ matches ) ) { $ matches [ 3 ] = isset ( $ matches [ 3 ] ) ? $ matches [ 3 ] : 1 ; if ( $ matches [ 1 ] > 100 or $ matches [ 2 ] > 100 or $ matches [ 3 ] > 100 ) { return [ 50 , 50 , 1.0 ] ; } return [ ( int ) $ matches [ 1 ] , ( int ) $ matches [ 2 ] , ( float ) $ matches [ 3 ] , ] ; } return [ 50 , 50 , 1.0 ] ; }
|
Resolve crop with zoom .
|
8,209
|
public function run ( Image $ image ) { $ pixelate = $ this -> getPixelate ( ) ; if ( $ pixelate !== null ) { $ image -> pixelate ( $ pixelate ) ; } return $ image ; }
|
Perform pixelate image manipulation .
|
8,210
|
public function getPixelate ( ) { if ( ! is_numeric ( $ this -> pixel ) ) { return ; } if ( $ this -> pixel < 0 or $ this -> pixel > 1000 ) { return ; } return ( int ) $ this -> pixel ; }
|
Resolve pixelate amount .
|
8,211
|
public function get ( $ value ) { if ( is_numeric ( $ value ) and $ value > 0 ) { return ( double ) $ value * $ this -> dpr ; } if ( preg_match ( '/^(\d{1,2}(?!\d)|100)(w|h)$/' , $ value , $ matches ) ) { if ( $ matches [ 2 ] === 'h' ) { return ( double ) $ this -> image -> height ( ) * ( $ matches [ 1 ] / 100 ) ; } return ( double ) $ this -> image -> width ( ) * ( $ matches [ 1 ] / 100 ) ; } }
|
Resolve the dimension .
|
8,212
|
public function run ( Image $ image ) { if ( $ flip = $ this -> getFlip ( ) ) { if ( $ flip === 'both' ) { return $ image -> flip ( 'h' ) -> flip ( 'v' ) ; } return $ image -> flip ( $ flip ) ; } return $ image ; }
|
Perform flip image manipulation .
|
8,213
|
public function run ( Image $ image ) { $ brightness = $ this -> getBrightness ( ) ; if ( $ brightness !== null ) { $ image -> brightness ( $ brightness ) ; } return $ image ; }
|
Perform brightness image manipulation .
|
8,214
|
public function getBrightness ( ) { if ( ! preg_match ( '/^-*[0-9]+$/' , $ this -> bri ) ) { return ; } if ( $ this -> bri < - 100 or $ this -> bri > 100 ) { return ; } return ( int ) $ this -> bri ; }
|
Resolve brightness amount .
|
8,215
|
public function setManipulators ( array $ manipulators ) { foreach ( $ manipulators as $ manipulator ) { if ( ! ( $ manipulator instanceof ManipulatorInterface ) ) { throw new InvalidArgumentException ( 'Not a valid manipulator.' ) ; } } $ this -> manipulators = $ manipulators ; }
|
Set the manipulators .
|
8,216
|
public function run ( $ source , array $ params ) { $ image = $ this -> imageManager -> make ( $ source ) ; foreach ( $ this -> manipulators as $ manipulator ) { $ manipulator -> setParams ( $ params ) ; $ image = $ manipulator -> run ( $ image ) ; } return $ image -> getEncoded ( ) ; }
|
Perform image manipulations .
|
8,217
|
public function run ( Image $ image ) { if ( is_null ( $ this -> bg ) ) { return $ image ; } $ color = ( new Color ( $ this -> bg ) ) -> formatted ( ) ; if ( $ color ) { $ new = $ image -> getDriver ( ) -> newImage ( $ image -> width ( ) , $ image -> height ( ) , $ color ) ; $ new -> mime = $ image -> mime ; $ image = $ new -> insert ( $ image , 'top-left' , 0 , 0 ) ; } return $ image ; }
|
Perform background image manipulation .
|
8,218
|
public function run ( Image $ image ) { $ gamma = $ this -> getGamma ( ) ; if ( $ gamma ) { $ image -> gamma ( $ gamma ) ; } return $ image ; }
|
Perform gamma image manipulation .
|
8,219
|
public function getGamma ( ) { if ( ! preg_match ( '/^[0-9]\.*[0-9]*$/' , $ this -> gam ) ) { return ; } if ( $ this -> gam < 0.1 or $ this -> gam > 9.99 ) { return ; } return ( double ) $ this -> gam ; }
|
Resolve gamma amount .
|
8,220
|
public static function create ( $ baseUrl , $ signKey = null ) { $ httpSignature = null ; if ( ! is_null ( $ signKey ) ) { $ httpSignature = SignatureFactory :: create ( $ signKey ) ; } return new UrlBuilder ( $ baseUrl , $ httpSignature ) ; }
|
Create UrlBuilder instance .
|
8,221
|
public function run ( Image $ image ) { if ( $ watermark = $ this -> getImage ( $ image ) ) { $ markw = $ this -> getDimension ( $ image , 'markw' ) ; $ markh = $ this -> getDimension ( $ image , 'markh' ) ; $ markx = $ this -> getDimension ( $ image , 'markx' ) ; $ marky = $ this -> getDimension ( $ image , 'marky' ) ; $ markpad = $ this -> getDimension ( $ image , 'markpad' ) ; $ markfit = $ this -> getFit ( ) ; $ markpos = $ this -> getPosition ( ) ; $ markalpha = $ this -> getAlpha ( ) ; if ( $ markpad ) { $ markx = $ marky = $ markpad ; } $ size = new Size ( ) ; $ size -> setParams ( [ 'w' => $ markw , 'h' => $ markh , 'fit' => $ markfit , ] ) ; $ watermark = $ size -> run ( $ watermark ) ; if ( $ markalpha < 100 ) { $ watermark -> opacity ( $ markalpha ) ; } $ image -> insert ( $ watermark , $ markpos , intval ( $ markx ) , intval ( $ marky ) ) ; } return $ image ; }
|
Perform watermark image manipulation .
|
8,222
|
public function getImage ( Image $ image ) { if ( is_null ( $ this -> watermarks ) ) { return ; } if ( ! is_string ( $ this -> mark ) ) { return ; } if ( $ this -> mark === '' ) { return ; } $ path = $ this -> mark ; if ( $ this -> watermarksPathPrefix ) { $ path = $ this -> watermarksPathPrefix . '/' . $ path ; } if ( $ this -> watermarks -> has ( $ path ) ) { $ source = $ this -> watermarks -> read ( $ path ) ; if ( $ source === false ) { throw new FilesystemException ( 'Could not read the image `' . $ path . '`.' ) ; } return $ image -> getDriver ( ) -> init ( $ source ) ; } }
|
Get the watermark image .
|
8,223
|
public function getDimension ( Image $ image , $ field ) { if ( $ this -> { $ field } ) { return ( new Dimension ( $ image , $ this -> getDpr ( ) ) ) -> get ( $ this -> { $ field } ) ; } }
|
Get a dimension .
|
8,224
|
public function getAlpha ( ) { if ( ! is_numeric ( $ this -> markalpha ) ) { return 100 ; } if ( $ this -> markalpha < 0 or $ this -> markalpha > 100 ) { return 100 ; } return ( int ) $ this -> markalpha ; }
|
Get the alpha channel .
|
8,225
|
public function getSourcePath ( $ path ) { $ path = trim ( $ path , '/' ) ; $ baseUrl = $ this -> baseUrl . '/' ; if ( substr ( $ path , 0 , strlen ( $ baseUrl ) ) === $ baseUrl ) { $ path = trim ( substr ( $ path , strlen ( $ baseUrl ) ) , '/' ) ; } if ( $ path === '' ) { throw new FileNotFoundException ( 'Image path missing.' ) ; } if ( $ this -> sourcePathPrefix ) { $ path = $ this -> sourcePathPrefix . '/' . $ path ; } return rawurldecode ( $ path ) ; }
|
Get source path .
|
8,226
|
public function getCachePath ( $ path , array $ params = [ ] ) { $ sourcePath = $ this -> getSourcePath ( $ path ) ; if ( $ this -> sourcePathPrefix ) { $ sourcePath = substr ( $ sourcePath , strlen ( $ this -> sourcePathPrefix ) + 1 ) ; } $ params = $ this -> getAllParams ( $ params ) ; unset ( $ params [ 's' ] , $ params [ 'p' ] ) ; ksort ( $ params ) ; $ md5 = md5 ( $ sourcePath . '?' . http_build_query ( $ params ) ) ; $ cachedPath = $ this -> groupCacheInFolders ? $ sourcePath . '/' . $ md5 : $ md5 ; if ( $ this -> cachePathPrefix ) { $ cachedPath = $ this -> cachePathPrefix . '/' . $ cachedPath ; } if ( $ this -> cacheWithFileExtensions ) { $ ext = ( isset ( $ params [ 'fm' ] ) ? $ params [ 'fm' ] : pathinfo ( $ path ) [ 'extension' ] ) ; $ ext = ( $ ext === 'pjpg' ) ? 'jpg' : $ ext ; $ cachedPath .= '.' . $ ext ; } return $ cachedPath ; }
|
Get cache path .
|
8,227
|
public function cacheFileExists ( $ path , array $ params ) { return $ this -> cache -> has ( $ this -> getCachePath ( $ path , $ params ) ) ; }
|
Check if a cache file exists .
|
8,228
|
public function deleteCache ( $ path ) { if ( ! $ this -> groupCacheInFolders ) { throw new InvalidArgumentException ( 'Deleting cached image manipulations is not possible when grouping cache into folders is disabled.' ) ; } return $ this -> cache -> deleteDir ( dirname ( $ this -> getCachePath ( $ path ) ) ) ; }
|
Delete cached manipulations for an image .
|
8,229
|
public function getAllParams ( array $ params ) { $ all = $ this -> defaults ; if ( isset ( $ params [ 'p' ] ) ) { foreach ( explode ( ',' , $ params [ 'p' ] ) as $ preset ) { if ( isset ( $ this -> presets [ $ preset ] ) ) { $ all = array_merge ( $ all , $ this -> presets [ $ preset ] ) ; } } } return array_merge ( $ all , $ params ) ; }
|
Get all image manipulations params including defaults and presets .
|
8,230
|
public function makeImage ( $ path , array $ params ) { $ sourcePath = $ this -> getSourcePath ( $ path ) ; $ cachedPath = $ this -> getCachePath ( $ path , $ params ) ; if ( $ this -> cacheFileExists ( $ path , $ params ) === true ) { return $ cachedPath ; } if ( $ this -> sourceFileExists ( $ path ) === false ) { throw new FileNotFoundException ( 'Could not find the image `' . $ sourcePath . '`.' ) ; } $ source = $ this -> source -> read ( $ sourcePath ) ; if ( $ source === false ) { throw new FilesystemException ( 'Could not read the image `' . $ sourcePath . '`.' ) ; } $ tmp = tempnam ( sys_get_temp_dir ( ) , 'Glide' ) ; if ( file_put_contents ( $ tmp , $ source ) === false ) { throw new FilesystemException ( 'Unable to write temp file for `' . $ sourcePath . '`.' ) ; } try { $ write = $ this -> cache -> write ( $ cachedPath , $ this -> api -> run ( $ tmp , $ this -> getAllParams ( $ params ) ) ) ; if ( $ write === false ) { throw new FilesystemException ( 'Could not write the image `' . $ cachedPath . '`.' ) ; } } catch ( FileExistsException $ exception ) { } finally { unlink ( $ tmp ) ; } return $ cachedPath ; }
|
Generate manipulated image .
|
8,231
|
public function run ( Image $ image ) { if ( $ border = $ this -> getBorder ( $ image ) ) { list ( $ width , $ color , $ method ) = $ border ; if ( $ method === 'overlay' ) { return $ this -> runOverlay ( $ image , $ width , $ color ) ; } if ( $ method === 'shrink' ) { return $ this -> runShrink ( $ image , $ width , $ color ) ; } if ( $ method === 'expand' ) { return $ this -> runExpand ( $ image , $ width , $ color ) ; } } return $ image ; }
|
Perform border image manipulation .
|
8,232
|
public function getBorder ( Image $ image ) { if ( ! $ this -> border ) { return ; } $ values = explode ( ',' , $ this -> border ) ; $ width = $ this -> getWidth ( $ image , $ this -> getDpr ( ) , isset ( $ values [ 0 ] ) ? $ values [ 0 ] : null ) ; $ color = $ this -> getColor ( isset ( $ values [ 1 ] ) ? $ values [ 1 ] : null ) ; $ method = $ this -> getMethod ( isset ( $ values [ 2 ] ) ? $ values [ 2 ] : null ) ; if ( $ width ) { return [ $ width , $ color , $ method ] ; } }
|
Resolve border amount .
|
8,233
|
public function getWidth ( Image $ image , $ dpr , $ width ) { return ( new Dimension ( $ image , $ dpr ) ) -> get ( $ width ) ; }
|
Get border width .
|
8,234
|
public function runOverlay ( Image $ image , $ width , $ color ) { return $ image -> rectangle ( $ width / 2 , $ width / 2 , $ image -> width ( ) - ( $ width / 2 ) , $ image -> height ( ) - ( $ width / 2 ) , function ( $ draw ) use ( $ width , $ color ) { $ draw -> border ( $ width , $ color ) ; } ) ; }
|
Run the overlay border method .
|
8,235
|
public function runShrink ( Image $ image , $ width , $ color ) { return $ image -> resize ( $ image -> width ( ) - ( $ width * 2 ) , $ image -> height ( ) - ( $ width * 2 ) ) -> resizeCanvas ( $ width * 2 , $ width * 2 , 'center' , true , $ color ) ; }
|
Run the shrink border method .
|
8,236
|
public function runExpand ( Image $ image , $ width , $ color ) { return $ image -> resizeCanvas ( $ width * 2 , $ width * 2 , 'center' , true , $ color ) ; }
|
Run the expand border method .
|
8,237
|
public function setBaseUrl ( $ baseUrl ) { if ( substr ( $ baseUrl , 0 , 2 ) === '//' ) { $ baseUrl = 'http:' . $ baseUrl ; $ this -> isRelativeDomain = true ; } $ this -> baseUrl = rtrim ( $ baseUrl , '/' ) ; }
|
Set the base URL .
|
8,238
|
public function createSidebarMenu ( ) { $ menu = $ this -> factory -> createItem ( 'root' ) ; foreach ( $ this -> pool -> getAdminGroups ( ) as $ name => $ group ) { $ extras = [ 'icon' => $ group [ 'icon' ] , 'label_catalogue' => $ group [ 'label_catalogue' ] , 'roles' => $ group [ 'roles' ] , 'sonata_admin' => true , ] ; $ menuProvider = $ group [ 'provider' ] ?? 'sonata_group_menu' ; $ subMenu = $ this -> provider -> get ( $ menuProvider , [ 'name' => $ name , 'group' => $ group , ] ) ; $ subMenu = $ menu -> addChild ( $ subMenu ) ; $ subMenu -> setExtras ( array_merge ( $ subMenu -> getExtras ( ) , $ extras ) ) ; } $ event = new ConfigureMenuEvent ( $ this -> factory , $ menu ) ; $ this -> eventDispatcher -> dispatch ( ConfigureMenuEvent :: SIDEBAR , $ event ) ; return $ event -> getMenu ( ) ; }
|
Builds sidebar menu .
|
8,239
|
public function ifTrue ( $ bool ) { if ( null !== $ this -> apply ) { throw new \ RuntimeException ( 'Cannot nest ifTrue or ifFalse call' ) ; } $ this -> apply = ( true === $ bool ) ; return $ this ; }
|
Only nested add if the condition match true .
|
8,240
|
public function ifFalse ( $ bool ) { if ( null !== $ this -> apply ) { throw new \ RuntimeException ( 'Cannot nest ifTrue or ifFalse call' ) ; } $ this -> apply = ( false === $ bool ) ; return $ this ; }
|
Only nested add if the condition match false .
|
8,241
|
public function end ( ) { if ( null !== $ this -> currentGroup ) { $ this -> currentGroup = null ; } elseif ( null !== $ this -> currentTab ) { $ this -> currentTab = null ; } else { throw new \ RuntimeException ( 'No open tabs or groups, you cannot use end()' ) ; } return $ this ; }
|
Close the current group or tab .
|
8,242
|
protected function addFieldToCurrentGroup ( $ fieldName ) { $ currentGroup = $ this -> getCurrentGroupName ( ) ; $ groups = $ this -> getGroups ( ) ; $ groups [ $ currentGroup ] [ 'fields' ] [ $ fieldName ] = $ fieldName ; $ this -> setGroups ( $ groups ) ; return $ groups [ $ currentGroup ] ; }
|
Add the field name to the current group .
|
8,243
|
protected function getCurrentGroupName ( ) { if ( ! $ this -> currentGroup ) { $ this -> with ( $ this -> admin -> getLabel ( ) , [ 'auto_created' => true ] ) ; } return $ this -> currentGroup ; }
|
Return the name of the currently selected group . The method also makes sure a valid group name is currently selected .
|
8,244
|
private function legacyConstructor ( array $ args ) { $ choiceList = $ args [ 0 ] ; if ( ! $ choiceList instanceof ModelChoiceList && ! $ choiceList instanceof ModelChoiceLoader && ! $ choiceList instanceof LazyChoiceList ) { throw new RuntimeException ( 'First param passed to ModelsToArrayTransformer should be instance of ModelChoiceLoader or ModelChoiceList or LazyChoiceList' ) ; } $ this -> choiceList = $ choiceList ; $ this -> modelManager = $ args [ 1 ] ; $ this -> class = $ args [ 2 ] ; }
|
Simulates the old constructor for BC .
|
8,245
|
public function createAclUsersForm ( AdminObjectAclData $ data ) { $ aclValues = $ data -> getAclUsers ( ) ; $ formBuilder = $ this -> formFactory -> createNamedBuilder ( self :: ACL_USERS_FORM_NAME , FormType :: class ) ; $ form = $ this -> buildForm ( $ data , $ formBuilder , $ aclValues ) ; $ data -> setAclUsersForm ( $ form ) ; return $ form ; }
|
Gets the ACL users form .
|
8,246
|
public function createAclRolesForm ( AdminObjectAclData $ data ) { $ aclValues = $ data -> getAclRoles ( ) ; $ formBuilder = $ this -> formFactory -> createNamedBuilder ( self :: ACL_ROLES_FORM_NAME , FormType :: class ) ; $ form = $ this -> buildForm ( $ data , $ formBuilder , $ aclValues ) ; $ data -> setAclRolesForm ( $ form ) ; return $ form ; }
|
Gets the ACL roles form .
|
8,247
|
public function updateAclUsers ( AdminObjectAclData $ data ) { $ aclValues = $ data -> getAclUsers ( ) ; $ form = $ data -> getAclUsersForm ( ) ; $ this -> buildAcl ( $ data , $ form , $ aclValues ) ; }
|
Updates ACL users .
|
8,248
|
public function updateAclRoles ( AdminObjectAclData $ data ) { $ aclValues = $ data -> getAclRoles ( ) ; $ form = $ data -> getAclRolesForm ( ) ; $ this -> buildAcl ( $ data , $ form , $ aclValues ) ; }
|
Updates ACL roles .
|
8,249
|
protected function buildAcl ( AdminObjectAclData $ data , Form $ form , \ Traversable $ aclValues ) { $ masks = $ data -> getMasks ( ) ; $ acl = $ data -> getAcl ( ) ; $ matrices = $ form -> getData ( ) ; foreach ( $ aclValues as $ aclValue ) { foreach ( $ matrices as $ key => $ matrix ) { if ( $ aclValue instanceof UserInterface ) { if ( \ array_key_exists ( 'user' , $ matrix ) && $ aclValue -> getUsername ( ) === $ matrix [ 'user' ] ) { $ matrices [ $ key ] [ 'acl_value' ] = $ aclValue ; } } elseif ( \ array_key_exists ( 'role' , $ matrix ) && $ aclValue === $ matrix [ 'role' ] ) { $ matrices [ $ key ] [ 'acl_value' ] = $ aclValue ; } } } foreach ( $ matrices as $ matrix ) { if ( ! isset ( $ matrix [ 'acl_value' ] ) ) { continue ; } $ securityIdentity = $ this -> getSecurityIdentity ( $ matrix [ 'acl_value' ] ) ; $ maskBuilder = new $ this -> maskBuilderClass ( ) ; foreach ( $ data -> getUserPermissions ( ) as $ permission ) { if ( isset ( $ matrix [ $ permission ] ) && true === $ matrix [ $ permission ] ) { $ maskBuilder -> add ( $ permission ) ; } } if ( ! $ data -> isOwner ( ) ) { foreach ( $ data -> getOwnerPermissions ( ) as $ permission ) { if ( $ acl -> isGranted ( [ $ masks [ $ permission ] ] , [ $ securityIdentity ] ) ) { $ maskBuilder -> add ( $ permission ) ; } } } $ mask = $ maskBuilder -> get ( ) ; $ index = null ; $ ace = null ; foreach ( $ acl -> getObjectAces ( ) as $ currentIndex => $ currentAce ) { if ( $ currentAce -> getSecurityIdentity ( ) -> equals ( $ securityIdentity ) ) { $ index = $ currentIndex ; $ ace = $ currentAce ; break ; } } if ( $ ace ) { $ acl -> updateObjectAce ( $ index , $ mask ) ; } else { $ acl -> insertObjectAce ( $ securityIdentity , $ mask ) ; } } $ data -> getSecurityHandler ( ) -> updateAcl ( $ acl ) ; }
|
Builds ACL .
|
8,250
|
private function addExtension ( array & $ targets , string $ target , string $ extension , array $ attributes ) : void { if ( ! isset ( $ targets [ $ target ] ) ) { $ targets [ $ target ] = new \ SplPriorityQueue ( ) ; } $ priority = $ attributes [ 'priority' ] ?? 0 ; $ targets [ $ target ] -> insert ( new Reference ( $ extension ) , $ priority ) ; }
|
Add extension configuration to the targets array .
|
8,251
|
public function addNewInstance ( $ object , FieldDescriptionInterface $ fieldDescription ) { $ instance = $ fieldDescription -> getAssociationAdmin ( ) -> getNewInstance ( ) ; $ mapping = $ fieldDescription -> getAssociationMapping ( ) ; $ method = sprintf ( 'add%s' , Inflector :: classify ( $ mapping [ 'fieldName' ] ) ) ; if ( ! method_exists ( $ object , $ method ) ) { $ method = rtrim ( $ method , 's' ) ; if ( ! method_exists ( $ object , $ method ) ) { $ method = sprintf ( 'add%s' , Inflector :: classify ( Inflector :: singularize ( $ mapping [ 'fieldName' ] ) ) ) ; if ( ! method_exists ( $ object , $ method ) ) { throw new \ RuntimeException ( sprintf ( 'Please add a method %s in the %s class!' , $ method , ClassUtils :: getClass ( $ object ) ) ) ; } } } $ object -> $ method ( $ instance ) ; }
|
Add a new instance to the related FieldDescriptionInterface value .
|
8,252
|
public function getElementAccessPath ( $ elementId , $ entity ) { $ propertyAccessor = $ this -> pool -> getPropertyAccessor ( ) ; $ idWithoutIdentifier = preg_replace ( '/^[^_]*_/' , '' , $ elementId ) ; $ initialPath = preg_replace ( '#(_(\d+)_)#' , '[$2]_' , $ idWithoutIdentifier ) ; $ parts = explode ( '_' , $ initialPath ) ; $ totalPath = '' ; $ currentPath = '' ; foreach ( $ parts as $ part ) { $ currentPath .= empty ( $ currentPath ) ? $ part : '_' . $ part ; $ separator = empty ( $ totalPath ) ? '' : '.' ; if ( $ propertyAccessor -> isReadable ( $ entity , $ totalPath . $ separator . $ currentPath ) ) { $ totalPath .= $ separator . $ currentPath ; $ currentPath = '' ; } } if ( ! empty ( $ currentPath ) ) { throw new \ Exception ( sprintf ( 'Could not get element id from %s Failing part: %s' , $ elementId , $ currentPath ) ) ; } return $ totalPath ; }
|
Get access path to element which works with PropertyAccessor .
|
8,253
|
protected function getEntityClassName ( AdminInterface $ admin , $ elements ) { $ element = array_shift ( $ elements ) ; $ associationAdmin = $ admin -> getFormFieldDescription ( $ element ) -> getAssociationAdmin ( ) ; if ( 0 === \ count ( $ elements ) ) { return $ associationAdmin -> getClass ( ) ; } return $ this -> getEntityClassName ( $ associationAdmin , $ elements ) ; }
|
Recursively find the class name of the admin responsible for the element at the end of an association chain .
|
8,254
|
public function getUserPermissions ( ) { $ permissions = $ this -> getPermissions ( ) ; if ( ! $ this -> isOwner ( ) ) { foreach ( self :: $ ownerPermissions as $ permission ) { $ key = array_search ( $ permission , $ permissions , true ) ; if ( false !== $ key ) { unset ( $ permissions [ $ key ] ) ; } } } return $ permissions ; }
|
Get permissions that the current user can set .
|
8,255
|
protected function updateMasks ( ) { $ permissions = $ this -> getPermissions ( ) ; $ reflectionClass = new \ ReflectionClass ( new $ this -> maskBuilderClass ( ) ) ; $ this -> masks = [ ] ; foreach ( $ permissions as $ permission ) { $ this -> masks [ $ permission ] = $ reflectionClass -> getConstant ( 'MASK_' . $ permission ) ; } }
|
Cache masks .
|
8,256
|
public function getLinks ( $ nbLinks = null ) { if ( null === $ nbLinks ) { $ nbLinks = $ this -> getMaxPageLinks ( ) ; } $ links = [ ] ; $ tmp = $ this -> page - floor ( $ nbLinks / 2 ) ; $ check = $ this -> lastPage - $ nbLinks + 1 ; $ limit = $ check > 0 ? $ check : 1 ; $ begin = $ tmp > 0 ? ( $ tmp > $ limit ? $ limit : $ tmp ) : 1 ; $ i = ( int ) $ begin ; while ( $ i < $ begin + $ nbLinks && $ i <= $ this -> lastPage ) { $ links [ ] = $ i ++ ; } $ this -> currentMaxLink = \ count ( $ links ) ? $ links [ \ count ( $ links ) - 1 ] : 1 ; return $ links ; }
|
Returns an array of page numbers to use in pagination links .
|
8,257
|
public function setCursor ( $ pos ) { if ( $ pos < 1 ) { $ this -> cursor = 1 ; } else { if ( $ pos > $ this -> nbResults ) { $ this -> cursor = $ this -> nbResults ; } else { $ this -> cursor = $ pos ; } } }
|
Sets the current cursor .
|
8,258
|
protected function initializeIterator ( ) { $ this -> results = $ this -> getResults ( ) ; $ this -> resultsCounter = \ count ( $ this -> results ) ; }
|
Loads data into properties used for iteration .
|
8,259
|
protected function retrieveObject ( $ offset ) { $ queryForRetrieve = clone $ this -> getQuery ( ) ; $ queryForRetrieve -> setFirstResult ( $ offset - 1 ) -> setMaxResults ( 1 ) ; $ results = $ queryForRetrieve -> execute ( ) ; return $ results [ 0 ] ; }
|
Retrieve the object for a certain offset .
|
8,260
|
private function generateFallback ( $ name ) : void { if ( empty ( $ name ) ) { return ; } if ( preg_match ( AdminClass :: CLASS_REGEX , $ name , $ matches ) ) { if ( empty ( $ this -> group ) ) { $ this -> group = $ matches [ 3 ] ; } if ( empty ( $ this -> label ) ) { $ this -> label = $ matches [ 5 ] ; } } }
|
Set group and label from class name it not set .
|
8,261
|
public function applyConfigurationFromAttribute ( Definition $ definition , array $ attributes ) { $ keys = [ 'model_manager' , 'form_contractor' , 'show_builder' , 'list_builder' , 'datagrid_builder' , 'translator' , 'configuration_pool' , 'router' , 'validator' , 'security_handler' , 'menu_factory' , 'route_builder' , 'label_translator_strategy' , ] ; foreach ( $ keys as $ key ) { $ method = 'set' . Inflector :: classify ( $ key ) ; if ( ! isset ( $ attributes [ $ key ] ) || $ definition -> hasMethodCall ( $ method ) ) { continue ; } $ definition -> addMethodCall ( $ method , [ new Reference ( $ attributes [ $ key ] ) ] ) ; } }
|
This method read the attribute keys and configure admin class to use the related dependency .
|
8,262
|
private function replaceDefaultArguments ( array $ defaultArguments , Definition $ definition , Definition $ parentDefinition = null ) : void { $ arguments = $ definition -> getArguments ( ) ; $ parentArguments = $ parentDefinition ? $ parentDefinition -> getArguments ( ) : [ ] ; foreach ( $ defaultArguments as $ index => $ value ) { $ declaredInParent = $ parentDefinition && \ array_key_exists ( $ index , $ parentArguments ) ; $ argumentValue = $ declaredInParent ? $ parentArguments [ $ index ] : $ arguments [ $ index ] ; if ( null === $ argumentValue || 0 === \ strlen ( $ argumentValue ) ) { $ arguments [ $ declaredInParent ? sprintf ( 'index_%s' , $ index ) : $ index ] = $ value ; } } $ definition -> setArguments ( $ arguments ) ; }
|
Replace the empty arguments required by the Admin service definition .
|
8,263
|
public function configureAcls ( OutputInterface $ output , AdminInterface $ admin , \ Traversable $ oids , UserSecurityIdentity $ securityIdentity = null ) { $ countAdded = 0 ; $ countUpdated = 0 ; $ securityHandler = $ admin -> getSecurityHandler ( ) ; if ( ! $ securityHandler instanceof AclSecurityHandlerInterface ) { $ output -> writeln ( sprintf ( 'Admin `%s` is not configured to use ACL : <info>ignoring</info>' , $ admin -> getCode ( ) ) ) ; return [ 0 , 0 ] ; } $ acls = $ securityHandler -> findObjectAcls ( $ oids ) ; foreach ( $ oids as $ oid ) { if ( $ acls -> contains ( $ oid ) ) { $ acl = $ acls -> offsetGet ( $ oid ) ; ++ $ countUpdated ; } else { $ acl = $ securityHandler -> createAcl ( $ oid ) ; ++ $ countAdded ; } if ( null !== $ securityIdentity ) { $ securityHandler -> addObjectOwner ( $ acl , $ securityIdentity ) ; } $ securityHandler -> addObjectClassAces ( $ acl , $ securityHandler -> buildSecurityInformation ( $ admin ) ) ; try { $ securityHandler -> updateAcl ( $ acl ) ; } catch ( \ Exception $ e ) { $ output -> writeln ( sprintf ( 'Error saving ObjectIdentity (%s, %s) ACL : %s <info>ignoring</info>' , $ oid -> getIdentifier ( ) , $ oid -> getType ( ) , $ e -> getMessage ( ) ) ) ; } } return [ $ countAdded , $ countUpdated ] ; }
|
Configure the object ACL for the passed object identities .
|
8,264
|
private function retrieveFilterFieldDescription ( AdminInterface $ admin , string $ field ) : FieldDescriptionInterface { $ admin -> getFilterFieldDescriptions ( ) ; $ fieldDescription = $ admin -> getFilterFieldDescription ( $ field ) ; if ( ! $ fieldDescription ) { throw new \ RuntimeException ( sprintf ( 'The field "%s" does not exist.' , $ field ) ) ; } if ( null === $ fieldDescription -> getTargetEntity ( ) ) { throw new \ RuntimeException ( sprintf ( 'No associated entity with field "%s".' , $ field ) ) ; } return $ fieldDescription ; }
|
Retrieve the filter field description given by field name .
|
8,265
|
public function getInstance ( $ id ) { if ( ! \ in_array ( $ id , $ this -> adminServiceIds , true ) ) { $ msg = sprintf ( 'Admin service "%s" not found in admin pool.' , $ id ) ; $ shortest = - 1 ; $ closest = null ; $ alternatives = [ ] ; foreach ( $ this -> adminServiceIds as $ adminServiceId ) { $ lev = levenshtein ( $ id , $ adminServiceId ) ; if ( $ lev <= $ shortest || $ shortest < 0 ) { $ closest = $ adminServiceId ; $ shortest = $ lev ; } if ( $ lev <= \ strlen ( $ adminServiceId ) / 3 || false !== strpos ( $ adminServiceId , $ id ) ) { $ alternatives [ $ adminServiceId ] = $ lev ; } } if ( null !== $ closest ) { asort ( $ alternatives ) ; unset ( $ alternatives [ $ closest ] ) ; $ msg = sprintf ( 'Admin service "%s" not found in admin pool. Did you mean "%s" or one of those: [%s]?' , $ id , $ closest , implode ( ', ' , array_keys ( $ alternatives ) ) ) ; } throw new \ InvalidArgumentException ( $ msg ) ; } $ admin = $ this -> container -> get ( $ id ) ; if ( ! $ admin instanceof AdminInterface ) { throw new InvalidArgumentException ( sprintf ( 'Found service "%s" is not a valid admin service' , $ id ) ) ; } return $ admin ; }
|
Returns a new admin instance depends on the given code .
|
8,266
|
public function getAvailableFormats ( AdminInterface $ admin ) : array { $ adminExportFormats = $ admin -> getExportFormats ( ) ; if ( $ adminExportFormats !== [ 'json' , 'xml' , 'csv' , 'xls' ] ) { return $ adminExportFormats ; } return $ this -> exporter -> getAvailableFormats ( ) ; }
|
Queries an admin for its default export formats and falls back on global settings .
|
8,267
|
public function getExportFilename ( AdminInterface $ admin , string $ format ) : string { $ class = $ admin -> getClass ( ) ; return sprintf ( 'export_%s_%s.%s' , strtolower ( substr ( $ class , strripos ( $ class , '\\' ) + 1 ) ) , date ( 'Y_m_d_H_i_s' , strtotime ( 'now' ) ) , $ format ) ; }
|
Builds an export filename from the class associated with the provided admin the current date and the provided format .
|
8,268
|
public function searchAction ( Request $ request ) { $ searchAction = $ this -> container -> get ( SearchAction :: class ) ; return $ searchAction ( $ request ) ; }
|
The search action first render an empty page if the query is set then the template generates some ajax request to retrieve results for each admin . The Ajax query returns a JSON response .
|
8,269
|
private function guess ( \ Closure $ closure ) : Guess { $ guesses = [ ] ; foreach ( $ this -> guessers as $ guesser ) { if ( $ guess = $ closure ( $ guesser ) ) { $ guesses [ ] = $ guess ; } } return Guess :: getBestGuess ( $ guesses ) ; }
|
Executes a closure for each guesser and returns the best guess from the return values .
|
8,270
|
public function initialize ( ) { if ( ! $ this -> classnameLabel ) { $ this -> classnameLabel = substr ( ( string ) $ this -> getClass ( ) , strrpos ( ( string ) $ this -> getClass ( ) , '\\' ) + 1 ) ; } $ this -> baseCodeRoute = $ this -> getCode ( ) ; $ this -> configure ( ) ; }
|
define custom variable .
|
8,271
|
public function getBaseRoutePattern ( ) { if ( null !== $ this -> cachedBaseRoutePattern ) { return $ this -> cachedBaseRoutePattern ; } if ( $ this -> isChild ( ) ) { $ baseRoutePattern = $ this -> baseRoutePattern ; if ( ! $ this -> baseRoutePattern ) { preg_match ( self :: CLASS_REGEX , $ this -> class , $ matches ) ; if ( ! $ matches ) { throw new \ RuntimeException ( sprintf ( 'Please define a default `baseRoutePattern` value for the admin class `%s`' , \ get_class ( $ this ) ) ) ; } $ baseRoutePattern = $ this -> urlize ( $ matches [ 5 ] , '-' ) ; } $ this -> cachedBaseRoutePattern = sprintf ( '%s/%s/%s' , $ this -> getParent ( ) -> getBaseRoutePattern ( ) , $ this -> getParent ( ) -> getRouterIdParameter ( ) , $ baseRoutePattern ) ; } elseif ( $ this -> baseRoutePattern ) { $ this -> cachedBaseRoutePattern = $ this -> baseRoutePattern ; } else { preg_match ( self :: CLASS_REGEX , $ this -> class , $ matches ) ; if ( ! $ matches ) { throw new \ RuntimeException ( sprintf ( 'Please define a default `baseRoutePattern` value for the admin class `%s`' , \ get_class ( $ this ) ) ) ; } $ this -> cachedBaseRoutePattern = sprintf ( '/%s%s/%s' , empty ( $ matches [ 1 ] ) ? '' : $ this -> urlize ( $ matches [ 1 ] , '-' ) . '/' , $ this -> urlize ( $ matches [ 3 ] , '-' ) , $ this -> urlize ( $ matches [ 5 ] , '-' ) ) ; } return $ this -> cachedBaseRoutePattern ; }
|
Returns the baseRoutePattern used to generate the routing information .
|
8,272
|
public function getBaseRouteName ( ) { if ( null !== $ this -> cachedBaseRouteName ) { return $ this -> cachedBaseRouteName ; } if ( $ this -> isChild ( ) ) { $ baseRouteName = $ this -> baseRouteName ; if ( ! $ this -> baseRouteName ) { preg_match ( self :: CLASS_REGEX , $ this -> class , $ matches ) ; if ( ! $ matches ) { throw new \ RuntimeException ( sprintf ( 'Cannot automatically determine base route name, please define a default `baseRouteName` value for the admin class `%s`' , \ get_class ( $ this ) ) ) ; } $ baseRouteName = $ this -> urlize ( $ matches [ 5 ] ) ; } $ this -> cachedBaseRouteName = sprintf ( '%s_%s' , $ this -> getParent ( ) -> getBaseRouteName ( ) , $ baseRouteName ) ; } elseif ( $ this -> baseRouteName ) { $ this -> cachedBaseRouteName = $ this -> baseRouteName ; } else { preg_match ( self :: CLASS_REGEX , $ this -> class , $ matches ) ; if ( ! $ matches ) { throw new \ RuntimeException ( sprintf ( 'Cannot automatically determine base route name, please define a default `baseRouteName` value for the admin class `%s`' , \ get_class ( $ this ) ) ) ; } $ this -> cachedBaseRouteName = sprintf ( 'admin_%s%s_%s' , empty ( $ matches [ 1 ] ) ? '' : $ this -> urlize ( $ matches [ 1 ] ) . '_' , $ this -> urlize ( $ matches [ 3 ] ) , $ this -> urlize ( $ matches [ 5 ] ) ) ; } return $ this -> cachedBaseRouteName ; }
|
Returns the baseRouteName used to generate the routing information .
|
8,273
|
public function buildBreadcrumbs ( $ action , MenuItemInterface $ menu = null ) { @ trigger_error ( 'The ' . __METHOD__ . ' method is deprecated since version 3.2 and will be removed in 4.0.' , E_USER_DEPRECATED ) ; if ( isset ( $ this -> breadcrumbs [ $ action ] ) ) { return $ this -> breadcrumbs [ $ action ] ; } return $ this -> breadcrumbs [ $ action ] = $ this -> getBreadcrumbsBuilder ( ) -> buildBreadcrumbs ( $ this , $ action , $ menu ) ; }
|
Generates the breadcrumbs array .
|
8,274
|
public function hasAccess ( $ action , $ object = null ) { $ access = $ this -> getAccess ( ) ; if ( ! \ array_key_exists ( $ action , $ access ) ) { return false ; } if ( ! \ is_array ( $ access [ $ action ] ) ) { $ access [ $ action ] = [ $ access [ $ action ] ] ; } foreach ( $ access [ $ action ] as $ role ) { if ( false === $ this -> isGranted ( $ role , $ object ) ) { return false ; } } return true ; }
|
Hook to handle access authorization without throw Exception .
|
8,275
|
public function getDashboardActions ( ) { $ actions = [ ] ; if ( $ this -> hasRoute ( 'create' ) && $ this -> hasAccess ( 'create' ) ) { $ actions [ 'create' ] = [ 'label' => 'link_add' , 'translation_domain' => 'SonataAdminBundle' , 'template' => $ this -> getTemplate ( 'action_create' ) , 'url' => $ this -> generateUrl ( 'create' ) , 'icon' => 'plus-circle' , ] ; } if ( $ this -> hasRoute ( 'list' ) && $ this -> hasAccess ( 'list' ) ) { $ actions [ 'list' ] = [ 'label' => 'link_list' , 'translation_domain' => 'SonataAdminBundle' , 'url' => $ this -> generateUrl ( 'list' ) , 'icon' => 'list' , ] ; } return $ actions ; }
|
Get the list of actions that can be accessed directly from the dashboard .
|
8,276
|
final public function isDefaultFilter ( $ name ) { $ filter = $ this -> getFilterParameters ( ) ; $ default = $ this -> getDefaultFilterValues ( ) ; if ( ! \ array_key_exists ( $ name , $ filter ) || ! \ array_key_exists ( $ name , $ default ) ) { return false ; } return $ filter [ $ name ] === $ default [ $ name ] ; }
|
Checks if a filter type is set to a default value .
|
8,277
|
public function canAccessObject ( $ action , $ object ) { return $ object && $ this -> id ( $ object ) && $ this -> hasAccess ( $ action , $ object ) ; }
|
Check object existence and access without throw Exception .
|
8,278
|
final protected function getDefaultFilterValues ( ) { $ defaultFilterValues = [ ] ; $ this -> configureDefaultFilterValues ( $ defaultFilterValues ) ; foreach ( $ this -> getExtensions ( ) as $ extension ) { if ( method_exists ( $ extension , 'configureDefaultFilterValues' ) ) { $ extension -> configureDefaultFilterValues ( $ this , $ defaultFilterValues ) ; } } return $ defaultFilterValues ; }
|
Returns a list of default filters .
|
8,279
|
protected function configureTabMenu ( MenuItemInterface $ menu , $ action , AdminInterface $ childAdmin = null ) { $ this -> configureSideMenu ( $ menu , $ action , $ childAdmin ) ; }
|
Configures the tab menu in your admin .
|
8,280
|
protected function buildShow ( ) { if ( $ this -> show ) { return ; } $ this -> show = new FieldDescriptionCollection ( ) ; $ mapper = new ShowMapper ( $ this -> showBuilder , $ this -> show , $ this ) ; $ this -> configureShowFields ( $ mapper ) ; foreach ( $ this -> getExtensions ( ) as $ extension ) { $ extension -> configureShowFields ( $ mapper ) ; } }
|
build the view FieldDescription array .
|
8,281
|
protected function buildList ( ) { if ( $ this -> list ) { return ; } $ this -> list = $ this -> getListBuilder ( ) -> getBaseList ( ) ; $ mapper = new ListMapper ( $ this -> getListBuilder ( ) , $ this -> list , $ this ) ; if ( \ count ( $ this -> getBatchActions ( ) ) > 0 && $ this -> hasRequest ( ) && ! $ this -> getRequest ( ) -> isXmlHttpRequest ( ) ) { $ fieldDescription = $ this -> getModelManager ( ) -> getNewFieldDescriptionInstance ( $ this -> getClass ( ) , 'batch' , [ 'label' => 'batch' , 'code' => '_batch' , 'sortable' => false , 'virtual_field' => true , ] ) ; $ fieldDescription -> setAdmin ( $ this ) ; $ fieldDescription -> setTemplate ( $ this -> getTemplate ( 'batch' ) ) ; $ mapper -> add ( $ fieldDescription , 'batch' ) ; } $ this -> configureListFields ( $ mapper ) ; foreach ( $ this -> getExtensions ( ) as $ extension ) { $ extension -> configureListFields ( $ mapper ) ; } if ( $ this -> hasRequest ( ) && $ this -> getRequest ( ) -> isXmlHttpRequest ( ) ) { $ fieldDescription = $ this -> getModelManager ( ) -> getNewFieldDescriptionInstance ( $ this -> getClass ( ) , 'select' , [ 'label' => false , 'code' => '_select' , 'sortable' => false , 'virtual_field' => false , ] ) ; $ fieldDescription -> setAdmin ( $ this ) ; $ fieldDescription -> setTemplate ( $ this -> getTemplate ( 'select' ) ) ; $ mapper -> add ( $ fieldDescription , 'select' ) ; } }
|
build the list FieldDescription array .
|
8,282
|
protected function buildForm ( ) { if ( $ this -> form ) { return ; } if ( $ this -> isChild ( ) && $ this -> getParentAssociationMapping ( ) ) { $ parent = $ this -> getParent ( ) -> getObject ( $ this -> request -> get ( $ this -> getParent ( ) -> getIdParameter ( ) ) ) ; $ propertyAccessor = $ this -> getConfigurationPool ( ) -> getPropertyAccessor ( ) ; $ propertyPath = new PropertyPath ( $ this -> getParentAssociationMapping ( ) ) ; $ object = $ this -> getSubject ( ) ; $ value = $ propertyAccessor -> getValue ( $ object , $ propertyPath ) ; if ( \ is_array ( $ value ) || ( $ value instanceof \ Traversable && $ value instanceof \ ArrayAccess ) ) { $ value [ ] = $ parent ; $ propertyAccessor -> setValue ( $ object , $ propertyPath , $ value ) ; } else { $ propertyAccessor -> setValue ( $ object , $ propertyPath , $ parent ) ; } } $ formBuilder = $ this -> getFormBuilder ( ) ; $ formBuilder -> addEventListener ( FormEvents :: POST_SUBMIT , function ( FormEvent $ event ) { $ this -> preValidate ( $ event -> getData ( ) ) ; } , 100 ) ; $ this -> form = $ formBuilder -> getForm ( ) ; }
|
Build the form FieldDescription collection .
|
8,283
|
protected function getSubClass ( $ name ) { if ( $ this -> hasSubClass ( $ name ) ) { return $ this -> subClasses [ $ name ] ; } throw new \ RuntimeException ( sprintf ( 'Unable to find the subclass `%s` for admin `%s`' , $ name , \ get_class ( $ this ) ) ) ; }
|
Gets the subclass corresponding to the given name .
|
8,284
|
protected function attachInlineValidator ( ) { $ admin = $ this ; $ metadata = $ this -> validator -> getMetadataFor ( $ this -> getClass ( ) ) ; $ metadata -> addConstraint ( new InlineConstraint ( [ 'service' => $ this , 'method' => static function ( ErrorElement $ errorElement , $ object ) use ( $ admin ) { if ( $ admin -> hasSubject ( ) && spl_object_hash ( $ object ) !== spl_object_hash ( $ admin -> getSubject ( ) ) ) { return ; } $ admin -> validate ( $ errorElement , $ object ) ; foreach ( $ admin -> getExtensions ( ) as $ extension ) { $ extension -> validate ( $ admin , $ errorElement , $ object ) ; } } , 'serializingWarning' => true , ] ) ) ; }
|
Attach the inline validator to the model metadata this must be done once per admin .
|
8,285
|
protected function predefinePerPageOptions ( ) { array_unshift ( $ this -> perPageOptions , $ this -> maxPerPage ) ; $ this -> perPageOptions = array_unique ( $ this -> perPageOptions ) ; sort ( $ this -> perPageOptions ) ; }
|
Predefine per page options .
|
8,286
|
protected function getAccess ( ) { $ access = array_merge ( [ 'acl' => 'MASTER' , 'export' => 'EXPORT' , 'historyCompareRevisions' => 'EDIT' , 'historyViewRevision' => 'EDIT' , 'history' => 'EDIT' , 'edit' => 'EDIT' , 'show' => 'VIEW' , 'create' => 'CREATE' , 'delete' => 'DELETE' , 'batchDelete' => 'DELETE' , 'list' => 'LIST' , ] , $ this -> getAccessMapping ( ) ) ; foreach ( $ this -> extensions as $ extension ) { if ( method_exists ( $ extension , 'getAccessMapping' ) ) { $ access = array_merge ( $ access , $ extension -> getAccessMapping ( $ this ) ) ; } } return $ access ; }
|
Return list routes with permissions name .
|
8,287
|
private function buildRoutes ( ) : void { if ( $ this -> loaded [ 'routes' ] ) { return ; } $ this -> loaded [ 'routes' ] = true ; $ this -> routes = new RouteCollection ( $ this -> getBaseCodeRoute ( ) , $ this -> getBaseRouteName ( ) , $ this -> getBaseRoutePattern ( ) , $ this -> getBaseControllerName ( ) ) ; $ this -> routeBuilder -> build ( $ this , $ this -> routes ) ; $ this -> configureRoutes ( $ this -> routes ) ; foreach ( $ this -> getExtensions ( ) as $ extension ) { $ extension -> configureRoutes ( $ this , $ this -> routes ) ; } }
|
Build all the related urls to the current admin .
|
8,288
|
public function actionify ( $ action ) { if ( false !== ( $ pos = strrpos ( $ action , '.' ) ) ) { $ action = substr ( $ action , $ pos + 1 ) ; } if ( false === strpos ( $ this -> baseControllerName , ':' ) ) { $ action .= 'Action' ; } return lcfirst ( str_replace ( ' ' , '' , ucwords ( strtr ( $ action , '_-' , ' ' ) ) ) ) ; }
|
Convert a word in to the format for a symfony action action_name = > actionName .
|
8,289
|
public function get ( $ name , array $ options = [ ] ) { $ group = $ options [ 'group' ] ; $ menuItem = $ this -> menuFactory -> createItem ( $ options [ 'name' ] ) ; if ( empty ( $ group [ 'on_top' ] ) || false === $ group [ 'on_top' ] ) { foreach ( $ group [ 'items' ] as $ item ) { if ( $ this -> canGenerateMenuItem ( $ item , $ group ) ) { $ menuItem -> addChild ( $ this -> generateMenuItem ( $ item , $ group ) ) ; } } if ( false === $ menuItem -> hasChildren ( ) ) { $ menuItem -> setDisplay ( false ) ; } elseif ( ! empty ( $ group [ 'keep_open' ] ) ) { $ menuItem -> setAttribute ( 'class' , 'keep-open' ) ; $ menuItem -> setExtra ( 'keep_open' , $ group [ 'keep_open' ] ) ; } } elseif ( 1 === \ count ( $ group [ 'items' ] ) ) { if ( $ this -> canGenerateMenuItem ( $ group [ 'items' ] [ 0 ] , $ group ) ) { $ menuItem = $ this -> generateMenuItem ( $ group [ 'items' ] [ 0 ] , $ group ) ; $ menuItem -> setExtra ( 'on_top' , $ group [ 'on_top' ] ) ; } else { $ menuItem -> setDisplay ( false ) ; } } $ menuItem -> setLabel ( $ group [ 'label' ] ) ; return $ menuItem ; }
|
Retrieves the menu based on the group options .
|
8,290
|
public function renderWithExtraParams ( $ view , array $ parameters = [ ] , Response $ response = null ) { if ( ! $ this -> isXmlHttpRequest ( ) ) { $ parameters [ 'breadcrumbs_builder' ] = $ this -> get ( 'sonata.admin.breadcrumbs_builder' ) ; } $ parameters [ 'admin' ] = $ parameters [ 'admin' ] ?? $ this -> admin ; $ parameters [ 'base_template' ] = $ parameters [ 'base_template' ] ?? $ this -> getBaseTemplate ( ) ; $ parameters [ 'admin_pool' ] = $ this -> get ( 'sonata.admin.pool' ) ; return $ this -> originalRender ( $ view , $ parameters , $ response ) ; }
|
Renders a view while passing mandatory parameters on to the template .
|
8,291
|
public function batchActionDelete ( ProxyQueryInterface $ query ) { $ this -> admin -> checkAccess ( 'batchDelete' ) ; $ modelManager = $ this -> admin -> getModelManager ( ) ; try { $ modelManager -> batchDelete ( $ this -> admin -> getClass ( ) , $ query ) ; $ this -> addFlash ( 'sonata_flash_success' , $ this -> trans ( 'flash_batch_delete_success' , [ ] , 'SonataAdminBundle' ) ) ; } catch ( ModelManagerException $ e ) { $ this -> handleModelManagerException ( $ e ) ; $ this -> addFlash ( 'sonata_flash_error' , $ this -> trans ( 'flash_batch_delete_error' , [ ] , 'SonataAdminBundle' ) ) ; } return $ this -> redirectToList ( ) ; }
|
Execute a batch delete .
|
8,292
|
public function showAction ( $ id = null ) { $ request = $ this -> getRequest ( ) ; $ id = $ request -> get ( $ this -> admin -> getIdParameter ( ) ) ; $ object = $ this -> admin -> getObject ( $ id ) ; if ( ! $ object ) { throw $ this -> createNotFoundException ( sprintf ( 'unable to find the object with id: %s' , $ id ) ) ; } $ this -> checkParentChildAssociation ( $ request , $ object ) ; $ this -> admin -> checkAccess ( 'show' , $ object ) ; $ preResponse = $ this -> preShow ( $ request , $ object ) ; if ( null !== $ preResponse ) { return $ preResponse ; } $ this -> admin -> setSubject ( $ object ) ; $ fields = $ this -> admin -> getShow ( ) ; \ assert ( $ fields instanceof FieldDescriptionCollection ) ; if ( ! \ is_array ( $ fields -> getElements ( ) ) || 0 === $ fields -> count ( ) ) { @ trigger_error ( 'Calling this method without implementing "configureShowFields"' . ' is not supported since 3.40.0' . ' and will no longer be possible in 4.0' , E_USER_DEPRECATED ) ; } $ template = $ this -> admin -> getTemplate ( 'show' ) ; return $ this -> renderWithExtraParams ( $ template , [ 'action' => 'show' , 'object' => $ object , 'elements' => $ fields , ] , null ) ; }
|
Show action .
|
8,293
|
public function historyAction ( $ id = null ) { $ request = $ this -> getRequest ( ) ; $ id = $ request -> get ( $ this -> admin -> getIdParameter ( ) ) ; $ object = $ this -> admin -> getObject ( $ id ) ; if ( ! $ object ) { throw $ this -> createNotFoundException ( sprintf ( 'unable to find the object with id: %s' , $ id ) ) ; } $ this -> admin -> checkAccess ( 'history' , $ object ) ; $ manager = $ this -> get ( 'sonata.admin.audit.manager' ) ; if ( ! $ manager -> hasReader ( $ this -> admin -> getClass ( ) ) ) { throw $ this -> createNotFoundException ( sprintf ( 'unable to find the audit reader for class : %s' , $ this -> admin -> getClass ( ) ) ) ; } $ reader = $ manager -> getReader ( $ this -> admin -> getClass ( ) ) ; $ revisions = $ reader -> findRevisions ( $ this -> admin -> getClass ( ) , $ id ) ; $ template = $ this -> admin -> getTemplate ( 'history' ) ; return $ this -> renderWithExtraParams ( $ template , [ 'action' => 'history' , 'object' => $ object , 'revisions' => $ revisions , 'currentRevision' => $ revisions ? current ( $ revisions ) : false , ] , null ) ; }
|
Show history revisions for object .
|
8,294
|
public function historyViewRevisionAction ( $ id = null , $ revision = null ) { $ request = $ this -> getRequest ( ) ; $ id = $ request -> get ( $ this -> admin -> getIdParameter ( ) ) ; $ object = $ this -> admin -> getObject ( $ id ) ; if ( ! $ object ) { throw $ this -> createNotFoundException ( sprintf ( 'unable to find the object with id: %s' , $ id ) ) ; } $ this -> admin -> checkAccess ( 'historyViewRevision' , $ object ) ; $ manager = $ this -> get ( 'sonata.admin.audit.manager' ) ; if ( ! $ manager -> hasReader ( $ this -> admin -> getClass ( ) ) ) { throw $ this -> createNotFoundException ( sprintf ( 'unable to find the audit reader for class : %s' , $ this -> admin -> getClass ( ) ) ) ; } $ reader = $ manager -> getReader ( $ this -> admin -> getClass ( ) ) ; $ object = $ reader -> find ( $ this -> admin -> getClass ( ) , $ id , $ revision ) ; if ( ! $ object ) { throw $ this -> createNotFoundException ( sprintf ( 'unable to find the targeted object `%s` from the revision `%s` with classname : `%s`' , $ id , $ revision , $ this -> admin -> getClass ( ) ) ) ; } $ this -> admin -> setSubject ( $ object ) ; $ template = $ this -> admin -> getTemplate ( 'show' ) ; return $ this -> renderWithExtraParams ( $ template , [ 'action' => 'show' , 'object' => $ object , 'elements' => $ this -> admin -> getShow ( ) , ] , null ) ; }
|
View history revision of object .
|
8,295
|
public function historyCompareRevisionsAction ( $ id = null , $ base_revision = null , $ compare_revision = null ) { $ request = $ this -> getRequest ( ) ; $ this -> admin -> checkAccess ( 'historyCompareRevisions' ) ; $ id = $ request -> get ( $ this -> admin -> getIdParameter ( ) ) ; $ object = $ this -> admin -> getObject ( $ id ) ; if ( ! $ object ) { throw $ this -> createNotFoundException ( sprintf ( 'unable to find the object with id: %s' , $ id ) ) ; } $ manager = $ this -> get ( 'sonata.admin.audit.manager' ) ; if ( ! $ manager -> hasReader ( $ this -> admin -> getClass ( ) ) ) { throw $ this -> createNotFoundException ( sprintf ( 'unable to find the audit reader for class : %s' , $ this -> admin -> getClass ( ) ) ) ; } $ reader = $ manager -> getReader ( $ this -> admin -> getClass ( ) ) ; $ base_object = $ reader -> find ( $ this -> admin -> getClass ( ) , $ id , $ base_revision ) ; if ( ! $ base_object ) { throw $ this -> createNotFoundException ( sprintf ( 'unable to find the targeted object `%s` from the revision `%s` with classname : `%s`' , $ id , $ base_revision , $ this -> admin -> getClass ( ) ) ) ; } $ compare_object = $ reader -> find ( $ this -> admin -> getClass ( ) , $ id , $ compare_revision ) ; if ( ! $ compare_object ) { throw $ this -> createNotFoundException ( sprintf ( 'unable to find the targeted object `%s` from the revision `%s` with classname : `%s`' , $ id , $ compare_revision , $ this -> admin -> getClass ( ) ) ) ; } $ this -> admin -> setSubject ( $ base_object ) ; $ template = $ this -> admin -> getTemplate ( 'show_compare' ) ; return $ this -> renderWithExtraParams ( $ template , [ 'action' => 'show' , 'object' => $ base_object , 'object_compare' => $ compare_object , 'elements' => $ this -> admin -> getShow ( ) , ] , null ) ; }
|
Compare history revisions of object .
|
8,296
|
public function exportAction ( Request $ request ) { $ this -> admin -> checkAccess ( 'export' ) ; $ format = $ request -> get ( 'format' ) ; if ( ! $ this -> has ( 'sonata.admin.admin_exporter' ) ) { @ trigger_error ( 'Not registering the exporter bundle is deprecated since version 3.14.' . ' You must register it to be able to use the export action in 4.0.' , E_USER_DEPRECATED ) ; $ allowedExportFormats = ( array ) $ this -> admin -> getExportFormats ( ) ; $ class = ( string ) $ this -> admin -> getClass ( ) ; $ filename = sprintf ( 'export_%s_%s.%s' , strtolower ( ( string ) substr ( $ class , strripos ( $ class , '\\' ) + 1 ) ) , date ( 'Y_m_d_H_i_s' , strtotime ( 'now' ) ) , $ format ) ; $ exporter = $ this -> get ( 'sonata.admin.exporter' ) ; } else { $ adminExporter = $ this -> get ( 'sonata.admin.admin_exporter' ) ; $ allowedExportFormats = $ adminExporter -> getAvailableFormats ( $ this -> admin ) ; $ filename = $ adminExporter -> getExportFilename ( $ this -> admin , $ format ) ; $ exporter = $ this -> get ( 'sonata.exporter.exporter' ) ; } if ( ! \ in_array ( $ format , $ allowedExportFormats , true ) ) { throw new \ RuntimeException ( sprintf ( 'Export in format `%s` is not allowed for class: `%s`. Allowed formats are: `%s`' , $ format , $ this -> admin -> getClass ( ) , implode ( ', ' , $ allowedExportFormats ) ) ) ; } return $ exporter -> getResponse ( $ format , $ filename , $ this -> admin -> getDataSourceIterator ( ) ) ; }
|
Export data to specified format .
|
8,297
|
public function aclAction ( $ id = null ) { $ request = $ this -> getRequest ( ) ; if ( ! $ this -> admin -> isAclEnabled ( ) ) { throw $ this -> createNotFoundException ( 'ACL are not enabled for this admin' ) ; } $ id = $ request -> get ( $ this -> admin -> getIdParameter ( ) ) ; $ object = $ this -> admin -> getObject ( $ id ) ; if ( ! $ object ) { throw $ this -> createNotFoundException ( sprintf ( 'unable to find the object with id: %s' , $ id ) ) ; } $ this -> admin -> checkAccess ( 'acl' , $ object ) ; $ this -> admin -> setSubject ( $ object ) ; $ aclUsers = $ this -> getAclUsers ( ) ; $ aclRoles = $ this -> getAclRoles ( ) ; $ adminObjectAclManipulator = $ this -> get ( 'sonata.admin.object.manipulator.acl.admin' ) ; $ adminObjectAclData = new AdminObjectAclData ( $ this -> admin , $ object , $ aclUsers , $ adminObjectAclManipulator -> getMaskBuilderClass ( ) , $ aclRoles ) ; $ aclUsersForm = $ adminObjectAclManipulator -> createAclUsersForm ( $ adminObjectAclData ) ; $ aclRolesForm = $ adminObjectAclManipulator -> createAclRolesForm ( $ adminObjectAclData ) ; if ( 'POST' === $ request -> getMethod ( ) ) { if ( $ request -> request -> has ( AdminObjectAclManipulator :: ACL_USERS_FORM_NAME ) ) { $ form = $ aclUsersForm ; $ updateMethod = 'updateAclUsers' ; } elseif ( $ request -> request -> has ( AdminObjectAclManipulator :: ACL_ROLES_FORM_NAME ) ) { $ form = $ aclRolesForm ; $ updateMethod = 'updateAclRoles' ; } if ( isset ( $ form ) ) { $ form -> handleRequest ( $ request ) ; if ( $ form -> isValid ( ) ) { $ adminObjectAclManipulator -> $ updateMethod ( $ adminObjectAclData ) ; $ this -> addFlash ( 'sonata_flash_success' , $ this -> trans ( 'flash_acl_edit_success' , [ ] , 'SonataAdminBundle' ) ) ; return new RedirectResponse ( $ this -> admin -> generateObjectUrl ( 'acl' , $ object ) ) ; } } } $ template = $ this -> admin -> getTemplate ( 'acl' ) ; return $ this -> renderWithExtraParams ( $ template , [ 'action' => 'acl' , 'permissions' => $ adminObjectAclData -> getUserPermissions ( ) , 'object' => $ object , 'users' => $ aclUsers , 'roles' => $ aclRoles , 'aclUsersForm' => $ aclUsersForm -> createView ( ) , 'aclRolesForm' => $ aclRolesForm -> createView ( ) , ] , null ) ; }
|
Returns the Response object associated to the acl action .
|
8,298
|
protected function getRestMethod ( ) { $ request = $ this -> getRequest ( ) ; if ( Request :: getHttpMethodParameterOverride ( ) || ! $ request -> request -> has ( '_method' ) ) { return $ request -> getMethod ( ) ; } return $ request -> request -> get ( '_method' ) ; }
|
Returns the correct RESTful verb given either by the request itself or via the _method parameter .
|
8,299
|
protected function configure ( ) { $ request = $ this -> getRequest ( ) ; $ adminCode = $ request -> get ( '_sonata_admin' ) ; if ( ! $ adminCode ) { throw new \ RuntimeException ( sprintf ( 'There is no `_sonata_admin` defined for the controller `%s` and the current route `%s`' , \ get_class ( $ this ) , $ request -> get ( '_route' ) ) ) ; } $ this -> admin = $ this -> container -> get ( 'sonata.admin.pool' ) -> getAdminByAdminCode ( $ adminCode ) ; if ( ! $ this -> admin ) { throw new \ RuntimeException ( sprintf ( 'Unable to find the admin class related to the current controller (%s)' , \ get_class ( $ this ) ) ) ; } $ this -> templateRegistry = $ this -> container -> get ( $ this -> admin -> getCode ( ) . '.template_registry' ) ; if ( ! $ this -> templateRegistry instanceof TemplateRegistryInterface ) { throw new \ RuntimeException ( sprintf ( 'Unable to find the template registry related to the current admin (%s)' , $ this -> admin -> getCode ( ) ) ) ; } $ rootAdmin = $ this -> admin ; while ( $ rootAdmin -> isChild ( ) ) { $ rootAdmin -> setCurrentChild ( true ) ; $ rootAdmin = $ rootAdmin -> getParent ( ) ; } $ rootAdmin -> setRequest ( $ request ) ; if ( $ request -> get ( 'uniqid' ) ) { $ this -> admin -> setUniqid ( $ request -> get ( 'uniqid' ) ) ; } }
|
Contextualize the admin class depends on the current request .
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.