Back to Content class

Method check

public bool
check
()
Overloaded check function
Returns
  • bool True on success, false on failure
Since
  • 1.5
-
  • \Joomla\CMS\Table\Table::check()
Class: Content
Project: Joomla

Method check - Source code

/**
 * Overloaded check function
 *
 * @return  boolean  True on success, false on failure
 *
 * @see     Table::check()
 * @since   1.5
 */
public function check()
{
    try {
        parent::check();
    } catch (\Exception $e) {
        $this->setError($e->getMessage());
        return false;
    }
    if (trim($this->title) == '') {
        $this->setError(Text::_('COM_CONTENT_WARNING_PROVIDE_VALID_NAME'));
        return false;
    }
    if (trim($this->alias) == '') {
        $this->alias = $this->title;
    }
    $this->alias = ApplicationHelper::stringURLSafe($this->alias, $this->language);
    if (trim(str_replace('-', '', $this->alias)) == '') {
        $this->alias = Factory::getDate()->format('Y-m-d-H-i-s');
    }
    // Check for a valid category.
    if (!($this->catid = (int) $this->catid)) {
        $this->setError(Text::_('JLIB_DATABASE_ERROR_CATEGORY_REQUIRED'));
        return false;
    }
    if (trim(str_replace(' ', '', $this->fulltext)) == '') {
        $this->fulltext = '';
    }
    /**
     * Ensure any new items have compulsory fields set. This is needed for things like
     * frontend editing where we don't show all the fields or using some kind of API
     */
    if (!$this->id) {
        // Images can be an empty json string
        if (!isset($this->images)) {
            $this->images = '{}';
        }
        // URLs can be an empty json string
        if (!isset($this->urls)) {
            $this->urls = '{}';
        }
        // Attributes (article params) can be an empty json string
        if (!isset($this->attribs)) {
            $this->attribs = '{}';
        }
        // Metadata can be an empty json string
        if (!isset($this->metadata)) {
            $this->metadata = '{}';
        }
        // Hits must be zero on a new item
        $this->hits = 0;
    }
    // Set publish_up to null if not set
    if (!$this->publish_up) {
        $this->publish_up = null;
    }
    // Set publish_down to null if not set
    if (!$this->publish_down) {
        $this->publish_down = null;
    }
    // Check the publish down date is not earlier than publish up.
    if (!is_null($this->publish_up) && !is_null($this->publish_down) && $this->publish_down < $this->publish_up) {
        // Swap the dates.
        $temp = $this->publish_up;
        $this->publish_up = $this->publish_down;
        $this->publish_down = $temp;
    }
    // Clean up keywords -- eliminate extra spaces between phrases
    // and cr (\r) and lf (\n) characters from string
    if (!empty($this->metakey)) {
        // Only process if not empty
        // Array of characters to remove
        $badCharacters = ["\n", "\r", "\"", '<', '>'];
        // Remove bad characters
        $afterClean = StringHelper::str_ireplace($badCharacters, '', $this->metakey);
        // Create array using commas as delimiter
        $keys = explode(',', $afterClean);
        $cleanKeys = [];
        foreach ($keys as $key) {
            if (trim($key)) {
                // Ignore blank keywords
                $cleanKeys[] = trim($key);
            }
        }
        // Put array back together delimited by ", "
        $this->metakey = implode(', ', $cleanKeys);
    } else {
        $this->metakey = '';
    }
    if ($this->metadesc === null) {
        $this->metadesc = '';
    }
    return true;
}