Example #1
0
 /**
  * Model definition.
  */
 function define()
 {
     // Fields.
     $this->fields = array('id', 'slug', 'name', 'fields', 'inputs', 'subscribers', 'date_created', 'date_updated', 'fields' => function ($channel) {
         return orderby($channel['fields'], 'sort');
     }, 'fields_by_id' => function ($channel) {
         return Channels::get_fields_by_id($channel);
     }, 'entries' => function ($channel) {
         return get("/entries", array('channel_id' => $channel['id']));
     }, 'entry_count' => function ($channel) {
         return get("/entries/:count", array('channel_id' => $channel['id']));
     });
     $this->search_fields = array('name');
     // Indexes.
     $this->indexes = array('id' => 'unique', 'slug' => 'unique');
     // Query defaults.
     $this->query = array('order' => 'name ASC');
     // Validate.
     $this->validate = array('required' => array('name', 'slug'), 'unique' => array('slug'), ':fields' => array('required' => array('id', 'name', 'type'), 'unique' => array('id')));
     // Event binds.
     $this->binds = array('POST' => function ($event) {
         $data =& $event['data'];
         // Auto slug?
         if ($data['name'] && !$data['slug']) {
             $data['slug'] = hyphenate($data['name']);
         }
     }, 'POST.fields' => function ($event) {
         $data =& $event['data'];
         // Default field ID to underscored field name.
         if (isset($data['name']) && !$data['id']) {
             $data['id'] = underscore($data['name']);
         }
     });
 }
Example #2
0
 /**
  * Constructor.
  */
 function __construct($values, $model_name)
 {
     $this->model_name = $model_name;
     $uri = "/" . hyphenate($model_name);
     if ($this->model()) {
         $uri .= ($pk = $values[$this->model()->pk]) ? "/{$pk}" : '';
     }
     $this->uri($uri);
     parent::__construct($values);
 }
Example #3
0
 function generateUUID($hyphenate = false)
 {
     $currentTime = (string) microtime(true);
     $randNumber = (string) rand(10000, 1000000);
     $shuffledString = random_string('md5');
     if ($hyphenate) {
         return hyphenate(md5($currentTime . $randNumber . $shuffledString));
     }
     return md5($currentTime . $randNumber . $shuffledString);
 }
Example #4
0
function static_component($component, $data)
{
    $component = $component . '.php';
    $stat = dirname(dirname(__FILE__)) . '/modules/static/';
    $path = $stat . $component;
    if (file_exists($path)) {
        require_once $path;
    } else {
        $path = $stat . hyphenate($component);
        if (file_exists($path)) {
            require_once $path;
        } else {
            echo '<!-- Static component ' . $path . ' not found -->';
        }
    }
}
Example #5
0
 /**
  * Model definition.
  */
 function define()
 {
     // Fields.
     $this->fields = array('id', 'slug', 'name', 'parent_id', 'description', 'date_created', 'date_updated', 'parent' => function ($category) {
         return $category['parent_id'] ? get("/categories/{$category['parent_id']}") : null;
     }, 'children' => function ($category) {
         return get("/categories", array('parent_id' => $category['id'], 'limit' => null));
     }, 'child_count' => function ($category) {
         return get("/categories/:count", array('parent_id' => $category['id']));
     }, 'products' => function ($category) {
         return get("/products", array('category_ids' => $category['id'], 'limit' => null));
     }, 'product_count' => function ($category) {
         return get("/products/:count", array('category_ids' => $category['id']));
     });
     $this->search_fields = array('name');
     // Indexes.
     $this->indexes = array('id' => 'unique', 'slug' => 'unique');
     // Query options.
     $this->query = array('order' => 'parent_id ASC, name ASC');
     // Validate.
     $this->validate = array('required' => array('slug', 'name'), 'unique' => array('slug'));
     // Event binds.
     $this->binds = array('POST' => function ($event) {
         $data =& $event['data'];
         // Auto slug?
         if ($data['name'] && !$data['slug']) {
             $data['slug'] = hyphenate($data['name']);
             // Append parent slug?
             if ($parent = get("/categories/{$data['parent_id']}")) {
                 $data['slug'] = $parent['slug'] . '-' . $data['slug'];
             }
         }
     }, 'PUT, POST' => function ($event) {
         $data =& $event['data'];
         // Set parent id?
         if ($data['parent']) {
             $parent = get("/categories/{$data['parent']}");
             $data['parent_id'] = $parent['id'] ?: $data['parent_id'];
             unset($data['parent']);
         }
     });
 }
