MongoPage.php 10.1 KB
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
<?php

/**
 * Created by PhpStorm.
 * User: Zip
 * Date: 15/6/10
 * Time: 下午11:51
 */

abstract class MongoPage
{
    private $keyword;
    private $total;
    private $path;
    private $size;
    private $properties;
    private $query;
    private $length;
    private $pageSetSize = 11;
    private $rows = array();

    /**
     * 是否加载了本地化对象
     *
     * @var boolean
     * @since 1.0
     */
    private $localeLoaded = false;

    /**
     * 语言配置
     *
     * @var array
     * @since 1.0
     */
    private $messages = array();

    /**
     * 分页中代码当前页码的常量
     *
     */
    const PAGER_VARIABLE_STRING = "%{PAGE_NO}";

    /**
     * 构造器
     *
     * @since 2.0
     */
    function __construct()
    {
        $this->path = $_SERVER["PHP_SELF"];
    }

    /**
     * 取得当前页码,第一页为1
     *
     * @return integer
     */
    function current()
    {
        $keyword = $this->keyword();
        $pageNo = intval(x($keyword));
        if ($pageNo <= 0) {
            $pageNo = 1;
        }
        return min($pageNo, $this->length());
    }

    /**
     * 取得下一页页码
     *
     * @return integer
     */
    function next()
    {
        $length = $this->length();
        $current = $this->current();
        return $current < $length ? ($current + 1) : $length;
    }

    /**
     * 取得上一页页码
     *
     * @return integer
     */
    function prev()
    {
        $length = $this->length();
        $current = $this->current();
        return $current > 1 ? ($current - 1) : 1;
    }

    /**
     * 取得记录开始的偏移量
     *
     * @return integer
     */
    function offset()
    {
        $offset = $this->size() * ($this->current() - 1);
        if ($offset < 0) {
            $offset = 0;
        }
        if ($offset >= $this->total()) {
            $offset = max($this->size() * ($this->length() - 1), 0);
        }
        return $offset;
    }

    /**
     * 设置内容总数
     *
     * @param integer $total 内容总数
     * @return MongoPage
     */
    function setTotal($total)
    {
        $this->total = intval($total);
        if ($this->total < 0) {
            throw new \Exception("content total '{$total}' can't be small than 0");
        }
        return $this;
    }

    /**
     * 数据总数
     *
     * @return integer
     * @since 1.0
     */
    function total()
    {
        return $this->total;
    }

    /**
     * 设置分页链接中的关键字
     *
     * @param string $keyword 关键字
     * @return MongoPage
     */
    function setKeyword($keyword)
    {
        $this->keyword = $keyword;
        return $this;
    }

    /**
     * 取得分页用的关键字
     *
     * 从1.0开始,如果没有关键字,则默认为page
     *
     * @return string
     */
    function keyword()
    {
        if (!$this->keyword) {
            $this->keyword = "page";
        }
        return $this->keyword;
    }

    /**
     * 设置每页记录数
     *
     * @param integer $size 大于0的数字
     * @return MongoPage
     */
    function setSize($size)
    {
        $this->size = intval($size);
        if ($this->size < 1) {
            throw new \Exception("page size '{$size}' can't be small than 1");
        }
        return $this;
    }

    /**
     * 取得每页记录数
     *
     * @return integer
     */
    function size()
    {
        if ($this->size < 1) {
            $this->size = 10;
        }
        return $this->size;
    }

    /**
     * 设置链接的路径
     *
     * @param string $path 路径
     * @return MongoPage
     */
    function setPath($path)
    {
        $this->path = $path;
        return $this;
    }

    /**
     * 取得程序路径
     *
     * @return string
     * @since 1.0
     */
    function path()
    {
        return $this->path;
    }

    /**
     * 设置属性
     *
     * @param array $properties 属性列表
     * @return MongoPage
     */
    function setProperties(array $properties)
    {
        $this->properties = $properties;
        return $this;
    }

    /**
     * 取得设置的属性
     *
     * @return array
     * @since 1.0
     */
    function properties()
    {
        return $this->properties;
    }

    /**
     * 设置查询
     *
     * @param mixed $query string|array
     * @return MongoPage
     */
    function setQuery($query)
    {
        if (is_array($query)) {
            $_query = array();
            foreach ($query as $key => $value) {
                if ($key == $this->keyword()) {
                    continue;
                }
                if (is_array($value)) {
                    foreach ($value as $key1 => $value1) {
                        $_query[] = "{$key}[]=" . urlencode($value1);
                    }
                } else {
                    $_query[] = "{$key}=" . urlencode($value);
                }
            }
            $query = implode("&", $_query);
        }
        $this->query = $query;
        return $this;
    }

