/** * Upload a file to the wiki via AJAX * * @return string */ public function ajaxCreateTask() { // Check if they're logged in if (User::isGuest()) { echo json_encode(array('error' => Lang::txt('Must be logged in.'))); return; } // Ensure we have an ID to work with $pid = strtolower(Request::getInt('pid', 0)); if (!$pid) { echo json_encode(array('error' => Lang::txt('COM_RESOURCES_NO_ID'))); return; } // Create database entry $asset = new Resource($this->database); $asset->title = 'A link'; $asset->introtext = $asset->title; $asset->created = Date::toSql(); $asset->created_by = User::get('id'); $asset->published = 1; $asset->publish_up = Date::toSql(); $asset->publish_down = '0000-00-00 00:00:00'; $asset->standalone = 0; $asset->access = 0; $asset->path = Request::getVar('url', 'http://'); $asset->type = 11; $asset->path = str_replace(array('|', '\\', '{', '}', '^'), array('%7C', '%5C', '%7B', '%7D', '%5E'), $asset->path); if (!Validate::url($asset->path)) { echo json_encode(array('success' => false, 'errors' => array(Lang::txt('Link provided is not a valid URL.')), 'file' => $asset->path, 'directory' => '', 'parent' => $pid, 'id' => 0)); return; } if (!$asset->check()) { echo json_encode(array('success' => false, 'errors' => $asset->getErrors(), 'file' => $asset->path, 'directory' => '', 'parent' => $pid, 'id' => 0)); return; } if (!$asset->store()) { echo json_encode(array('success' => false, 'errors' => $asset->getErrors(), 'file' => 'http://', 'directory' => '', 'parent' => $pid, 'id' => 0)); return; } // Instantiate a Resources Assoc object $assoc = new Assoc($this->database); // Get the last child in the ordering $assoc->ordering = $assoc->getLastOrder($pid); $assoc->ordering = $assoc->ordering ? $assoc->ordering : 0; // Increase the ordering - new items are always last $assoc->ordering++; // Create new parent/child association $assoc->parent_id = $pid; $assoc->child_id = $asset->id; $assoc->grouping = 0; if (!$assoc->check()) { echo json_encode(array('success' => false, 'errors' => $assoc->getErrors(), 'file' => $asset->path, 'directory' => '', 'parent' => $pid, 'id' => $asset->id)); return; } if (!$assoc->store(true)) { echo json_encode(array('success' => false, 'errors' => $assoc->getErrors(), 'file' => $asset->path, 'directory' => '', 'parent' => $pid, 'id' => $asset->id)); return; } //echo result echo json_encode(array('success' => true, 'errors' => array(), 'file' => $asset->path, 'directory' => '', 'parent' => $pid, 'id' => $asset->id)); }