Exemple #1
0
 /**
  * Create
  *
  * $content (array) = content to be created
  */
 public function create(array $content)
 {
     # Check Content Type is Declared
     if (!isset($content['type'])) {
         $this->setResponse(100, 'Content type is required!');
         return false;
     }
     # Get Content Type
     $contentType = $this->types[$content['type']];
     # Check required fields
     foreach ($contentType['fields'] as $field) {
         # Validate required fields
         if ($this->fieldIsRequired($field) && empty($content[$field['name']])) {
             $this->setResponse(100, $field['name'] . ' Field required');
             return false;
         }
         # If there is a filter callback
         # set, run it
         if (isset($field['filter'])) {
             $content[$field['name']] = $field['filter']($content[$field['name']]);
         }
         # If this field is to be stored
         # in the content table, seperate it.
         if ($this->isContentField($field)) {
             # Add it to Content Fields array
             $contentFields[$field['name']] = $content[$field['name']];
             # Remove it from normal fields
             # array
             unset($content[$field['name']]);
         }
     }
     # Store Type key for later
     $contentFields['type'] = $content['type'];
     # Remove Type key
     unset($content['type']);
     # Create Alias if not set already
     if (!array_key_exists('alias', $contentFields) || empty($contentFields['alias'])) {
         $contentFields['alias'] = cck_url_alias($contentFields['title'], array('prefix' => $contentFields['type']));
         # If one is set, pass it through the generator
         # to clean it up
     } else {
         $contentFields['alias'] = cck_url_alias($contentFields['alias']);
     }
     # Create Content
     if (!($contentID = $this->db->insert('content', $contentFields))) {
         $this->setResponse(100, 'There was an error creating your content.');
         return false;
     }
     # Save Fields
     foreach ($content as $key => $value) {
         if (!$this->saveField($contentID, $key, $value)) {
             $this->setResponse(100, 'There was an error saving field: ' . $key . '.');
             return false;
         }
     }
     # Done!
     $this->setResponse(200, '<strong>' . $contentFields['title'] . '</strong> saved.');
     return true;
 }
<?php

use CCK\Helpers;
# Check Method is Post
if (!is_form_data()) {
    JSON::parse(100, 'negative', 'Invalid request. Requests to ' . $path . ' must be POST', null, true);
}
# Sanitize form data
$data = Helpers::sanitizeInput(form_data());
# Check 'alias' is present
if (!isset($data['alias']) || !is_string($data['alias'])) {
    JSON::parse(100, 'negative', 'Alias input is not valid.', null, true);
}
# Generate Alias
$alias = cck_url_alias($data['alias']);
# Return result
JSON::parse(200, 'positive', 'Alias generate', array('alias' => $alias), true);