Location: PHPKode > projects > Blandware AtLeap Lite - CMS on PHP > atleaplite/include/actions/resource.php
<?php
/*
 *  Copyright 2008 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/**
 * Actions for resources.
 *
 * @package     AtleapLite
 * @author      Roman Puchkovskiy
 * @license     http://www.apache.org/licenses/LICENSE-2.0  Apache License, Version 2.0
 */

/**
 */
require_once "HTTP/Upload.php";
require_once "DB/DataObject/Cast.php";

// ~ DAO functions


/**
 * Loads resources from DB.
 *
 * @param array $queryInfo optional query info
 * @param string $gridName optional grid name
 * @return array objects
 * @see loadObjects()
 */
function loadResources($queryInfo = array(), $gridName = 'resourcesGrid')
{
    return loadObjects('resource', getResourceDescriptor(), $gridName, $queryInfo);
}

/**
 * Creates or updates a resource.
 *
 * @param int $id       ID of category to update (ignored when creating)
 * @param object $form  form object
 * @param bool $create  if true, resource is created, else it's updated
 * @return bool|object false if failed or object
 */
function addOrUpdateResource($id, &$form, $create)
{
    if ($create) {
        $dao =& getDao('resource');
    } else {
        $dao =& staticGet('resource', $id);
        if (!$dao) {
            // no such object
            return false;
        }
    }

    formToDao($form, $dao, getResourceDescriptor());

    if (isResourceSavedToSession()) {
        list($name, $dao->data) = loadResourceFromSession();
        $dao->size = strlen($dao->data);
        $dao->type = getResourceType($name);
    } else {
        $upload = new HTTP_Upload();
        $file = $upload->getFiles("file");
        if ($create || $file->isValid()) {
            $dao->data = file_get_contents($file->getProp('tmp_name'));
            $dao->size = strlen($dao->data);
            $dao->type = getResourceType($file->getProp('name'));
        }
    }
    $dao->modification_date = getNowDate();
    $dao->data = DB_DataObject_Cast::blob($dao->data);
    if ($create) {
        $dao->insert();
    } else {
        $dao->update();
    }

    return $dao;
}

// ~ Actions

/**
 * Shows a page with list of resources.
 */
function listResources()
{
    global $smarty, $perPage, $pagerDelta, $bottomLinks;

    processGridParams('resourcesGrid'/*, array('resource_active' => -1)*/);
    $queryInfo = buildDefaultQueryInfo();
    $queryInfo['select'] = getSelectForResourceList();
//    $queryInfo['additionalSelect'] = array("(published AND ('" . getNowDate() . "' BETWEEN publication_date AND expiration_date)) as resource_active");
    $partial = loadResources($queryInfo);
    $total = $partial['total'];
    $resources = $partial['rows'];
    $pager = getPager(array('totalItems' => $total));
    exportPagerData($pager, $smarty, $resources);
    assignSortDirs('resourcesGrid', $smarty, getResourceDescriptor());
    assignFilters($smarty, 'resourcesGrid');

    $smarty->assign('template', 'resource/list.tpl');
    $smarty->assign('title', getMessage('resource.list.title'));

    $smarty->assign('allowedViewResource', allowed('viewResource'));
    $smarty->assign('allowedUpdateResource', allowed('updateResource'));
    $smarty->assign('allowedDeleteResource', allowed('deleteResource'));
    $smarty->assign('deletePermId', 'deleteResource');

    $smarty->initGrid('resourcesGrid', getResourceDescriptor(), $_GET['action']);
    $smarty->assign('allYesNoList', getAllYesNoList());

    setSystemMenuItemBold('manageResources');

    $bottomLinks[] = array('link' => buildUrl('callCreateResource'),
                           'text' => getMessage('common.button.create'), 
                           'permId' => 'createResource',
                           'button' => true);
}

/**
 * Allows to view resource properties.
 */
function viewResource()
{
    global $smarty, $bottomLinks;

    $bottomLinks[] = array('link' => getResourceListUrl('file_name'),
                           'text' => getMessage('common.button.ok'),
                           'permId' => 'listResources',
                           'button' => true);
    $bottomLinks[] = array('link' => buildUrl('callUpdateResource', array('id' => $_GET['id'])),
                           'text' => getMessage('common.button.edit'),
                           'permId' => 'updateResource',
                           'button' => true);

    $dao =& staticGet('resource', $_GET['id']);
    $smarty->assign('viewed', $dao);

    $smarty->assign('template', 'resource/view.tpl');
    $smarty->assign('title', getMessage('resource.view.title'));
}

