This is the API documentation for concrete5 version 5.6 and earlier. View Current API Documentation
  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 
<?php
defined('C5_EXECUTE') or die("Access Denied.");

class Concrete5_Controller_AttributeType_DateTime extends AttributeTypeController  {

    public $helpers = array('form');
    
    protected $searchIndexFieldDefinition = 'T NULL';

    public function saveKey($data) {
        $akDateDisplayMode = $data['akDateDisplayMode'];
        if (!$akDateDisplayMode) {
            $akDateDisplayMode = 'date_time';
        }
        $this->setDisplayMode($akDateDisplayMode);
    }
    
    public function setDisplayMode($akDateDisplayMode) {
        $db = Loader::db();
        $ak = $this->getAttributeKey();
        $db->Replace('atDateTimeSettings', array(
            'akID' => $ak->getAttributeKeyID(), 
            'akDateDisplayMode' => $akDateDisplayMode
        ), array('akID'), true);
    }
    
    public function type_form() {
        $this->load();
    }

    public function getDisplayValue() {
        $this->load();
        $v = $this->getValue();
        if ($v == '' || $v == false) {
            return '';
        }
        $dh = Loader::helper('date');
        /* @var $dh DateHelper */
        $timestamp = strtotime($v);
        switch($this->akDateDisplayMode) {
            case 'date':
                $r = $dh->formatSpecial('DATE_ATTRIBUTE_TYPE_MDY', $timestamp, 'system');
                break;
            default:
                if ($dh->formatCustom('H:i:s', $v) != '00:00:00') {
                    $r = t(
                        /*i18n: %1$s is a time, %2$s is a date*/ '%1$s on %2$s',
                        $dh->formatSpecial('DATE_ATTRIBUTE_TYPE_T', $timestamp),
                        $dh->formatSpecial('DATE_ATTRIBUTE_TYPE_MDY', $timestamp)
                    );
                }
                else {
                    $r = $dh->formatSpecial('DATE_ATTRIBUTE_TYPE_MDY', $timestamp);
                }
                break;
        }
        return $r;
    }
    
    public function searchForm($list) {
        $dateFrom = $this->request('from');
        $dateTo = $this->request('to');
        if ($dateFrom) {
            $dateFrom = date('Y-m-d', strtotime($dateFrom));
            $list->filterByAttribute($this->attributeKey->getAttributeKeyHandle(), $dateFrom, '>=');
        }
        if ($dateTo) {
            $dateTo = date('Y-m-d', strtotime($dateTo));
            $list->filterByAttribute($this->attributeKey->getAttributeKeyHandle(), $dateTo, '<=');
        }
        return $list;
    }
    
    public function form() {
        $this->load();
        $dt = Loader::helper('form/date_time');
        $caValue = $this->getValue();
        $html = Loader::helper('html');
        switch($this->akDateDisplayMode) {
            case 'text':
                $form = Loader::helper('form');
                print $form->text($this->field('value'), $this->getDisplayValue());
                break;
            case 'date':
                $this->addHeaderItem($html->css('jquery.ui.css'));
                $this->addHeaderItem($html->javascript('jquery.ui.js'));
                print $dt->date($this->field('value'), $caValue);
                break;
            default:
                $this->addHeaderItem($html->css('jquery.ui.css'));
                $this->addHeaderItem($html->javascript('jquery.ui.js'));
                print $dt->datetime($this->field('value'), $caValue);
                break;
        }
    }

    public function exportKey($akey) {
        $this->load();
        $type = $akey->addChild('type');
        $type->addAttribute('mode', $this->akDateDisplayMode);
        return $akey;
    }

    public function importKey($akey) {
        if (isset($akey->type)) {
            $data['akDateDisplayMode'] = $akey->type['mode'];
            $this->saveKey($data);
        }
    }

    public function validateForm($data) {
        if(!isset($this->akDateDisplayMode)) {
            $this->load();
        }
        switch($this->akDateDisplayMode) {
            case 'date_time':
                if(empty($data['value_dt']) || (!is_numeric($data['value_h'])) || (!is_numeric($data['value_m']))) {
                    return false;
                }
                switch(DATE_FORM_HELPER_FORMAT_HOUR) {
                    case '12':
                        if(empty($data['value_a'])) {
                            return false;
                        }
                        break;
                }
                return true;
            default:
                return $data['value'] != '';
        }
    }

    public function getValue() {
        $db = Loader::db();
        $value = $db->GetOne("select value from atDateTime where avID = ?", array($this->getAttributeValueID()));
        return $value;
    }

    public function search() {
        $dt = Loader::helper('form/date_time');
        $html = $dt->date($this->field('from'), $this->request('from'), false);
        $html .= ' ' . t('to') . ' ';
        $html .= $dt->date($this->field('to'), $this->request('to'), false);
        print $html;
    }
    
    public function saveValue($value) {
        if ($value != '') {
            $value = date('Y-m-d H:i:s', strtotime($value));
        } else {
            $value = null;
        }
        
        $db = Loader::db();
        $db->Replace('atDateTime', array('avID' => $this->getAttributeValueID(), 'value' => $value), 'avID', true);
    }

    public function duplicateKey($newAK) {
        $this->load();
        $db = Loader::db();
        $db->Execute('insert into atDateTimeSettings (akID, akDateDisplayMode) values (?, ?)', array($newAK->getAttributeKeyID(), $this->akDateDisplayMode));   
    }
    
    public function saveForm($data) {
        $this->load();
        $dt = Loader::helper('form/date_time');
        switch($this->akDateDisplayMode) {
            case 'text':
                $this->saveValue($data['value']);
                break;
            case 'date':
            case 'date_time':
                $value = $dt->translate('value', $data);
                $this->saveValue($value);
                break;
        }
    }
    
    protected function load() {
        $ak = $this->getAttributeKey();
        if (!is_object($ak)) {
            return false;
        }
        
        $db = Loader::db();
        $row = $db->GetRow('select akDateDisplayMode from atDateTimeSettings where akID = ?', $ak->getAttributeKeyID());
        $this->akDateDisplayMode = $row['akDateDisplayMode'];

        $this->set('akDateDisplayMode', $this->akDateDisplayMode);
    }
    
    public function deleteKey() {
        $db = Loader::db();
        $arr = $this->attributeKey->getAttributeValueIDList();
        foreach($arr as $id) {
            $db->Execute('delete from atDateTime where avID = ?', array($id));
        }
    }
    public function deleteValue() {
        $db = Loader::db();
        $db->Execute('delete from atDateTime where avID = ?', array($this->getAttributeValueID()));
    }

}