tudu-web-1 /library/Dao/Td/Tudu/Label.php

Language PHP Lines 551
MD5 Hash 243cccf7b7a336d5243b090732e6f664
Repository https://github.com/polokk/tudu-web-1.git View Raw File
  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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
<?php
/**
 * Tudu Dao
 *
 * LICENSE
 *
 *
 * @category   Dao
 * @package    Dao_Td
 * @subpackage Tudu
 * @copyright  Copyright (c) 2009-2010 Shanghai Best Oray Information S&T CO., Ltd.
 * @link       http://www.oray.com/
 * @version    $Id: Label.php 2590 2012-12-31 10:04:53Z cutecube $
 */

/**
 * @category   Dao
 * @package    Dao_Td
 * @subpackage Tudu
 * @copyright  Copyright (c) 2009-2010 Shanghai Best Oray Information S&T CO., Ltd.
 */
class Dao_Td_Tudu_Label extends Oray_Dao_Abstract
{
    /**
     * Initialize object
     *
     * Called from {@link __construct()} as final step of object instantiation.
     *
     * @return void
     */
    public function init()
    {}

    /**
     * Get labels
     *
     * SEELCT unique_id AS uniqueid, label_id AS labelid, label_alias AS labelalias,
     * total_num AS total_num, unread_num AS unreadnum,
     * is_system AS issystem, color, bgcolor, sync_time AS synctime
     * FORM td_label
     * WHERE unique_id = ?
     * ORDER BY label_name ASC
     *
     * @param array $condition
     * @param array $filter
     * @param array $sort
     * @param int   $maxCount
     * @return Oray_Dao_Recordset
     */
    public function getLabels(array $condition, $filter = null, $sort = null, $maxCount = null)
    {
        $table   = "td_label";
        $columns = "unique_id AS uniqueid, label_id AS labelid, label_alias AS labelalias, "
                 . "total_num AS totalnum, unread_num AS unreadnum, is_show AS isshow, display, "
                 . "is_system AS issystem, color, bgcolor, sync_time AS synctime, order_num AS ordernum";
        $where   = array();
        $order   = array();
        $limit   = "";

        // $condition ...

        if (isset($condition['uniqueid'])) {
            $where[] = 'unique_id = ' . $this->_db->quote($condition['uniqueid']);
        }

        if (isset($filter['issystem'])) {
            $where[] = 'is_system = ' . $filter['issystem'] ? 1 : 0;
        }

        if (empty($where)) {
            return new Oray_Dao_Recordset();
        }

        // $filter ...

        // WHERE
        $where = implode(' AND ', $where);
        if ($where) {
            $where = 'WHERE ' . $where;
        }

        $sort  = $this->_formatSort($sort);
        foreach ($sort as $key => $val) {
            switch ($key) {
                case 'alias':
                    $key = 'label_alias';
                    break;
                case 'issystem':
                    $key = 'is_system';
                    break;
                case 'ordernum':
                    $key = 'order_num';
                    break;
                default:
                    continue 2;
            }
            $order[] = $key . ' ' . $val;
        }

        // ORDER
        $order = implode(', ', $order);
        if ($order) {
            $order = 'ORDER BY ' . $order;
        }

        // LIMIT
        if (is_int($maxCount) && $maxCount > 0) {
            $limit = 'LIMIT ' . $maxCount;
        }

        $sql = "SELECT $columns FROM $table $where $order $limit";
        $records = $this->_db->fetchAll($sql);

        return new Oray_Dao_Recordset($records, 'Dao_Td_Tudu_Record_Label');
    }

    /**
     *
     * @param array $condition
     * @param array $filter
     */
    public function getLabel(array $condition, $filter = null)
    {
        $table   = "td_label";
        $columns = "unique_id AS uniqueid, label_id AS labelid, label_alias AS labelalias, "
                 . "total_num AS totalnum, unread_num AS unreadnum, is_show AS isshow, "
                 . "is_system AS issystem, color, bgcolor, sync_time AS synctime, order_num AS ordernum";
        $where   = array();

        // $condition ...

        if (isset($condition['uniqueid'])) {
            $where[] = 'unique_id = ' . $this->_db->quote($condition['uniqueid']);
        }

        if (isset($filter['labelid'])) {
            $where[] = 'label_id = ' . $this->_db->quote($filter['labelid']);
        }

        if (empty($where)) {
            return null;
        }

        $where = 'WHERE ' . implode(' AND ', $where);

        $sql = "SELECT $columns FROM $table $where LIMIT 1";

        try {
            $record = $this->_db->fetchRow($sql);

            if (!$record) {
                return null;
            }

            return Oray_Dao::record('Dao_Td_Tudu_Record_Label', $record);
        } catch (Zend_Db_Exception $e) {
            $this->_catchException($e, __METHOD__);
            return null;
        }
    }

    /**
     * Get labels
     *
     * @param string $uniqueId
     * @param array  $filter
     * @param array  $sort
     * @param int    $maxCount
     * @return Oray_Dao_Recordset
     */
    public function getLabelsByUniqueId($uniqueId, $filter = null, $sort = 'alias ASC', $maxCount = null)
    {
        return $this->getLabels(array('uniqueid' => $uniqueId), $filter, $sort, $maxCount);
    }