    /**
     * 添加查询条件
     *
     * <code>
     * $page->addQuery(array(
     *         "e" => 5,
     *         "f" => 6
     *    ));
     *    $page->addQuery("g=7");
     * </code>
     *
     * @param mixed $query string|array
     * @return MongoPage
     */
    function addQuery($query)
    {
        if (is_array($query)) {
            $_query = array();
            foreach ($query as $key => $value) {
                if ($key == $this->keyword()) {
                    continue;
                }
                if (is_array($value)) {
                    foreach ($value as $key1 => $value1) {
                        $_query[] = "{$key}[]=" . urlencode($value1);
                    }
                } else {
                    $_query[] = "{$key}=" . urlencode($value);
                }
            }
            $query = implode("&", $_query);
        }
        $this->query .= ($this->query ? "&" : "") . $query;
        return $this;
    }

    /**
     * 开启自动构造查询条件功能
     *
     * @param boolean $bool 是否开启该功能
     * @param string|array $except 要去除的参数名
     * @param string|array $only 限制的参数名
     * @return MongoPage
     */
    function setAutoQuery($bool = true, $except = "", $only = "")
    {
        if (!is_array($except)) {
            $except = preg_split("/\\s+,\\s+/", $except);
        }
        if (!is_array($only) && strlen($only) > 0) {
            $only = preg_split("/\\s+,\\s+/", $only);
        }
        if ($bool) {
            $x = xn();
            foreach ($x as $name => $value) {
                if ($except && in_array($name, $except)) {
                    unset($x[$name]);
                }
                if ($only && !in_array($name, $only)) {
                    unset($x[$name]);
                }
            }
            $this->setQuery($x);
        }
        return $this;
    }

    /**
     * 取得查询
     *
     * @return array
     * @since 1.0
     */
    function query()
    {
        return $this->query;
    }

    /**
     * 取得一个分页好号对应的URL
     *
     * @param integer $pageNo 分页号
     * @return string
     * @since 1.0
     */
    function url($pageNo)
    {
        $query = $this->query();
        if (strstr($query, self::PAGER_VARIABLE_STRING)) {
            $query = str_replace(self::PAGER_VARIABLE_STRING, $pageNo, $query);
        } else {
            if ($query == "") {
                $query = $this->keyword() . "=" . $pageNo;
            } else {
                $query .= "&" . $this->keyword() . "=" . $pageNo;
            }
        }
        return $this->path() . "?" . $query;
    }

    /**
     * 取得总分页数
     *
     * @return integer
     * @since 1.0
     */
    function length()
    {
        if ($this->size() == 0) {
            return 0;
        }
        return ceil($this->total() / $this->size());
    }

    /**
     * 添加记录
     *
     * @param mixed $row 记录
     * @return MongoPage
     */
    function addRow($row)
    {
        $this->rows[] = $row;
        return $this;
    }

    /**
     * 添加记录集
     *
     * @param array $rows 记录集
     * @return MongoPage
     */
    function addRows(array $rows)
    {
        foreach ($rows as $row) {
            $this->rows[] = $row;
        }
        return $this;
    }

    /**
     * 取得记录集
     *
     * @return array
     */
    function rows()
    {
        return $this->rows;
    }

    /**
     * 设置记录集
     *
     * @param array|iterable $rows 记录集
     * @return MongoPage
     */
    function setRows($rows)
    {
        $this->rows = $rows;
        return $this;
    }

    /**
     * 取得键值对应的消息文本
     *
     * @param string $key 键值
     * @return string
     * @since 1.0
     */
    protected function message($key)
    {
        if (!$this->localeLoaded) {
            $locale = __LANG__;
            if (!$locale) {
                $locale = "default";
            }

            $message = x("~" . $key);
            if ($message) {
                return $message;
            }

            //简写
            $dirname = dirname(__FILE__) . "/lang";
            $langFile = $dirname . "/" . $locale . ".php";
            if (is_file($langFile)) {
                require($langFile);
                $this->messages = $message;
            }
            $this->localeLoaded = true;
        }
        if (is_array($this->messages) && array_key_exists($key, $this->messages)) {
            return $this->messages[$key];
        }
        return null;
    }

    /**
     * 转换成字符串
     *
     * @return string
     */
    public abstract function __toString();

    /**
     * 设置分页集尺寸
     *
     * @param integer $num 大于1
     * @return MongoPage
     * @since 1.0
     */
    function setPageSetNum($num)
    {
        $this->pageSetSize = $num;
        return $this;
    }

    /**
     * 取得分页集尺寸
     *
     * @return integer
     * @since 1.0
     */
    function pageSetNum()
    {
        return $this->pageSetSize;
    }

    static function pageWithStyle($style, array $params = null)
    {
        exit(__METHOD__ . " need to be implemented.");
    }
}