Tuskfish API V1.1.2
  • Package
  • Class

Packages

  • content
  • core
  • database
  • installation
  • security
  • user
  • utilities

Classes

  • TfArticle
  • TfAudio
  • TfBlock
  • TfBlockHandler
  • TfCache
  • TfCollection
  • TfCollectionHandler
  • TfContentControllerFactory
  • TfContentHandler
  • TfContentHandlerFactory
  • TfContentObject
  • TfContentObjectController
  • TfCriteria
  • TfCriteriaFactory
  • TfCriteriaItem
  • TfDatabase
  • TfDownload
  • TfFileHandler
  • TfImage
  • TfLogger
  • TfMetadata
  • TfPaginationControl
  • TfPreference
  • TfPreferenceHandler
  • TfRss
  • TfSearchContent
  • TfSession
  • TfStatic
  • TfTag
  • TfTagHandler
  • TfTaglinkHandler
  • TfTemplate
  • TfTree
  • TfUser
  • TfUtils
  • TfValidator
  • TfValidatorFactory
  • TfVideo
  • TfYubikeyAuthenticator

Traits

  • TfContentTypes
  • TfLanguage
  • TfMagicMethods
  • TfMimetypes
  • TfRights

Functions

  • checkPasswordStrength
  • getUrl
  • hashPassword
  • tf_autoload
  • tfContentModuleAutoload
  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 
<?php
/**
 * TfContentObjectController class file.
 * 
 * @copyright   Simon Wilkinson 2013+ (https://tuskfish.biz)
 * @license     https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html GNU General Public License (GPL) V2
 * @author      Simon Wilkinson <[email protected]>
 * @version     Release: 1.0
 * @since       1.0
 * @package     content
 */
// Enable strict type declaration.
declare(strict_types=1);
if (!defined("TFISH_ROOT_PATH")) die("TFISH_ERROR_ROOT_PATH_NOT_DEFINED");
/**
 * Controls basic content object operations (add, edit, delete, toggle and update). It encapsulates
 * the admin controller script functionality for these operations.
 * 
 *
 * @copyright   Simon Wilkinson 2013+ (https://tuskfish.biz)
 * @license     https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html GNU General Public License (GPL) V2
 * @author      Simon Wilkinson <[email protected]>
 * @version     Release: 1.0
 * @since       1.0
 * @package     content
 * @var         TfValidator $validator Instance of the Tuskfish data validator class.
 * @var         TfDatabase $db Instance of the Tuskfish database class.
 * @var         TfCriteriaFactory $criteriaFactory Instance of the Tuskfish criteria factory class.
 * @var         TfContentHandlerFactory $contentHandlerFactory Instance of the Tuskfish content handler factory class.
 * @var         TfTemplate $template Instance of the Tuskfish template object class.
 * @var         TfPreference $preference Instance of the Tuskfish site preferences class.
 * @var         TfCache $cache Instance of the Tuskfish site cache class.
 */
