/** * DM2 creation callback, binds to the current content topic. */ private function _create_article($title) { $this->_article = new midcom_db_article(); $author = $this->_find_email_person($this->_request_data['from']); if (!$author) { debug_add("Author '{$this->_request_data['from']}' not found", MIDCOM_LOG_WARN); if ($this->_config->get('api_email_abort_authornotfound') !== false) { throw new midcom_error("Author '{$this->_request_data['from']}' not found"); } $this->_article->author = midcom_connection::get_user(); } else { // TODO: This code needs a bit of rethinking $author_user = midcom::get('auth')->get_user($author->guid); if (!$this->_content_topic->can_do('midgard:create', $author_user)) { throw new midcom_error('Author doesn\'t have posting privileges'); } $this->_article->author = $author->id; } //Default to first user in DB if author is not set if (!$this->_article->author) { $qb = midcom_db_person::new_query_builder(); $qb->add_constraint('username', '<>', ''); $qb->set_limit(1); $results = $qb->execute(); unset($qb); if (empty($results)) { //No users found throw new midcom_error('Cannot set any author for the article'); } $this->_article->author = $results[0]->id; } $resolver = new midcom_helper_reflector_nameresolver($this->_article); $this->_article->topic = $this->_content_topic->id; $this->_article->title = $title; $this->_article->allow_name_catenate = true; $this->_article->name = $resolver->generate_unique_name('title'); if (empty($this->_article->name)) { debug_add('Could not generate unique name for the new article from title, using timestamp', MIDCOM_LOG_INFO); $this->_article->name = time(); $resolver = new midcom_helper_reflector_nameresolver($this->_article); if (!$resolver->name_is_unique()) { throw new midcom_error('Failed to create unique name for the new article, aborting.'); } } if (!$this->_article->create()) { debug_print_r('Failed to create article:', $this->_article); throw new midcom_error('Failed to create a new article. Last Midgard error was: ' . midcom_connection::get_error_string()); } $this->_article->parameter('midcom.helper.datamanager2', 'schema_name', $this->_config->get('api_email_schema')); return true; }
/** * Make sure we have unique filename * * @todo Isn't there a reflector method that already does this (for filenames)? */ private function _generate_unique_name($filename) { $attachment = new midcom_db_attachment(); $attachment->name = $filename; $attachment->parentguid = $this->storage->object->guid; $resolver = new midcom_helper_reflector_nameresolver($attachment); if (!$resolver->name_is_unique()) { debug_add("Name '{$attachment->name}' is not unique, trying to generate", MIDCOM_LOG_INFO); $ext = ''; if (preg_match('/^(.*)(\\..*?)$/', $filename, $ext_matches)) { $ext = $ext_matches[2]; } $filename = $resolver->generate_unique_name('name', $ext); } return $filename; }
/** * Make sure our name is nice and clean * * @see http://trac.midgard-project.org/ticket/809 */ public function _on_validate() { $schema = $this->storage->_schema->fields[$this->name]; $copy = $this->_copy_object($this->storage->object); $property = $schema['storage']['location']; $resolver = new midcom_helper_reflector_nameresolver($copy); if (empty($this->value)) { if (isset($this->_datamanager->types[$this->title_field]) && $this->_datamanager->types[$this->title_field]->value) { $copy->{$property} = midcom_helper_misc::generate_urlname_from_string($this->_datamanager->types[$this->title_field]->value); $this->value = $resolver->generate_unique_name(); } } $copy->{$property} = $this->value; if (!$resolver->name_is_safe($property)) { $this->validation_error = sprintf($this->_l10n->get('type urlname: name is not "URL-safe", try "%s"'), midcom_helper_misc::generate_urlname_from_string($this->value)); return false; } if (!$this->allow_unclean && !$resolver->name_is_clean($property)) { $this->validation_error = sprintf($this->_l10n->get('type urlname: name is not "clean", try "%s"'), midcom_helper_misc::generate_urlname_from_string($this->value)); return false; } if (!$resolver->name_is_unique()) { $new_name = $resolver->generate_unique_name(); if ($this->allow_catenate) { // If allowed to, silently use the generated name $this->value = $new_name; $this->_orig_value = $new_name; $copy->{$property} = $this->value; } else { $this->validation_error = sprintf($this->_l10n->get('type urlname: name is already taken, try "%s"'), $new_name); return false; } } return true; }
* @author The Midgard Project, http://www.midgard-project.org * @copyright The Midgard Project, http://www.midgard-project.org * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License */ // FIXME: make generic $article = new midcom_db_article(); $article->topic = 5; $article->title = 'Duplicate name test with (with allow_catenate)'; $article->name = 'gathering-09'; $article->allow_name_catenate = true; $reflected_name_property = midcom_helper_reflector::get_name_property($article); $resolver = new midcom_helper_reflector_nameresolver($article); $reflected_name = $resolver->get_object_name(); echo "Reflector thinks name is '{$reflected_name}' (from property '{$reflected_name_property}')<br>\n"; if ($resolver->name_is_safe()) { echo "OK: '{$reflected_name}' is considered URL-safe<br>\n"; } else { echo "ERROR: '{$reflected_name}' is NOT considered URL-safe<br>\n"; } if ($resolver->name_is_clean()) { echo "OK: '{$reflected_name}' is considered 'clean'<br>\n"; } else { echo "WARN: '{$reflected_name}' is NOT considered 'clean'<br>\n"; } if ($resolver->name_is_unique()) { echo "OK: '{$reflected_name}' is unique (among siblings)<br>\n"; } else { echo "ERROR: '{$reflected_name}' is NOT unique (among siblings)<br>\n"; } $new_name = $resolver->generate_unique_name(); echo "midcom_helper_reflector_nameresolver::generate_unique_name(\$article) returned '{$new_name}'<br>\n";