public function load_by_file_key($key) { $this->initialise(); /* Make sure name is set */ if (isset($key)) { /* We need to check this table has a name field */ $fields = array_keys($this->_data); if (in_array('file_key', $fields)) { $sql = $this->data_source->sql; $sql->select('*')->from($this->table_name); /* Do we have a date_deleted field? */ if (in_array('date_deleted', $fields)) { $name_condition = new sql_cond('file_key', sql::EQUALS, sql::q($key)); $date_deleted_condition = new sql_cond('date_deleted', sql::IS, new sql_null()); $sql->where(new sql_and($name_condition, $date_deleted_condition)); } else { $sql->where(new sql_cond('file_key', sql::EQUALS, sql::q($key))); } /* Get the results */ $results = $sql->execute()->results(); if (count($results) == 1) { $this->trigger(self::EVENT_ON_LOAD_BY_FILE_KEY); return $this->load_by_data($results[0]); } elseif (count($results) == 0) { $this->error("Unable to find a record with key {$key}"); } elseif (count($results) > 1) { $this->error(count($results) . " records found with key '{$key}'."); } } else { $this->error('Unable to load by file_key, this table has no \'file_key\' field.'); } } else { $this->error('Unable to load by file_key, no file_key supplied'); } return false; }
public function load_by_image_id_and_actions($image_id, $actions) { $this->initialise(); if (is_assoc($actions)) { $sql = $this->data_source->sql; $sql->select('*')->from($this->table_name); $where = new sql_and(new sql_cond('image_id', sql::EQUALS, sql::q($image_id)), new sql_cond('date_deleted', sql::IS, new sql_null())); $key_pairs = ['action_resized_to_height' => 'No', 'action_resized_to_width' => 'No', 'action_resized' => 'No', 'action_scaled' => 'No', 'action_gaussian_blur' => 'No', 'action_cropped' => 'No', 'action_cropped_from_center' => 'No', 'action_squared' => 'No']; if (isset($actions['actions'])) { $actions['actions'] = explode(",", $actions['actions']); foreach ($actions['actions'] as $action) { switch ($action) { case "resize_to_height": $key_pairs['action_resized_to_height'] = 'Yes'; $key_pairs['height'] = $actions['height']; break; case "resize_to_width": $key_pairs['action_resized_to_width'] = 'Yes'; $key_pairs['width'] = $actions['width']; break; case "resize": $key_pairs['action_resized'] = 'Yes'; $key_pairs['height'] = $actions['height']; $key_pairs['width'] = $actions['width']; break; case "scale": $key_pairs['action_scaled'] = 'Yes'; $key_pairs['scale'] = $actions['scale']; break; case "gaussian_blur": $key_pairs['action_gaussian_blur'] = 'Yes'; break; case "crop": $key_pairs['action_cropped'] = 'Yes'; $key_pairs['height'] = $actions['height']; $key_pairs['width'] = $actions['width']; $key_pairs['start_x'] = $actions['x']; $key_pairs['start_y'] = $actions['y']; break; case "crop_from_center": $key_pairs['action_cropped_from_center'] = 'Yes'; $key_pairs['height'] = $actions['height']; $key_pairs['width'] = $actions['width']; break; case "square": $key_pairs['action_squared'] = 'Yes'; $key_pairs['size'] = $actions['size']; break; } } } foreach ($key_pairs as $key => $value) { if ($value) { $where->add(new sql_cond($key, sql::EQUALS, sql::q($value))); } } $sql->where($where); $results = $sql->execute()->results(); if (count($results)) { return $this->load_by_data($results[0]); } } else { $this->error('No actions provided'); } return false; }