| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class Entry |
| 4 |
* |
| 5 |
* @since 1.10.0 |
| 6 |
* @package QuillForms |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace QuillForms; |
| 10 |
|
| 11 |
use ArrayAccess; |
| 12 |
use Exception; |
| 13 |
|
| 14 |
/** |
| 15 |
* Entry class |
| 16 |
* |
| 17 |
* @since 1.10.0 |
| 18 |
*/ |
| 19 |
class Entry implements ArrayAccess { |
| 20 |
|
| 21 |
|
| 22 |
/** |
| 23 |
* ID |
| 24 |
* |
| 25 |
* @since 1.10.0 |
| 26 |
* |
| 27 |
* @var int|null null if not saved. |
| 28 |
*/ |
| 29 |
public $ID; |
| 30 |
|
| 31 |
/** |
| 32 |
* Form id |
| 33 |
* |
| 34 |
* @since 1.10.0 |
| 35 |
* |
| 36 |
* @var int |
| 37 |
*/ |
| 38 |
public $form_id; |
| 39 |
|
| 40 |
/** |
| 41 |
* Date created |
| 42 |
* |
| 43 |
* @since 1.10.0 |
| 44 |
* |
| 45 |
* @var string GMT/UTC date/time with format 'Y-m-d H:i:s'. |
| 46 |
*/ |
| 47 |
public $date_created; |
| 48 |
|
| 49 |
/** |
| 50 |
* Date updated |
| 51 |
* |
| 52 |
* @since 1.10.0 |
| 53 |
* |
| 54 |
* @var string GMT/UTC date/time with format 'Y-m-d H:i:s'. |
| 55 |
*/ |
| 56 |
public $date_updated; |
| 57 |
|
| 58 |
/** |
| 59 |
* Records |
| 60 |
* Format: [ |
| 61 |
* '{section}' => [ // section is like 'fields', 'variables', ... |
| 62 |
* '{id}' => [ |
| 63 |
* 'value' => {value}, // 'value' maybe directly unreadable and can need process. |
| 64 |
* ] |
| 65 |
* ], |
| 66 |
* ] |
| 67 |
* |
| 68 |
* @since 1.10.0 |
| 69 |
* |
| 70 |
* @var array |
| 71 |
*/ |
| 72 |
public $records; |
| 73 |
|
| 74 |
/** |
| 75 |
* Meta |
| 76 |
* Format: [ |
| 77 |
* '{key}' => [ |
| 78 |
* {value}, // value is always readable. |
| 79 |
* ] |
| 80 |
* ] |
| 81 |
* |
| 82 |
* @since 1.10.0 |
| 83 |
* |
| 84 |
* @var array |
| 85 |
*/ |
| 86 |
public $meta; |
| 87 |
|
| 88 |
/** |
| 89 |
* Hash id |
| 90 |
* |
| 91 |
* @since 3.6.4 |
| 92 |
* |
| 93 |
* @var string|null |
| 94 |
*/ |
| 95 |
public $hash_id; |
| 96 |
/** |
| 97 |
* Get record value |
| 98 |
* |
| 99 |
* @since 1.13.0 |
| 100 |
* |
| 101 |
* @param string $type Record type. |
| 102 |
* @param string $id Record id. |
| 103 |
* @return mixed |
| 104 |
*/ |
| 105 |
public function get_record_value( $type, $id ) { |
| 106 |
$section = Entry_Record_Types::instance()->get( $type )['section']; |
| 107 |
return $this->records[ $section ][ $id ]['value'] ?? null; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Set record value |
| 112 |
* |
| 113 |
* @since 1.13.0 |
| 114 |
* |
| 115 |
* @param string $type Record type. |
| 116 |
* @param string $id Record id. |
| 117 |
* @param mixed $value Record value. |
| 118 |
* @return void |
| 119 |
*/ |
| 120 |
public function set_record_value( $type, $id, $value ) { |
| 121 |
$section = Entry_Record_Types::instance()->get( $type )['section']; |
| 122 |
$this->records[ $section ][ $id ]['value'] = $value; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Get meta value |
| 127 |
* |
| 128 |
* @since 1.13.0 |
| 129 |
* |
| 130 |
* @param string $id Meta id. |
| 131 |
* @return mixed |
| 132 |
*/ |
| 133 |
public function get_meta_value( $id ) { |
| 134 |
return $this->meta[ $id ]['value'] ?? null; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Set meta value |
| 139 |
* |
| 140 |
* @since 1.13.0 |
| 141 |
* |
| 142 |
* @param string $id Meta id. |
| 143 |
* @param mixed $value Meta value. |
| 144 |
* @return void |
| 145 |
*/ |
| 146 |
public function set_meta_value( $id, $value ) { |
| 147 |
$this->meta[ $id ]['value'] = $value; |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Get readable records |
| 152 |
* Returns the same $this->records with 'readable_value' with each record |
| 153 |
* |
| 154 |
* @since 1.10.0 |
| 155 |
* |
| 156 |
* @param array|null $form_data The form data. |
| 157 |
* @param string $context The context. |
| 158 |
* @return array |
| 159 |
*/ |
| 160 |
public function get_readable_records( $form_data, $context = 'html' ) { |
| 161 |
$records = $this->records; |
| 162 |
foreach ( Entry_Record_Types::instance()->get_all() as $type => $arg ) { |
| 163 |
foreach ( array_keys( $this->records[ $arg['section'] ] ?? array() ) as $id ) { |
| 164 |
$records[ $arg['section'] ][ $id ]['readable_value'] = $this->get_record_readable_value( $type, $id, $form_data, $context ); |
| 165 |
} |
| 166 |
} |
| 167 |
return $records; |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Get record readable value |
| 172 |
* |
| 173 |
* @since 1.10.0 |
| 174 |
* |
| 175 |
* @param string $record_type Record type. |
| 176 |
* @param string $record_id Record id. |
| 177 |
* @param array $form_data Form data. |
| 178 |
* @param string $context Context. |
| 179 |
* @return mixed |
| 180 |
*/ |
| 181 |
public function get_record_readable_value( $record_type, $record_id, $form_data, $context = 'html' ) { |
| 182 |
$get_readable_value = Entry_Record_Types::instance()->get( $record_type )['get_readable_value'] ?? null; |
| 183 |
if ( $get_readable_value ) { |
| 184 |
return $get_readable_value( $record_id, $this, $form_data, $context ); |
| 185 |
} else { |
| 186 |
return $this->get_record_value( $record_type, $record_id ); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
// @codingStandardsIgnoreStart |
| 191 |
// ArrayAccess methods, used to get 'id' and 'form_id' from entry as an array for backwards compatibility |
| 192 |
public function offsetExists( $offset ): bool { |
| 193 |
return in_array( $offset, array( 'id', 'form_id' ), true ); |
| 194 |
} |
| 195 |
public function offsetGet( $offset ): mixed { |
| 196 |
switch ( $offset ) { |
| 197 |
case 'id': |
| 198 |
return $this->ID; |
| 199 |
case 'form_id': |
| 200 |
return $this->form_id; |
| 201 |
default: |
| 202 |
return null; |
| 203 |
} |
| 204 |
} |
| 205 |
public function offsetSet( $offset, $value ): void { |
| 206 |
throw new Exception( 'Not allowed to set object properties through array access.' ); |
| 207 |
} |
| 208 |
public function offsetUnset( $offset ): void { |
| 209 |
throw new Exception( 'Not allowed to set object properties through array access.' ); |
| 210 |
} |
| 211 |
// @codingStandardsIgnoreEnd |
| 212 |
|
| 213 |
} |
| 214 |
|