-
Notifications
You must be signed in to change notification settings - Fork 193
Model Labels
Model | Extends | Path |
---|---|---|
Labels_Model | Plain_Model | /application/models/labels_model.php |
This model controls the I/O of data for the labels
table in the database. It does all your reading and writing to and from the database.
Before you can use the model, you must load the model into memory. The second argument below is the object variable name you will use to access the model. It can be whatever you'd like. We keep them as the something descriptive.
$this->load->model('labels_model', 'labels');
Property | Visibility | Default Value | Description |
---|---|---|---|
$sort | Public | string | The default sort direction for reads. |
Called automatically which in turn calls the parent constructor and sets the model $data_types
properties.
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.
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. |
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`](https://github.com/plainmade/nilai/wiki/Helpers-Data.md). |
$options.active | Number | 1 | No | Set to 1 for active, or 0 for inactive. |
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`](https://github.com/plainmade/nilai/wiki/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. |
$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
}
$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
}
$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
}
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.
Variable | Type | Default | Required | Description |
---|---|---|---|---|
$labels | object | N/A | Yes | The complete list of labels to be formatted and returned. |
$labels = $this->formatResults($labels);
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.
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. |
$page | integer | null | No | The start position to return records from. |
// 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
}