namespace Drupal\technical_presentations\Controller; use Drupal\Core\Controller\ControllerBase; //use App\Models\Exchange; use Symfony\Component\Routing\RouteCollection; use Symfony\Component\HttpFoundation\Request; class TechnicalPresentationController extends ControllerBase { /** * Returns a simple page. * * @return array * A simple renderable array. */ public function presentation(string $title){ //Strip the title from '-' to get the title - optional add the id# to it for simplified query //get last array item for the id //$exTitle = str_replace("-", " ", $title); //extra-control-of-machines-IEEE-AVR-19 $presentationArray = explode("-", $title) ; //print_r($exchangeArray); $reversed = array_reverse($presentationArray); $id = $reversed[0]; //To get another database (here : 'external') $con = \Drupal\Core\Database\Database::getConnection('default','external'); $query = $con->select('technical_presentations', 'tp'); $query->condition('tp.id', $id); $query->fields('tp', array('id', 'title','imagePath','description','authors','abstract','filePath','presentationPath','category','date','private','tags','noteHalf','nb_note','nb_download','nb_views')); /*echo $query; print_r((string) $query); print_r($query->arguments());*/ $presentations = $query->execute()->fetchAll(); // Do something with your variables here. foreach ($presentations as $record) { // Do something with each $record. $id = $record->id; $title = $record->title; $imgPath = $record->imagePath; $description = $record->description; $authors = $record->authors; $abstract = $record->abstract; $filePath = $record->filePath; $presentationPath = $record->presentationPath; $category = $record->category; $date = $record->date; $private = $record->private; $tags = $record->tags; $nb_note = $record->nb_note; $noteHalf = $record->noteHalf; $nb_download = $record->nb_download; $nb_views = $record->nb_views; /*TODO: write object class to pass instead*/ } //translate the tags into an array from string $tagArr = explode(',',$tags); //print_r($tagArr); $similarPresentations = $this->getSimilarTechPresentations($tagArr,$id); $logged_in = \Drupal::currentUser()->isAuthenticated(); $username = \Drupal::currentUser()->getDisplayName(); $useremail = \Drupal::currentUser()->getEmail(); return [ // Your theme hook name. '#theme' => 'technical_presentations_theme_hook', // Your variables. '#tp_id' => $id, '#tp_title' => $title, '#tp_imgPath' => $imgPath, '#tp_description' => $description, '#tp_authors' => $authors, '#tp_abstract' => $abstract, '#tp_filePath' => $filePath, '#tp_presentationPath' => $presentationPath, '#tp_category' => $category, '#tp_date' => $date, '#tp_private' => $private, '#tp_tag' => $tagArr, '#tp_nb_note' => $nb_note, // chansen Mar 6 2023 $note was not defined error removed '#tp_noteHalf' => $noteHalf, '#tp_nb_download' => $nb_download, '#tp_nb_views' => $nb_views, '#tp_similar' => $similarPresentations, '#logged_in' =>$logged_in, '#tp_username' => $username, '#tp_useremail' => $useremail, '#title' => $title, '#link' => '', //is this required? '#attached' => [ 'library' => [ 'technical_presentations/technical_presentations-style', ], ], ]; } public function getSimilarTechPresentations(array $tags, string $id) { //echo "getSimilarTechPresenations"; $items = array(); $finalSimilar = array(); $sim=0; foreach($tags as $tag){ $con = \Drupal\Core\Database\Database::getConnection('default','external'); $query = $con->select('technical_presentations', 'tp'); $query->fields('tp', array('id', 'title','imagePath','date')); $orGroup1 = $query->orConditionGroup() ->condition('tp.title', '%%%'.$query->escapeLike(trim($tag)) . '%%%', 'LIKE') ->condition('tp.description', '%%%'.$query->escapeLike(trim($tag)) . '%%%', 'LIKE') ->condition('tp.authors', '%%%'.$query->escapeLike(trim($tag)) . '%%%', 'LIKE'); $query->orderby('tp.date', 'DESC'); $extras = $query->execute()->fetchAll(); foreach($extras as $extra){ if(!array_search($extra->id,$items) ){ if($id !== $extra->id && $sim < 10){ array_push($items, $extra->id); $agoTime = $this->timeago($extra->date);//$dateTimeFormatter->formatDiff($someDate, $now); $extra->date = $agoTime; $finalSimilar[$sim]=$extra; $sim++; } } } } return $finalSimilar; } /** * function to convert the date of the content to time passed like most social media. */ public function timeago(string $date){ $currentTime = time(); $timestamp = strtotime($date); $strTime = array("second", "minute", "hour", "day", "month", "year"); $length = array("60","60","24","30","12","10"); if($currentTime >= $timestamp) { $diff = time()- $timestamp; for($i = 0; $diff >= $length[$i] && $i < count($length)-1; $i++) { $diff = $diff / $length[$i]; } $diff = round($diff); return $diff . " " . $strTime[$i] . "(s) ago "; } } /** * this function is not used in this yet. It may be for when the login works and hides some of the text therefore it needs to truncate the text. */ public function truncateOpaque($string, $max_length = 30, $id, $replacement = '', $trunc_at_space = false) { $string = nl2br(htmlspecialchars($string), false); $max_length -= strlen($replacement); $string_length = strlen($string); if($string_length <= $max_length) return $string; else $point = '
'; if( $trunc_at_space && ($space_position = strrpos($string, ' ', $max_length-$string_length)) ) $max_length = $space_position; return substr_replace($string, $replacement . $point, $max_length); } } The website encountered an unexpected error. Please try again later.