Example #1
0
 /**
  * Method to get the content types.
  *
  * @return  array  An array of JContentType objects.
  *
  * @since   12.1
  * @throws  RuntimeException
  */
 public function getTypes()
 {
     $types = array();
     // Get the cache store id.
     $storeId = $this->getStoreId('getTypes');
     // Attempt to retrieve the types from cache first.
     $cached = $this->retrieve($storeId);
     // Check if the cached value is usable.
     if (is_array($cached)) {
         return $cached;
     }
     // Build the query to get the content types.
     $query = $this->db->getQuery(true);
     $query->select('a.*');
     $query->from($query->qn('#__content_types') . ' AS a');
     // Get the content types.
     $this->db->setQuery($query);
     $results = $this->db->loadObjectList();
     // Reorganize the type information.
     foreach ($results as $result) {
         // Create a new JContentType object.
         $type = $this->factory->getType();
         // Bind the type data.
         $type->bind($result);
         // Add the type, keyed by alias.
         $types[$result->alias] = $type;
     }
     // Store the types in cache.
     return $this->store($storeId, $types);
 }
Example #2
0
 /**
  * Method to update one item
  *
  * @return  JContent  A JContent object
  *
  * @since   1.0
  * @throws  UnexpectedValueException
  */
 public function createItem()
 {
     // Get the content type.
     $contentType = $this->state->get('content.type');
     // Check if the content type is set.
     if (empty($contentType)) {
         throw new UnexpectedValueException(sprintf('%s->createItem() could not find the content type.', get_class($this)));
     }
     // Get new content
     $content = $this->factory->getContent($contentType);
     // Get fields for new content and check them
     $fields = $this->state->get('content.fields');
     if (empty($fields)) {
         throw new UnexpectedValueException('Missing fields for new object');
     }
     // Get each field for the new content
     $fieldsArray = preg_split('#[\\s,]+#', $fields, null, PREG_SPLIT_NO_EMPTY);
     foreach ($fieldsArray as $key => $fieldName) {
         $field = null;
         if ($this->state->exists('fields.' . $fieldName)) {
             $field = $this->state->get('fields.' . $fieldName);
         }
         if ($field == null) {
             throw new UnexpectedValueException('Missing field ' . $fieldName);
         }
         $content->__set($fieldName, $field);
     }
     // Create content
     $content = $content->create();
     $content->temporary = 0;
     $content->update();
     return $content;
 }
Example #3
0
 /**
  * Method to instantiate the model.
  *
  * @param   JContentFactory  $factory  The content factory.
  * @param   JDatabaseDriver  $db       The database adpater.
  * @param   JRegistry        $state    The model state.
  *
  * @since   1.0
  */
 public function __construct(JContentFactory $factory = null, JDatabaseDriver $db = null, JRegistry $state = null, $mappingFile = null)
 {
     parent::__construct($state);
     // Set factory
     if ($factory == null) {
         $this->factory = JContentFactory::getInstance();
     } else {
         $this->factory = $factory;
     }
     // Set database
     if ($db == null) {
         $this->db = JFactory::getDbo();
     } else {
         $this->db = $db;
     }
     $this->readMappingData($mappingFile);
 }
Example #4
0
 /**
  * Method to copy the content object.
  *
  * @return  JContent  The content object copy.
  *
  * @since   12.1
  * @throws  LogicException
  * @throws  RuntimeException
  */
 public function copy()
 {
     // Assert the object is loaded.
     $this->assertIsLoaded();
     // Create a new content item.
     $copy = $this->factory->getContent($this->type->alias)->create();
     // Get the data from this item.
     $data = (array) $this->dump();
     // Remove unique values.
     unset($data['content_id']);
     unset($data['featured']);
     unset($data['created_date']);
     unset($data['created_user_id']);
     unset($data['likes']);
     unset($data['revision']);
     // Update the content title and alias.
     $data['title'] = JString::increment($data['title']);
     $data['alias'] = JFilterOutput::stringURLSafe($data['title']);
     // Update the copy data.
     $copy->bind($data);
     return $copy;
 }