Example #6
0
 /**
  * Model definition.
  */
 function define()
 {
     // Fields.
     $this->fields = array('id', 'slug', 'name', 'channel_id', 'date_created', 'date_updated', 'channel' => function ($entry) {
         return get("/channels/{$entry['channel_id']}");
     }, 'meta' => function ($entry, $model) {
         return $model->get_meta($entry);
     });
     $this->search_fields = array('slug', 'name', 'title', 'content');
     // Indexes.
     $this->indexes = array('id' => 'unique', 'slug, channel_id' => 'unique');
     // Validate.
     $this->validate = array('required' => array('slug', 'channel_id'), 'unique' => array('slug'));
     // Query defaults.
     $this->query = array('order' => 'date_created DESC', 'limit' => 100);
     // Event binds.
     $this->binds = array('GET' => function ($event) {
         $data =& $event['data'];
         // Get by channel ID or slug.
         if ($data['channel']) {
             if ($channel = get("/channels/{$data['channel']}")) {
                 $data['channel_id'] = $channel['id'];
                 unset($data['channel']);
             } else {
                 // Channel not found.
                 return false;
             }
         }
     }, 'POST' => function ($event) {
         $data =& $event['data'];
         // Default slug.
         if (empty($data['slug'])) {
             if (isset($data['slug']) && ($name = $data['name'] ?: $data['title'])) {
                 $data['slug'] = hyphenate($name);
             } else {
                 $data['slug'] = $data['id'] ?: md5(microtime());
             }
         }
     }, 'PUT' => function ($event, $model) {
         $data =& $event['data'];
         // Version this entry?
         if ($data[':version'] && ($entry = get("/entries/{$event['id']}"))) {
             if ($data[':version'] != $entry['date_updated']) {
                 $ver_by = $data['version_by'];
                 $conflicts = $entry['version_conflicts'];
                 // Check diff with channel fields.
                 foreach ((array) $entry['channel']['fields'] as $field) {
                     $field_id = $field['id'];
                     if (isset($data[$field_id]) && isset($entry[$field_id]) && $data[$field_id] != $entry[$field_id]) {
                         $conflicts[$ver_by][$field_id]['yours'] = $data[$field_id];
                         $conflicts[$ver_by][$field_id]['theirs'] = $entry[$field_id];
                     }
                 }
                 // Used to resolve stacking conflicts.
                 $conflicts[$ver_by]['version_by']['theirs'] = $entry['version_by'];
                 $conflicts[$ver_by]['date_updated']['theirs'] = $entry['date_updated'];
                 if ($conflicts) {
                     put($entry, array('version_conflicts' => $conflicts, 'date_updated' => $entry['date_updated']));
                     return false;
                 }
             }
         }
     }, 'PUT, POST' => function ($event) {
         $data =& $event['data'];
         // Set entry channel?
         if (isset($data['channel'])) {
             $channel = get("/channels/{$data['channel']}");
             $data['channel_id'] = $channel['id'];
             unset($data['channel']);
         }
     });
 }
Example #7
0
 /**
  * Delete an image file from storage.
  */
 function delete_image($id, $name)
 {
     $name = hyphenate($name);
     $image_file = $config->app['public_path'] . image(array('id' => $id, 'name' => $name, 'type' => strtolower($this->name)));
     if (is_file($image_file)) {
         return unlink($image_file);
     }
 }
Example #8
0
 /**
  * Model definition.
  */
 function define()
 {
     // Fields.
     $this->fields = array('id', 'slug', 'name', 'price', 'cost', 'sku', 'weight', 'description', 'category_ids', 'variants', 'items', 'stock', 'images', 'is_bundle', 'is_active', 'date_created', 'date_updated', 'items' => function ($product) {
         return get("/products", array(':with' => orderby($product['items'], 'sort')));
     }, 'weight' => function ($product) {
         return Products::get_weight($product);
     }, 'categories' => function ($product) {
         return get("/categories", array('id' => array('$in' => (array) $product['category_ids'])));
     });
     // Search fields.
     $this->search_fields = array('name', 'sku', 'description');
     // Indexes.
     $this->indexes = array('id' => 'unique', 'slug' => 'unique', 'sku');
     // Validate.
     $this->validate = array('required' => array('slug', 'name', 'price'), 'unique' => array('slug', 'sku'), ':items' => array('required' => array('id', 'quantity')));
     // Event binds.
     $this->binds = array('GET' => function ($event) {
         $data =& $event['data'];
         // Get by category ID or slug.
         if ($data['category']) {
             if ($category = get("/categories/{$data['category']}")) {
                 $data['category_ids'] = $category['id'];
                 unset($data['category']);
             } else {
                 // Category not found.
                 return false;
             }
         }
         // Default category sort?
         if (!$data['order'] && is_numeric($data['category_ids'])) {
             $data['order'] = "category_sort.{$data['category_ids']}";
         }
         if ($data['pricing']) {
             // Avoid conflicts with actual pricing field.
             $data[':pricing'] = $data['pricing'];
             unset($data['pricing']);
         }
     }, 'POST' => function ($event) {
         $data =& $event['data'];
         // Auto slug?
         if ($data['name'] && !$data['slug']) {
             $data['slug'] = hyphenate($data['name']);
         }
     }, 'POST, PUT' => function ($event, $model) {
         $data =& $event['data'];
         // Set product categories?
         if (isset($data['categories'])) {
             foreach ((array) $data['categories'] as $category_id) {
                 if (!is_numeric($category_id)) {
                     $category = get("/categories/{$category_id}");
                     $category_id = $category['id'];
                 }
                 if ($category_id) {
                     $data['category_ids'][] = $category_id;
                 }
             }
             unset($data['categories']);
         }
         // Updating category ids?
         if (is_array($data['category_ids'])) {
             sort($data['category_ids']);
         }
     }, 'POST.variants' => function ($event) {
         $data =& $event['data'];
         // Unset default override keys
         if (!$data['sku']) {
             unset($data['sku']);
         }
         if (!$data['price']) {
             unset($data['price']);
         }
     }, 'after:GET' => function ($result, $event) {
         $data =& $event['data'];
         if ($result && $data[':pricing']) {
             // Handle special pricing.
             $result = Products::get_pricing($result, $data[':pricing']);
         }
     });
 }