/** * Saves a file from a url. * * @param string $url The full url for the file to save. * @param string $type The file type to store in the db (file, image, video) * * @return Returns the media id if created successfully, boolean false otherwise. */ public static function saveFromUrl($url, $type) { if (!($nameAndPath = self::_getNewNameAndPath(basename($url)))) { return false; } list($newFilename, $fullPath) = $nameAndPath; if ($urlContent = file_get_contents($url)) { if (file_put_contents($fullPath, $urlContent)) { $id = Media::m('file')->insert(array('name' => $newFilename, 'path' => Media::getMediaPath(), 'size' => filesize($fullPath), 'mime_type' => self::_determineMimeType($fullPath), 'file_type' => $type)); return $id; } else { self::$_error = 'Unable to save ' . $type . ' from URL.'; } } else { self::$_error = 'Unable to read file contents from URL.'; Dev::debug('media', 'ERROR: Cant save from url: ' . $url); } return false; }
public function compileValue() { if (null !== $this->_compile_value) { return $this->_compile_value; } $r = $this->admin_object->reflection->getProperty($this->class_property); $property_name = $this->class_property; $property_code = '_property_value'; if ($this->template) { try { $this->admin_object->generator->loadTwigTemplatePath($this->template); } catch (\InvalidArgumentException $e) { throw $e; $this->setChainException($e); $this->throwError(" %s->%s use template:`%s` not found", $this->admin_name, $this->class_property, $this->template); } $code = '{% include "' . $this->template . '" with { admin: admin, action: action, property:' . json_encode($this->class_property) . ', object: _object, value:attribute(_object, "' . $this->class_property . '") } %}'; } else { if ($this->code) { $code = $this->code; } else { if ($this->admin_object->workflow && $this->class_property === $this->admin_object->workflow->property) { $code = '{{ admin.getObjectWorkflowLabel(_object) }}'; } else { if ($this->admin_object->owner && $this->class_property === $this->admin_object->owner->owner_property) { // $code = '{{ admin.owneradmin.string( attribute(_object, "' . $this->class_property . '") ) }}' ; $map = $this->getPropertyDoctrineAssociationMapping(); $code = $this->getMapValueCode($property_code, $map); } else { $code = '{{ ' . $property_code . ' }}'; if ($this->form_element) { if ($this->form_element instanceof Form\Integer) { if ($this->form_element instanceof Form\Range) { $code = $property_code; if (null !== $this->form_element->precision) { $code .= sprintf('|number_format(%d, ".", ",")', $this->form_element->precision); } $code = '<span class="number">{{ ' . $code . ' }}</span>'; if (null !== $this->form_element->_unit) { $code .= $this->form_element->_unit->getTwigCode(); } } else { if ($this->form_element instanceof Form\Money) { $code = '{{ ' . sprintf('sf_money(%s, %d, "%s")', $property_code, $this->form_element->precision, $this->form_element->currency) . ' }}'; } else { if ($this->form_element instanceof Form\Percent) { $code = '{{ ' . sprintf('%s|number_format(%d, ".", ",")', $property_code, $this->form_element->precision) . ' }}'; } else { $code = '{{ ' . sprintf('%s|number_format(0, ".", ",")', $property_code) . ' }}'; } } } } else { if ($this->form_element instanceof Form\Choice) { $code = '{{ admin.getChoiceText("' . $this->class_property . '", _object) }}'; if ($this->form_element instanceof Form\Owner) { $code = '{{ ' . $property_code . ' }}'; } else { if ($this->form_element instanceof Form\Entity) { $map = $this->getPropertyDoctrineAssociationMapping(); $code = $this->getMapValueCode($property_code, $map, $action = 'view'); } else { if ($this->form_element instanceof Form\Checkbox) { } else { if ($this->form_element instanceof Form\Radio) { } else { if ($this->form_element instanceof Form\Bool) { } else { if ($this->form_element instanceof Form\Country) { } else { if ($this->form_element->multiple) { $code = '{{ admin.getChoicesText("' . $this->class_property . '", _object) }}'; } } } } } } } } else { if ($this->form_element instanceof Form\DateTime) { $format = $this->format ?: $this->form_element->format; $code = '{{ sf_date_format(' . $property_code . ', "' . $format . '" ) }}'; if ($this->form_element instanceof Form\Birthday) { } } else { if ($this->form_element instanceof Form\File) { if ($this->form_element instanceof Form\Image) { // $this->form_element->image_size $code = '{% if ' . $property_code . ' %}' . sprintf('<div class="sf_image_view" style="width:%spx;height:%spx;"><img src="{{ web_assets_dir }}{{ %s }}" /></div>', $this->form_element->image_size[0], $this->form_element->image_size[1], $property_code) . '{% endif %}'; } else { $code = '{% if ' . $property_code . ' %}' . sprintf('<div class="sf_file_view"><a href="{{ web_assets_dir }}{{ %s }}" >{{ %s }}</a></div>', $property_code, $property_code) . '{% endif %}'; } } else { if ($this->form_element instanceof Form\Embed) { $map = $this->getPropertyDoctrineAssociationMapping(); $code = $this->getMapValueCode($property_code, $map); } else { if ($this->form_element instanceof Form\Text) { if ($this->form_element instanceof Form\Textarea) { if ($this->form_element instanceof Form\Html) { $code = '{{ ' . $property_code . ' | raw }}'; } else { if ($this->form_element instanceof Form\Markdown) { } } } else { if ($this->form_element instanceof Form\Color) { } else { if ($this->form_element instanceof Form\Url) { } else { if ($this->form_element instanceof Form\Email) { } else { if ($this->form_element instanceof Form\Password) { $code = '******'; } else { if ($this->admin_object->property_value_name === $this->class_property) { // add view to this action $url = '{{ admin.path("view", _object) }}'; $code = '{% if admin.auth("view", _object) %}'; $code .= sprintf('<a href="%s">{{ %s }}</a>', $url, $property_code); $code .= '{% else %}'; $code .= sprintf('{{ %s }}', $property_code); $code .= '{% endif %}'; } } } } } } } else { if ($this->form_element instanceof Form\Tree) { $map = $this->getPropertyDoctrineAssociationMapping(); $code = $this->getMapValueCode($property_code, $map); } else { if ($this->form_element instanceof \Symforce\UserBundle\Form\MetaType\AuthorizeMetaType) { $code = '*'; } else { \Dev::debug($this->admin_object->name, $this->class_property, get_class($this->form_element)); } } } } } } } } } else { // check form type /** * @var \Doctrine\ORM\Mapping\ClassMetadata */ $meta = $this->getEntityMetadata(); $map = $this->getPropertyDoctrineAssociationMapping(); if ($map) { $target_class = $map['targetEntity']; // to_one if ($map['type'] === \Doctrine\ORM\Mapping\ClassMetadataInfo::MANY_TO_ONE) { // find parent route $code = $this->getMapValueCode($property_code, $map); } else { if ($map['type'] === \Doctrine\ORM\Mapping\ClassMetadataInfo::ONE_TO_MANY) { // find child route $child_admin_object = $this->admin_object->generator->getAdminByClass($target_class); $url = sprintf('{{ admin.childpath(_object, "%s", "%s", "list", %s ) }}', $this->class_property, $child_admin_object->name, $property_code); $text = $child_admin_object->label->getTwigCode(); $code = '{% if ' . sprintf('sf_auth("%s", "list")', $child_admin_object->name) . ' %}'; $code .= sprintf('<a href="%s">%s ({{ %s.count() }})</a>', $url, $text, $property_code); $code .= '{% else %}'; $code .= sprintf('%s ({{ %s.count() }}) ', $text, $property_code); $code .= '{% endif %}'; } else { if ($map['type'] === \Doctrine\ORM\Mapping\ClassMetadataInfo::ONE_TO_ONE) { // find sibling entity $code = $this->getMapValueCode($property_code, $map); } else { throw new \Exception('bigger error'); } } } } else { $orm_type = $this->getPropertyDoctrineType(); if ($orm_type) { if ('date' === $orm_type) { $format = $this->format ?: "Y/m/d"; $code = '{{ sf_date_format(' . $property_code . ', "' . $format . '" ) }}'; } else { if ('datetime' === $orm_type) { $format = $this->format ?: "Y/m/d H:i:s"; $code = '{{ sf_date_format(' . $property_code . ', "' . $format . '" ) }}'; } else { if ('time' === $orm_type) { $format = $this->format ?: "H:i:s"; $code = '{{ sf_date_format(' . $property_code . ', "' . $format . '" ) }}'; } else { if ('integer' === $orm_type) { } else { if ('text' === $orm_type) { } else { if ('string' === $orm_type) { } else { if ('boolean' === $orm_type) { } else { if ('array' === $orm_type) { // list foreach array value ? } else { \Dev::debug($this->admin_object->name, $this->class_property, $orm_type); } } } } } } } } } else { // not sure what to do // \Dev::debug($this->admin_object->name, $this->class_property, $orm_type ); } } } if ($this->tag) { $code = $this->tag->compileTag($code); } if ($this->href) { // @TODO add link handle $code = $this->href->compileTag($code); } } } } } $this->_compile_value = $code; return $code; }