public static function get_fieid_attribute(Validation $val, $name, $default_value = null, $is_textarea = false, $optional_attr = array()) { $field = $val->fieldset()->field($name); $label = ''; $input_attr = array(); $is_required = false; if (is_callable(array($field, 'get_attribute'))) { $input_attr = $field->get_attribute(); $input_attr = Arr::filter_keys($input_attr, array('validation', 'label'), true); if ((is_null($default_value) || empty($default_value) && !strlen($default_value)) && !is_null($field->get_attribute('value'))) { $default_value = $field->get_attribute('value'); } $is_required = $field->get_attribute('required') == 'required'; $label = $field->get_attribute('label'); } if (!is_array($optional_attr)) { $optional_attr = (array) $optional_attr; } if ($optional_attr) { $input_attr += $optional_attr; } if (empty($input_attr['id'])) { $input_attr['id'] = Site_Form::get_field_id($name); } if (empty($input_attr['class'])) { $input_attr['class'] = 'form-control'; } return array($default_value, $label, $is_required, $input_attr); }
public function execute($job, $data) { // Trigger price update/change event and modify data $data = \Event::instance('supplier')->trigger('update', $data, 'array'); $data = call_user_func_array('\\Arr::merge_assoc', $data); // This is all about update $data = $data[0]; // Do not update some fields $set = \Arr::filter_keys($data, array('external_id', 'supplier_id'), true); $data['updated_at'] = time(); return Model_Price::query()->set($set)->where('external_id', $data['external_id'])->where('supplier_id', $data['supplier_id'])->update(); }
/** * Finds every image field in the DB and re-builds the stored array info for each */ public function action_reset_images() { try { set_time_limit(0); ini_set('memory_limit', '512M'); } catch (\Exception $e) { // Nothing! } $em = \D::manager(); $driver = $em->getConfiguration()->getMetadataDriverImpl(); $tables_fields = array(); $sql = array(); // Loop through all the model metadata and check for image fields foreach ($driver->getAllClassNames() as $class) { $metadata = $em->getClassMetadata($class); $fields = $metadata->fieldMappings; $image_fields = array(); foreach ($fields as $field_name => $field) { if ($field['type'] == 'image') { $image_fields[] = $field_name; } } if (count($image_fields) === 0) { continue; } $items = $class::select('item')->getQuery()->getResult(); foreach ($items as $num => $item) { $item->set('updated_at', new \Datetime()); $data = array(); foreach ($image_fields as $image_field) { $image_value = $item->{$image_field}; if (is_array($image_value)) { $data[$image_field] = \Arr::filter_keys($image_value, array('src', 'width', 'height', 'alt')); } } $item->populate($data); \D::manager()->persist($item); } } \D::manager()->flush(); $this->heading = \Lang::get('admin.messages.reset_images_success'); $this->template = 'admin/generic.twig'; }
public function action_edit($id = null) { $model = $this->find($id); $form = $this->form(); if (\Input::method() == 'POST') { $post = \Input::post(); $validator = $this->validation(); $result = $validator->run($post); if ($result->isValid()) { $data = \Arr::filter_keys($post, $result->getValidated()); $categories = $data['category_id']; unset($data['category_id']); $model->categories = []; foreach ($categories as $category) { $model->categories[] = Model_Category::find($category); } $model->set($data)->save(); $context = array('template' => 'success', 'from' => '%item%', 'to' => $this->get_name()); \Logger::instance('alert')->info(gettext('%item% successfully created.'), $context); return $this->redirect($this->url); } else { $form->repopulate(); $context = array('errors' => $result->getErrors()); \Logger::instance('alert')->error(gettext('There were some errors.'), $context); } } else { $data = $model->to_array(); $data['category_id'] = array_keys($model->categories); $form->populate($data); } $this->set_title(strtr(gettext('Edit %item%'), ['%item%' => $this->get_name()])); $this->template->content = $this->view('admin/skeleton/edit'); $this->template->content->set('model', $model, false); $this->template->content->set('form', $form, false); isset($errors) and $this->template->content->set('errors', $errors, false); }
/** * Saves the object to the database by either creating a new record * or updating an existing record. Sets the default values if set. * * @param bool $validate wether to validate the input * @return mixed Rows affected and or insert ID */ public function save($validate = true) { if ($this->frozen()) { throw new \Exception('Cannot modify a frozen row.'); } $vars = $this->to_array(); // Set default if there are any isset(static::$_defaults) and $vars = $vars + static::$_defaults; if ($validate and isset(static::$_rules) and !empty(static::$_rules)) { $vars = $this->pre_validate($vars); $validated = $this->post_validate($this->run_validation($vars)); if ($validated) { $validated = array_filter($this->validation()->validated(), function ($val) { return $val !== null; }); $vars = $validated + $vars; } else { return false; } } $vars = $this->prep_values($vars); if (isset(static::$_properties)) { $vars = \Arr::filter_keys($vars, static::$_properties); } if (isset(static::$_updated_at)) { if (isset(static::$_mysql_timestamp) and static::$_mysql_timestamp === true) { $vars[static::$_updated_at] = \Date::forge()->format('mysql'); } else { $vars[static::$_updated_at] = \Date::forge()->get_timestamp(); } } if ($this->is_new()) { if (isset(static::$_created_at)) { if (isset(static::$_mysql_timestamp) and static::$_mysql_timestamp === true) { $vars[static::$_created_at] = \Date::forge()->format('mysql'); } else { $vars[static::$_created_at] = \Date::forge()->get_timestamp(); } } $query = \DB::insert(static::$_table_name)->set($vars); $this->pre_save($query); $result = $query->execute(static::get_connection(true)); if ($result[1] > 0) { // workaround for PDO connections not returning the insert_id if ($result[0] === false and isset($vars[static::primary_key()])) { $result[0] = $vars[static::primary_key()]; } $this->set($vars); empty($result[0]) or $this->{static::primary_key()} = $result[0]; $this->is_new(false); } return $this->post_save($result); } $query = \DB::update(static::$_table_name)->set($vars)->where(static::primary_key(), '=', $this->{static::primary_key()}); $this->pre_update($query); $result = $query->execute(static::get_connection(true)); $result > 0 and $this->set($vars); return $this->post_update($result); }
/** * Tests Arr::filter_keys() * * @test */ public function test_filter_keys() { $data = array('epic' => 'win', 'weak' => 'sauce', 'foo' => 'bar'); $expected = array('epic' => 'win', 'foo' => 'bar'); $expected_remove = array('weak' => 'sauce'); $keys = array('epic', 'foo'); $this->assertEquals(Arr::filter_keys($data, $keys), $expected); $this->assertEquals(Arr::filter_keys($data, $keys, true), $expected_remove); }
/** * {@inheritdoc} */ protected function _send() { $mandrill = new \Mandrill($this->config['mandrill']['key']); $message = new Mandrill_Messages($mandrill); $headers = $this->extra_headers; // Get recipients $to = $this->build_rcpt(); $cc = $this->build_rcpt('cc'); $bcc = $this->build_rcpt('bcc'); $to = array_merge($bcc, $cc, $to); // Get recipient merge vars $merge_vars = array(); foreach ($this->rcpt_merge_vars as $rcpt => $_merge_vars) { $merge_vars[] = array('rcpt' => $rcpt, 'vars' => \Arr::keyval_to_assoc($_merge_vars, 'name', 'content')); } // Get recipient meta data $metadata = array(); foreach ($this->rcpt_metadata as $rcpt => $_metadata) { $metadata[] = array('rcpt' => $rcpt, 'values' => $_metadata); } // Get attachments $attachments = array(); foreach ($this->attachments['attachment'] as $cid => $attachment) { $attachments[] = array('type' => $attachment['mime'], 'name' => $attachment['file'][1], 'content' => $attachment['contents']); } // Get inline images $images = array(); foreach ($this->attachments['inline'] as $cid => $attachment) { if (\Str::starts_with($attachment['mime'], 'image/')) { $name = substr($cid, 4); // remove cid: $images[] = array('type' => $attachment['mime'], 'name' => $name, 'content' => $attachment['contents']); } } // Get reply-to addresses if (!empty($this->reply_to)) { $headers['Reply-To'] = static::format_addresses($this->reply_to); } $important = false; if (in_array($this->config['priority'], array(\Email::P_HIGH, \Email::P_HIGHEST))) { $important = true; } $message_data = array('html' => $this->body, 'text' => isset($this->alt_body) ? $this->alt_body : '', 'subject' => $this->subject, 'from_email' => $this->config['from']['email'], 'from_name' => $this->config['from']['name'], 'to' => $to, 'headers' => $headers, 'global_merge_vars' => \Arr::keyval_to_assoc($this->merge_vars, 'name', 'content'), 'merge_vars' => $merge_vars, 'metadata' => $this->metadata, 'recipient_metadata' => $metadata, 'attachments' => $attachments, 'images' => $images, 'important' => $important); $message_options = \Arr::filter_keys($this->get_config('mandrill.message_options', array()), array_keys($message_data), true); $message_data = \Arr::merge($message_data, $message_options); $send_options = extract($this->config['mandrill']['send_options'], EXTR_SKIP); $message->send($message_data, $async, $ip_pool, $send_at); return true; }
protected function prep_props(array $props) { // Skip tree fields and primary keys $skip_fields = \Arr::merge(Model_Menu::primary_key(), Model_Menu::tree_config(), array('children')); \Arr::delete($skip_fields, array('read-only', 'title_field')); // Model properties $properties = \Arr::filter_keys($props, array_keys(Model_Menu::properties())); $properties = \Arr::filter_keys($properties, $skip_fields, true); //Model 'fields' property fields $fields = \Arr::filter_keys($props, array_keys($properties), true); $fields = \Arr::filter_keys($fields, $skip_fields, true); is_array($properties['fields']) or $properties['fields'] = array(); $properties['fields'] = \Arr::merge($properties['fields'], $fields); return $properties; }
/** * Update existing products, insert new ones * * @param boolean $cached Update from already downloaded files * @param boolean $force Force update of all values * @return boolean All products have been processed */ public function update($cached = false, $force = false) { // Get data from supplier if (!($products = $this->_update($cached))) { return false; } $count = array(0, 0, 0, 0, 0); $available = array(); // Get current prices from supplier $price = \DB::select('external_id', 'price', 'available')->from(Model_Price::table())->where('supplier_id', $this->model->id)->execute()->as_array('external_id'); // Cast values to the appropriate data type array_walk($price, function (&$product, $id) use(&$price) { $product['price'] = \Num::currency($product['price']); $product['available'] = intval($product['available']); unset($price[$id]['external_id']); }); foreach ($products as $id => $product) { // Default data casting and values $product['price'] = \Arr::get($product, 'price'); if (is_null($product['price'])) { continue; } else { $product['price'] = \Num::currency($product['price']); } $product['available'] = intval(\Arr::get($product, 'available', 1)); // Check if product already exists: update it if yes and insert if not if (array_key_exists($id, $price)) { // Check if product's price has been changed, or just became (un)available if ($product['price'] !== $price[$id]['price'] or $force === true) { // Method for updating meta fields as well $fields = $this->get_config('update.fields', array()); $fields = \Arr::merge($fields, array('price', 'available')); // Foolproofness: set the update array manually $product = \Arr::filter_keys($product, $fields); \Arr::set($product, array('external_id' => $id, 'supplier_id' => $this->model->id)); // Get job and queue $job = $this->get_config('update.job', 'Indigo\\Erp\\Stock\\Job_Supplier_Update'); $queue = $this->get_config('update.queue', 'update'); // Use Queue if available (greater performance) if (\Package::loaded('queue')) { $count[0] += \Queue::push($queue, $job, array($product, $price[$id])) ? 1 : 0; } else { try { $job = new $job(); $count[0] += $job->execute(null, $product); } catch (\Exception $e) { } } } elseif ($product['available'] !== $price[$id]['available']) { $available[$product['available']][] = $id; } else { $count[4]++; } } else { // Foolproofness: removing some fields from insert data $product = \Arr::filter_keys($product, array('id', 'product_id'), true); \Arr::set($product, array('external_id' => $id, 'supplier_id' => $this->model->id)); // Get job and queue $job = $this->get_config('insert.job', 'Indigo\\Erp\\Stock\\Job_Supplier_Insert'); $queue = $this->get_config('insert.queue', 'update'); // Use Queue if available (greater performance) if (\Package::loaded('queue')) { $count[3] += \Queue::push($queue, $job, $product) ? 1 : 0; } else { try { $job = new $job(); $count[3] += $job->execute(null, $product); } catch (\Exception $e) { } } } // Remove processed product from list unset($price[$id]); } // Already unavailable products should not be updated $price = array_filter($price, function ($item) use(&$count) { if ($item['available'] !== 0) { return true; } else { // This product is not updated, so increase this counter $count[4]++; return false; } }); // Unprocessed products are treated as unavailable as we processed the whole stock $available[0] = \Arr::merge(\Arr::get($available, 0, array()), array_keys($price)); // Update availability information $available = $this->_available($available); $count[1] = $available[0]; $count[2] = $available[1]; // Set the last updated time for supplier prices $this->model->set('last_update', time())->save(); // Log success \Log::info($this->model->name . ' frissítve: ' . $count[0] . ' frissítés, ' . $count[1] . ' lett elérhetetlen, ' . $count[2] . ' lett elérhető, ' . $count[3] . ' felvéve, ' . $count[4] . ' változatlan.'); // All products have been processed return array_sum($count) == count($products); }
/** * Reset option(s) - Set default options, defined in APPPATH/config/populu.php * * @param mixed Name(s) of option(s) or null to reset all options * @return void */ public static function reset($names = null) { Config::load('populu', true); $default = Config::get('populu.default_options'); $names = (array) $names; empty($names) or $default = Arr::filter_keys($default, $names); static::set($default, null, true); }
/** * Gets a list of translatable fields for a class */ public static function getTranslatable($class) { return \Arr::filter_keys(\Arr::get(static::$translatable, $class, array()), $class::excludeTranslations(), true); }