예제 #1
0
 private function uploadImage()
 {
     // files storage folder
     $public_dir = $_SERVER['DOCUMENT_ROOT'] . DS . 'uploads/images/';
     if (!($exists = is_dir($public_dir))) {
         $exists = @mkdir($public_dir, 0775, true);
     }
     $_FILES['file']['type'] = strtolower($_FILES['file']['type']);
     if ($_FILES['file']['type'] == 'image/png' || $_FILES['file']['type'] == 'image/jpg' || $_FILES['file']['type'] == 'image/gif' || $_FILES['file']['type'] == 'image/jpeg' || $_FILES['file']['type'] == 'image/pjpeg') {
         // setting file's mysterious name
         //$filename = md5(date('YmdHis')).'.'.$extension;
         $path_parts = pathinfo($_FILES['file']['name']);
         $raw_filename = preg_replace("/\\.[^.\\s]{3,4}\$/", "", $_FILES['file']['name']);
         $filename = \CMF::slug($raw_filename) . '.' . $path_parts['extension'];
         $file = $public_dir . $filename;
         if (file_exists($file)) {
             $file = $public_dir . date('ymdHis') . $filename;
         }
         // copying
         copy($_FILES['file']['tmp_name'], $file);
         // displaying file
         $array = array('filelink' => '/uploads/images/' . $filename);
         return stripslashes(json_encode($array));
     }
 }
예제 #2
0
 /** @inheritdoc */
 public static function displayForm($value, &$settings, $model)
 {
     $settings = static::settings($settings);
     $settings['label'] = isset($settings['label']) ? $settings['label'] : true;
     $settings['required'] = isset($settings['required']) ? $settings['required'] : false;
     $settings['errors'] = $model->getErrorsForField($settings['mapping']['fieldName']);
     $settings['has_errors'] = count($settings['errors']) > 0;
     $attributes = array('class' => 'field-type-file video controls control-group' . ($settings['has_errors'] ? ' error' : ''), 'data-field-name' => $settings['mapping']['fieldName'], 'id' => 'field_' . \CMF::slug($settings['mapping']['fieldName']));
     $value = static::getValue($value, $settings, $model);
     $content = strval(\View::forge('admin/fields/video.twig', array('settings' => $settings, 'value' => $value), false));
     $settings['value'] = $value;
     if (!(isset($settings['wrap']) && $settings['wrap'] === false)) {
         $content = html_tag('div', $attributes, $content);
     }
     return array('content' => $content, 'widget' => false, 'assets' => array(), 'js_data' => $settings);
 }
예제 #3
0
 /** @inheritdoc */
 public static function displayForm($value, &$settings, $model)
 {
     if (!is_array($value)) {
         $value = array('src' => $value);
     }
     $metadata = $model->metadata();
     $settings = static::settings($settings);
     $settings['label'] = isset($settings['label']) ? $settings['label'] : true;
     $settings['required'] = isset($settings['required']) ? $settings['required'] : false;
     $settings['errors'] = $model->getErrorsForField($settings['mapping']['fieldName']);
     $settings['has_errors'] = count($settings['errors']) > 0;
     $settings['model'] = $metadata->name;
     $preview_value = isset($value) && isset($value['src']) ? str_replace($settings['path'], '', $value['src']) : '';
     $content = strval(\View::forge('admin/fields/file.twig', array('settings' => $settings, 'value' => $value, 'preview_value' => $preview_value), false));
     $attributes = array('class' => 'field-type-file file controls control-group' . ($settings['has_errors'] ? ' error' : ''), 'data-field-name' => $settings['mapping']['fieldName'], 'id' => 'field_' . \CMF::slug($settings['mapping']['fieldName']));
     if (!(isset($settings['wrap']) && $settings['wrap'] === false)) {
         $content = html_tag('div', $attributes, $content);
     }
     return array('content' => $content, 'widget' => false, 'assets' => array(), 'js_data' => $settings);
 }
예제 #4
0
 /**
  * Returns a unique path for a file. Check that the name does not exist,
  * and appends a suffix otherwise.
  * @param string $directory Target directory
  * @param string $filename The name of the file to use.
  */
 public static function getUniqueFilePath($directory, $filename)
 {
     // Allow only one process at the time to get a unique file name, otherwise
     // if multiple people would upload a file with the same name at the same time
     // only the latest would be saved.
     if (function_exists('sem_acquire')) {
         $lock = sem_get(ftok(__FILE__, 'u'));
         sem_acquire($lock);
     }
     $pathinfo = pathinfo($filename);
     $base = \CMF::slug($pathinfo['filename'], false);
     $ext = isset($pathinfo['extension']) ? $pathinfo['extension'] : '';
     $ext = $ext == '' ? $ext : '.' . $ext;
     $directory = rtrim($directory, DIRECTORY_SEPARATOR);
     $unique = $base;
     $suffix = 0;
     if (file_exists($directory . DIRECTORY_SEPARATOR . $unique . $ext)) {
         // Get unique file name for the file, by appending an integer suffix.
         $counter = 0;
         do {
             $suffix = '_' . ++$counter;
             $unique = $base . $suffix;
         } while (file_exists($directory . DIRECTORY_SEPARATOR . $unique . $ext));
     }
     $result = $directory . DIRECTORY_SEPARATOR . $unique . $ext;
     // Create an empty target file
     if (!touch($result)) {
         // Failed
         $result = false;
     }
     if (function_exists('sem_acquire')) {
         sem_release($lock);
     }
     return $result;
 }