/**
 * Displays a list of resources to user.
 */
function showResources()
{
    global $smarty, $perPage, $pagerDelta, $bottomLinks;

    $queryInfo = array('select' => getSelectForResourceList(), 'orderBy' => 'id',
            'where' => "published = 1 AND ('" . getNowDate() . "' BETWEEN publication_date AND expiration_date)");
    $queryInfo['fields']['type'] = RESOURCE_TYPE_FILE;
    $partial = loadObjects('resource', getResourceDescriptor(), null, $queryInfo);
    $total = $partial['total'];
    $resources = $partial['rows'];
    $pager = getPager(array('totalItems' => $total, 'perPage' => 10000));
    exportPagerData($pager, $smarty, $resources);
    assignSortDirs('resourcesUserGrid', $smarty, getResourceDescriptor());
    assignFilters($smarty, 'resourcesUserGrid');

    $smarty->assign('template', 'resource/show.tpl');
    $smarty->assign('title', getMessage('resource.show.title'));
    $smarty->setAdminLayout(false);

    setSystemMenuItemBold('showResources');
}

/**
 * Creates a resource form.
 *
 * @param string $name      form name
 * @param string $method    HTTP method
 * @param string $action    action
 * @param bool $create      whether this is for for creation, not for updating
 * @param bool $init        whether form needs to be initialized from dao
 * @param int $id optional  resource ID (for updating)
 * @return object created form
 */
function &createResourceForm($name, $method, $action, $create, $init,
        $id = null) {
    $params = array();
    if (!$create) {
        $params['id'] = $id;
    }

    $saved = isResourceSavedToSession();

    $form = new FormBase($name, $method, buildUrl($action, $params));
    $form->setMaxFileSize(MAX_UPLOAD_FILE_SIZE);

    if (!$saved) {
        $attrs = array();
        if ($create) {
            $attrs['onchange'] = "fillFileName('$name', 'file', 'fileName', 'title');";
        }
        $form->addFileElement('file', getMessage('resource.form.file'), $attrs);
    }
    $form->addTextElement('fileName', getMessage('resource.form.fileName'));
    $form->addTextElement('title', getMessage('resource.form.title'));
    $form->addCheckboxElement('download', getMessage('resource.form.download'));
    if ($create && $init) {
        $downloadElem =& $form->getElement('download');
        $downloadElem->setChecked(true);
    }
    $form->addCheckboxElement('published', getMessage('resource.form.published'));
    if ($create && $init) {
        $publishedElem =& $form->getElement('published');
        $publishedElem->setValue(true);
    }
    addCommonCalendarDateElement($form, 'publicationDate', getMessage('resource.form.publicationDate'));
    $form->addCalendarDateElement('expirationDate', getMessage('page.form.expirationDate'), null,
            array('phpDatePattern' => DATE_PATTERN,
                'calendarDatePattern' => CALENDAR_DATE_PATTERN,
                'imgSrc' => 'images/calendar.gif',
                'startDate' => mktime(0, 0, 0, date("m"), date("d"), date("Y")+1)
                ));

    $form->addProceedElement($create ? getMessage('common.button.create') : getMessage('common.button.update'));
    $form->addCancelElement(getMessage('common.button.cancel'));
    if (!$create && allowed('deleteResource')) {
        // we don't have a real back button here, so we utilize this one for
        // delete button
        $form->addBackElement(getMessage('common.button.delete'), array('onclick' => "return confirm('" . getMessage('resource.form.confirmDelete') . "')"));
    }

    if (!$create && $init) {
        $dao =& staticGet('resource', $id);
        daoToForm($dao, $form, getResourceDescriptor());
    }
    
    if ($create && !$saved) {
        $form->addRequiredRule('file', getMessage('resource.error.file.required'));
        $form->addUploadedFileRule('file', getMessage('resource.error.file.uploadedFile', MAX_UPLOAD_FILE_SIZE));
    }
    $form->addRequiredRule('fileName', getMessage('resource.error.fileName.required'));
    $form->addFileNameRule('fileName', getMessage('resource.error.fileName.fileName'));
    $form->addRequiredRule('title', getMessage('resource.error.title.required'));
    $form->addFormRule($create ? 'validateCreateResourceForm' : 'validateUpdateResourceForm');

    return $form;
}

/**
 * Shows a page with form to create a resource.
 */