    /**
     *
     * @param array $condition
     * @return int
     */
    public function getLabelCount(array $condition)
    {
        $table = 'td_label';
        $where = array();

        if (!empty($condition['uniqueid'])) {
            $where[] = 'unique_id = ' . $this->_db->quote($condition['uniqueid']);
        }

        if (isset($condition['issystem'])) {
            $where[] = 'is_system = ' . ($condition['issystem'] ? 1 : 0);
        }

        if (!$where) {
            return false;
        }

        $where = implode(' AND ', $where);

        $sql   = "SELECT COUNT(0) FROM {$table} WHERE {$where}";
        $count = (int) $this->_db->fetchOne($sql);

        return $count;
    }

    /**
     * Create label
     *
     * @param $params
     * @return int|false
     */
    public function createLabel(array $params)
    {
        if (empty($params['uniqueid'])
            || !isset($params['labelid'])
            || !isset($params['labelalias'])) {
            return false;
        }

        $table = "td_label";
        $bind  = array(
            'unique_id' => $params['uniqueid'],
            'label_id' => $params['labelid'],
            'label_alias' => $params['labelalias'],
            'is_system' => empty($params['issystem']) ? 0 : 1
            );

        if (isset($params['isshow'])) {
            $bind['is_show'] = $params['isshow'];
        }
        if (isset($params['color'])) {
            $bind['color'] = $params['color'];
        }
        if (isset($params['bgcolor'])) {
            $bind['bgcolor'] = $params['bgcolor'];
        }
        if (isset($params['ordernum']) && is_int($params['ordernum'])) {
            $bind['order_num'] = $params['ordernum'];
        }
        if (isset($params['display']) && is_int($params['display'])) {
            $bind['display'] = $params['display'];
        }

        try {
            $this->_db->insert($table, $bind);
        } catch (Zend_Db_Exception $e) {
            $this->_catchException($e, __METHOD__);
            return false;
        }

        return true;
    }

    /**
     * Update label
     *
     * @param string $uniqueId
     * @param string $labelId
     * @param array $params
     * @return boolean
     */
    public function updateLabel($uniqueId, $labelId, array $params)
    {
        // 系统标签禁止修改
        if (!is_string($labelId) || $labelId[0] === '^') {
            return false;
        }

        $table = "td_label";
        $bind  = array();
        $where = "unique_id = " . $this->_db->quote($uniqueId)
               . " AND label_id = " . $this->_db->quote($labelId);

        if (isset($params['labelalias'])) {
            $bind['label_alias'] = $params['labelalias'];
        }

        if (isset($params['isshow'])) {
            $bind['is_show'] = $params['isshow'];
        }

        if (isset($params['color'])) {
            $bind['color'] = $params['color'];
        }

        if (isset($params['bgcolor'])) {
            $bind['bgcolor'] = $params['bgcolor'];
        }

        if (isset($params['ordernum']) && is_int($params['ordernum'])) {
            $bind['order_num'] = $params['ordernum'];
        }

        if (!$bind) {
            return false;
        }

        try {
            $this->_db->update($table, $bind, $where);
        } catch (Zend_Db_Exception $e) {
            $this->_catchException($e, __METHOD__);
            return false;
        }

        return true;
    }

    /**
     * 更新标签显示方式
     *
     * @param string $uniqueId
     * @param string $labelId
     * @param string $type
     * @return boolean
     */
    public function showLabel($uniqueId, $labelId, $type)
    {
        //所有图度、图度箱不允许此操作
        if ($labelId == '^all' || $labelId == '^i') {
            return false;
        }

        $table = "td_label";
        $bind  = array('is_show' => $type);
        $where = "unique_id = " . $this->_db->quote($uniqueId)
               . " AND label_id = " . $this->_db->quote($labelId);

        try {
            $this->_db->update($table, $bind, $where);
        } catch (Zend_Db_Exception $e) {
            $this->_catchException($e, __METHOD__);
            return false;
        }

        return true;
    }

