diff --git a/includes/class-Sharable.php b/includes/class-Sharable.php index bec42d2..e6ab044 100644 --- a/includes/class-Sharable.php +++ b/includes/class-Sharable.php @@ -1,313 +1,313 @@ . // load dependent traits class_exists( Event::class, true ); trait SharableTrait { /** * Get the sharable ID * * @return int */ public function getSharableID() { return $this->nonnull( Sharable::ID ); } /** * Check if this Sharable has a parent * * See https://gitpull.it/T557 * * @return boolean */ public function hasSharableParent() { return $this->has( Sharable::PARENT ); } /** * Get the ID of the parent Sharable (if any) * * See https://gitpull.it/T557 * * @return mixed */ public function getSharableParentID() { return $this->get( Sharable::PARENT ); } /** * Get the localized sharable title * * @param $args array Title arguments like 'prop' * @return string */ public function getSharableTitle( $args = [] ) { $sharable_title = $this->get( Sharable::TITLE ); if( ! isset( $sharable_title ) ) { return $this->getDefaultSharableTitle( $args ); } return __( $sharable_title ); } /** * Retrieve something usable as a title * * @param $args array Title arguments like 'prop' * @return string */ public function getDefaultSharableTitle( $args = [] ) { + // document, video, youtube, ... $sharable_type = $this->get( Sharable::TYPE ); + // in case of a YouTube URL is not feasible to get an useful + // information from the URL, so the default title is something else if( $sharable_type === 'youtube' ) { if( isset( $args['prop'] ) && $args['prop'] ) { return sprintf( __("il %s"), __("video esterno") ); } else { return __("video esterno"); } } + // URL or relative pathname $sharable_path = $this->get( Sharable::PATH ); - // Get filename from "/asd/asd/asd/(filename)" - $i = 0; - while( strpos( $sharable_path, _, $i ) !== false ) { - $i++; - } - return substr( $sharable_path, $i ); + // just the filename and not its path + return basename( $sharable_path ); } /** * Is it an image? * * @return bool */ public function isSharableImage() { return $this->isSharableType( 'image' ); } /** * Is it a video? * * @return bool */ public function isSharableVideo() { return $this->isSharableType( 'video' ); } /** * Is it a document? * * @return bool */ public function isSharableDocument() { return $this->isSharableType( 'document' ); } /** * Is it an iframe (like a YouTube video?) * * @return bool */ public function isSharableIframe() { return $this->isSharableType( 'youtube' ); } /** * Is it of a certain type? * * @param $type string * @return bool */ private function isSharableType( $type ) { return $this->get( Sharable::TYPE ) === $type; } /** * It can be downloaded? * * @return bool */ public function isSharableDownloadable() { return ! $this->isSharableIframe(); } /** * @TODO: $base = ROOT is wrong and should be $absolute = false */ function getSharablePath( $base = ROOT ) { $type = $this->get( Sharable::TYPE ); $path = $this->get( Sharable::PATH ); if( 'youtube' === $type ) { return "https://www.youtube.com/watch?v={$path}"; } return site_page( $path, $base ); } /** * Get the MIME type * * @return string|null */ public function getSharableMIME() { return $this->get( Sharable::MIME ); } /** * Get the license * * @return License */ public function getSharableLicense() { return license( $this->get( Sharable::LICENSE ) ); } /** * Get the Sharable edit URL * * @return self */ public function getSharableEditURL() { return Sharable::editURL( [ 'id' => $this->getSharableID(), ] ); } /** * Normalize a Sharable object */ protected function normalizeSharable() { $this->integers( Sharable::ID, Sharable::PARENT, Event ::ID ); } } /** * A Sharable is an attachment related to a Talk */ class Sharable extends Queried { use SharableTrait; use EventTrait; /** * Database table name */ const T = 'sharable'; /** * Sharable ID column */ const ID = 'sharable_ID'; /** * Sharable title column */ const TITLE = 'sharable_title'; /** * Sharable type column */ const TYPE = 'sharable_type'; /** * Sharable path column */ const PATH = 'sharable_path'; /** * Sharable mime type column */ const MIME = 'sharable_mimetype'; /** * Sharable license column */ const LICENSE = 'sharable_license'; /** * Name of the parent sharable_ID column * * See https://gitpull.it/T557 */ const PARENT = 'sharable_parent'; /** * Sharable univoque ID */ const ID_ = self::T . DOT . self::ID; /** * Sharable event ID */ const EVENT_ = self::T . DOT . Event::ID; /** * Constructor */ public function __construct() { $this->normalizeSharable(); $this->normalizeEvent(); } /** * Factory by an event * * @param $event_ID int Event ID * @return Query */ public static function factoryByEvent( $event_ID ) { return ( new QuerySharable ) ->whereEventID( $event_ID ); } /** * All basic fields * * @return array */ public static function fields() { return [ self::ID_, self::TITLE, self::TYPE, self::PATH, self::MIME, self::LICENSE, self::PARENT, ]; } /** * Create the Sharable edit URL * * @param array $args * @return string */ public static function editURL( $args ) { $url = site_page( ADMIN_BASE_URL . '/sharable.php' ); return http_build_get_query( $url, $args ); } }