function callCreateResource()
{
    global $smarty, $bottomLinks;

    saveReturnUrlToSession('resource');
    setSystemMenuItemBold('manageResources');

    removeResourceFromSession();

    $form =& createResourceForm('createResource', 'POST', 'createResource', true, true);

    showForm($smarty, $form, 'resource/createUpdate.tpl', getMessage('resource.create.title'));
}

/**
 * Creates a resource.
 */
function createResource()
{
    global $smarty, $bottomLinks;

    if (isCancelled()) {
        removeResourceFromSession();
        redirectToReturnUrlOrStandardResourceUrl('file_name');
    }

    setSystemMenuItemBold('manageResources');

    saveResourceToSessionIfUploaded('file');

    $form =& createResourceForm('createResource', 'POST', 'createResource', true, false);

    if ($form->validate()) {
        addOrUpdateResource(null, $form, true);
        removeResourceFromSession();
        redirectToReturnUrlOrStandardResourceUrl();
    } else {
        showForm($smarty, $form, 'resource/createUpdate.tpl', getMessage('resource.create.title'));
    }
}

/**
 * Shows a page with form to update a resource.
 */
function callUpdateResource()
{
    global $smarty, $bottomLinks;

    saveReturnUrlToSession('resource');
    setSystemMenuItemBold('manageResources');

    removeResourceFromSession();

    $form =& createResourceForm('updateResource', 'POST', 'updateResource', false, true, $_GET['id']);

    $dao =& staticGet('resource', $_GET['id']);
    if ($dao->isImage()) {
        $smarty->assign('imageFileName', $dao->file_name);
    }
    showForm($smarty, $form, 'resource/createUpdate.tpl', getMessage('resource.update.title'));
}

/**
 * Updates a resource.
 */
function updateResource()
{
    global $smarty, $bottomLinks;

    if (isCancelled()) {
        removeResourceFromSession();
        redirectToReturnUrlOrStandardResourceUrl('file_name');
    }

    // the 'back' button is used for delete button
    if (isBackPressed()) {
        $returnUrl = loadReturnUrlFromSession('resource');
        if (!allowed('deleteResource')) {
            setupForb(null, $returnUrl);
            return;
        }
        $deleted = deleteResourceById($_GET['id']);
        if (!$returnUrl) {
            $returnUrl = getResourceListUrl();
        }
        if ($deleted) {
            redirect($returnUrl);
        } else {
            $smarty->assign('template', 'error.tpl');
            $smarty->assign('reason', getMessage('resource.error.cannotDelete'));
            $smarty->assign('link', $returnUrl);
        }
    }

    setSystemMenuItemBold('manageResources');

    saveResourceToSessionIfUploaded('file');

    $form =& createResourceForm('updateResource', 'POST', 'updateResource', false, false, $_GET['id']);

    $dao =& staticGet('resource', $_GET['id']);
    if ($dao->isImage()) {
        $smarty->assign('imageFileName', $dao->file_name);
    }

    if ($form->validate()) {
        $oldDao =& staticGet('resource', $_GET['id']);
        removeResourceFromCache($oldDao->file_name);
        addOrUpdateResource($_GET['id'], $form, false);
        removeResourceFromSession();
        redirectToReturnUrlOrStandardResourceUrl();
    } else {
        showForm($smarty, $form, 'resource/createUpdate.tpl', getMessage('resource.update.title'));
    }
}

/**
 * Validates a form which is used to create a resource.
 *
 * @param array $fields assoc array from field names to values
 * @return bool|array true if form is valid or assoc array with errors
 */
function validateCreateResourceForm($fields) {
    return validateResourceForm($fields, true);
}

/**
 * Validates a form which is used to update a resource.
 *
 * @param array $fields assoc array from field names to values
 * @return bool|array true if form is valid or assoc array with errors
 */
function validateUpdateResourceForm($fields) {
    return validateResourceForm($fields, false);
}

/**
 * Validates a resource form.
 *
 * @param array $fields assoc array from field names to values
 * @param bool $create  whether this is creation form
 * @return bool|array true if form is valid or assoc array with errors
 */
function validateResourceForm($fields, $create)
{
    if (resourceHasDuplicates($fields, $create)) {
        return array('fileName' => getMessage('resource.error.duplicate.fileName'));
    }

    return true;
}

/**
 * Returns whether resource has duplicates.
 *
 * @param array $fields         assoc array from field names to values
 * @param bool $create          whether this is creation form being validated
 */
