Add bookmark exports and sharing + various fixes

This commit is contained in:
Joey Kimsey
2024-12-15 17:20:57 -05:00
parent 3ef97138a2
commit ab2f009e5b
26 changed files with 975 additions and 297 deletions

View File

@ -1,10 +1,10 @@
<?php
/**
* app/plugins/bugreport/controllers/bugreport.php
* app/plugins/bookmarks/controllers/bookmarks.php
*
* This is the bug reports controller.
*
* @package TP BugReports
* @package TP Bookmarks
* @version 3.0
* @author Joey Kimsey <Joey@thetempusproject.com>
* @link https://TheTempusProject.com
@ -27,6 +27,7 @@ use TheTempusProject\Houdini\Classes\Components;
use TheTempusProject\Houdini\Classes\Forms as HoudiniForms;
use TheTempusProject\Houdini\Classes\Navigation;
use TheTempusProject\Houdini\Classes\Template;
use TheTempusProject\Hermes\Functions\Route as Routes;
class Bookmarks extends Controller {
protected static $bookmarks;
@ -53,6 +54,7 @@ class Bookmarks extends Controller {
$userFolderTabsView = '';
}
Components::set( 'userFolderTabs', $userFolderTabsView );
Components::set( 'SITE_URL', Routes::getAddress() );
Views::raw( $tabsView );
}
@ -150,9 +152,13 @@ class Bookmarks extends Controller {
Issues::add( 'error', [ 'There was an error creating your bookmark.' => Check::userErrors() ] );
return Views::view( 'bookmarks.bookmarks.create' );
}
self::$bookmarks->refreshInfo( $result );
// self::$bookmarks->refreshInfo( $result );
Session::flash( 'success', 'Your Bookmark has been created.' );
Redirect::to( 'bookmarks/bookmarks/'. $folderID );
if ( ! empty( $folderID ) ) {
Redirect::to( 'bookmarks/bookmarks/'. $folderID );
} else {
Redirect::to( 'bookmarks/index' );
}
}
public function editBookmark( $id = null ) {
@ -318,6 +324,36 @@ class Bookmarks extends Controller {
/**
* Functionality
*/
public function publish( $id = null ) {
$bookmark = self::$bookmarks->findById( $id );
if ( $bookmark == false ) {
Session::flash( 'error', 'Bookmark not found.' );
return Redirect::to( 'bookmarks/index' );
}
if ( $bookmark->createdBy != App::$activeUser->ID ) {
Session::flash( 'error', 'You do not have permission to modify this bookmark.' );
return Redirect::to( 'bookmarks/index' );
}
self::$bookmarks->publish( $id );
Session::flash( 'success', 'Bookmark mad Public.' );
return Redirect::to( 'bookmarks/share' );
}
public function retract( $id = null ) {
$bookmark = self::$bookmarks->findById( $id );
if ( $bookmark == false ) {
Session::flash( 'error', 'Bookmark not found.' );
return Redirect::to( 'bookmarks/index' );
}
if ( $bookmark->createdBy != App::$activeUser->ID ) {
Session::flash( 'error', 'You do not have permission to modify this bookmark.' );
return Redirect::to( 'bookmarks/index' );
}
self::$bookmarks->retract( $id );
Session::flash( 'success', 'Bookmark made Private.' );
return Redirect::to( 'bookmarks/share' );
}
public function hideBookmark( $id = null ) {
$bookmark = self::$bookmarks->findById( $id );
if ( $bookmark == false ) {
@ -452,6 +488,108 @@ class Bookmarks extends Controller {
// dv ( $out );
}
public function export() {
$folders = self::$folders->byUser();
if ( ! Input::exists('submit') ) {
return Views::view( 'bookmarks.export', $folders );
}
if ( ! Forms::check( 'exportBookmarks' ) ) {
Issues::add( 'error', [ 'There was an error exporting your bookmarks.' => Check::userErrors() ] );
return Views::view( 'bookmarks.export', $folders );
}
$htmlDoc = '';
$htmlDoc .= '<!DOCTYPE NETSCAPE-Bookmark-file-1>' . PHP_EOL;
$htmlDoc .= '<!-- This is an automatically generated file.' . PHP_EOL;
$htmlDoc .= ' It will be read and overwritten.' . PHP_EOL;
$htmlDoc .= ' DO NOT EDIT! -->' . PHP_EOL;
$htmlDoc .= '<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">' . PHP_EOL;
$htmlDoc .= '<TITLE>Bookmarks</TITLE>' . PHP_EOL;
$htmlDoc .= '<H1>Bookmarks</H1>' . PHP_EOL;
$htmlDoc .= '<DL><p>' . PHP_EOL;
foreach ( Input::post('BF_') as $key => $id ) {
if ( $id == 'unsorted' ) {
continue;
}
$folder = self::$folders->findById( $id );
if ( $folder == false ) {
Session::flash( 'error', 'Folder not found.' );
return Redirect::to( 'bookmarks/index' );
}
$links = self::$bookmarks->byFolder( $folder->ID );
$htmlDoc .= $this->exportFolder( $folder->title, $folder->createdAt, $folder->createdAt, $links );
}
$htmlDoc .= '</DL><p>' . PHP_EOL;
$folder = UPLOAD_DIRECTORY . App::$activeUser->username;
if ( !file_exists( $folder ) ) {
mkdir( $folder, 0777, true );
}
$file = $folder . DIRECTORY_SEPARATOR . 'export.html';
$result = file_put_contents( $file, $htmlDoc );
if ($result !== false) {
$filename = basename($file);
$downloadUrl = '/uploads/' . App::$activeUser->username . '/export.html';
Session::flash( 'success', 'Your Export is available for download <a target="_blank" href="' . $downloadUrl . '">here</a>.' );
Redirect::to( 'bookmarks/export' );
} else {
Session::flash( 'error', 'There was an issue exporting your bookmarks, please try again.' );
return Redirect::to( 'bookmarks/export' );
}
}
private function exportFolder( $title, $editedAt, $createdAt, $links ) {
$htmlDoc = '<DT><H3 ADD_DATE="'.$createdAt.'" LAST_MODIFIED="'.$editedAt.'">'.$title.'</H3>' . PHP_EOL;
$htmlDoc .= '<DL><p>' . PHP_EOL;
if ( ! empty( $links ) ) {
foreach ( $links as $key => $link ) {
$htmlDoc .= $this->exportLink( $link->url, $link->icon, $link->createdAt, $link->title );
}
}
$htmlDoc .= '</DL><p>' . PHP_EOL;
return $htmlDoc;
}
private function exportLink( $url, $icon, $createdAt, $title ) {
$htmlDoc = '<DT><A HREF="'.$url.'" ADD_DATE="'.$createdAt.'" ICON="'.$icon.'">'.$title.'</A>' . PHP_EOL;
return $htmlDoc;
}
public function share( $id = '' ) {
$panelArray = [];
$folders = self::$folders->byUser();
foreach ( $folders as $key => $folder ) {
$panel = new \stdClass();
$folderObject = new \stdClass();
if ( $folder->privacy == 'private' ) {
$folderObject->privacyBadge = '<span class="mx-2 badge bg-success">Private</span>';
$links = self::$bookmarks->publicByFolder( $folder->ID );
} else {
$folderObject->privacyBadge = '<span class="mx-2 badge bg-danger">Public</span>';
$links = self::$bookmarks->byFolder( $folder->ID );
}
$folderObject->bookmarks = $links;
$folderObject->ID = $folder->ID;
$folderObject->uuid = $folder->uuid;
$folderObject->title = $folder->title;
$folderObject->color = $folder->color;
$folderObject->bookmarkListRows = Views::simpleView( 'bookmarks.components.shareListRows', $folderObject->bookmarks );
$panel->panel = Views::simpleView( 'bookmarks.components.shareListPanel', [$folderObject] );
$panelArray[] = $panel;
}
return Views::view( 'bookmarks.share', $panelArray );
}
public function parseBookmarks($htmlContent)
{
$started = false;
@ -511,9 +649,6 @@ class Bookmarks extends Controller {
return $out;
}
private function setFolderSelect( $folderID ) {
$options = self::$folders->simpleByUser();
$out = '';