Insert Query in Drupal 7 - drupal

$table_name = 'tbl_users';
$data = array('datetime'=>'NOW()',
'ipadress' => $ipaddress,
'name' => $name,
'dob' => $dob,
'nationality' => $nationality,
'address' => $address,
'city' => $city,
'state' => $state,
'pincode' => $pincode,
'phone' => $phone,
'email' => $email,
'mobile' => $mobile,
'weight' => $weight,
'height'=> $height,
'marital' => $marital,
'degree' => $degree,
'institute' => $institute,
'special' => $special,
'yearofpaas' => $yearofpaas,
'grade' => $grade,
'emplyment_history' => $emplyment_history,
'merits' => $merits,
'major_achivements' => $major_achivements,
'interview_attended' => $interview_attended,
'details' => $details,
'minctc_position' => $minctc_position,
'cv_file' => $cv_file,
'declaration' => $declaration);
echo "<pre>";
print_r($data);
drupal_write_record($table_name, $data);
I have this insert query but somehow records are not going in to the table..can anyone please help me...whats the problem in the query ?????

You're passing in NOW() as a string, which won't work. Try instead:
$data = array('timestamp' => date("Y-m-d H:i:s"),

drupal_write_record only works if table schema is present in my_module.install file.
From here: drupal_write_record saves (inserts or updates) a record to the database based upon the schema.
So, make sure that schema of tbl_users table is present.
Also, as mentioned by Dan U. use date("Y-m-d H:i:s") instead of 'Now()'.

The dblog can give you a good idea about the problem in your query. The path is admin/reports/dblog.

Related

Comparing arrays in WP_Query with custom fields

I'm having an issue with comparing values between two arrays in wp_query
I have the two roles administrator and forhandler_salg ($roles returns an array with these two values), but it only queries posts with the "public" key set to true
I got more posts when I changed the "allowed_userroles" key compare value to "LIKE", but those aren't the right posts
$roles = "";
if (is_user_logged_in()) {
$user = wp_get_current_user();
$roles = (array) $user->roles;
}
$args = array(
'post_type' => 'downloads',
'posts_per_page' => '-1',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'public',
'value' => true,
'compare' => '=',
),
# Not working
array(
'key' => 'allowed_userroles',
'value' => $roles,
'compare' => 'IN',
),
),
);
print_r($args) returns
Array
(
[post_type] => downloads
[posts_per_page] => -1
[meta_query] => Array
(
[relation] => OR
[0] => Array
(
[key] => public
[value] => 1
[compare] => =
)
[1] => Array
(
[key] => allowed_userroles
[value] => Array
(
[16] => administrator
[17] => forhandler_salg
)
[compare] => IN
)
)
)
And a post with the "public" custom field as true and some added roles returns this, when I print_r() the allowed_userroles for the post:
Array
(
[0] => role1
[1] => role2
[2] => role3
)
How would I go about checking if allowed_userroles in the query has values that are also present in $roles?
EDIT: "like" works if I put in a role manually
Ok, for these kinds of problems, it's easier to start with your basic array of arguments and then modify it.
Since this question is tagged with ACF, ACF is likely storing this as a single meta entry. Furthermore, if this is an array of values, ACF is very likely storing it as a serialized PHP string in the database (e.g., a:2:{i:0;s:4:"1234";i:1;s:3:"qed";}).
Let's start with the base code:
$roles = [];
if (is_user_logged_in()) {
$user = wp_get_current_user();
$roles = (array) $user->roles;
}
$args = array(
'post_type' => 'downloads',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'public',
'value' => "1",
'compare' => '=',
),
),
);
In this case, you'll need to use a LIKE to attempt a sub-string match.
foreach($roles as $role) {
$args['meta_query'][] = [
'key' => 'allowed_userroles',
'value' => $role, // can't remember if wp will wrap value with %
'compare' => 'LIKE',
];
}
Ideally, you'd want to match based on the string length as well, but that might be a bit prohibitive. If you keep running into issues getting the exact results out, you could run two separate queries. The first query would be for all public items, and the second query would be for non-public items. You could then take the results of the second query and then filter out values that do not have the correct meta, since it'll be easier to process using get_field('allowed_userroles').

