Plugin Directory

Changeset 994135


Ignore:
Timestamp:
09/21/2014 08:53:25 AM (12 years ago)
Author:
freshlabs
Message:

Moved post specific functions ti WPSimileTimelinePost.class

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wp-simile-timeline/trunk/inc/WPSimileTimelineDatabase.class.php

    r994064 r994135  
    2525*/
    2626class WPSimileTimelineDatabase{
    27     function WPSimileTimelineDatabase(){
    28        
    29     }
    30    
    31     function doUpdates(){
    32         global $wpdb;
    33        
    34         $terms_table = WPSimileTimelineTerm::getTableName();
    35                
    36                 // TODO: check if columns exist
    37  
    38                 // @since: 0.4.9
    39                 // Alter posts columns for event dates to CHAR(20) to store dates B.C.
    40                 // 20 Characters: A/B für after/before christ + 19 for YYYY-MM-DD HH:ii:ss
    41                 $post_event_dates = WPSimileTimelinePost::getPostEventTypes();
    42                 foreach($post_event_dates as $column):
    43                     if(WPSimileTimelineDatabase::columnExists($wpdb->posts, $column)){
    44                         $wpdb->query("ALTER TABLE " .$wpdb->posts . " CHANGE `" . $column . "` `" . $column . "` VARCHAR(20) NOT NULL DEFAULT '0000-00-00 00:00:00'; ");
    45                     }
    46                 endforeach;
    47                
    48        
    49         // Add column for icon in terms table
    50                 // @since: 0.4.8.5
    51                 $column_name = 'icon';
    52         $wpdb->query("ALTER TABLE $terms_table ADD COLUMN $column_name VARCHAR( 255 ) NOT NULL AFTER `color`");
    53     }
    54    
    55     /* ---------------------------------------------------------------------------------
    56      * checks if a given column exists in a database table
    57      * --------------------------------------------------------------------------------*/
    58     function columnExists($table, $column){
    59         global $wpdb;
    60         $column_exists = false;
    61         $q = $wpdb->query($wpdb->prepare("SHOW COLUMNS FROM $table LIKE %s", $column));
    62         if($q == 1){
    63             $column_exists = true;
    64         }
    65         return $column_exists;
    66     }
    67    
    68     /* -------------------------------------------------------------------------
    69      * Functions to ADD tables and columns
    70      * ----------------------------------------------------------------------*/
    71    
    72     /*
    73      * Adds extra column in posts table for start and end dates
    74      */
    75     function addEventColumn($column_name){
    76         global $wpdb;
    77         if(!WPSimileTimelineDatabase::columnExists($wpdb->posts, $column_name)) {
    78             $wpdb->query($wpdb->prepare("ALTER TABLE $wpdb->posts ADD COLUMN $column_name datetime NOT NULL DEFAULT %s", array("0000-00-00 00:00:00") ));
    79         }
    80     }
    81    
    82     /* -------------------------------------------------------------------------
    83      * Functions to DELETE tables and columns
    84      * ----------------------------------------------------------------------*/
     27   
     28    function __construct(){
    8529
    86     /*
    87      * Delete a table
    88      */
    89     function deleteTable($tn){     
    90         global $wpdb;
    91         $table_name = $wpdb->prefix . $tn;
    92         $wpdb->query("DROP TABLE $table_name");
    93     }
     30    }
    9431
    95     /*
    96      * removes the database column on uninstalling
    97      * This deletes all event dates set for posts
    98      */
    99     function removeEventColumn($column_name) {
    100         global $wpdb;
    101         if(WPSimileTimelineDatabase::columnExists($wpdb->posts, $column_name)) {
    102             $wpdb->query("ALTER TABLE $wpdb->posts DROP COLUMN $column_name");
    103         }
    104     }
    105    
    106     /*
    107      * Save start and end dates of an event(post) to the database
    108      */
    109     function updateEventDates($post_id){
    110         if(!empty($_POST)){
    111             WPSimileTimelineDatabase::saveEventDate($post_id, $_POST, 'start');
    112             WPSimileTimelineDatabase::saveEventDate($post_id, $_POST, 'latest_start');
    113             WPSimileTimelineDatabase::saveEventDate($post_id, $_POST, 'end');
    114             WPSimileTimelineDatabase::saveEventDate($post_id, $_POST, 'earliest_end');
    115         }
    116     }
    117    
    118     /*
    119      * Save time value of an event(post) to the database
    120      * used by both, start and end update functions
    121      */
    122     function saveEventDate($pID, $p, $type='start'){
    123         global $wpdb;
    124         $column = 'stl_timeline_event_'.$type;
    125         $index = 'stl_timeline_'.$type;
    126         $edit = isset($p[$index]['edit']) ? $p['stl_timeline_'.$type]['edit'] : 0;
    127         $reset = isset($p[$index]['reset']) ? $p['stl_timeline_'.$type]['reset'] : 0;
    128         // if "Edit timestamp" is selected
    129         if ($edit==1 && $reset==0) {
    130                         // TODO: Handle dates BC here
    131             $stl_aa = $p[$index]['year'];
    132             $stl_mm = $p[$index]['month'];
    133             $stl_jj = $p[$index]['day'];
    134             $stl_hh = $p[$index]['hour'];
    135             $stl_mn = $p[$index]['minute'];
    136             $stl_ss = $p[$index]['second'];
    137             $stl_jj = ($stl_jj > 31) ? 31 : $stl_jj;
    138             $stl_hh = ($stl_hh > 23) ? $stl_hh -24 : $stl_hh;
    139             $stl_mn = ($stl_mn > 59) ? $stl_mn -60 : $stl_mn;
    140             $stl_ss = ($stl_ss > 59) ? $stl_ss -60 : $stl_ss;
    141             $postdata = "$stl_aa-$stl_mm-$stl_jj $stl_hh:$stl_mn:$stl_ss";
    142             $stl_tee = $postdata;
    143                         print_r($stl_tee);exit();
    144             $wpdb->query($wpdb->prepare("UPDATE $wpdb->posts SET $column = '%s' WHERE ID = %d", array($stl_tee, $pID)));
    145         }
    146         // Reset timestamp to 0
    147         if ($reset==1) {
    148             $wpdb->query($wpdb->prepare("UPDATE $wpdb->posts SET $column = '0000-00-00 00:00:00' WHERE ID = %d", $pID));
    149         }
    150     }
    151    
    152     /**
    153      * Get the minimum or maximum date of wpdb->posts.$column in $categories
    154      */
    155     function queryEventDate($minmax, $column, $categories){
    156         global $wpdb;
    157        
    158         $relation = "SELECT object_id FROM $wpdb->term_relationships " .
    159                     "INNER JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id AND  $wpdb->term_taxonomy.term_id IN ($categories) )";
    160                    
    161         $query =    "SELECT $minmax($wpdb->posts.$column) from $wpdb->posts INNER JOIN ($relation) t1 ON (t1.object_id=$wpdb->posts.ID) " .
    162                     "WHERE $column != '0000-00-00 00:00:00' AND post_status='publish' AND post_type='post'";
    163    
    164         return $wpdb->get_var($query);
    165     }
    166    
    167     /* ---------------------------------------------------------------------------------
    168      * stl_get_extreme_event_date
    169      * Get the date of the very first or last post dependant from the start date
    170      * --------------------------------------------------------------------------------*/
    171     function getMinMaxEventDate($minmax, $type, $format='r', $categories){
    172         global $wpdb;
    173        
    174         $column = 'stl_timeline_event_' . $type;
    175        
    176         // try to get the extreme post date from stl-columns
    177         $date = WPSimileTimelineDatabase::queryEventDate($minmax, $column, $categories);
    178        
    179         if(empty($date)){
    180             $column = 'post_date';
    181             // get usual post date when start or end date isn't set
    182             $date = WPSimileTimelineDatabase::queryEventDate($minmax, $column, $categories);
    183         }
    184         if($format != null){
    185             $date = adodb_date2($format, $date);
    186         }
    187         return $date;
    188     }
     32    function doUpdates(){
     33        global $wpdb;
     34
     35        $terms_table = WPSimileTimelineTerm::getTableName();
     36
     37        // TODO: check if columns exist
     38
     39        // @since: 0.4.9
     40        // Alter posts columns for event dates to CHAR(20) to store dates B.C.
     41        // 20 Characters: A/B für after/before christ + 19 for YYYY-MM-DD HH:ii:ss
     42        $post_event_dates = WPSimileTimelinePost::getPostEventTypes();
     43        foreach($post_event_dates as $column):
     44            if(WPSimileTimelineDatabase::columnExists($wpdb->posts, $column)){
     45                $wpdb->query("ALTER TABLE " .$wpdb->posts . " CHANGE `" . $column . "` `" . $column . "` VARCHAR(20) NOT NULL DEFAULT '0000-00-00 00:00:00'; ");
     46            }
     47        endforeach;
     48
     49
     50        // Add column for icon in terms table
     51        // @since: 0.4.8.5
     52        $column_name = 'icon';
     53        $wpdb->query("ALTER TABLE $terms_table ADD COLUMN $column_name VARCHAR( 255 ) NOT NULL AFTER `color`");
     54    }
     55
     56    /**
     57     * Checks if a given column exists in a database table
     58     * @param type $table
     59     * @param type $column
     60     * @return boolean
     61     */
     62    function columnExists($table, $column){
     63        global $wpdb;
     64        $column_exists = false;
     65        $q = $wpdb->query($wpdb->prepare("SHOW COLUMNS FROM $table LIKE %s", $column));
     66        if($q == 1){
     67                $column_exists = true;
     68        }
     69        return $column_exists;
     70    }
     71
     72    /**
     73     * Delete a table
     74     * @param type $tn
     75     */
     76    function deleteTable($tn){     
     77        global $wpdb;
     78        $table_name = $wpdb->prefix . $tn;
     79        $wpdb->query("DROP TABLE $table_name");
     80    }   
     81
    18982}
    19083?>
Note: See TracChangeset for help on using the changeset viewer.