/** * Process the compose step * * @return void */ public function step_compose_process() { // Initiate extended database class $fields = Request::getVar('fields', array(), 'post'); $row = Resource::oneOrNew($fields['id'])->set($fields); $isNew = $row->get('id') < 1 || substr($row->get('id'), 0, 4) == '9999'; //$row->created = ($row->created) ? $row->created : Date::toSql(); //$row->created_by = ($row->created_by) ? $row->created_by : User::get('id'); // Set status to "composing" if ($isNew) { $row->set('published', 2); } $row->set('published', (int) $row->get('published', 2)); $row->set('publish_up', $row->get('publish_up') && $row->get('publish_up') != '0000-00-00 00:00:00' ? $row->get('publish_up') : Date::toSql()); $row->set('publish_down', $row->get('publish_down') && $row->get('publish_down') != '0000-00-00 00:00:00' ? $row->get('publish_down') : '0000-00-00 00:00:00'); $row->set('modified', Date::toSql()); $row->set('modified_by', User::get('id')); $row->set('access', (int) $row->get('access', 0)); $row->set('fulltxt', trim(preg_replace('/\\\\/', "%5C", $row->get('fulltxt')))); $row->set('introtext', String::truncate(strip_tags($row->get('fulltxt')), 500)); // Get custom areas, add wrapper tags, and compile into fulltxt $type = Type::oneOrFail($row->get('type')); include_once PATH_CORE . DS . 'components' . DS . 'com_resources' . DS . 'models' . DS . 'elements.php'; $elements = new Elements(array(), $type->customFields); $schema = $elements->getSchema(); $fields = array(); if (is_object($schema)) { foreach ($schema->fields as $field) { $fields[$field->name] = $field; } } $fulltxt = $row->get('fulltxt'); $nbtag = Request::getVar('nbtag', array(), 'post'); $found = array(); foreach ($nbtag as $tagname => $tagcontent) { $f = ''; $fulltxt .= "\n" . '<nb:' . $tagname . '>'; if (is_array($tagcontent)) { $c = count($tagcontent); $num = 0; foreach ($tagcontent as $key => $val) { if (trim($val)) { $num++; } $fulltxt .= '<' . $key . '>' . trim($val) . '</' . $key . '>'; } if ($c == $num) { $f = 'found'; } } else { $f = trim($tagcontent); if ($f) { $fulltxt .= trim($tagcontent); } } $fulltxt .= '</nb:' . $tagname . '>' . "\n"; if (!$f && isset($fields[$tagname]) && $fields[$tagname]->required) { $this->setError(Lang::txt('COM_CONTRIBUTE_REQUIRED_FIELD_CHECK', $fields[$tagname]->label)); } $found[] = $tagname; } $row->set('fulltxt', $fulltxt); foreach ($fields as $field) { if (!in_array($field->name, $found) && $field->required) { $found[] = $field->name; $this->setError(Lang::txt('COM_CONTRIBUTE_REQUIRED_FIELD_CHECK', $field->label)); } } $row->set('title', preg_replace('/\\s+/', ' ', $row->get('title'))); $row->set('title', $this->_txtClean($row->get('title'))); // Strip any scripting there may be if (trim($row->get('fulltxt'))) { $row->set('fulltxt', \Components\Resources\Helpers\Html::stripStyles($row->get('fulltxt'))); $row->set('fulltxt', $this->_txtClean($row->get('fulltxt'))); $row->set('footertext', $this->_txtClean($row->get('footertext'))); } // Fall back to step if any errors found if ($this->getError()) { $this->step--; $this->view->step = $this->step; $this->view->setLayout('compose'); return $this->step_compose($row); } // reset id if ($isNew) { $row->set('id', 0); } // Store new content if (!$row->save()) { $this->setError(Lang::txt('Error: Failed to store changes.')); $this->step--; $this->view->step = $this->step; $this->view->setLayout('compose'); return $this->step_compose($row); } // build path to temp upload folder and future permanent folder $session = App::get('session'); $created = Date::format('Y-m-d 00:00:00'); $oldPath = $row->basepath() . Html::build_path($created, $session->get('resources_temp_id'), ''); $newPath = $row->filespace(); // if we have a temp dir, move it to permanent location if (is_dir($oldPath)) { \Filesystem::move($oldPath, $newPath); $old = DS . $session->get('resources_temp_id') . DS; $new = DS . $row->id . DS; // update all images in abstract $row->set('introtext', str_replace($old, $new, $row->get('introtext'))); $row->set('fulltxt', str_replace($old, $new, $row->get('fulltxt'))); $row->save(); // clear temp id $session->clear('resources_temp_id'); } // Is it a new resource? if ($isNew) { // Automatically attach this user as the first author Request::setVar('pid', $row->get('id')); Request::setVar('id', $row->get('id')); Request::setVar('authid', User::get('id')); include_once __DIR__ . DS . 'authors.php'; $authors = new Authors(); $authors->saveTask(0); } // Log activity $recipients = array(['resource', $row->get('id')], ['user', $row->get('created_by')]); foreach ($row->authors()->where('authorid', '>', 0)->rows() as $author) { $recipients[] = ['user', $author->get('authorid')]; } Event::trigger('system.logActivity', ['activity' => ['action' => $isNew ? 'updated' : 'created', 'scope' => 'resource', 'scope_id' => $row->get('id'), 'description' => Lang::txt('COM_RESOURCES_ACTIVITY_ENTRY_' . (!$isNew ? 'UPDATED' : 'CREATED'), '<a href="' . Route::url('index.php?option=com_resources&id=' . $row->get('id')) . '">' . $row->get('title') . '</a>'), 'details' => array('title' => $row->get('title'), 'url' => Route::url('index.php?option=com_resources&id=' . $row->get('id')))], 'recipients' => $recipients]); }
/** * Process the compose step * * @return void */ public function step_compose_process() { // Initiate extended database class $row = new Resource($this->database); $row->load(Request::getInt('id', 0)); if (!$row->bind($_POST)) { throw new Exception($row->getError(), 500); } $isNew = $row->id < 1 || substr($row->id, 0, 4) == '9999'; $row->created = $row->created ? $row->created : Date::toSql(); $row->created_by = $row->created_by ? $row->created_by : User::get('id'); // Set status to "composing" if ($isNew) { $row->published = 2; } else { $row->published = $row->published ?: 2; } $row->publish_up = $row->publish_up && $row->publish_up != '0000-00-00 00:00:00' ? $row->publish_up : Date::toSql(); $row->publish_down = $row->publish_down && $row->publish_down != '0000-00-00 00:00:00' ? $row->publish_down : '0000-00-00 00:00:00'; $row->modified = Date::toSql(); $row->modified_by = User::get('id'); $row->access = $row->access ?: 0; $row->fulltxt = trim(preg_replace('/\\\\/', "%5C", $row->fulltxt)); $row->introtext = String::truncate(strip_tags($row->fulltxt), 500); //$row->fulltxt = $this->_txtAutoP($row->fulltxt, 1); // Get custom areas, add wrapper tags, and compile into fulltxt $type = new Type($this->database); $type->load($row->type); include_once PATH_CORE . DS . 'components' . DS . 'com_resources' . DS . 'models' . DS . 'elements.php'; $elements = new Elements(array(), $type->customFields); $schema = $elements->getSchema(); $fields = array(); if (is_object($schema)) { foreach ($schema->fields as $field) { $fields[$field->name] = $field; } } $nbtag = isset($_POST['nbtag']) ? $_POST['nbtag'] : array(); $found = array(); foreach ($nbtag as $tagname => $tagcontent) { $f = ''; $row->fulltxt .= "\n" . '<nb:' . $tagname . '>'; if (is_array($tagcontent)) { $c = count($tagcontent); $num = 0; foreach ($tagcontent as $key => $val) { if (trim($val)) { $num++; } $row->fulltxt .= '<' . $key . '>' . trim($val) . '</' . $key . '>'; } if ($c == $num) { $f = 'found'; } } else { $f = trim($tagcontent); if ($f) { $row->fulltxt .= trim($tagcontent); //(isset($fields[$tagname]) && $fields[$tagname]->type == 'textarea') ? $this->_txtAutoP(trim($tagcontent), 1) : trim($tagcontent); } } $row->fulltxt .= '</nb:' . $tagname . '>' . "\n"; if (!$f && isset($fields[$tagname]) && $fields[$tagname]->required) { $this->setError(Lang::txt('COM_CONTRIBUTE_REQUIRED_FIELD_CHECK', $fields[$tagname]->label)); } $found[] = $tagname; } foreach ($fields as $field) { if (!in_array($field->name, $found) && $field->required) { $found[] = $field->name; $this->setError(Lang::txt('COM_CONTRIBUTE_REQUIRED_FIELD_CHECK', $field->label)); } } $row->title = preg_replace('/\\s+/', ' ', $row->title); $row->title = $this->_txtClean($row->title); // Strip any scripting there may be if (trim($row->fulltxt)) { $row->fulltxt = \Components\Resources\Helpers\Html::stripStyles($row->fulltxt); $row->fulltxt = $this->_txtClean($row->fulltxt); //$row->fulltxt = $this->_txtAutoP($row->fulltxt, 1); $row->footertext = $this->_txtClean($row->footertext); } // Check content if (!$row->check()) { $this->setError($row->getError()); } // Fall back to step if any errors found if ($this->getError()) { $this->step--; $this->view->step = $this->step; $this->view->setLayout('compose'); $this->step_compose($row); return; } // reset id if ($isNew) { $row->id = null; } // Store new content if (!$row->store()) { $this->setError(Lang::txt('Error: Failed to store changes.')); $this->step--; $this->view->step = $this->step; $this->view->setLayout('compose'); $this->step_compose($row); return; } // build path to temp upload folder and future permanent folder $session = App::get('session'); $created = Date::format('Y-m-d 00:00:00'); $oldPath = PATH_APP . DS . trim($this->config->get('uploadpath', '/site/resources'), DS) . Html::build_path($created, $session->get('resources_temp_id'), ''); $newPath = PATH_APP . DS . trim($this->config->get('uploadpath', '/site/resources'), DS) . Html::build_path($row->created, $row->id, ''); // if we have a temp dir, move it to permanent location if (is_dir($oldPath)) { \Filesystem::move($oldPath, $newPath); $old = DS . $session->get('resources_temp_id') . DS; $new = DS . $row->id . DS; // update all images in abstract $row->introtext = str_replace($old, $new, $row->introtext); $row->fulltxt = str_replace($old, $new, $row->fulltxt); $row->store(); // clear temp id $session->clear('resources_temp_id'); } // Checkin the resource $row->checkin(); // Is it a new resource? if ($isNew) { // Get the resource ID if (!$row->id) { $row->id = $row->insertid(); } // Automatically attach this user as the first author Request::setVar('pid', $row->id); Request::setVar('id', $row->id); Request::setVar('authid', User::get('id')); include_once __DIR__ . DS . 'authors.php'; $authors = new Authors(); $authors->saveTask(0); } }