    /**
     *
     * @param string $uniqueId
     * @param string $labelId
     * @param string $type
     * @return boolean
     */
    public function sortLabel($uniqueId, $labelId, $type, $issytem = false)
    {
        $labels = $this->getLabelsByUniqueId($uniqueId, array('issystem' => $issytem), array('ordernum' => 'DESC'))->toArray();

        $count = count($labels);
        $index = 0;
        $target = null;
        $exchange = null;

        if ($count <= 1) {
            return false;
        }

        foreach ($labels as $idx => $label) {
            if ($issytem) {
                if ($label['labelalias'] == $labelId) {
                    $index  = $idx;
                    $target = $label;
                    break;
                }
            } else {
                if ($label['labelid'] == $labelId) {
                    $index  = $idx;
                    $target = $label;
                    break;
                }
            }
        }

        if (($type == 'up' && $index == 0) || ($type == 'down' && $index == $count - 1)) {
            return false;
        }

        $exchangeIndex = $type == 'up' ? $index - 1 : $index + 1;
        $exchange = $labels[$exchangeIndex];

        try {
            if ($issytem) {
                $this->_db->update(
                    'td_label',
                    array('order_num' => $exchange['ordernum']),
                    'unique_id = ' . $this->_db->quote($uniqueId) . ' AND label_alias = ' . $this->_db->quote($labelId)
                );

                $this->_db->update(
                    'td_label',
                    array('order_num' => $target['ordernum']),
                    'unique_id = ' . $this->_db->quote($uniqueId) . ' AND label_alias = ' . $this->_db->quote($exchange['labelalias'])
                );
            } else {
                $this->updateLabel($uniqueId, $labelId, array(
                    'ordernum' => $exchange['ordernum']
                ));

                $this->updateLabel($uniqueId, $exchange['labelid'], array(
                    'ordernum' => $target['ordernum']
                ));
            }
        } catch (Zend_Db_Exception $e) {
            $this->_catchException($e, __METHOD__);
            return false;
        }

        return true;
    }

    /**
     * Delete label
     *
     * @param string $uniqueId
     * @param string $labelId
     * @return boolean
     */
    public function deleteLabel($uniqueId, $labelId)
    {
        // 系统标签禁止删除
        if (!is_string($labelId) || $labelId[0] === '^') {
            return false;
        }

        $uniqueId = $this->_db->quote($uniqueId);
        $labelId  = $this->_db->quote($labelId);

        $sqls[] = "DELETE FROM td_tudu_label WHERE unique_id = $uniqueId AND label_id = $labelId";
        $sqls[] = "DELETE FROM td_label WHERE unique_id = $uniqueId AND label_id = $labelId";

        try {
            foreach($sqls as $sql) {
                $this->_db->query($sql);
            }
        } catch (Zend_Db_Exception $e) {
            $this->_catchException($e, __METHOD__);
            return false;
        }

        return true;
    }

    /**
     * 整理标签排序
     *
     * @param $uniqueId
     */
    public function tidyLabelSort($uniqueId)
    {
        $uniqueId = $this->_db->quote($uniqueId);
        $sql = 'SELECT label_id AS labelid, order_num AS ordernum FROM td_label '
             . 'WHERE unique_id = ' . $uniqueId . ' AND is_system = 0 ORDER BY order_num DESC';

        $records = $this->_db->fetchAll($sql);

        $count = count($records);
        try {
            foreach ($records as $index => $record) {
                $this->_db->update(
                    'td_label',
                    array('order_num' => $count - $index),
                    'unique_id = ' . $uniqueId . ' AND label_id = ' . $this->_db->quote($record['labelid'])
                );
            }
        } catch (Zend_Db_Exception $e) {
            $this->_catchException($e, __METHOD__);

            return false;
        }

        return true;
    }

    /**
     * 图度数自增
     *
     * @param $uniqueId
     * @param $labelId
     * @param $value
     * @return boolean
     */
    public function increment($uniqueId, $labelId, $value = 1)
    {
        if ($value < 1) return false;
        $sql = 'UPDATE td_label SET total_num = total_num + ' . (int) $value
             . ' WHERE unique_id = ' . $this->_db->quote($uniqueId)
             . ' AND label_id = ' . $this->_db->quote($labelId);
        try {
            $this->_db->query($sql);
        } catch (Zend_Db_Exception $e) {
            $this->_catchException($e, __METHOD__);
            return false;
        }
        return true;
    }

    /**
     * 图度数自减
     *
     * @param $uniqueId
     * @param $labelId
     * @param $value
     * @return boolean
     */
    public function decrement($uniqueId, $labelId, $value = 1)
    {
        if ($value < 1) return false;
        $sql = 'UPDATE td_label SET total_num = total_num - ' . (int) $value
             . ' WHERE unique_id = ' . $this->_db->quote($uniqueId)
             . ' AND label_id = ' . $this->_db->quote($labelId);
        try {
            $this->_db->query($sql);
        } catch (Zend_Db_Exception $e) {
            $this->_catchException($e, __METHOD__);
            return false;
        }
        return true;
    }


    /**
     * 统计标签图度数
     *
     * @param string $uniqueId
     * @param string $labelId
     */
    public function calculateLabel($uniqueId, $labelId)
    {
        $sql = "call sp_td_calculate_label(" . $this->_db->quote($uniqueId) . "," . $this->_db->quote($labelId) . ")";
        try {
            $this->_db->query($sql);
        } catch (Zend_Db_Exception $e) {
            $this->_catchException($e, __METHOD__);
            return false;
        }

        return true;
    }

    /**
     * 获取标签ID
     *
     *
     * @return string
     */
    public static function getLabelId()
    {
        $labelId = base_convert(substr(microtime(true) * 10000, 0, -1), 10, 16) . str_pad(dechex(mt_rand(0, 0xfffff)), 5, '0', STR_PAD_LEFT);
        return $labelId;
    }
}
Back to Top