Skip to content
Jeff Johns edited this page Feb 4, 2014 · 11 revisions
Model Extends Table Path
Labels_Model Plain_Model labels /application/models/labels_model.php

Loading the model

$this->load->model('labels_model', 'labels');

Properties

Property Visibility Default Value Description
$sort Public created_on ASC The default sort direction for reads.

Methods

__construct - Public

Called automatically which in turn calls the parent constructor and sets the model $data_types properties.


create - Public

Used to create new records in the labels table. There are two types of labels, smart and label. The latter is a system-level only, normal label. The smart label can be a default system-level smart label or a user defined system-label.

Arguments (Overall)

Variable Type Default Required Description
$options array array() Yes An associative array that contains the column names and the values for the record to be created. Array keys are the column names for each value.

Arguments (System-Level Normal Label)

If creating a system-level normal label below are the fields that can be submitted.

Variable Type Default Required Description
$options.name String N/A Yes The name of the label.
$options.slug String N/A Yes The unique slug used for lookups. To easily create this use the [data helper method of `generateSlug`](Helpers-Data.md).
$options.active Number 1 No Set to 1 for active, or 0 for inactive.

Arguments (Smart Labels)

If creating a system-level or user-defined smart labels below are the fields that can be submitted.

Variable Type Default Required Description
$options.smart_label_id String N/A Yes The `labels.label_id` of the normal label to apply when this rule is matched.
$options.domain String N/A Yes The FQDN (IE: google.com)
$options.path String Null No The path to match with the domain.
$options.smart_key MD5 N/A Yes The MD5 hash of the domain + path used for easy lookups. To easily create this use the [data helper method of `getSmartLabelInfo`](Helpers-Data.md).
$options.active Number 1 No Set to 1 for active, or 0 for inactive.
$options.user_id Number Null No Null for system-level smart label or a valid `users.user_id` for ownership.

Example (Normal Labels)

$name = 'Read';
$slug = generateSlug($name);

// Attempt to create normal label
$this->load->model('labels_model', 'labels');
$label = $this->labels->create(array('name' => $name, 'slug' => $slug));

// If label was created
if (isset($label->label_id)) {
    // Good to go
}

// Some sort of validation error
elseif (isset($label['errors']) {
    // Validation errors were found
}

// Some sort of database write error, check logs
else {
    // will return false
}

Example (Smart Labels - User Defined)

$url        = 'http://amazon.com/gp/product';
$smart_info = getSmartLabelInfo($url);

// Attempt to create the smart label
$this->load->model('labels_model', 'labels');
$label = $this->labels->create(array(
    'domain'    => $smart_info['domain'],
    'path'      => $smart_info['path'],
    'smart_key' => $smart_info['key'],
    'user_id'   => $this->user_id
));

// If label was created
if (isset($label->label_id)) {
    // Good to go
}

Example (Smart Labels - System Defined)

$url        = 'http://youtube.com/watch';
$smart_info = getSmartLabelInfo($url);

// Attempt to create the smart label
// Do NOT pass the user_id
$this->load->model('labels_model', 'labels');
$label = $this->labels->create(array(
    'domain'    => $smart_info['domain'],
    'path'      => $smart_info['path'],
    'smart_key' => $smart_info['key']
));

// If label was created
if (isset($label->label_id)) {
    // Good to go
}

formatResults - Private

Since the labels table actually stores 3 different types of labels, we need to format the data a bit before it is returned. By formatting each row, it will set the type (smart or label) and if smart add some definitions for settings (smart_label_name, smart_label_slug and smart_label_id), each of those are inherited from the parent (normal label) the smart label applies when the rule is matched.

Arguments

Variable Type Default Required Description
$labels object N/A Yes The complete list of labels to be formatted and returned.

Example

$labels = $this->formatResults($labels);

readComplete - Public

Used to read all label data and return it in a nicely formatted structure. You can always use $this->labels->read to get the raw data from the table anytime you wish.

Arguments

Variable Type Default Required Description
$where mixed N/A Yes If numeric the where clause will be filled in with the id submitted and the correct column, otherwise it will just use the where clause you submitted.
$limit integer 1 No The max records to return.
$page integer 1 No The current page of records to return. Used as an offset in the LIMIT statement.
$start integer null No The start position to return records from.

Example

// Return all info for labels.label_id = 15
// info will be accessible like $label->label_id since it's a single item
$this->load->model('labels_model', 'labels');
$label = $this->labels->readComplete(15);
if (! isset($label->label_id)) {
   // Good to go
}
else {
    // Handle your errors
}

// Return all normal labels
$this->load->model('labels_model', 'labels');
$labels = $this->labels->readComplete("labels.user_id IS NULL AND labels.smart_key IS NULL");
if ($labels !== false) {
    foreach ($labels as $label) {
      // Doowutchalike
    }
}
else {
    // Handle your errors
}

// Return 5 system smart labels
$this->load->model('labels_model', 'labels');
$labels = $this->labels->readComplete("labels.user_id IS NULL AND labels.smart_key IS NOT NULL", 5);
if ($labels !== false) {
    foreach ($labels as $label) {
      // Doowutchalike
    }
}
else {
    // Handle your errors
}

// Return all user defined system smart labels
$this->load->model('labels_model', 'labels');
$labels = $this->labels->readComplete("labels.user_id='56' AND labels.smart_key IS NOT NULL");
if ($labels !== false) {
    foreach ($labels as $label) {
      // Doowutchalike
    }
}
else {
    // Handle your errors
}
Clone this wiki locally