Example #1
0
	/**
	 * Returns the file size raw or formatted to largest increment possible
	 *
	 * @param bool $raw
	 * @param bool $thumbnail
	 * @return string|int
	 */
	public function getFileSize( $raw = false, $thumbnail = false )
	{
		if ( Application::Cms()->getClientId() ) {
			$thumbnail						=	false;
		}

		static $cache						=	array();

		$id									=	$this->getFilePath( $thumbnail );

		if ( ! isset( $cache[$id] ) ) {
			$fileSize						=	0;

			if ( $this->checkExists( $thumbnail ) ) {
				$domain						=	$this->getLinkDomain();

				if ( $domain ) {
					if ( ! in_array( $domain, array( 'youtube', 'youtu' ) ) ) {
						try {
							$request		=	new GuzzleHttp\Client();

							$header			=	$request->head( $id );

							if ( ( $header !== false ) && ( $header->getStatusCode() == 200 ) ) {
								$fileSize	=	(int) $header->getHeader( 'Content-Length' );
							}
						} catch( Exception $e ) {}
					}
				} else {
					$fileSize				=	@filesize( $id );
				}
			}

			$cache[$id]						=	$fileSize;
		}

		if ( ! $raw ) {
			return cbgalleryClass::getFormattedFileSize( $cache[$id] );
		}

		return $cache[$id];
	}
	/**
	 * Displays item create/edit page
	 *
	 * @param int         $id
	 * @param string      $type
	 * @param TabTable    $tab
	 * @param UserTable   $user
	 * @param UserTable   $viewer
	 * @param null|string $message
	 * @param null|string $messageType
	 */
	public function showItemEdit( $id, $type, $tab, $user, $viewer, $message = null, $messageType = 'error' )
	{
		global $_CB_framework, $_CB_database;

		/** @var Registry $params */
		$params								=	$tab->params;

		$row								=	new cbgalleryItemTable();

		$row->load( (int) $id );

		if ( ! $row->get( 'id' ) ) {
			$row->set( 'folder', $this->input( 'folder', 0, GetterInterface::INT ) );
		}

		$cbModerator						=	Application::User( (int) $viewer->get( 'id' ) )->isGlobalModerator();
		$canAccess							=	false;

		if ( ! $row->get( 'id' ) ) {
			if ( ( $user->get( 'id' ) != $viewer->get( 'id' ) ) && ( ! $cbModerator ) ) {
				$user						=	$viewer;
			}

			$canAccess						=	cbgalleryClass::canUserCreate( $viewer, $type, false );
		} elseif ( ( $row->get( 'type' ) == $type ) && ( $cbModerator || ( $viewer->get( 'id' ) == $row->get( 'user_id' ) ) ) ) {
			$canAccess						=	true;
		}

		if ( $row->get( 'folder' ) ) {
			$returnUrl						=	$_CB_framework->pluginClassUrl( $this->element, false, array( 'action' => 'folders', 'func' => 'show', 'type' => $type, 'id' => (int) $row->get( 'folder' ), 'user' => (int) $user->get( 'id' ), 'tab' => (int) $tab->get( 'tabid' ) ) );
		} else {
			$returnUrl						=	$_CB_framework->userProfileUrl( (int) $row->get( 'user_id', $user->get( 'id' ) ), false, $tab->get( 'tabid' ) );
		}

		if ( ! $canAccess ) {
			cbRedirect( $returnUrl, CBTxt::T( 'Not authorized.' ), 'error' );
		}

		$minFileSize						=	$this->params->get( $type . '_item_min_size', 0 );
		$maxFileSize						=	$this->params->get( $type . '_item_max_size', 1024 );

		switch( $type ) {
			case 'photos':
				$typeTranslated				=	CBTxt::T( 'Photo' );
				break;
			case 'files':
				$typeTranslated				=	CBTxt::T( 'File' );
				break;
			case 'videos':
				$typeTranslated				=	CBTxt::T( 'Video' );
				break;
			case 'music':
				$typeTranslated				=	CBTxt::T( 'Music' );
				break;
			default:
				$typeTranslated				=	CBTxt::T( 'Item' );
				break;
		}

		$extLimit							=	cbgalleryClass::getExtensions( $type );

		switch( $type ) {
			case 'photos':
			case 'videos':
			case 'music':
				$folderType					=	CBTxt::T( 'Album' );
				break;
			default:
				$folderType					=	CBTxt::T( 'Folder' );
				break;
		}

		cbgalleryClass::getTemplate( 'item_edit' );

		$input								=	array();

		$publishedTooltip					=	cbTooltip( null, CBTxt::T( 'ITEM_PUBLISHED_DESCRIPTION', 'Select publish status of the [type]. If unpublished the [type] will not be visible to the public.', array( '[type]' => $typeTranslated ) ), null, null, null, null, null, 'data-hascbtooltip="true"' );

		$input['published']					=	moscomprofilerHTML::yesnoSelectList( 'published', 'class="form-control"' . ( $publishedTooltip ? ' ' . $publishedTooltip : null ), (int) $this->input( 'post/published', $row->get( 'published', 1 ), GetterInterface::INT ) );

		$titleTooltip						=	cbTooltip( null, CBTxt::T( 'ITEM_TITLE_DESCRIPTION', 'Optionally input a title. If no title is provided the filename will be displayed as the title.', array( '[type]' => $typeTranslated ) ), null, null, null, null, null, 'data-hascbtooltip="true"' );

		$input['title']						=	'<input type="title" id="title" name="title" value="' . htmlspecialchars( $this->input( 'post/title', $row->get( 'title' ), GetterInterface::STRING ) ) . '" class="form-control" size="25"' . ( $titleTooltip ? ' ' . $titleTooltip : null ) . ' />';

		$listFolders						=	array();

		if ( cbgalleryClass::canUserCreate( $viewer, $type, true ) ) {
			$listFolders[]					=	moscomprofilerHTML::makeOption( -1, CBTxt::T( 'ITEM_NEW_FOLDER', 'New [type]', array( '[type]' => $folderType ) ) );
		}

		if ( $params->get( 'tab_' . $type . '_uncategorized', 1 ) ) {
			$listFolders[]					=	moscomprofilerHTML::makeOption( 0, CBTxt::T( 'Uncategorized' ) );
		}

		$query								=	'SELECT *'
											.	"\n FROM " . $_CB_database->NameQuote( '#__comprofiler_plugin_gallery_folders' )
											.	"\n WHERE " . $_CB_database->NameQuote( 'type' ) . " = " . $_CB_database->Quote( $type )
											.	"\n AND " . $_CB_database->NameQuote( 'user_id' ) . " = " . (int) $row->get( 'user_id', $user->get( 'id' ) )
											.	"\n ORDER BY " . $_CB_database->NameQuote( 'date' ) . " DESC";
		$_CB_database->setQuery( $query );
		$folders							=	$_CB_database->loadObjectList( null, 'cbgalleryFolderTable', array( $_CB_database ) );

		/** @var cbgalleryFolderTable[] $folders */
		foreach ( $folders as $folder ) {
			$listFolders[]					=	moscomprofilerHTML::makeOption( (int) $folder->get( 'id' ), ( $folder->get( 'title' ) ? $folder->get( 'title' ) : cbFormatDate( $folder->get( 'date' ), true, false ) ) );
		}

		$folderTooltip						=	cbTooltip( null, CBTxt::T( 'ITEM_FOLDER_DESCRIPTION', 'Select the [folder_type] for this [type].', array( '[folder_type]' => $folderType, '[type]' => $typeTranslated ) ), null, null, null, null, null, 'data-hascbtooltip="true"' );

		$input['folder']					=	moscomprofilerHTML::selectList( $listFolders, 'folder', 'class="form-control"' . ( $folderTooltip ? ' ' . $folderTooltip : null ), 'value', 'text', $this->input( 'post/folder', $row->get( 'folder', 0 ), GetterInterface::INT ), 1, false, false );

		$allowUpload						=	$this->params->get( $type . '_item_upload', 1 );
		$allowLink							=	$this->params->get( $type . '_item_link', 0 );

		if ( $allowUpload && $allowLink ) {
			$uploadButton					=	CBTxt::T( 'UPLOAD_ITEM_TYPE', 'Upload [type]', array( '[type]' => $typeTranslated ) );
			$linkButton						=	CBTxt::T( 'LINK_ITEM_TYPE', 'Link [type]', array( '[type]' => $typeTranslated ) );

			$js								=	"$( '#method' ).on( 'change', function() {"
											.		"var value = $( this ).val();"
											.		"if ( value == 1 ) {"
											.			"$( '#itemUpload' ).removeClass( 'hidden' ).find( 'input' ).removeClass( 'cbValidationDisabled' );"
											.			"$( '#itemLink' ).addClass( 'hidden' ).find( 'input' ).addClass( 'cbValidationDisabled' );";

			if ( ! $row->get( 'id' ) ) {
				$js							.=			"$( '.galleryButtonSubmit' ).val( '" . addslashes( $uploadButton ) . "' );";
			}

			$js								.=		"} else if ( value == 2 ) {"
											.			"$( '#itemUpload' ).addClass( 'hidden' ).find( 'input' ).addClass( 'cbValidationDisabled' ).val( '' );"
											.			"$( '#itemLink' ).removeClass( 'hidden' ).find( 'input' ).removeClass( 'cbValidationDisabled' );";

			if ( ! $row->get( 'id' ) ) {
				$js							.=			"$( '.galleryButtonSubmit' ).val( '" . addslashes( $linkButton ) . "' );";
			}

			$js								.=		"} else {"
											.			"$( '#itemUpload' ).addClass( 'hidden' ).find( 'input' ).addClass( 'cbValidationDisabled' ).val( '' );"
											.			"$( '#itemLink' ).addClass( 'hidden' ).find( 'input' ).addClass( 'cbValidationDisabled' );"
											.		"}"
											.	"}).change();";

			$_CB_framework->outputCbJQuery( $js );

			$listMethods					=	array();

			if ( $row->get( 'id' ) ) {
				$listMethods[]				=	moscomprofilerHTML::makeOption( 0, CBTxt::T( 'No Change' ) );
			}

			if ( $allowUpload ) {
				$listMethods[]				=	moscomprofilerHTML::makeOption( 1, CBTxt::T( 'Upload' ) );
			}

			if ( $allowLink ) {
				$listMethods[]				=	moscomprofilerHTML::makeOption( 2, CBTxt::T( 'Link' ) );
			}

			$input['method']				=	moscomprofilerHTML::selectList( $listMethods, 'method', 'class="form-control"', 'value', 'text', $this->input( 'post/method', 0, GetterInterface::INT ), 1, false, false );
		} else {
			$input['method']				=	null;
		}

		$fileValidation						=	array();

		if ( $minFileSize || $maxFileSize ) {
			$fileValidation[]				=	cbValidator::getRuleHtmlAttributes( 'filesize', array( $minFileSize, $maxFileSize, 'KB' ) );
		}

		if ( $extLimit ) {
			$fileValidation[]				=	cbValidator::getRuleHtmlAttributes( 'extension', implode( ',', $extLimit ) );
		}

		if ( $allowUpload ) {
			$fileTooltip					=	cbTooltip( null, CBTxt::T( 'ITEM_UPLOAD_DESCRIPTION', 'Select the file to upload.', array( '[type]' => $typeTranslated ) ), null, null, null, null, null, 'data-hascbtooltip="true"' );

			$input['upload']				=	'<input type="file" id="file" name="file" value="" class="form-control' . ( ! $row->get( 'id' ) ? ' required' : null ) . '"' . ( $fileTooltip ? ' ' . $fileTooltip : null ) . ( $fileValidation ? implode( ' ', $fileValidation ) : null ) . ' />';

			$input['upload_limits']			=	array();

			if ( $extLimit ) {
				$input['upload_limits'][]	=	CBTxt::T( 'ITEM_UPLOAD_LIMITS_EXT', 'Your file must be of [ext] type.', array( '[ext]' => implode( ', ', $extLimit ) ) );
			}

			if ( $minFileSize ) {
				$input['upload_limits'][]	=	CBTxt::T( 'ITEM_UPLOAD_LIMITS_MIN', 'Your file should exceed [size].', array( '[size]' => cbgalleryClass::getFormattedFileSize( $minFileSize * 1024 ) ) );
			}

			if ( $maxFileSize ) {
				$input['upload_limits'][]	=	CBTxt::T( 'ITEM_UPLOAD_LIMITS_MAX', 'Your file should not exceed [size].', array( '[size]' => cbgalleryClass::getFormattedFileSize( $maxFileSize * 1024 ) ) );
			}
		} else {
			$input['upload']				=	null;
			$input['upload_limits']			=	null;
		}

		if ( $allowLink ) {
			$linkTooltip					=	cbTooltip( null, CBTxt::T( 'ITEM_LINK_DESCRIPTION', 'Input the URL to the file to link.', array( '[type]' => $typeTranslated ) ), null, null, null, null, null, 'data-hascbtooltip="true"' );

			$input['link']					=	'<input type="text" id="value" name="value" value="' . htmlspecialchars( $this->input( 'post/value', ( $row->getLinkDomain() ? $row->get( 'value' ) : null ), GetterInterface::STRING ) ) . '" size="40" class="form-control' . ( ! $row->get( 'id' ) ? ' required' : null ) . '"' . ( $linkTooltip ? ' ' . $linkTooltip : null ) . ' />';

			$input['link_limits']			=	array();

			if ( $extLimit ) {
				if ( $type == 'videos' ) {
					$extLimit[]				=	'youtube';
				}

				$input['link_limits'][]		=	CBTxt::T( 'ITEM_LINK_LIMITS_EXT', 'Your file must be of [ext] type.', array( '[ext]' => implode( ', ', $extLimit ) ) );
			}
		} else {
			$input['link']					=	null;
			$input['link_limits']			=	null;
		}

		$descriptionTooltip					=	cbTooltip( null, CBTxt::T( 'ITEM_DESCRIPTION_DESCRIPTION', 'Optionally input a description.', array( '[type]' => $typeTranslated ) ), null, null, null, null, null, 'data-hascbtooltip="true"' );

		$input['description']				=	'<textarea id="description" name="description" class="form-control" cols="40" rows="5"' . ( $descriptionTooltip ? ' ' . $descriptionTooltip : null ) . '>' . htmlspecialchars( $this->input( 'post/description', $row->get( 'description' ), GetterInterface::STRING ) ) . '</textarea>';

		$ownerTooltip						=	cbTooltip( null, CBTxt::T( 'ITEM_OWNER_DESCRIPTION', 'Input owner as single integer user_id.', array( '[type]' => $typeTranslated ) ), null, null, null, null, null, 'data-hascbtooltip="true"' );

		$input['user_id']					=	'<input type="text" id="user_id" name="user_id" value="' . (int) $this->input( 'post/user_id', $row->get( 'user_id', $user->get( 'id' ) ), GetterInterface::INT ) . '" class="digits required form-control" size="6"' . ( $ownerTooltip ? ' ' . $ownerTooltip : null ) . ' />';

		if ( $message ) {
			$_CB_framework->enqueueMessage( $message, $messageType );
		}

		HTML_cbgalleryItemEdit::showItemEdit( $row, $input, $type, $tab, $user, $viewer, $this );
	}