D6: drupal_render in form causes various problems (default value, ID, date_select)

I have a problem with drupal_render (assuming that drupal_render is the right way for me to get what I want - feel free to correct me =).
I am building a form. Since the FAPI does not provide a "table"-field, I want to make one myself. My approach: use the theme()-function, specifically theme('table', ...) or theme_table(), and fill it with the respective form fields (with the intention of adding AHAH functionality later on). This forces me to use drupal_render as the value for the table cells, which causes some problems with the form elements.
The table collects numbers of employees by year, for the organisation the user is editing at this moment. The code looks as follows:
$form['employees'] = array(
'#type' => 'fieldset',
'#title' => t('Employees'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$employee_query = db_query("SELECT * FROM {employees} WHERE id_organisation = %d", $org['idoOrganisation']);
$employee = array();
while ($row = db_fetch_array($employee_query)) {
$employee[] = $row;
}
$header = array(
t('Year'),
t('Total'),
t('Internal'),
t('External'),
t('Aerospace')
);
$em_delta = 0;
$rows = array();
foreach($employee as $em_delta => $value) {
$form['employees'][$em_delta]['year'] = array(
'#title' => '',
'#type' => 'date_select', // Comes with the date module
'#date_format' => $format_year,
'#date_label_position' => 'within',
'#date_year_range' => '-50:+3',
'#default_value' => $value[$em_delta]['year'],
'#id' => 'edit-employees-' . $em_delta . '-year', // Allready a quickfix, since the form is rendered without id
'#name' => 'employees['.$em_delta.'][year]', // Same here
);
$form['employees'][$em_delta]['total'] = array(
'#type' => 'textfield',
'#title' => '',
'#default_value' => $value['total'],,
'#size' => 1,
'#id' => 'edit-employees-' . $em_delta . '-total',
'#name' => 'employees['.$em_delta.'][total]'
);
$form['employees'][$em_delta]['internal'] = array(
'#type' => 'textfield',
'#title' => '',
'#default_value' => $value[$em_delta]['internal'],
'#size' => 1,
'#id' => 'edit-employees-' . $em_delta . '-internal',
'#name' => 'employees['.$em_delta.'][internal]',
);
$form['employees'][$em_delta]['external'] = array(
'#type' => 'textfield',
'#title' => '',
'#default_value' => $value[$em_delta]['external'],
'#size' => 1,
'#id' => 'edit-employees-' . $em_delta . '-external',
'#name' => 'employees['.$em_delta.'][external]',
);
$form['employees'][$em_delta]['aero'] = array(
'#type' => 'textfield',
'#title' => '',
'#default_value' => $value[$em_delta]['aero'],
'#size' => 1,
'#id' => 'edit-employees-' . $em_delta . '-aero',
'#name' => 'employees['.$em_delta.'][aero]',
);
$rows[] = array(
drupal_render($form['employees'][$em_delta]['year']),
drupal_render($form['employees'][$em_delta]['total']),
drupal_render($form['employees'][$em_delta]['internal']),
drupal_render($form['employees'][$em_delta]['external']),
drupal_render($form['employees'][$em_delta]['aero']),
);
}
$form['employees']['table'] = array (
'#value' => theme('table', $header, $rows, array(), NULL)
);
Here are the problems I am encountering:
ID- and Name-Attributes of the form elements are empty. I found something on this on the drupal site and have made my peace with it (although I don't understand it), setting those attributes manually now.
Default-values of the text fields are ignored. The fields are empty. When I let drupal_get_form render the field, the default_value shows. Someone around here suggested to set the #value-property instead, but then again I read that this is something completly different and may cause problems.
The date_select-Field is not rendered in it's entirety. The wrappers are there, the select field however appears outside of the code, just before the table (i.e. where it appears in the code).
Let's hope that's it =)
Can anybody help? What am I doing wrong?
A colleague of mine pointed out that using drupal_render within the form function is not event remotely close to being a good idea, as it removes part of the form from the whole process of validating and submitting.
Thus, figuring out why the function does not work as intended is futile. The better approach would be to simply generate the necessary amount of form fields, let them be rendered as they are within drupal_get_form(), and use the forms theme-function later on to put them into a table.
Stupid me =)

Magento - How to add Ship to information to order grid in Magento 1.5.1.0

I found a great way to put the customer email address on the Magento Admin Sales Order grid via this Q&A (http://stackoverflow.com/questions/6416864/how-to-add-customer-email-to-order-grid-in-magento-1-4/6906254#6906254) by Ben Incani and it works great.
My question is: Using this method, how can I add the Ship to information (name, address, city, state, zip)?
I've tried doing two versions (that sort of work) but do not work fully so I am a bit stuck...
This is the code that works for customer emails:
$collection->getSelect()->joinLeft(array('sfo'=>'sales_flat_order'),'sfo.entity_id=main_table.entity_id',array('sfo.customer_email', 'sfo.shipping_description'));
Now, when trying to go into the database table that has the Ship to information I tried this:
$collection->getSelect()->joinLeft(array('sfoa'=>'sales_flat_order_address'),'sfoa.parent_id=sfo.entity_id',array('sfoa.postcode'));
This returns an error log with the message:
a:5:{i:0;s:68:"Item (Mage_Sales_Model_Order) with the same id "10860" already exist";i:1;s:5104:"#0
Trying this code (which most closely follows the original customer email code):
$collection->getSelect()->joinLeft(array('sfoa'=>'sales_flat_order_address'),'sfoa.entity_id=main_table.entity_id',array('sfoa.postcode'));
yields a grid that I can view with a populated column. However, the values in the column are NOT the correct Postal Codes - I can't even figure out what values it is pulling???
I guess one of my issues is that I don't exactly know what main_table.entity_id refers to (although I have a guess).
Anyway, I feel that I'm close and if someone can answer how I successfully get the information with this method, I'd be eternally grateful! Can anyone
Revised Answer (Due to two problematic errors)
I'm rewriting this answer in a more friendly, step by step way to hopefully help someone else out.
Note: This is for a local change at app/code/local/Mage/Adminhtml/Block/Sales/Order/Grid.php
A. In order to get everything working correclty, you first need to change the _getCollectionClass() from this:
protected function _getCollectionClass()
{
return 'sales/order_grid_collection';
}
to this:
protected function _getCollectionClass()
{
//return 'sales/order_grid_collection';
return 'sales/order_collection';
}
I ran into a major headache when doing this which was this:
SQLSTATE[23000]: Integrity constraint violation: 1052 Column ‘created_at’ in where clause is ambiguous
This happens when you try to filter/search the grid by the Purchase On column.
In order to avoid/fix this error, you need to change the collectioin AND add the following to _prepareCollection() AND add a filter_index to each of the columns added to the grid.
You'll also run into another headache
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'billing_name' in 'where clause'
IF in the _prepareCollection() you try to dynamically create the columns of the Billing Name or Shipping Name which looks like this:
$collection->getSelect()->columns(new Zend_Db_Expr("CONCAT(s2.firstname, ' ',s2.lastname) AS billing_name"));
$collection->getSelect()->columns(new Zend_Db_Expr("CONCAT(s1.firstname, ' ',s1.lastname) AS shipping_name"));
When this is done, there is no real easy way (that I came across to fix this), if it all.
In order to avoid these headaches (after you change the _getCollectionClass() to the above) do the following:
B. Change the _prepareCollection() to this:
protected function _prepareCollection()
{
$collection = Mage::getResourceModel($this->_getCollectionClass());
$collection->getSelect()->joinLeft(array('sfog' => 'sales_flat_order_grid'),'main_table.entity_id = sfog.entity_id',array('sfog.shipping_name','sfog.billing_name'));
$collection->getSelect()->joinLeft(array('sfo'=>'sales_flat_order'),'sfo.entity_id=main_table.entity_id',array('sfo.customer_email','sfo.weight','sfo.discount_description','sfo.increment_id','sfo.store_id','sfo.created_at','sfo.status','sfo.base_grand_total','sfo.grand_total'));
$collection->getSelect()->joinLeft(array('sfoa'=>'sales_flat_order_address'),'main_table.entity_id = sfoa.parent_id AND sfoa.address_type="shipping"',array('sfoa.street','sfoa.city','sfoa.region','sfoa.postcode','sfoa.telephone'));
$this->setCollection($collection);
return parent::_prepareCollection();
}
C. Then for the existing columns in _prepareColumns() add a filer_index to each:
Example:
$this->addColumn('billing_name', array(
'header' => Mage::helper('sales')->__('Bill to Name'),
'index' => 'billing_name',
'filter_index' => 'sfog.billing_name',
));
D. Then add the columns you want to add like this:
Example:
$this->addColumn('customer_email', array(
'header' => Mage::helper('sales')->__('Customer Email'),
'index' => 'customer_email',
'filter_index' => 'sfo.customer_email',
'width' => '50px',
));
I have a similar requirement some time back, where I need to add customer email and shipping region to the sales order grid.
For achieving the requirement I have rewritten class Mage_Adminhtml_Block_Sales_Order_Grid as below in my custom module.
class Custom_OrderGrid_Block_Adminhtml_Sales_Order_Grid extends Mage_Adminhtml_Block_Sales_Order_Grid {
protected function _getCollectionClass() {
return 'sales/order_collection';
}
protected function _prepareCollection() {
$collection = Mage::getResourceModel($this->_getCollectionClass());
$collection->getSelect()->joinLeft(array('sfog' => 'sales_flat_order_grid'), 'main_table.entity_id = sfog.entity_id', array('sfog.shipping_name', 'sfog.billing_name'));
$collection->getSelect()->joinLeft(array('sfo' => 'sales_flat_order'), 'sfo.entity_id=main_table.entity_id', array('sfo.customer_email', 'sfo.increment_id', 'sfo.store_id', 'sfo.created_at', 'sfo.status', 'sfo.base_grand_total', 'sfo.grand_total'));
$collection->getSelect()->joinLeft(array('sfoa' => 'sales_flat_order_address'), 'main_table.entity_id = sfoa.parent_id AND sfoa.address_type="shipping"', array('sfoa.region'));
$this->setCollection($collection);
return Mage_Adminhtml_Block_Widget_Grid::_prepareCollection();
}
protected function _prepareColumns() {
$this->addColumn('real_order_id', array(
'header' => Mage::helper('sales')->__('Order #'),
'width' => '80px',
'type' => 'text',
'index' => 'increment_id',
'filter_index' => 'sfo.increment_id'
));
if (!Mage::app()->isSingleStoreMode()) {
$this->addColumn('store_id', array(
'header' => Mage::helper('sales')->__('Purchased From (Store)'),
'index' => 'store_id',
'type' => 'store',
'store_view' => true,
'display_deleted' => true,
'filter_index' => 'sfo.store_id'
));
}
$this->addColumn('created_at', array(
'header' => Mage::helper('sales')->__('Purchased On'),
'index' => 'created_at',
'type' => 'datetime',
'width' => '100px',
'filter_index' => 'sfo.created_at'
));
$this->addColumn('billing_name', array(
'header' => Mage::helper('sales')->__('Bill to Name'),
'index' => 'billing_name',
'filter_index' => 'sfog.billing_name'
));
$this->addColumn('shipping_name', array(
'header' => Mage::helper('sales')->__('Ship to Name'),
'index' => 'shipping_name',
'filter_index' => 'sfog.shipping_name'
));
$this->addColumn('customer_email', array(
'header' => Mage::helper('sales')->__('Customer Email'),
'index' => 'customer_email',
'filter_index' => 'sfo.customer_email',
'width' => '50px',
));
$this->addColumn('region', array(
'header' => Mage::helper('sales')->__('Shipping State'),
'index' => 'region',
'filter_index' => 'sfoa.region',
'width' => '50px',
));
$this->addColumn('base_grand_total', array(
'header' => Mage::helper('sales')->__('G.T. (Base)'),
'index' => 'base_grand_total',
'type' => 'currency',
'currency' => 'base_currency_code',
'filter_index' => 'sfo.base_grand_total'
));
$this->addColumn('grand_total', array(
'header' => Mage::helper('sales')->__('G.T. (Purchased)'),
'index' => 'grand_total',
'type' => 'currency',
'currency' => 'order_currency_code',
'filter_index' => 'sfo.grand_total'
));
$this->addColumn('status', array(
'header' => Mage::helper('sales')->__('Status'),
'index' => 'status',
'type' => 'options',
'width' => '70px',
'filter_index' => 'sfo.status',
'options' => Mage::getSingleton('sales/order_config')->getStatuses(),
));
if (Mage::getSingleton('admin/session')->isAllowed('sales/order/actions/view')) {
$this->addColumn('action', array(
'header' => Mage::helper('sales')->__('Action'),
'width' => '50px',
'type' => 'action',
'getter' => 'getId',
'actions' => array(
array(
'caption' => Mage::helper('sales')->__('View'),
'url' => array('base' => '*/sales_order/view'),
'field' => 'order_id'
)
),
'filter' => false,
'sortable' => false,
'index' => 'stores',
'is_system' => true,
));
}
$this->addRssList('rss/order/new', Mage::helper('sales')->__('New Order RSS'));
$this->addExportType('*/*/exportCsv', Mage::helper('sales')->__('CSV'));
$this->addExportType('*/*/exportExcel', Mage::helper('sales')->__('Excel XML'));
return Mage_Adminhtml_Block_Widget_Grid::_prepareColumns();
}
}
I hope this will help others.
The reason about that error "Mage_Sales_Model_Order) with the same id "10860" already exist" it's because sales_flat_order_address creates two records for every order, one for shipping address and another one for billing address, if you add this filter on the _prepareCollection() you can make it work
$collection->getSelect()->where("address_type='shipping'");

set default value for custom field type: list_boolean / options_onoff

$instance = array(
'field_name' => $field_name,
'entity_type' => $entity,
'bundle' => $bundle,
'field types' => 'list_boolean',
'widget' => array(
'type' => 'options_onoff',
'settings' => array('display_label' => 1)
),
'default_value' => array(array('value' => 1)),
);
this is not taken, and i have to save it twice in the admin contenttype - field/edit,
until it takes it ...
i now exported the finished field with the features module,
and took the generated code - suddenly it works, with default_value
i guess i was missing the property module on the field, also field types is inexistant ..
In your field definition, you have to set the allowed_values in the settings array in order for the default_value in the instance to get picked up.
so like this assuming you are doing this in a module
$fields[] = array(
'field_name' => '$field_name',
'type' => 'list_boolean',
'settings' => array(
'allowed_values' => drupal_map_assoc(range(0, 1)),
),
);
Instead of using 'default_value', I got it to work by using 'default_value_function' and creating a function that returns array(array('value' => 1)).

Can I have fixed cropping with images?

How can I specify a default value and hide this checkbox ?
[states] => Array
(
[#type] => checkboxes
[#title] => Status
[#options] => Array
(
[active] => Active users
[inactive] => Inactive users
)
[#description] => Subscriptions matching the selected states will be exported.
[#required] => 1
)
I've tried
$form['states']['#default_value'] = 'active';
but it gives me an error..
thanks
Your code as written will produce a syntax error, since you're declaring the 'states' array wrong. This is a PHP syntax thing that doesn't really have anything to do with Drupal's forms API.
Try:
$form['states'] => Array (
'#type' => 'checkboxes',
'#title' => t('Status'),
'#options' => Array (
'active' => 'Active users',
'inactive' => 'Inactive users',
),
'#description' => t('Subscriptions matching the selected states will be exported.'),
'#required' => 1,
'#default_value' => 'active',
);
Unrelated to the syntax issue, a better way to do this would be to reset the entire form element as a '#type' = 'value'. For example $form['states']['#type'] = 'value'; $form['states']['value'] = 'whatever'

Resources