function setParent($parent) { //Added this case to remove parent from the entity if ($parent === null) { $this->parent = null; return; } elseif (!is_object($parent)) { return; } $parent->checkPermission('createChild'); $error1 = App::txt('O pai não pode ser o filho.'); $error2 = App::txt('O pai deve ser do mesmo tipo que o filho.'); if (!key_exists('parent', $this->_validationErrors)) { $this->_validationErrors['parent'] = array(); } if ($parent && $parent->id === $this->id) { $this->_validationErrors['parent'][] = $error1; } elseif (key_exists('parent', $this->_validationErrors) && in_array($error1, $this->_validationErrors['parent'])) { $key = array_search($error, $this->_validationErrors['parent']); unset($this->_validationErrors['parent'][$key]); } if ($parent && $parent->className !== $this->className) { $this->_validationErrors['parent'][] = $error2; } elseif (key_exists('parent', $this->_validationErrors) && in_array($error2, $this->_validationErrors['parent'])) { $key = array_search($error, $this->_validationErrors['parent']); unset($this->_validationErrors['parent'][$key]); } if (!$this->_validationErrors['parent']) { unset($this->_validationErrors['parent']); } $this->parent = $parent; }
/** * Sets the value of the location property. * * You can set the value of this property using a GeoPoint object or using an array, * * <code> * // example with GeoPoint * $entity->location = new GeoPoint(-45.123, -23.345); * * // example with arrays * $entity->location = [-45.123, -23.345]; * $entity->location = ['x' => -45.123, 'y' => -23.345]; * $entity->location = ['longitude' => -45.123, 'latitude' => -23.345]; * * </code> * * This method sets the value of the property _geoLocation with a PostGIS ST_Geography type. * * @param \MapasCulturais\Types\GeoPoint|array $location */ function setLocation($location) { $x = $y = null; if (!$location instanceof GeoPoint) { if (is_array($location) && key_exists('x', $location) && key_exists('y', $location)) { $x = $location['x']; $y = $location['y']; } elseif (is_array($location) && key_exists('longitude', $location) && key_exists('latitude', $location)) { $x = $location['longitude']; $y = $location['latitude']; } elseif (is_array($location) && count($location) === 2 && is_numeric($location[0]) && is_numeric($location[1])) { $x = $location[0]; $y = $location[1]; } else { throw new \Exception(App::txt('The location must be an instance of \\MapasCulturais\\Types\\GeoPoint or an array with two numeric values')); } $location = new GeoPoint($x, $y); } if (is_numeric($x) && is_numeric($y)) { $rsm = new \Doctrine\ORM\Query\ResultSetMapping(); $sql = "SELECT ST_GeographyFromText('POINT({$location->longitude} {$location->latitude})') AS geo"; $rsm->addScalarResult('geo', 'geo'); $query = App::i()->em->createNativeQuery($sql, $rsm); $this->_geoLocation = $query->getSingleScalarResult(); } $this->location = $location; }
/** * Sets the Longitude of the point. * * @param float $val Longitude * * @throws \Exception if the value is greater than 180 or less then -180 */ public function setLongitude($val) { if ($val > 180 || $val < -180) { throw new \Exception(App::txt('Longitude must be a float between 180 and -180')); } $this->longitude = $val; }
function setParent(\MapasCulturais\Entity $parent = null) { if (is_object($this->parent) && is_object($parent) && $this->parent->equals($parent)) { return; } $error1 = App::txt('O pai não pode ser o filho.'); $error2 = App::txt('O pai deve ser do mesmo tipo que o filho.'); if (!key_exists('parent', $this->_validationErrors)) { $this->_validationErrors['parent'] = []; } if ($parent && $parent->id === $this->id) { $this->_validationErrors['parent'][] = $error1; } elseif (key_exists('parent', $this->_validationErrors) && in_array($error1, $this->_validationErrors['parent'])) { $key = array_search($error, $this->_validationErrors['parent']); unset($this->_validationErrors['parent'][$key]); } if ($parent && $parent->className !== $this->className) { $this->_validationErrors['parent'][] = $error2; } elseif (key_exists('parent', $this->_validationErrors) && in_array($error2, $this->_validationErrors['parent'])) { $key = array_search($error, $this->_validationErrors['parent']); unset($this->_validationErrors['parent'][$key]); } if (!$this->_validationErrors['parent']) { unset($this->_validationErrors['parent']); } $this->_newParent = $parent; }
/** * Set the parent entity. * * @param \MapasCulturais\Entity $parent */ function setParent(\MapasCulturais\Entity $parent = null) { if (is_object($this->parent) && is_object($parent) && $this->parent->equals($parent)) { return; } $error_diff_type = App::txt('The parent entity must be of the same type'); $error_same_obj = App::txt('The parent and the child are the same object'); $error_circ_ref = App::txt('Circular reference'); $is_object = $parent && is_object($parent); if (!key_exists('parent', $this->_validationErrors)) { $this->_validationErrors['parent'] = []; } if ($is_object) { if ($parent->equals($this)) { $this->_validationErrors['parent'][] = $error_same_obj; } else { if ($parent->getClassName() !== $this->getClassName()) { $this->_validationErrors['parent'][] = $error_diff_type; } else { $_parent = $parent; while ($_parent = $_parent->getParent()) { if ($_parent->equals($this)) { $this->_validationErrors['parent'][] = $error_circ_ref; continue; } } } } } if (!$this->_validationErrors['parent']) { unset($this->_validationErrors['parent']); } $this->_newParent = $parent; }
function getGroup() { if ($this->group == 'registration') { return App::txt("project registration"); } else { return $this->group; } }
/** * Validates the file and if it is not valid returns the error message * */ function getError(\MapasCulturais\Entities\File $file) { $ok = false; foreach ($this->_validations as $validation) { if (preg_match("#{$validation}#i", $file->mimeType)) { $ok = true; break; } } return !$ok ? App::txt($this->errorMessage) : ''; }
/** * Returns the ApiOutput Object. * * @return \MapasCulturais\ApiOutput */ protected function getApiOutput() { $app = App::i(); $type = key_exists('@type', $this->data) ? $this->data['@type'] : $app->config['app.defaultApiOutput']; $responder = $app->getRegisteredApiOutputById($type); if (!$responder) { echo sprintf(App::txt("type %s is not registered."), $type); App::i()->stop(); } else { return $responder; } }
public function addStylesToArray($group, $script, array &$array) { if (!in_array($script[1], $array)) { foreach ($script[2] as $dep) { if (key_exists($dep, $this->_enqueuedStyles[$group])) { $this->addScriptToArray($group, $this->_enqueuedStyles[$group][$dep], $array); } else { throw new \Exception(sprintf(App::txt('Missing script dependence: %s depends on %s'), $script[0], $dep)); } } $array[] = $script[1]; } }
/** * Validates the entity properties and returns the errors messages. * * The entity errors messages uses php gettext. * * If this entity uses metadata, this method will call getMetadataValidationErrors() method * * <code> * /** * * Example of the array of errors: * {@*} * array( * 'name' => [ 'The name is required' ], * 'email' => [ 'The first error message', 'The second error message' ] * ) * </code> * * @see \MapasCulturais\App::txt() The MapasCulturais GetText method * @see \MapasCulturais\Traits\Metadata::getMetadataValidationErrors() Metadata Validation Errors * * @return array */ public function getValidationErrors() { $errors = $this->_validationErrors; $class = get_called_class(); foreach ($class::$validations as $property => $validations) { if (!$this->{$property} && !key_exists('required', $validations)) { continue; } foreach ($validations as $validation => $error_message) { $validation = trim($validation); $ok = true; if ($validation == 'required') { $ok = (bool) $this->{$property}; } elseif ($validation == 'unique') { $ok = $this->validateUniquePropertyValue($property); } elseif (strpos($validation, 'v::') === 0) { $validation = str_replace('v::', 'MapasCulturais\\Validator::', $validation); eval('$ok = ' . $validation . '->validate($this->' . $property . ');'); } else { $value = $this->{$property}; eval('$ok = ' . $validation . ';'); } if (!$ok) { if (!key_exists($property, $errors)) { $errors[$property] = []; } $errors[$property][] = App::txt($error_message); } } } if ($this->usesTypes() && !$this->_type) { $errors['type'] = [App::txt('The type is required')]; } elseif ($this->usesTypes() && !$this->validateType()) { $errors['type'] = [App::txt('Invalid type')]; } if ($this->usesMetadata()) { $errors = $errors + $this->getMetadataValidationErrors(); } if ($this->usesTaxonomies()) { $errors = $errors + $this->getTaxonomiesValidationErrors(); } return $errors; }
/** * Handle file uploads and creates the File Entities associating them to the owner of the id. * * You can upload files with description. * * For example, if you want to change the avatar of the agent with id 99 you need to post the below form: * <code> * <form method="post" action="/agent/upload/id:99" enctype="multipart/form-data"> * <input type="file" name="avatar" /> * </form> * </code> * * Example of form to upload a file with description: * <code> * <form method="post" action="/agent/upload/id:99" enctype="multipart/form-data"> * <input type="file" name="download" /> * <input type="text" name="description[downloads]" /> * </form> * </code> * * This action response a json with uploaded files info or errors. * * Example of an error response: * <code> * [{"error":"The uploaded file is not a valid image."}] * </code> * * Example of a successfull avatar upload response: * <code> * {"avatar":{"url":"http:\/\/mapasculturais.domain/files/filesuploaded.jpg","description":null}} * </code> * * * @see http://wideimage.sourceforge.net/documentation/manipulating-images/ * */ function POST_upload() { /** * @todo Melhores Mensagens de erro */ $this->requireAuthentication(); $owner = $this->requestedEntity; if (!$owner) { $this->errorJson(App::txt('the owner not exists')); return; } $file_class_name = $owner->getFileClassName(); $app = App::i(); // if no files uploaded or no id in request data, return an error if (empty($_FILES) || !$this->data['id']) { $this->errorJson(App::txt('no files uploaded')); return; } $result = []; $files = []; // the group of the files is the key in $_FILES array foreach (array_keys($_FILES) as $group_name) { // $this->errorJson('asd '.$this->id.' '.$group_name.' '.$app->getRegisteredFileGroup($this->id, $group_name)); $upload_group = $app->getRegisteredFileGroup($this->id, $group_name); // if the group exists if ($upload_group = $app->getRegisteredFileGroup($this->id, $group_name)) { try { $file = $app->handleUpload($group_name, $file_class_name); // if multiple files was uploaded and this group is unique, don't save this group of files. if (is_array($file) && $upload_group->unique) { continue; // else if multiple files was uploaded and this group accepts multiple files, set the group to this files and add them to $files array } elseif (is_array($file) && !$upload_group->unique) { foreach ($file as $f) { if ($error = $upload_group->getError($f)) { $files[] = ['error' => $error, 'group' => $upload_group]; } else { $f->group = $group_name; $files[] = $f; } } // else if a single file was uploaded, add the group to this file and add this file to $files array } else { if (key_exists('description', $this->data) && is_array($this->data['description']) && key_exists($group_name, $this->data['description'])) { $file->description = $this->data['description'][$group_name]; } if ($error = $upload_group->getError($file)) { $files[] = ['error' => $error, 'group' => $upload_group]; } else { $file->group = $group_name; $files[] = $file; } } } catch (\MapasCulturais\Exceptions\FileUploadError $e) { $files[] = ['error' => App::txt($e->message), 'group' => $upload_group]; } } } // if no files was added to $files array, return an error if (empty($files)) { $this->errorJson(App::txt('no valid files uploaded')); return; } else { $all_files_contains_error = true; foreach ($files as $f) { if (is_object($f)) { $all_files_contains_error = false; break; } } if ($all_files_contains_error) { $result = []; foreach ($files as $error) { if (key_exists('group', $error) && $error['group']->unique) { $result[$error['group']->name] = $error['error']; } else { $result[] = $error; } } $this->errorJson($result); return; } } foreach ($files as $file) { $upload_group = $app->getRegisteredFileGroup($this->id, $file->group); $file->owner = $owner; // if this group is unique, deletes the existent file if ($upload_group->unique) { $old_file = $app->repo($file_class_name)->findOneBy(['owner' => $owner, 'group' => $file->group]); if ($old_file) { $old_file->delete(); } } $file->save(); $file_group = $file->group; if ($upload_group->unique) { $result[$file_group] = $file; } else { if (!key_exists($file->group, $result)) { $result[$file->group] = []; } $result[$file_group][] = $file; } } $app->em->flush(); $this->json($result); return; }