class TfContentObjectController
{
    protected $validator;
    protected $db;
    protected $criteriaFactory;
    protected $contentHandlerFactory;
    protected $template;
    protected $preference;
    protected $cache;
    
        
    /**
     * Constructor.
     * 
     * @param TfValidator $validator Instance of the validator class.
     * @param TfDatabase $db Instance of the database class.
     * @param TfCriteriaFactory $criteriaFactory Instance of the criteria factory class.
     * @param TfContentHandlerFactory $contentHandlerFactory Instance of the content handler class.
     * @param TfTemplate $template Instance of the template class.
     * @param TfPreference $preference Instance of the site preferences class.
     * @param TfCache $cache Instance of the cache class.
     */
    public function __construct(TfValidator $validator, TfDatabase $db,
            TfCriteriaFactory $criteriaFactory, TfContentHandlerFactory $contentHandlerFactory,
            TfTemplate $template, TfPreference $preference, TfCache $cache)
    {
        if (is_a($validator, 'TfValidator')) {
            $this->validator = $validator; 
        } else {
            trigger_error(TFISH_ERROR_NOT_VALIDATOR, E_USER_ERROR);
        }
        
        if (is_a($db, 'TfDatabase')) {
            $this->db = $db; 
        } else {
            trigger_error(TFISH_ERROR_NOT_DATABASE, E_USER_ERROR);
        }
        
        if (is_a($criteriaFactory, 'TfCriteriaFactory')) {
            $this->criteriaFactory = $criteriaFactory; 
        } else {
            trigger_error(TFISH_ERROR_NOT_CRITERIA_FACTORY, E_USER_ERROR);
        }
        
        if (is_a($contentHandlerFactory, 'TfContentHandlerFactory')) {
            $this->contentHandlerFactory = $contentHandlerFactory;
        }  else {
            trigger_error(TFISH_ERROR_NOT_CONTENT_HANDLER_FACTORY, E_USER_ERROR);
        }
        
        if (is_a($template, 'TfTemplate')) {
            $this->template = $template;
        }  else {
            trigger_error(TFISH_ERROR_NOT_TEMPLATE_OBJECT, E_USER_ERROR);
        }
        
        if (is_a($preference, 'TfPreference')) {
            $this->preference = $preference;
        }  else {
            trigger_error(TFISH_ERROR_NOT_PREFERENCE, E_USER_ERROR);
        }
        
        if (is_a($cache, 'TfCache')) {
            $this->cache = $cache;
        }  else {
            trigger_error(TFISH_ERROR_NOT_CACHE, E_USER_ERROR);
        }
    }
    
    /**
     * Add a content object to the site.
     */
    public function addContent()
    {
        $content = new TfContentObject($this->validator);
        $contentHandler = $this->contentHandlerFactory->getHandler('content');
        $collectionHandler = $this->contentHandlerFactory->getHandler('collection');
        
        $this->template->pageTitle = TFISH_ADD_CONTENT;
        $this->template->op = 'submit'; // Critical to launch correct form submission action.
        $this->template->contentTypes = $contentHandler->getTypes();
        $this->template->rights = $content->getListOfRights();
        $this->template->languages = $this->preference->getListOfLanguages();
        $this->template->tags = $contentHandler->getTagList(false);
        // Make a parent tree select box options.
        $collections = $collectionHandler->getObjects();
        $parentTree = new TfTree($collections, 'id', 'parent');
        $this->template->parentSelectOptions = $parentTree->makeParentSelectBox();
        $this->template->allowedProperties = $content->getPropertyWhitelist();
        $this->template->zeroedProperties = array(
            'image' => array('image'),
            'tags' => array(
                'creator',
                'language',
                'rights',
                'publisher',
                'tags')
        );
        $this->template->form = TFISH_CONTENT_MODULE_FORM_PATH . "dataEntry.html";
        $this->template->tfMainContent = $this->template->render('form');
    }
    
    /**
     * Request confirmation to delete a content object from the site.
     * 
     * Actual deletion is carried out in the deleteContent() option, not here. This is just the
     * sanity check.
     * 
     * @param int $id ID of the content object to confirm deletion of.
     */
    public function confirmDelete(int $id)
    {
        $cleanId = (int) $id;
        
        if (!$this->validator->isInt($cleanId, 1)) {
            trigger_error(TFISH_ERROR_NOT_INT, E_USER_ERROR);
        }
        
        $contentHandler = $this->contentHandlerFactory->getHandler('content');
        $this->template->pageTitle = TFISH_CONFIRM_DELETE;
        $this->template->content = $contentHandler->getObject($cleanId);
        $this->template->form = TFISH_CONTENT_MODULE_FORM_PATH . "confirmDelete.html";
        $this->template->tfMainContent = $this->template->render('form');
    }
    
