/** * Returns the files of this entity. * * If no group is passed, this method will returns all files grouped by file groups. * * @return \MapasCulturais\Entities\File[] The array of files. */ function getFiles($group = null) { $app = App::i(); if ($this instanceof File) { $files = $this->getChildren(); $result = []; foreach ($files as $file) { $result[substr($file->group, 4)] = $file; } } else { $result = \MapasCulturais\Entities\File::sortFilesByGroup($this->__files); } if ($group) { $registeredGroup = $app->getRegisteredFileGroup($this->controllerId, $group); if ($registeredGroup && $registeredGroup->unique) { $result = isset($result[$group]) ? $result[$group] : null; } else { $result = isset($result[$group]) ? $result[$group] : []; } } return $result; }
/** * Returns the full path to the file. * * @param \MapasCulturais\Entities\File $file The file to get the path * @param bool $relative Returns the relative path? * * @hook **storage.path ($file, &$path)** * @hook **storage.path({$owner_entity}) ($file, &$path)** * @hook **storage.path({$owner_entity}:{$file_group}) ($file, &$path)** */ public function getPath(\MapasCulturais\Entities\File $file, $relative = false) { $app = App::i(); $owner = $file->getOwner(); $result = $this->_getPath($file, $relative); $app->applyHookBoundTo($this, 'storage.path', ['file' => $file, 'path' => &$result]); if ($owner) { $app->applyHookBoundTo($this, 'storage.path(' . $owner->getHookClassPath() . ':' . $file->group . ')', ['file' => $file, 'path' => &$result]); } return $result; }
function register(Agent $agent, File $registrationForm = null) { $app = App::i(); $app->applyHookBoundTo($this, 'project.register:before', array($agent, $registrationForm)); if (!$this->isRegistrationOpen()) { throw new \MapasCulturais\Exceptions\PermissionDenied(App::i()->user, $this, 'register'); } $group = $app->projectRegistrationAgentRelationGroupName; $relation_class = $this->getAgentRelationEntityClassName(); if ($this->isRegistered($agent)) { return $app->txt("This agent is already registered in this project."); } $relation = new $relation_class(); $relation->agent = $agent; $relation->owner = $this; $relation->group = $group; $relation->status = ProjectAgentRelation::STATUS_REGISTRATION; $relation->save(); if ($registrationForm) { $registrationForm->owner = $relation; $registrationForm->save(); } $app->em->flush(); $this->clearAgentRelationCache(); $app->applyHookBoundTo($this, 'project.register:after', array($relation)); return $relation; }
/** * Returns a transformed image. * * @param string $wideimage_operations * * @example $image->transform('resize(200, 100)->crop(50, 50, 30, 20)->rotate(20)') * * @return File */ protected function _transform($transformation_name, $wideimage_operations) { if (!trim($wideimage_operations)) { return $this; } $wideimage_operations = strtolower(str_replace(' ', '', $wideimage_operations)); $hash = md5($this->md5 . $this->name . $wideimage_operations); // modify the filename adding the hash before the file extension. ex: file.png => file-5762a89ee8e05021006d6c35095903b5.png $image_name = preg_replace("#(\\.[[:alnum:]]+)\$#i", '-' . $hash . '$1', $this->name); if (key_exists($transformation_name, $this->files)) { return $this->files[$transformation_name]; } if (!file_exists($this->getPath())) { return $this; } $new_image = \WideImage\WideImage::load($this->getPath()); eval('$new_image = $new_image->' . $wideimage_operations . ';'); $tmp_filename = sys_get_temp_dir() . '/' . $image_name; $new_image->saveToFile($tmp_filename); $image = new File(array('error' => UPLOAD_ERR_OK, 'name' => $image_name, 'type' => $this->mimeType, 'tmp_name' => $tmp_filename, 'size' => filesize($tmp_filename))); $image->group = $transformation_name; $image->setOwner($this); $image->save(true); return $image; }