/** * Save Resource Tags * * @return void */ private function _saveTagsData() { // save tags $resourcesTags = new Tags($this->record->resource->id); $resourcesTags->setTags($this->record->tags, $this->_user->get('id'), 1, 1); }
/** * Get a tag cloud for this resource * * @param integer $admin Include admin tags? * @return boolean False if errors, string on success */ public function getTagCloud($admin = 0) { if ($this->_id == 0) { return false; } include_once __DIR__ . DS . 'tags.php'; $rt = new Tags($this->_id); $this->tagCloud = $rt->render('cloud'); return $this->tagCloud; }
/** * Saves a resource * Redirects to main listing * * @return void */ public function saveTask() { // Check for request forgeries Request::checkToken(); // Initiate extended database class $row = new Resource($this->database); if (!$row->bind($_POST)) { throw new Exception($row->getError(), 400); } $isNew = 0; if ($row->id < 1) { $isNew = 1; } if ($isNew) { // New entry $row->created = $row->created ? $row->created : Date::toSql(); $row->created_by = $row->created_by ? $row->created_by : User::get('id'); $row->access = 0; } else { $old = new Resource($this->database); $old->load($row->id); $created_by_id = Request::getInt('created_by_id', 0); // Updating entry $row->modified = Date::toSql(); $row->modified_by = User::get('id'); if ($created_by_id) { $row->created_by = $row->created_by ? $row->created_by : $created_by_id; } else { $row->created_by = $row->created_by ? $row->created_by : User::get('id'); } } // publish up $row->publish_up = Date::of($row->publish_up, Config::get('offset'))->toSql(); // publish down if (!$row->publish_down || trim($row->publish_down) == '0000-00-00 00:00:00' || trim($row->publish_down) == 'Never') { $row->publish_down = '0000-00-00 00:00:00'; } else { $row->publish_down = Date::of($row->publish_down, Config::get('offset'))->toSql(); } // Get parameters $params = Request::getVar('params', array(), 'post'); if (is_array($params)) { $txt = new \Hubzero\Config\Registry(''); foreach ($params as $k => $v) { $txt->set($k, $v); } $row->params = $txt->toString(); } // Get attributes $attribs = Request::getVar('attrib', array(), 'post'); if (is_array($attribs)) { $txta = new \Hubzero\Config\Registry(''); foreach ($attribs as $k => $v) { if ($k == 'timeof') { if (strtotime(trim($v)) === false) { $v = NULL; } $v = trim($v) ? Date::of($v, Config::get('offset'))->toSql() : NULL; } $txta->set($k, $v); } $row->attribs = $txta->toString(); } // Get custom areas, add wrappers, and compile into fulltxt if (isset($_POST['nbtag'])) { $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 \Components\Resources\Models\Elements(array(), $type->customFields); $schema = $elements->getSchema(); $fields = array(); foreach ($schema->fields as $field) { $fields[$field->name] = $field; } $nbtag = $_POST['nbtag']; $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); } } $row->fulltxt .= '</nb:' . $tagname . '>' . "\n"; if (!$tagcontent && isset($fields[$tagname]) && $fields[$tagname]->required) { throw new Exception(Lang::txt('RESOURCES_REQUIRED_FIELD_CHECK', $fields[$tagname]->label), 500); } $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)); } } } // Code cleaner for xhtml transitional compliance if ($row->type != 7) { $row->introtext = str_replace('<br>', '<br />', $row->introtext); $row->fulltxt = str_replace('<br>', '<br />', $row->fulltxt); } // Check content if (!$row->check()) { throw new Exception($row->getError(), 500); } // Store content if (!$row->store()) { throw new Exception($row->getError(), 500); } // Checkin resource $row->checkin(); // Rename the temporary upload directory if it exist $tmpid = Request::getInt('tmpid', 0, 'post'); if ($tmpid != Html::niceidformat($row->id)) { // Build the full paths $path = Html::dateToPath($row->created); $dir_id = Html::niceidformat($row->id); $tmppath = Utilities::buildUploadPath($path . DS . $tmpid); $newpath = Utilities::buildUploadPath($path . DS . $dir_id); // Attempt to rename the temp directory if (\Filesystem::exists($tmppath)) { $result = \Filesystem::move($tmppath, $newpath); if ($result !== true) { $this->setError($result); } } $row->path = str_replace($tmpid, Html::niceidformat($row->id), $row->path); $row->store(); } // Incoming tags $tags = Request::getVar('tags', '', 'post'); // Save the tags $rt = new Tags($row->id); $rt->setTags($tags, User::get('id'), 1, 1); // Incoming authors if ($row->type != 7) { $authorsOldstr = Request::getVar('old_authors', '', 'post'); $authorsNewstr = Request::getVar('new_authors', '', 'post'); if (!$authorsNewstr) { $authorsNewstr = $authorsOldstr; } include_once dirname(dirname(__DIR__)) . DS . 'tables' . DS . 'contributor.php'; $authorsNew = explode(',', $authorsNewstr); $authorsOld = explode(',', $authorsOldstr); // We have either a new ordering or new authors or both if ($authorsNewstr) { for ($i = 0, $n = count($authorsNew); $i < $n; $i++) { $rc = new Contributor($this->database); $rc->subtable = 'resources'; $rc->subid = $row->id; if (is_numeric($authorsNew[$i])) { $rc->authorid = $authorsNew[$i]; } else { $rc->authorid = $rc->getUserId($authorsNew[$i]); } $rc->ordering = $i; $rc->role = trim(Request::getVar($authorsNew[$i] . '_role', '')); $rc->name = trim(Request::getVar($authorsNew[$i] . '_name', '')); $rc->organization = trim(Request::getVar($authorsNew[$i] . '_organization', '')); $authorsNew[$i] = $rc->authorid; if (in_array($authorsNew[$i], $authorsOld)) { //echo 'update: ' . $rc->authorid . ', ' . $rc->role . ', ' . $rc->name . ', ' . $rc->organization . '<br />'; // Updating record $rc->updateAssociation(); } else { //echo 'create: ' . $rc->authorid . ', ' . $rc->role . ', ' . $rc->name . ', ' . $rc->organization . '<br />'; // New record $rc->createAssociation(); } } } // Run through previous author list and check to see if any IDs had been dropped if ($authorsOldstr) { $rc = new Contributor($this->database); for ($i = 0, $n = count($authorsOld); $i < $n; $i++) { if (!in_array($authorsOld[$i], $authorsNew)) { $rc->deleteAssociation($authorsOld[$i], $row->id, 'resources'); } } } } // If this is a child, add parent/child association $pid = Request::getInt('pid', 0, 'post'); if ($isNew && $pid) { $this->_attachChild($row->id, $pid); } // Is this a standalone resource and we need to email approved submissions? if ($row->standalone == 1 && $this->config->get('email_when_approved')) { // If the state went from pending to published if ($row->published == 1 && $old->published == 3) { $this->_emailContributors($row, $this->database); } } // Redirect App::redirect($this->buildRedirectURL($pid), Lang::txt('COM_RESOURCES_ITEM_SAVED')); }
/** * Get a list of tags on this resource * Accepts either a numeric array index or a string [id, raw] * If index, it'll return the entry matching that index in the list * If string, it'll return either a list of IDs or raw tags * * @param mixed $idx Index value * @return array */ public function tags($idx = null) { if (!$this->exists()) { return array(); } if (!isset($this->tags)) { $this->tags = array(); include_once PATH_CORE . DS . 'components' . DS . 'com_resources' . DS . 'helpers' . DS . 'tags.php'; $rt = new Helpers\Tags($this->resource->id); if ($results = $rt->tags('list')) { $this->tags = $results; } } if ($idx !== null) { if (is_numeric($idx)) { if (isset($this->tags[$idx])) { return $this->tags[$idx]; } else { $this->setError(Lang::txt('Index not found: ') . __CLASS__ . '::' . __METHOD__ . '[' . $idx . ']'); return false; } } else { if (is_string($idx)) { switch (strtolower($idx)) { case 'id': $ids = array(); foreach ($this->tags as $tag) { $ids[] = (int) $tag->id; } return $ids; break; case 'raw': $raw = array(); foreach ($this->tags as $tag) { $raw[] = stripslashes($tag->raw_tag); } return $raw; break; default: return $this->tags; break; } } } } return $this->tags; }
/** * Display an RSS feed * * @return void */ public function feedTask() { Document::setType('feed'); // Start a new feed object $doc = Document::instance(); // Incoming $id = Request::getInt('id', 0); $alias = Request::getVar('alias', ''); // Ensure we have an ID or alias to work with if (!$id && !$alias) { App::redirect(Route::url('index.php?option=' . $this->_option)); return; } // Load the resource $resource = new Resource($this->database); if ($alias) { $resource->load($alias); $id = $resource->id; } else { $resource->load($id); $alias = $resource->alias; } // Make sure we got a result from the database if (!$resource) { App::abort(404, Lang::txt('COM_RESOURCES_RESOURCE_NOT_FOUND')); return; } // Make sure the resource is published and standalone if ($resource->published == 0 || $resource->standalone != 1) { App::abort(403, Lang::txt('COM_RESOURCES_ALERTNOTAUTH')); return; } // Make sure they have access to view this resource if ($this->checkGroupAccess($resource)) { App::abort(403, Lang::txt('COM_RESOURCES_ALERTNOTAUTH')); return; } // Incoming $filters = array(); if ($resource->type == 2) { $filters['sortby'] = Request::getVar('sortby', 'ordering'); } else { $filters['sortby'] = Request::getVar('sortby', 'ranking'); } $filters['limit'] = Request::getInt('limit', 100); $filters['start'] = Request::getInt('limitstart', 0); $filters['year'] = Request::getInt('year', 0); $filters['id'] = $resource->id; $feedtype = Request::getVar('content', 'audio'); // Initiate a resource helper class $helper = new Helper($resource->id, $this->database); $rows = $helper->getStandaloneChildren($filters); $base = rtrim(Request::base(), '/'); $title = $resource->title; $feedtypes_abr = array(" ", "slides", "audio", "video", "sd_video", "hd_video"); $feedtypes_full = array(" & ", "Slides", "Audio", "Video", "SD full", "HD"); $type = str_replace($feedtypes_abr, $feedtypes_full, $feedtype); $title = '[' . $type . '] ' . $title; // Build some basic RSS document information $dtitle = \Hubzero\Utility\Sanitize::clean(stripslashes($title)); $doc->title = trim(\Hubzero\Utility\String::truncate(html_entity_decode($dtitle), 250)); $doc->description = htmlspecialchars(html_entity_decode(\Hubzero\Utility\Sanitize::clean(stripslashes($resource->introtext))), ENT_COMPAT, 'UTF-8'); $doc->copyright = \Lang::txt('COM_RESOURCES_RSS_COPYRIGHT', date("Y"), Config::get('sitename')); $doc->category = Lang::txt('COM_RESOURCES_RSS_CATEGORY'); $doc->link = Route::url('index.php?option=' . $this->_option . '&id=' . $resource->id); $rt = new Tags($resource->id); $rtags = $rt->tags(); $tagarray = array(); $categories = array(); $subcategories = array(); if ($rtags) { foreach ($rtags as $tag) { if (substr($tag->get('tag'), 0, 6) == 'itunes') { $tbits = explode(':', $tag->get('raw_tag')); if (count($tbits) > 2) { $subcategories[] = end($tbits); } else { $categories[] = str_replace('itunes:', '', $tag->get('raw_tag')); } } elseif ($tag->get('admin') == 0) { $tagarray[] = $tag->get('raw_tag'); } } } $tags = implode(', ', $tagarray); $tags = trim(\Hubzero\Utility\String::truncate($tags, 250)); $tags = rtrim($tags, ','); $helper->getUnlinkedContributors(); $cons = $helper->ul_contributors; $cons = explode(';', $cons); $author = ''; foreach ($cons as $con) { if ($con) { $author = trim($con); break; } } $doc->itunes_summary = html_entity_decode(\Hubzero\Utility\Sanitize::clean(stripslashes($resource->introtext))); if (count($categories) > 0) { $doc->itunes_category = $categories[0]; if (count($subcategories) > 0) { $doc->itunes_subcategories = $subcategories; } } $doc->itunes_explicit = 'no'; $doc->itunes_keywords = $tags; $doc->itunes_author = $author; $itunes_image_name = 'itunes_' . str_replace(' ', '_', strtolower($feedtype)); $dimg = $this->_checkForImage($itunes_image_name, $this->config->get('uploadpath'), $resource->created, $resource->id); if ($dimg) { $dimage = new \Hubzero\Document\Type\Feed\Image(); $dimage->url = $dimg; $dimage->title = trim(\Hubzero\Utility\String::truncate(html_entity_decode($dtitle . ' ' . Lang::txt('COM_RESOURCES_RSS_ARTWORK')), 250)); $dimage->link = $base . $doc->link; $doc->itunes_image = $dimage; } $owner = new \Hubzero\Document\Type\Feed\ItunesOwner(); $owner->email = Config::get('mailfrom'); $owner->name = Config::get('sitename'); $doc->itunes_owner = $owner; // Start outputing results if any found if (count($rows) > 0) { foreach ($rows as $row) { // Prepare the title $title = strip_tags($row->title); $title = html_entity_decode($title); // URL link to resource $link = DS . ltrim(Route::url('index.php?option=' . $this->_option . '&id=' . $row->id), DS); // Strip html from feed item description text $description = html_entity_decode(\Hubzero\Utility\Sanitize::stripAll(stripslashes($row->introtext))); $author = ''; @($date = $row->publish_up ? date('r', strtotime($row->publish_up)) : ''); // Instantiate a resource helper $rhelper = new Helper($row->id, $this->database); // Get any podcast/vodcast files $podcast = ''; $type_model = new Type($this->database); $all_logical_types = $type_model->getTypes(28); // 28 means 'logical' types. $queried_logical_types = @explode(' ', $feedtype); if (is_null($queried_logical_types) || !is_array($queried_logical_types)) { App::abort(404, Lang::txt('COM_RESOURCES_RESOURCE_FEED_BAD_REQUEST')); return; } $relevant_logical_types_by_id = array(); foreach ($queried_logical_types as $queried) { $as_mnemonic = preg_replace('/[_-]/', ' ', $queried); foreach ($all_logical_types as $logical_type) { if (preg_match_all('/Podcast \\(([^()]+)\\)/', $logical_type->type, $matches) == 1 && strcasecmp($matches[1][0], $as_mnemonic) == 0) { $relevant_logical_types_by_id[$logical_type->id] = $logical_type; break; } elseif ($as_mnemonic == 'slides' && $logical_type->type == 'Presentation Slides') { $relevant_logical_types_by_id[$logical_type->id] = $logical_type; break; } elseif ($as_mnemonic == 'notes' && $logical_type->type == 'Lecture Notes') { $relevant_logical_types_by_id[$logical_type->id] = $logical_type; break; } } } $rhelper->getChildren(); $podcasts = array(); $children = array(); if ($rhelper->children && count($rhelper->children) > 0) { $grandchildren = $rhelper->children; foreach ($grandchildren as $grandchild) { if (isset($relevant_logical_types_by_id[(int) $grandchild->logicaltype])) { if (stripslashes($grandchild->introtext) != '') { $gdescription = html_entity_decode(\Hubzero\Utility\Sanitize::clean(stripslashes($grandchild->introtext))); } array_push($podcasts, $grandchild->path); array_push($children, $grandchild); } } } // Get the contributors of this resource $rhelper->getContributors(); $author = strip_tags($rhelper->contributors); $rtt = new Tags($row->id); $rtags = $rtt->render('string'); if (trim($rtags)) { $rtags = trim(\Hubzero\Utility\String::truncate($rtags, 250)); $rtags = rtrim($rtags, ','); } // Get attributes //$attribs = new \Hubzero\Config\Registry($row->attribs); if ($children) { $attribs = new \Hubzero\Config\Registry($children[0]->attribs); } foreach ($podcasts as $podcast) { // Load individual item creator class $item = new \Hubzero\Document\Type\Feed\Item(); $item->title = $title; $item->link = $link; $item->description = $description; $item->date = $date; $item->category = $row->typetitle ? $row->typetitle : ''; $item->author = $author; $img = $this->_checkForImage('ituness_artwork', $this->config->get('uploadpath'), $row->created, $row->id); if ($img) { $image = new \Hubzero\Document\Type\Feed\Image(); $image->url = $img; $image->title = $title . ' ' . Lang::txt('COM_RESOURCES_RSS_ARTWORK'); $image->link = $base . $link; $item->itunes_image = $image; } $item->itunes_summary = $description; $item->itunes_explicit = 'no'; $item->itunes_keywords = $rtags; $item->itunes_author = $author; if ($attribs->get('duration')) { $item->itunes_duration = $attribs->get('duration'); } if ($podcast) { $podcastp = $podcast; $podcast = DS . ltrim($this->_fullPath($podcast), DS); if (substr($podcastp, 0, strlen($this->config->get('uploadpath'))) == $this->config->get('uploadpath')) { // Do nothing } else { $podcastp = trim($this->config->get('uploadpath'), DS) . DS . ltrim($podcastp, DS); } $podcastp = PATH_APP . DS . ltrim($podcastp, DS); if (file_exists($podcastp)) { $fs = filesize($podcastp); $enclosure = new \Hubzero\Document\Type\Feed\Enclosure(); $enclosure->url = $podcast; switch (Html::getFileExtension($podcast)) { case 'm4v': $enclosure->type = 'video/x-m4v'; break; case 'mp4': $enclosure->type = 'video/mp4'; break; case 'wmv': $enclosure->type = 'video/wmv'; break; case 'mov': $enclosure->type = 'video/quicktime'; break; case 'qt': $enclosure->type = 'video/quicktime'; break; case 'mpg': $enclosure->type = 'video/mpeg'; break; case 'mpeg': $enclosure->type = 'video/mpeg'; break; case 'mpe': $enclosure->type = 'video/mpeg'; break; case 'mp2': $enclosure->type = 'video/mpeg'; break; case 'mpv2': $enclosure->type = 'video/mpeg'; break; case 'mp3': $enclosure->type = 'audio/mpeg'; break; case 'm4a': $enclosure->type = 'audio/x-m4a'; break; case 'aiff': $enclosure->type = 'audio/x-aiff'; break; case 'aif': $enclosure->type = 'audio/x-aiff'; break; case 'wav': $enclosure->type = 'audio/x-wav'; break; case 'ra': $enclosure->type = 'audio/x-pn-realaudio'; break; case 'ram': $enclosure->type = 'audio/x-pn-realaudio'; break; case 'ppt': $enclosure->type = 'application/vnd.ms-powerpoint'; break; case 'pps': $enclosure->type = 'application/vnd.ms-powerpoint'; break; case 'pdf': $enclosure->type = 'application/pdf'; break; case 'doc': $enclosure->type = 'application/msword'; break; case 'txt': $enclosure->type = 'text/plain'; break; case 'html': $enclosure->type = 'text/html'; break; case 'htm': $enclosure->type = 'text/html'; break; } $enclosure->length = $fs; $item->guid = $podcast; $item->enclosure = $enclosure; } // Loads item info into rss array $doc->addItem($item); } } } } }
/** * Saves changes to the tag list on a resource * Redirects back to main resource listing * * @return void */ public function saveTask() { // Check for request forgeries Request::checkToken(); // Incoming $id = Request::getInt('id', 0); $entered = Request::getVar('tags', ''); $selected = Request::getVar('tgs', array(0)); // Process tags $tagging = new TagCloud($id); $tagArray = $tagging->parseTags($entered); $tagArray2 = $tagging->parseTags($entered, 1); $diffTags = array_diff($tagArray, $selected); foreach ($diffTags as $diffed) { array_push($selected, $tagArray2[$diffed]); } $tags = implode(',', $selected); $tagging->setTags($tags, User::get('id'), 1); // Redirect App::redirect(Route::url('index.php?option=' . $this->_option . '&controller=items', false), Lang::txt('COM_RESOURCES_TAGS_UPDATED', $id)); }
/** * Check if the tags step is completed * * @param integer $id Resource ID * @return integer 1 = step completed, 0 = not completed */ public function step_tags_check($id) { if ($id) { $rt = new Tags($id); $tags = $rt->tags()->count(); if ($tags > 0) { return 1; } } return 0; }
/** * Build and return the url * * @return string */ public function tags($as = 'list') { require_once dirname(dirname(__DIR__)) . DS . 'helpers' . DS . 'tags.php'; $cloud = new Tags($this->get('id')); if ($as == 'list') { $tags = array(); foreach ($cloud->tags() as $tag) { array_push($tags, $tag->tag); } return $tags; } else { return $cloud->tags(); } }
/** * Saves changes to the tag list on a resource * Redirects back to main resource listing * * @return void */ public function saveTask() { // Check for request forgeries Request::checkToken(); // Incoming $id = Request::getInt('id', 0); $entered = Request::getVar('tags', ''); $selected = Request::getVar('tgs', array(0)); // Process tags $tagging = new TagCloud($id); $tagArray = $tagging->parseTags($entered); $tagArray2 = $tagging->parseTags($entered, 1); $diffTags = array_diff($tagArray, $selected); foreach ($diffTags as $diffed) { array_push($selected, $tagArray2[$diffed]); } $tags = implode(',', $selected); $tagging->setTags($tags, User::get('id'), 1); Notify::success(Lang::txt('COM_RESOURCES_TAGS_UPDATED', $id)); // Redirect $this->cancelTask(); }