    /**
     * Delete a content object from the database.
     * 
     * @param int $id Id of the content object to be deleted.
     */
    public function deleteContent(int $id)
    {
        $cleanId = (int) $id;
        
        if (!$this->validator->isInt($cleanId, 1)) {
            trigger_error(TFISH_ERROR_NOT_INT, E_USER_ERROR);
        }
                
        $contentHandler = $this->contentHandlerFactory->getHandler('content');
        $result = $contentHandler->delete($cleanId);
                
        if ($result) {
            $this->cache->flushCache();
            $this->template->pageTitle = TFISH_SUCCESS;
            $this->template->alertClass = 'alert-success';
            $this->template->message = TFISH_OBJECT_WAS_DELETED;
        } else {
            $this->template->pageTitle = TFISH_FAILED;
            $this->template->alertClass = 'alert-danger';
            $this->template->message = TFISH_OBJECT_DELETION_FAILED;
        }
        $this->template->backUrl = 'admin.php';
        $this->template->form = TFISH_FORM_PATH . "response.html";
        $this->template->tfMainContent = $this->template->render('form');
    }
    
    /**
     * Edit a content object.
     * 
     * Opens a data entry form pre-filled with the object's data, for editing.
     * 
     * @param int $id ID of the content object to be edited.
     */
    public function editContent(int $id)
    {
        $cleanId = (int) $id;
        
        if (!$this->validator->isInt($cleanId, 1)) {
            trigger_error(TFISH_ERROR_NOT_INT, E_USER_ERROR);
        }
            
        $criteria = $this->criteriaFactory->getCriteria();
        $criteria->add($this->criteriaFactory->getItem('id', $cleanId));
        $statement = $this->db->select('content', $criteria);
        if (!$statement) {
            trigger_error(TFISH_ERROR_NO_SUCH_OBJECT, E_USER_NOTICE);
            header("Location: admin.php");
        }
        // Build the content object.
        $row = $statement->fetch(PDO::FETCH_ASSOC);
        $contentHandler = $this->contentHandlerFactory->getHandler('content');
        $content = $contentHandler->convertRowToObject($row, false);
        // Make a parent tree select box options.
        $collectionHandler = $this->contentHandlerFactory->getHandler('collection');
        $collections = $collectionHandler->getObjects();
        $parentTree = new TfTree($collections, 'id', 'parent');            
        // Assign to template.
        $this->template->pageTitle = TFISH_EDIT_CONTENT;
        $this->template->op = 'update'; // Critical to launch correct submission action.
        $this->template->action = TFISH_UPDATE;
        $this->template->content = $content;
        $this->template->contentTypes = $contentHandler->getTypes();
        $this->template->rights = $content->getListOfRights();
        $this->template->languages = $this->preference->getListOfLanguages();
        $this->template->tags = $contentHandler->getTagList(false);
        $this->template->parentSelectOptions = 
                $parentTree->makeParentSelectBox((int) $row['parent']);
        $this->template->form = TFISH_CONTENT_MODULE_FORM_PATH . "dataEdit.html";
        $this->template->tfMainContent = $this->template->render('form');
    }
    
    /**
     * Submit a new content object for insertion into the database.
     * 
     * @param array $formData Data from the content data entry form.
     */
    public function submitContent(array $formData)
    {
        if (!isset($formData['type'])) {
            trigger_error(TFISH_ERROR_ILLEGAL_VALUE, E_USER_ERROR);
            exit;
        }
        $cleanType = $this->validator->trimString($formData['type']);
        $contentHandler = $this->contentHandlerFactory->getHandler('content');
        $typeWhitelist = $contentHandler->getTypes();
        if (!array_key_exists($cleanType, $typeWhitelist)) {
            trigger_error(TFISH_ERROR_ILLEGAL_VALUE, E_USER_ERROR);
            exit;
        }
        $content = new $cleanType($this->validator);
        $content->loadPropertiesFromArray($_REQUEST);
        $result = $contentHandler->insert($content);
        if ($result) {
            $this->cache->flushCache();
            $this->template->pageTitle = TFISH_SUCCESS;
            $this->template->alertClass = 'alert-success';
            $this->template->message = TFISH_OBJECT_WAS_INSERTED;
        } else {
            $this->template->title = TFISH_FAILED;
            $this->template->alertClass = 'alert-danger';
            $this->template->message = TFISH_OBJECT_INSERTION_FAILED;
        }
        $this->template->backUrl = 'admin.php';
        $this->template->form = TFISH_FORM_PATH . "response.html";
        $this->template->tfMainContent = $this->template->render('form');
    }
    