예제 #5
0
 /**
  * Takes an entity and works out whether it has a relation to CMF's URL Model. If so,
  * it updates the properties of the associated URL object.
  * 
  * @param object $entity
  * @param \Doctrine\ORM\EntityManager $em
  * @param \Doctrine\ORM\UnitOfWork|null $uow
  * @return void
  */
 protected function process(&$entity, &$em, &$uow)
 {
     $entity_class = get_class($entity);
     $entity_namespace = trim(\CMF::slug(str_replace('\\', '/', \Inflector::get_namespace($entity_class))), '/');
     $metadata = $em->getClassMetadata($entity_class);
     // Ignore URL entities themselves
     if ($metadata->name == 'CMF\\Model\\URL') {
         return;
     }
     $url_associations = $metadata->getAssociationsByTargetClass('CMF\\Model\\URL');
     if (!empty($url_associations)) {
         // A bit hacky, but if this is a root tree item don't bother
         if (property_exists($entity, 'is_root') && $entity->is_root === true) {
             return;
         }
         $url_field = null;
         foreach ($url_associations as $key => $association) {
             if ($association['type'] == ClassMetadataInfo::ONE_TO_ONE && $association['orphanRemoval']) {
                 $url_field = $key;
                 break;
             }
         }
         if ($url_field == null) {
             return;
         }
         $settings = $entity->settings();
         $url_settings = isset($settings[$url_field]) ? $settings[$url_field] : array();
         $url_item = $entity->get($url_field);
         if ($new_url = is_null($url_item)) {
             $url_item = new \CMF\Model\URL();
         }
         // Don't run if this is an alias...
         $alias = $url_item->alias;
         if (!is_null($alias) && !empty($alias)) {
             return;
         }
         // Don't run if this is an external link...
         if ($url_item->isExternal()) {
             return;
         }
         $prefix = $this->getPrefix($entity);
         $slug = '';
         if (isset($url_settings['keep_updated']) && !$url_settings['keep_updated']) {
             $slug = \CMF::slug($url_item->slug);
         } else {
             $slug = $entity->urlSlug();
         }
         $url = $prefix . $slug;
         if ($url != '/') {
             $url = rtrim($url, '/');
         }
         $current_url = $url_item->url;
         $entity_id = $entity->id;
         $url_id = $url_item->get('id');
         // Check for duplicates, only if this is an already existing item
         if (!empty($entity_id) && !is_null($entity_id)) {
             // Set data from the entity if the prefix is null
             if (is_null($prefix)) {
                 $prefix = $this->getPrefix($entity);
                 $url = $prefix . $slug;
             }
             // Set data from the entity if the slug is null
             if (is_null($slug)) {
                 $slug = $entity->urlSlug();
                 $url = $prefix . $slug;
             }
             // Set it to the item's ID if empty
             if (is_null($slug)) {
                 $slug = $entity_id . "";
                 $url = $prefix . $slug;
             }
             $slug_orig = $slug;
             $unique = $this->checkUnique($url, $entity_id, $url_id);
             $counter = 2;
             while (!$unique) {
                 $slug = $slug_orig . '-' . $counter;
                 $url = $prefix . $slug;
                 $unique = $this->checkUnique($url, $entity_id, $url_id);
                 $counter++;
             }
             // Add it to the list of saved URLs
             $this->savedUrls[$url] = $entity_id;
         }
         $url_item->set('item_id', $entity->get('id'));
         $url_item->set('prefix', $prefix);
         $url_item->set('slug', $slug);
         $url_item->set('url', $url);
         $url_item->set('type', $metadata->name);
         $entity->set($url_field, $url_item);
         $em->persist($url_item);
         // Skip this if the url hasn't changed
         if (!$new_url && $current_url == $url) {
             return;
         }
         $url_metadata = $em->getClassMetadata('CMF\\Model\\URL');
         $url_changeset = $uow->getEntityChangeSet($url_item);
         if (!empty($url_changeset)) {
             $uow->recomputeSingleEntityChangeSet($url_metadata, $url_item);
         } else {
             $uow->computeChangeSet($url_metadata, $url_item);
         }
         $uow->recomputeSingleEntityChangeSet($metadata, $entity);
         $associations = $metadata->getAssociationMappings();
         foreach ($associations as $association_name => $association) {
             // Only do it if it's the inverse side, to prevent the dreaded infinite recursion
             if (!$association['isOwningSide']) {
                 $items = $entity->{$association_name};
                 if (!empty($items)) {
                     foreach ($items as $item) {
                         $this->process($item, $em, $uow);
                     }
                 }
             }
         }
     }
 }
예제 #6
0
 /**
  * The URL-friendly identifier for the model. Uses the static slug_fields property to generate it.
  * @see  CMF\Model\Base::$_slug_fields
  * @return string
  */
 public function urlSlug()
 {
     $class_name = get_class($this);
     $self = $this;
     $values = array_map(function ($prop) use($self) {
         return $self->{$prop};
     }, $class_name::$_slug_fields);
     return \CMF::slug(implode(' ', $values), true, true);
 }