function resourceHasDuplicates($fields, $create) {
    $id = $_GET['id'];
    $fileName = $fields['fileName'];
    $dao =& getDao('resource');
    $escapedFileName = $dao->escape($fileName);

    $dao->selectAdd();
    $dao->selectAdd('count(*) as _c');

    $dao->whereAdd();
    $dao->whereAdd("(file_name = '$escapedFileName')");
    if (!$create) {
        $dao->whereAdd("(id != $id)");
    }
    $dao->find(true);
    return $dao->_c > 0;
}

/**
 * Deletes a resource by its ID. Also removes it from the cache.
 *
 * @param int $id   resource ID
 * @return bool whether removal was successful
 */
function deleteResourceById($id)
{
    global $smarty, $bottomLinks;

    $dao =& staticGet('resource', $id);

    removeResourceFromCache($dao->file_name);
    return tryDeleteResource($dao);
}

/**
 * Deletes a resource.
 */
function deleteResource()
{
    global $smarty, $bottomLinks;

    setSystemMenuItemBold('manageResources');

    if (deleteResourceById($_GET['id'])) {
        saveReturnUrlToSession('resource');
        redirectToReturnUrlOrStandardResourceUrl();
    } else {
        $smarty->assign('template', 'error.tpl');
        $smarty->assign('reason', getMessage('resource.error.cannotDelete'));
        $smarty->assign('link', getResourceListUrl());
    }
}

/**
 * Deletes several resources.
 */
function massDeleteResources()
{
    if (isset($_GET['checked'])) {
        foreach ($_GET['checked'] as $id => $on) {
            $dao =& staticGet('resource', $id);
            tryDeleteResource($dao);
        }
    }
    redirect(getResourceListUrl());
}

/**
 * Tryies to delete a resource.
 *
 * @param object $dao   object to delete
 * @return true if deletion was successful
 */
function tryDeleteResource($dao) {
    removeResourceFromCache($dao->file_name);
    return $dao->delete();
}

/**
 * Removes a resource from cache.
 *
 * @param string $fileName  resource file name
 */
function removeResourceFromCache($fileName) {
    $cacheManager = new CacheManager();
    $cacheManager->removeResource($fileName);
}

/**
 * Returns URL to list of resources.
 *
 * @param string $colon optional    colon by which to sort
 * @return URL
 */
function getResourceListUrl($colon = null) {
    if (empty($colon)) {
        $colon = $_SESSION['sortColon'];
    }
    return buildUrl('listResources'/*, array('sortColon' => $colon)*/);
}

/**
 * If there's a return URL in session, redirects to it. Otherwise, redirects to
 * the standard resource URL.
 *
 * @param string $colon colon which will be used for sorting
 */
function redirectToReturnUrlOrStandardResourceUrl($colon = null) {
    $returnUrl = loadReturnUrlFromSession('resource');
    if ($returnUrl) {
        redirect($returnUrl);
    } else {
        redirect(getResourceListUrl($colon));
    }
}

/**
 * Saves a resource to session if it was uploaded.
 *
 * @param string $field name of field under which file was uploaded
 */
function saveResourceToSessionIfUploaded($field) {
    $upload = new HTTP_Upload();
    $file = $upload->getFiles($field);
    if ($file->isValid()) {
        saveResourceToSession($file->getProp('name'), file_get_contents($file->getProp('tmp_name')));
    }
}

/**
 * Saves resource to session.
 *
 * @param string $name  initial file name (used to determine a resource type)
 * @param string $data  file data
 */
function saveResourceToSession($name, $data) {
    $_SESSION['savedResourceName'] = $name;
    $_SESSION['savedResourceData'] = $data;
}

/**
 * Loads resource from session.
 *
 * @return array file name, file data
 */
function loadResourceFromSession() {
    return array($_SESSION['savedResourceName'], $_SESSION['savedResourceData']);
}

/**
 * Removes resource from session.
 */
function removeResourceFromSession() {
    unset($_SESSION['savedResourceName']);
    unset($_SESSION['savedResourceData']);
}

/**
 * Returns whether there's a resource saved in a session.
 */
function isResourceSavedToSession() {
    return isset($_SESSION['savedResourceData']);
}

/**
 * Builds a select-clause array for resources to be shown in grid.
 *
 * @return array fields to be selected
 */
function getSelectForResourceList() {
    return array('id', 'title', 'size', 'download', 'file_name', 'published',
            'modification_date', 'publication_date', 'expiration_date', 'type');
}


?>
Return current item: Blandware AtLeap Lite - CMS on PHP