/** * 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; }
function import_file($file, $parent_id) { $qb = midcom_db_article::new_query_builder(); $qb->add_constraint('topic', '=', $parent_id); $qb->add_constraint('name', '=', $file->name); $existing = $qb->execute(); if (count($existing) > 0 && $existing[0]->topic == $parent_id) { $article = $existing[0]; echo "Using existing article {$article->name} (#{$article->id}) from #{$article->topic}\n"; } else { $article = new midcom_db_article(); $article->topic = $parent_id; $article->name = $file->name; if (!$article->create()) { echo "Failed to create article {$article->name}: " . midcom_connection::get_error_string() . "\n"; return false; } echo "Created article {$article->name} (#{$article->id}) under #{$article->topic}\n"; } $article->title = $file->title; $article->abstract = $file->abstract; $article->content = $file->content; $article->parameter('midcom.helper.datamanager2', 'schema_name', $file->schema); flush(); return $article->update(); }