record.php
78 lines
| 1 | <?php |
| 2 | |
| 3 | class PMXI_File_Record extends PMXI_Model_Record { |
| 4 | /** |
| 5 | * Initialize model instance |
| 6 | * @param array[optional] $data Array of record data to initialize object with |
| 7 | */ |
| 8 | public function __construct($data = array()) { |
| 9 | parent::__construct($data); |
| 10 | $this->setTable(PMXI_Plugin::getInstance()->getTablePrefix() . 'files'); |
| 11 | } |
| 12 | |
| 13 | /** |
| 14 | * @see PMXI_Model_Record::insert() |
| 15 | */ |
| 16 | public function insert() { |
| 17 | $file_contents = NULL; |
| 18 | if ($this->offsetExists('contents')) { |
| 19 | $file_contents = $this['contents']; |
| 20 | unset($this->contents); |
| 21 | } |
| 22 | |
| 23 | parent::insert(); |
| 24 | |
| 25 | if (isset($this->id) and ! is_null($file_contents)) { |
| 26 | file_put_contents(PMXI_Plugin::ROOT_DIR . '/history/' . $this->id, $file_contents); |
| 27 | } |
| 28 | |
| 29 | $list = new PMXI_File_List(); |
| 30 | $list->sweepHistory(); |
| 31 | return $this; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * @see PMXI_Model_Record::update() |
| 36 | */ |
| 37 | public function update() { |
| 38 | $file_contents = NULL; |
| 39 | if ($this->offsetExists('contents')) { |
| 40 | $file_contents = $this['contents']; |
| 41 | unset($this->contents); |
| 42 | } |
| 43 | |
| 44 | parent::update(); |
| 45 | |
| 46 | if (isset($this->id) and ! is_null($file_contents)) { |
| 47 | file_put_contents(PMXI_Plugin::ROOT_DIR . '/history/' . $this->id, $file_contents); |
| 48 | } |
| 49 | |
| 50 | return $this; |
| 51 | } |
| 52 | |
| 53 | public function __isset($field) { |
| 54 | if ('contents' == $field and ! $this->offsetExists($field)) { |
| 55 | return isset($this->id) and file_exists(PMXI_Plugin::ROOT_DIR . '/history/' . $this->id); |
| 56 | } |
| 57 | return parent::__isset($field); |
| 58 | } |
| 59 | |
| 60 | public function __get($field) { |
| 61 | if ('contents' == $field and ! $this->offsetExists('contents')) { |
| 62 | if (isset($this->contents)) { |
| 63 | $this['contents'] = file_get_contents(PMXI_Plugin::ROOT_DIR . '/history/' . $this->id); |
| 64 | } else { |
| 65 | $this->contents = NULL; |
| 66 | } |
| 67 | } |
| 68 | return parent::__get($field); |
| 69 | } |
| 70 | |
| 71 | public function delete() { |
| 72 | if ($this->id) { // delete history file first |
| 73 | $file_name = PMXI_Plugin::ROOT_DIR . '/history/' . $this->id; |
| 74 | is_file($file_name) and unlink($file_name); |
| 75 | } |
| 76 | return parent::delete(); |
| 77 | } |
| 78 | } |