    /**
     * Toggle a content object online or offline.
     * 
     * Offline content objects are not available on the front end of the site and are not returned
     * in search results or in RSS feeds. Exception is tags, which are always online. Marking a tag
     * as offline will remove it from the front end tag select box, to keep it uncluttered.
     * 
     * @param int $id ID of the content object to toggle on/offline.
     */
    public function toggleOnlineStatus(int $id)
    {
        $cleanId = (int) $id;
        
        if (!$this->validator->isInt($cleanId, 0)) {
            trigger_error(TFISH_ERROR_NOT_INT, E_USER_ERROR);
        }
        
        $contentHandler = $this->contentHandlerFactory->getHandler('content');
        $result = $contentHandler->toggleOnlineStatus($cleanId);
        if ($result) {
            $this->cache->flushCache();
            $this->template->pageTitle = TFISH_SUCCESS;
            $this->template->alertClass = 'alert-success';
            $this->template->message = TFISH_OBJECT_WAS_UPDATED;
        } else {
            $this->template->pageTitle = TFISH_FAILED;
            $this->template->alertClass = 'alert-danger';
            $this->template->message = TFISH_OBJECT_UPDATE_FAILED;
        }
        $this->template->backUrl = 'admin.php';
        $this->template->form = TFISH_FORM_PATH . "response.html";
        $this->template->tfMainContent = $this->template->render('form');
    }
    
    /**
     * Update an existing content object.
     * 
     * @param array $formData Data from the edit content form.
     */
    public function updateContent(array $formData)
    {
        if (!isset($formData['type'])) {
            trigger_error(TFISH_ERROR_ILLEGAL_VALUE, E_USER_ERROR);
            exit;
        }
        $type = $this->validator->trimString($formData['type']);
        $contentHandler = $this->contentHandlerFactory->getHandler('content');
        $typeWhitelist = $contentHandler->getTypes();
        if (!array_key_exists($type, $typeWhitelist)) {
            trigger_error(TFISH_ERROR_ILLEGAL_VALUE, E_USER_ERROR);
            exit;
        }
        $content = new $type($this->validator);
        $content->loadPropertiesFromArray($formData);
        // As this object is being sent to storage, need to decode some entities that were encoded
        // for display.
        $fieldsToDecode = array('title', 'creator', 'publisher', 'caption');
        foreach ($fieldsToDecode as $field) {
            if (isset($content->field)) {
                $content->$field = htmlspecialchars_decode($content->field, ENT_NOQUOTES);
            }
        }
        // Properties that are used within attributes must have quotes encoded.
        $fieldsToDecode = array('metaTitle', 'seo', 'metaDescription');
        foreach ($fieldsToDecode as $field) {
            if (isset($content->field)) {
                $content->$field = htmlspecialchars_decode($content->field, ENT_QUOTES);
            }
        }
        // Update the database row and display a response.
        $result = $contentHandler->update($content);
        if ($result) {
            $this->cache->flushCache();
            $this->template->pageTitle = TFISH_SUCCESS;
            $this->template->alertClass = 'alert-success';
            $this->template->message = TFISH_OBJECT_WAS_UPDATED;
            $this->template->id = $content->id;
        } else {
            $this->template->pageTitle = TFISH_FAILED;
            $this->template->alertClass = 'alert-danger';
            $this->template->message = TFISH_OBJECT_UPDATE_FAILED;
        }
        $this->template->backUrl = 'admin.php';
        $this->template->form = TFISH_CONTENT_MODULE_FORM_PATH . "responseEdit.html";
        $this->template->tfMainContent = $this->template->render('form');
    }
   
}
Tuskfish API V1.1.2 API documentation generated by ApiGen