Skip to content

Instantly share code, notes, and snippets.

View ka215's full-sized avatar

ka2 ka215

View GitHub Profile
@ka215
ka215 / non-narrow-down.php
Last active June 7, 2016 05:15
「Custom DataBase Tables」のショートコード[cdbt-edit]および[cdbt-view]でログインしているuserIDのデータのみを表示させる(ただし管理者は全データにアクセス可能)を実現するためのフィルターフック。
<?php
// As a prerequisite, there are stored each user ID to the "user_id" column (as numric type) in the "your_table_name" as target table.
// (対象テーブル「your_table_name」には「user_id」カラム(数値型)にユーザーIDが格納されているという前提)
function custom_filter_get_data_sql( $sql, $table_name, $sql_clauses ) {
if ( ! is_admin() && "your_table_name" === $table_name ) {
$_current_user_id = 0; // For guest user
if ( is_user_logged_in() ) {
$current_user = wp_get_current_user();
$_current_user_id = $current_user->ID;
@ka215
ka215 / example.php
Created May 31, 2016 05:43
In the dynamic table layout of Custom DataBase Table plugin, how to cancel a draggable table.
<?php
function table_layout_fixed( $component_options, $shortcode_name, $table ){
if ( 'cdbt-view' === $shortcode_name && 'your_table_name' === $table ) {
$component_options['customAfterRender'] = <<<EJS
$('#'+options.tableId).css({tableLayout:'auto'}).parent('.panel-table-wrapper').kinetic('detach');
EJS;
}
return $component_options;
}
@ka215
ka215 / example.php
Created May 31, 2016 05:43
In the dynamic table layout of Custom DataBase Table plugin, how to cancel a draggable table.
<?php
function table_layout_fixed( $component_options, $shortcode_name, $table ){
if ( 'cdbt-view' === $shortcode_name && 'your_table_name' === $table ) {
$component_options['customAfterRender'] = <<<EJS
$('#'+options.tableId).css({tableLayout:'auto'}).parent('.panel-table-wrapper').kinetic('detach');
EJS;
}
return $component_options;
}
@ka215
ka215 / file0.js
Created June 2, 2016 07:40
JavaScriptのすぐ使える7つのユーティリティ関数 ref: http://qiita.com/ka215/items/d059a78e29adef3978b5
/**
* Return as an object by parsing the query string of the current URL
*/
$.QueryString = (function(queries) {
if ('' === queries) { return {}; }
var results = {};
for (var i=0; i<queries.length; ++i) {
var param = queries[i].split('=');
if (param.length !== 2) { continue; }
results[param[0]] = decodeURIComponent(param[1].replace(/\+/g, ' '));
@ka215
ka215 / clickable_url_column.php
Created June 7, 2016 03:33
You should use this filter hook code for the Custom DataBase Tables if you want to be able to click the specific column value that stored the strings of url at the time of using shortcode. Also this filter is enable since plugin version 2.x.
<?php
// Please insert this code to as like your theme's "functions.php".
// Note: you should be modified variable of "$target_table" and "$url_column" in this code.
function cdbt_custom_clickable_url( $columns, $shortcode_name, $table ) {
$target_table = 'string'; // Your table name that you want to filter.
$url_column = 'varchar'; // This column name should be stored the url strings, and column type is "varchar".
if ( $target_table === $table ) {
foreach ( $columns as $_i => $_column ) {
if ( $url_column === $_column['property'] ) {
@ka215
ka215 / modify_datatime_format.php
Last active June 8, 2016 02:09
For Custom DataBase Tables plugin; this filter hook code is sample to modify the display format of any datetime column of the specified table. About the allowed the alias of datetime format, please refer: https://ka2.org/cdbt/v2/basic-info/plugin-options/general-setting/#datetime-formats
<?php
// You should be optimize the variables of "$target_table" and "$target_column" in proportion to your table structure.
function cdbt_custom_datetime_format( $columns, $shortcode_name, $table ) {
$target_table = 'your_table_name';
$target_column = 'datetime_column_name';
if ( $target_table === $table ) {
foreach ( $columns as $_i => $_column ) {
if ( $target_column === $_column['property'] ) {
$_custom_column = sprintf( 'convert_datetime(rowData["%s"], ["%s"])', $target_column, 'F j, Y' );
@ka215
ka215 / narrow-down-one-column.php
Created June 15, 2016 03:46
This example of filter hook is in which to narrow down the data by a plurality of conditions to the values stored in one column. This code should be inserted to as like "functions.php" in your theme.
<?php
// In the variables of "$target_tables", "$narrow_column", "$narrow_keywords", you should be optimized by your environment and project.
function custom_filter_sql( $sql, $table_name, $sql_clauses ) {
$target_tables = [ 'your_table_name' ];
$narrow_column = 'specific_column_name';
$narrow_keywords = [ 'keyword1', 'keyword2', 'keyword3' ];
if ( in_array( $table_name, $target_tables ) ) {
if ( is_array( $sql_clauses[1] ) ) {
// Narrowing is find_data()
@ka215
ka215 / filter_date_type_column.php
Created June 15, 2016 06:33
If you want to filter in each years at the datetime type column as like the "date" type, you will resolve by using this filter hooks. However, because some plugin core sources should be modified, it's not able to work correctly in the current version (2.1.32). In the next version 2.1.33, I'm going to support.
<?php
// Note: this code will be enabled since the CDBT plugin version 2.1.33.
function add_filtered_column_sql( $sql, $table_name, $sql_clauses ) {
$target_tables = [ 'your_table_name' ];
$filter_date_column = 'date_type_column'; // Column name that you want to filter by the filtering dropdown box
if ( in_array( $table_name, $target_tables ) && strpos( $sql_clauses[0], $filter_date_column ) !== false ) {
$select_clause = $sql_clauses[0] .',CAST(YEAR(`'. $filter_date_column .'`) AS CHAR) AS `year`'; // "year" is alias column for filtering
$_overwrite_sql = <<< SQL
SELECT %s
@ka215
ka215 / enum_aggregate.php
Last active June 25, 2016 09:57
ユーザーごとにenum型の登録値を合計して、ショートコードに一覧化するフィルターフックおよび集計関数の例。 デモはこちら -> http://demo.ka2.org/narrow-by-sum/
<?php
/**
* Function to aggregate the value of the enum type
*
* @param string $target_table [requied]
* @param string $userid_col [requied]
* @param string $enum_col [required]
* @param string $period [optional] It must be a target date string, ex."2016-06-23"
*/
function enum_aggregate( $target_table, $userid_col, $enum_col, $period=null ) {
@ka215
ka215 / filter_after_user_search.php
Last active June 25, 2016 07:36
This example of filter hook is for rendering dynamically the shortcode by using that search value after searching at the frontend.
<?php
/**
* The markup content of frontend (as post content):
* ---
* <form method="get">
* <input type="search" name="search_keyword" value="">
* <button type="submit">Search</button>
* </form>
* [cdbt-view table="your_table_name" narrow_keyword="ID:0"]
* ---