Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 382 |
|
0.00% |
0 / 12 |
CRAP | |
0.00% |
0 / 1 |
| zbsDAL_eventreminders | |
0.00% |
0 / 382 |
|
0.00% |
0 / 12 |
21756 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
56 | |||
| getSingle | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getIDList | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
| getEventreminder | |
0.00% |
0 / 60 |
|
0.00% |
0 / 1 |
650 | |||
| getEventreminders | |
0.00% |
0 / 71 |
|
0.00% |
0 / 1 |
702 | |||
| getEventReminderCount | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
56 | |||
| addUpdateEventreminder | |
0.00% |
0 / 126 |
|
0.00% |
0 / 1 |
2352 | |||
| setSentStatus | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
12 | |||
| deleteEventreminder | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
110 | |||
| deleteEventRemindersForEvent | |
0.00% |
0 / 29 |
|
0.00% |
0 / 1 |
132 | |||
| tidy_eventreminder | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
56 | |||
| db_ready_eventreminder | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /* |
| 3 | * Jetpack CRM |
| 4 | * https://jetpackcrm.com |
| 5 | * V3.0+ |
| 6 | * |
| 7 | * Copyright 2020 Automattic |
| 8 | * |
| 9 | * Date: 14/01/19 |
| 10 | */ |
| 11 | |
| 12 | defined( 'ZEROBSCRM_PATH' ) || exit( 0 ); |
| 13 | |
| 14 | /** |
| 15 | * ZBS DAL >> Event Reminders (for events/tasks) |
| 16 | * |
| 17 | * @author Woody Hayday <hello@jetpackcrm.com> |
| 18 | * @version 2.0 |
| 19 | * @access public |
| 20 | * @see https://jetpackcrm.com/kb |
| 21 | */ |
| 22 | class zbsDAL_eventreminders extends zbsDAL_ObjectLayer { |
| 23 | |
| 24 | protected $objectType = ZBS_TYPE_TASK_REMINDER; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.PropertyNotSnakeCase, Squiz.Commenting.VariableComment.Missing |
| 25 | protected $objectDBPrefix = 'zbser_'; |
| 26 | protected $objectModel = array( |
| 27 | |
| 28 | // ID |
| 29 | 'ID' => array( |
| 30 | 'fieldname' => 'ID', |
| 31 | 'format' => 'int', |
| 32 | ), |
| 33 | |
| 34 | // site + team generics |
| 35 | 'zbs_site' => array( |
| 36 | 'fieldname' => 'zbs_site', |
| 37 | 'format' => 'int', |
| 38 | ), |
| 39 | 'zbs_team' => array( |
| 40 | 'fieldname' => 'zbs_team', |
| 41 | 'format' => 'int', |
| 42 | ), |
| 43 | 'zbs_owner' => array( |
| 44 | 'fieldname' => 'zbs_owner', |
| 45 | 'format' => 'int', |
| 46 | ), |
| 47 | |
| 48 | // other fields |
| 49 | 'event' => array( |
| 50 | 'fieldname' => 'zbser_event', |
| 51 | 'format' => 'int', |
| 52 | ), |
| 53 | 'remind_at' => array( |
| 54 | 'fieldname' => 'zbser_remind_at', |
| 55 | 'format' => 'int', |
| 56 | ), |
| 57 | 'sent' => array( |
| 58 | 'fieldname' => 'zbser_sent', |
| 59 | 'format' => 'int', |
| 60 | ), |
| 61 | 'created' => array( |
| 62 | 'fieldname' => 'zbser_created', |
| 63 | 'format' => 'uts', |
| 64 | ), |
| 65 | 'lastupdated' => array( |
| 66 | 'fieldname' => 'zbser_lastupdated', |
| 67 | 'format' => 'uts', |
| 68 | ), |
| 69 | |
| 70 | ); |
| 71 | |
| 72 | function __construct( $args = array() ) { |
| 73 | |
| 74 | #} =========== LOAD ARGS ============== |
| 75 | $defaultArgs = array( |
| 76 | |
| 77 | // 'tag' => false, |
| 78 | |
| 79 | ); |
| 80 | foreach ( $defaultArgs as $argK => $argV ) { |
| 81 | $this->$argK = $argV; |
| 82 | if ( is_array( $args ) && isset( $args[ $argK ] ) ) { |
| 83 | if ( is_array( $args[ $argK ] ) ) { |
| 84 | $newData = $this->$argK; |
| 85 | if ( ! is_array( $newData ) ) { |
| 86 | $newData = array(); |
| 87 | } foreach ( $args[ $argK ] as $subK => $subV ) { |
| 88 | $newData[ $subK ] = $subV; |
| 89 | }$this->$argK = $newData; |
| 90 | } else { |
| 91 | $this->$argK = $args[ $argK ]; } |
| 92 | } |
| 93 | } |
| 94 | #} =========== / LOAD ARGS ============= |
| 95 | } |
| 96 | |
| 97 | // ==================================================================================== |
| 98 | // =========== EVENTREMINDER ======================================================= |
| 99 | |
| 100 | // generic get Company (by ID) |
| 101 | // Super simplistic wrapper used by edit page etc. (generically called via dal->contacts->getSingle etc.) |
| 102 | public function getSingle( $ID = -1 ) { |
| 103 | |
| 104 | return $this->getEventreminder( $ID ); |
| 105 | } |
| 106 | |
| 107 | // generic get (by ID list) |
| 108 | // Super simplistic wrapper used by MVP Export v3.0 |
| 109 | public function getIDList( $IDs = false ) { |
| 110 | |
| 111 | return $this->getEventReminders( |
| 112 | array( |
| 113 | 'inArr' => $IDs, |
| 114 | 'page' => -1, |
| 115 | 'perPage' => -1, |
| 116 | ) |
| 117 | ); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * returns full eventreminder line +- details |
| 122 | * |
| 123 | * @param int id contact id |
| 124 | * @param array $args Associative array of arguments |
| 125 | * |
| 126 | * @return array eventreminder object |
| 127 | */ |
| 128 | public function getEventreminder( $id = -1, $args = array() ) { |
| 129 | |
| 130 | global $zbs; |
| 131 | |
| 132 | #} =========== LOAD ARGS ============== |
| 133 | $defaultArgs = array( |
| 134 | |
| 135 | // permissions |
| 136 | 'ignoreowner' => zeroBSCRM_DAL2_ignoreOwnership( ZBS_TYPE_TASK_REMINDER ), // this'll let you not-check the owner of obj |
| 137 | |
| 138 | // returns scalar ID of line |
| 139 | 'onlyID' => false, |
| 140 | |
| 141 | 'fields' => false, // false = *, array = fieldnames |
| 142 | |
| 143 | ); |
| 144 | foreach ( $defaultArgs as $argK => $argV ) { |
| 145 | $$argK = $argV; |
| 146 | if ( is_array( $args ) && isset( $args[ $argK ] ) ) { |
| 147 | if ( is_array( $args[ $argK ] ) ) { |
| 148 | $newData = $$argK; |
| 149 | if ( ! is_array( $newData ) ) { |
| 150 | $newData = array(); |
| 151 | } foreach ( $args[ $argK ] as $subK => $subV ) { |
| 152 | $newData[ $subK ] = $subV; |
| 153 | }$$argK = $newData; |
| 154 | } else { |
| 155 | $$argK = $args[ $argK ]; } |
| 156 | } |
| 157 | } |
| 158 | #} =========== / LOAD ARGS ============= |
| 159 | |
| 160 | #} Check ID |
| 161 | $id = (int) $id; |
| 162 | if ( |
| 163 | ( ! empty( $id ) && $id > 0 ) |
| 164 | || |
| 165 | ( ! empty( $email ) ) |
| 166 | || |
| 167 | ( ! empty( $externalSource ) && ! empty( $externalSourceUID ) ) |
| 168 | ) { |
| 169 | |
| 170 | global $ZBSCRM_t, $wpdb; |
| 171 | $wheres = array( 'direct' => array() ); |
| 172 | $whereStr = ''; |
| 173 | $additionalWhere = ''; |
| 174 | $params = array(); |
| 175 | $res = array(); |
| 176 | $extraSelect = ''; |
| 177 | |
| 178 | #} ============= PRE-QUERY ============ |
| 179 | |
| 180 | $selector = 'eventreminder.*'; |
| 181 | if ( is_array( $fields ) ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable |
| 182 | $selector = ''; |
| 183 | |
| 184 | // always needs id, so add if not present |
| 185 | if ( ! in_array( 'ID', $fields ) ) { |
| 186 | $selector = 'eventreminder.ID'; |
| 187 | } |
| 188 | |
| 189 | foreach ( $fields as $f ) { |
| 190 | if ( ! empty( $selector ) ) { |
| 191 | $selector .= ','; |
| 192 | } |
| 193 | $selector .= 'eventreminder.' . $f; |
| 194 | } |
| 195 | } elseif ( $onlyID ) { |
| 196 | $selector = 'eventreminder.ID'; |
| 197 | } |
| 198 | |
| 199 | #} ============ / PRE-QUERY =========== |
| 200 | |
| 201 | #} Build query |
| 202 | $query = 'SELECT ' . $selector . $extraSelect . ' FROM ' . $ZBSCRM_t['eventreminders'] . ' as eventreminder'; |
| 203 | #} ============= WHERE ================ |
| 204 | |
| 205 | if ( ! empty( $id ) && $id > 0 ) { |
| 206 | |
| 207 | #} Add ID |
| 208 | $wheres['ID'] = array( 'ID', '=', '%d', $id ); |
| 209 | |
| 210 | } |
| 211 | |
| 212 | #} ============ / WHERE ============== |
| 213 | |
| 214 | #} Build out any WHERE clauses |
| 215 | $wheresArr = $this->buildWheres( $wheres, $whereStr, $params ); |
| 216 | $whereStr = $wheresArr['where']; |
| 217 | $params = $params + $wheresArr['params']; |
| 218 | #} / Build WHERE |
| 219 | |
| 220 | #} Ownership v1.0 - the following adds SITE + TEAM checks, and (optionally), owner |
| 221 | $params = array_merge( $params, $this->ownershipQueryVars( $ignoreowner ) ); // merges in any req. |
| 222 | $ownQ = $this->ownershipSQL( $ignoreowner ); |
| 223 | if ( ! empty( $ownQ ) ) { |
| 224 | $additionalWhere = $this->spaceAnd( $additionalWhere ) . $ownQ; // adds str to query |
| 225 | } |
| 226 | #} / Ownership |
| 227 | |
| 228 | #} Append to sql (this also automatically deals with sortby and paging) |
| 229 | $query .= $this->buildWhereStr( $whereStr, $additionalWhere ) . $this->buildSort( 'ID', 'DESC' ) . $this->buildPaging( 0, 1 ); |
| 230 | |
| 231 | try { |
| 232 | |
| 233 | #} Prep & run query |
| 234 | $queryObj = $this->prepare( $query, $params ); |
| 235 | $potentialRes = $wpdb->get_row( $queryObj, OBJECT ); |
| 236 | |
| 237 | } catch ( Exception $e ) { |
| 238 | |
| 239 | #} General SQL Err |
| 240 | $this->catchSQLError( $e ); |
| 241 | |
| 242 | } |
| 243 | |
| 244 | #} Interpret Results (ROW) |
| 245 | if ( isset( $potentialRes ) && isset( $potentialRes->ID ) ) { |
| 246 | |
| 247 | #} Has results, tidy + return |
| 248 | |
| 249 | #} Only ID? return it directly |
| 250 | if ( $onlyID ) { |
| 251 | return $potentialRes->ID; |
| 252 | } |
| 253 | |
| 254 | // tidy |
| 255 | if ( is_array( $fields ) ) { |
| 256 | // guesses fields based on table col names |
| 257 | $res = $this->lazyTidyGeneric( $potentialRes ); |
| 258 | } else { |
| 259 | // proper tidy |
| 260 | $res = $this->tidy_eventreminder( $potentialRes ); |
| 261 | } |
| 262 | |
| 263 | return $res; |
| 264 | |
| 265 | } |
| 266 | } // / if ID |
| 267 | |
| 268 | return false; |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * returns eventreminder detail lines |
| 273 | * |
| 274 | * @param array $args Associative array of arguments |
| 275 | * |
| 276 | * @return array of eventreminder lines |
| 277 | */ |
| 278 | public function getEventreminders( $args = array() ) { |
| 279 | |
| 280 | global $zbs; |
| 281 | |
| 282 | #} ============ LOAD ARGS ============= |
| 283 | $defaultArgs = array( |
| 284 | |
| 285 | // Search/Filtering (leave as false to ignore) |
| 286 | 'eventID' => false, |
| 287 | |
| 288 | // due date |
| 289 | 'dueBefore' => false, // uts (due before) |
| 290 | 'dueAfter' => false, // uts (due after) |
| 291 | |
| 292 | // status |
| 293 | 'sent' => 0, // (if set bool) (false = hasn't been sent, true = has been sent) |
| 294 | |
| 295 | // returns |
| 296 | 'count' => false, |
| 297 | 'withDueUTS' => false, // if true returns 'due' field (UTS due) |
| 298 | |
| 299 | 'sortByField' => 'ID', |
| 300 | 'sortOrder' => 'ASC', |
| 301 | 'page' => 0, // this is what page it is (gets * by for limit) |
| 302 | 'perPage' => 100, |
| 303 | 'whereCase' => 'AND', // DEFAULT = AND |
| 304 | |
| 305 | // permissions |
| 306 | 'ignoreowner' => zeroBSCRM_DAL2_ignoreOwnership( ZBS_TYPE_TASK_REMINDER ), // this'll let you not-check the owner of obj |
| 307 | |
| 308 | ); |
| 309 | foreach ( $defaultArgs as $argK => $argV ) { |
| 310 | $$argK = $argV; |
| 311 | if ( is_array( $args ) && isset( $args[ $argK ] ) ) { |
| 312 | if ( is_array( $args[ $argK ] ) ) { |
| 313 | $newData = $$argK; |
| 314 | if ( ! is_array( $newData ) ) { |
| 315 | $newData = array(); |
| 316 | } foreach ( $args[ $argK ] as $subK => $subV ) { |
| 317 | $newData[ $subK ] = $subV; |
| 318 | }$$argK = $newData; |
| 319 | } else { |
| 320 | $$argK = $args[ $argK ]; } |
| 321 | } |
| 322 | } |
| 323 | #} =========== / LOAD ARGS ============= |
| 324 | |
| 325 | global $ZBSCRM_t, $wpdb, $zbs; |
| 326 | $wheres = array( 'direct' => array() ); |
| 327 | $whereStr = ''; |
| 328 | $additionalWhere = ''; |
| 329 | $params = array(); |
| 330 | $res = array(); |
| 331 | $joinQ = ''; |
| 332 | $extraSelect = ''; |
| 333 | |
| 334 | #} ============= PRE-QUERY ============ |
| 335 | |
| 336 | #} Capitalise this |
| 337 | $sortOrder = strtoupper( $sortOrder ); |
| 338 | |
| 339 | // If just count, turn off any extra gumpf |
| 340 | if ( $count ) { |
| 341 | |
| 342 | $withDueUTS = false; |
| 343 | |
| 344 | } |
| 345 | |
| 346 | // include 'due' column |
| 347 | // @phan-suppress-next-line PhanImpossibleCondition -- This is false by default but can be overridden by $args. |
| 348 | if ( $withDueUTS ) { |
| 349 | |
| 350 | $extraSelect .= ',(eventreminder.zbser_remind_at + (SELECT zbse_start FROM ' . $ZBSCRM_t['events'] . ' WHERE ID = eventreminder.zbser_event)) due'; |
| 351 | |
| 352 | } |
| 353 | |
| 354 | #} ============ / PRE-QUERY =========== |
| 355 | |
| 356 | #} Build query |
| 357 | $query = 'SELECT eventreminder.*' . $extraSelect . ' FROM ' . $ZBSCRM_t['eventreminders'] . ' as eventreminder' . $joinQ; |
| 358 | |
| 359 | #} Count override |
| 360 | if ( $count ) { |
| 361 | $query = 'SELECT COUNT(eventreminder.ID) FROM ' . $ZBSCRM_t['eventreminders'] . ' as eventreminder' . $joinQ; |
| 362 | } |
| 363 | |
| 364 | #} ============= WHERE ================ |
| 365 | |
| 366 | #} associated event id |
| 367 | if ( ! empty( $eventID ) && $eventID > 0 ) { |
| 368 | |
| 369 | // has id + type to match to (e.g. quote 123) |
| 370 | // simpler than this, we're not using objid links: $wheres['associatedObjType'] = array('ID','IN','(SELECT zbsol_objid_from FROM '.$ZBSCRM_t['objlinks']." WHERE zbsol_objtype_from = ".ZBS_TYPE_LINEITEM." AND zbsol_objtype_to = ".ZBS_TYPE_TASK." AND zbsol_objid_to = %d)",$eventID); |
| 371 | $wheres['zbser_event'] = array( 'zbser_event', '=', '%d', $eventID ); |
| 372 | |
| 373 | } |
| 374 | |
| 375 | // dueBefore |
| 376 | if ( ! empty( $dueBefore ) && $dueBefore > 0 ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase,VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable |
| 377 | |
| 378 | // is (event reminder at (e.g. -86400) + event start time (uts)) before $dueBefore |
| 379 | $wheres['direct'][] = array( '(eventreminder.zbser_remind_at + (SELECT zbse_start FROM ' . $ZBSCRM_t['events'] . ' WHERE ID = eventreminder.zbser_event) < %d)', array( $dueBefore ) ); |
| 380 | |
| 381 | } |
| 382 | // dueAfter |
| 383 | if ( ! empty( $dueAfter ) && $dueAfter > 0 ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase,VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable |
| 384 | |
| 385 | // is (event reminder at (e.g. -86400) + event start time (uts)) after $dueAfter |
| 386 | $wheres['direct'][] = array( '(eventreminder.zbser_remind_at + (SELECT zbse_start FROM ' . $ZBSCRM_t['events'] . ' WHERE ID = eventreminder.zbser_event) > %d)', array( $dueAfter ) ); |
| 387 | |
| 388 | } |
| 389 | |
| 390 | // sent (if set as bool) |
| 391 | if ( $sent === true ) { |
| 392 | |
| 393 | // has been sent |
| 394 | $wheres['zbser_sent'] = array( 'zbser_sent', '=', '1' ); |
| 395 | |
| 396 | } elseif ( $sent === false ) { |
| 397 | |
| 398 | // has not been sent |
| 399 | $wheres['zbser_sent'] = array( 'zbser_sent', '=', '-1' ); |
| 400 | |
| 401 | } |
| 402 | |
| 403 | #} ============ / WHERE =============== |
| 404 | |
| 405 | #} CHECK this + reset to default if faulty |
| 406 | if ( ! in_array( $whereCase, array( 'AND', 'OR' ) ) ) { |
| 407 | $whereCase = 'AND'; |
| 408 | } |
| 409 | |
| 410 | #} Build out any WHERE clauses |
| 411 | $wheresArr = $this->buildWheres( $wheres, $whereStr, $params, $whereCase ); |
| 412 | $whereStr = $wheresArr['where']; |
| 413 | $params = $params + $wheresArr['params']; |
| 414 | #} / Build WHERE |
| 415 | |
| 416 | #} Ownership v1.0 - the following adds SITE + TEAM checks, and (optionally), owner |
| 417 | $params = array_merge( $params, $this->ownershipQueryVars( $ignoreowner ) ); // merges in any req. |
| 418 | $ownQ = $this->ownershipSQL( $ignoreowner, 'contact' ); |
| 419 | if ( ! empty( $ownQ ) ) { |
| 420 | $additionalWhere = $this->spaceAnd( $additionalWhere ) . $ownQ; // adds str to query |
| 421 | } |
| 422 | #} / Ownership |
| 423 | |
| 424 | #} Append to sql (this also automatically deals with sortby and paging) |
| 425 | $query .= $this->buildWhereStr( $whereStr, $additionalWhere ) . $this->buildSort( $sortByField, $sortOrder ) . $this->buildPaging( $page, $perPage ); |
| 426 | |
| 427 | try { |
| 428 | |
| 429 | #} Prep & run query |
| 430 | $queryObj = $this->prepare( $query, $params ); |
| 431 | |
| 432 | #} Catch count + return if requested |
| 433 | if ( $count ) { |
| 434 | return $wpdb->get_var( $queryObj ); |
| 435 | } |
| 436 | |
| 437 | #} else continue.. |
| 438 | $potentialRes = $wpdb->get_results( $queryObj, OBJECT ); |
| 439 | |
| 440 | } catch ( Exception $e ) { |
| 441 | |
| 442 | #} General SQL Err |
| 443 | $this->catchSQLError( $e ); |
| 444 | |
| 445 | } |
| 446 | |
| 447 | #} Interpret results (Result Set - multi-row) |
| 448 | if ( isset( $potentialRes ) && is_array( $potentialRes ) && count( $potentialRes ) > 0 ) { |
| 449 | |
| 450 | #} Has results, tidy + return |
| 451 | foreach ( $potentialRes as $resDataLine ) { |
| 452 | |
| 453 | // tidy |
| 454 | $resArr = $this->tidy_eventreminder( $resDataLine ); |
| 455 | |
| 456 | $res[] = $resArr; |
| 457 | |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | return $res; |
| 462 | } |
| 463 | |
| 464 | /** |
| 465 | * Returns a count of event reminders (owned) |
| 466 | * .. inc by status |
| 467 | * |
| 468 | * @return int count |
| 469 | */ |
| 470 | public function getEventReminderCount( $args = array() ) { |
| 471 | |
| 472 | #} ============ LOAD ARGS ============= |
| 473 | $defaultArgs = array( |
| 474 | |
| 475 | // Search/Filtering (leave as false to ignore) |
| 476 | |
| 477 | // permissions |
| 478 | 'ignoreowner' => true, // this'll let you not-check the owner of obj |
| 479 | |
| 480 | ); |
| 481 | foreach ( $defaultArgs as $argK => $argV ) { |
| 482 | $$argK = $argV; |
| 483 | if ( is_array( $args ) && isset( $args[ $argK ] ) ) { |
| 484 | if ( is_array( $args[ $argK ] ) ) { |
| 485 | $newData = $$argK; |
| 486 | if ( ! is_array( $newData ) ) { |
| 487 | $newData = array(); |
| 488 | } foreach ( $args[ $argK ] as $subK => $subV ) { |
| 489 | $newData[ $subK ] = $subV; |
| 490 | }$$argK = $newData; |
| 491 | } else { |
| 492 | $$argK = $args[ $argK ]; } |
| 493 | } |
| 494 | } |
| 495 | #} =========== / LOAD ARGS ============= |
| 496 | |
| 497 | $whereArr = array(); |
| 498 | |
| 499 | return $this->DAL()->getFieldByWHERE( |
| 500 | array( |
| 501 | 'objtype' => ZBS_TYPE_TASK_REMINDER, |
| 502 | 'colname' => 'COUNT(ID)', |
| 503 | 'where' => $whereArr, |
| 504 | 'ignoreowner' => $ignoreowner, |
| 505 | ) |
| 506 | ); |
| 507 | } |
| 508 | |
| 509 | /** |
| 510 | * adds or updates a eventreminder object |
| 511 | * |
| 512 | * @param array $args Associative array of arguments |
| 513 | * id (if update), owner, data (array of field data) |
| 514 | * |
| 515 | * @return int line ID |
| 516 | */ |
| 517 | public function addUpdateEventreminder( $args = array() ) { |
| 518 | |
| 519 | global $ZBSCRM_t, $wpdb, $zbs; |
| 520 | |
| 521 | #} ============ LOAD ARGS ============= |
| 522 | $defaultArgs = array( |
| 523 | |
| 524 | 'id' => -1, |
| 525 | 'owner' => -1, |
| 526 | |
| 527 | // fields (directly) |
| 528 | 'data' => array( |
| 529 | |
| 530 | 'event' => -1, |
| 531 | 'remind_at' => '', |
| 532 | 'sent' => '', |
| 533 | 'lastupdated' => '', |
| 534 | |
| 535 | // allow this to be set for MS sync etc. |
| 536 | 'created' => -1, |
| 537 | |
| 538 | ), |
| 539 | |
| 540 | 'limitedFields' => -1, // if this is set it OVERRIDES data (allowing you to set specific fields + leave rest in tact) |
| 541 | // ^^ will look like: array(array('key'=>x,'val'=>y,'type'=>'%s')) |
| 542 | |
| 543 | 'silentInsert' => false, // this was for init Migration - it KILLS all IA for newEventreminder (because is migrating, not creating new :) this was -1 before |
| 544 | |
| 545 | 'do_not_update_blanks' => false, // this allows you to not update fields if blank (same as fieldoverride for extsource -> in) |
| 546 | |
| 547 | ); |
| 548 | foreach ( $defaultArgs as $argK => $argV ) { |
| 549 | $$argK = $argV; |
| 550 | if ( is_array( $args ) && isset( $args[ $argK ] ) ) { |
| 551 | if ( is_array( $args[ $argK ] ) ) { |
| 552 | $newData = $$argK; |
| 553 | if ( ! is_array( $newData ) ) { |
| 554 | $newData = array(); |
| 555 | } foreach ( $args[ $argK ] as $subK => $subV ) { |
| 556 | $newData[ $subK ] = $subV; |
| 557 | }$$argK = $newData; |
| 558 | } else { |
| 559 | $$argK = $args[ $argK ]; } |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | #} =========== / LOAD ARGS ============ |
| 564 | |
| 565 | #} ========== CHECK FIELDS ============ |
| 566 | |
| 567 | $id = (int) $id; |
| 568 | |
| 569 | // here we check that the potential owner CAN even own |
| 570 | if ( $owner > 0 && ! user_can( $owner, 'admin_zerobs_usr' ) ) { |
| 571 | $owner = -1; |
| 572 | } |
| 573 | |
| 574 | // if owner = -1, add current |
| 575 | if ( ! isset( $owner ) || $owner === -1 ) { |
| 576 | $owner = zeroBSCRM_user(); } |
| 577 | |
| 578 | if ( is_array( $limitedFields ) ) { |
| 579 | |
| 580 | // LIMITED UPDATE (only a few fields.) |
| 581 | if ( ! is_array( $limitedFields ) || count( $limitedFields ) <= 0 ) { |
| 582 | return false; |
| 583 | } |
| 584 | // REQ. ID too (can only update) |
| 585 | if ( empty( $id ) || $id <= 0 ) { |
| 586 | return false; |
| 587 | } |
| 588 | } else { |
| 589 | |
| 590 | // NORMAL, FULL UPDATE |
| 591 | |
| 592 | } |
| 593 | |
| 594 | // if no eventID, return false |
| 595 | $event = -1; |
| 596 | if ( ! empty( $data['event'] ) ) { |
| 597 | $event = (int) $data['event']; // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable -- the block containing this line guarantees the var exists. |
| 598 | } |
| 599 | if ( $event <= 0 && ! $limitedFields ) { |
| 600 | return false; |
| 601 | } |
| 602 | |
| 603 | #} If no status, and default is specified in settings, add that in :) |
| 604 | /* |
| 605 | if (is_null($data['status']) || !isset($data['status']) || empty($data['status'])){ |
| 606 | |
| 607 | // Default status for obj? -> this one gets for contacts -> $zbsCustomerMeta['status'] = zeroBSCRM_getSetting('defaultstatus'); |
| 608 | |
| 609 | } */ |
| 610 | |
| 611 | #} ========= / CHECK FIELDS =========== |
| 612 | |
| 613 | #} ========= OVERRIDE SETTING (Deny blank overrides) =========== |
| 614 | |
| 615 | // either ext source + setting, or set by the func call |
| 616 | if ( $do_not_update_blanks ) { |
| 617 | |
| 618 | // this setting says 'don't override filled-out data with blanks' |
| 619 | // so here we check through any passed blanks + convert to limitedFields |
| 620 | // only matters if $id is set (there is somt to update not add |
| 621 | if ( isset( $id ) && ! empty( $id ) && $id > 0 ) { |
| 622 | |
| 623 | // get data to copy over (for now, this is required to remove 'fullname' etc.) |
| 624 | $dbData = $this->db_ready_eventreminder( $data ); |
| 625 | // unset($dbData['id']); // this is unset because we use $id, and is update, so not req. legacy issue |
| 626 | // unset($dbData['created']); // this is unset because this uses an obj which has been 'updated' against original details, where created is output in the WRONG format :) |
| 627 | |
| 628 | $origData = $data; // $data = array(); |
| 629 | $limitedData = array(); // array(array('key'=>'zbser_x','val'=>y,'type'=>'%s')) |
| 630 | |
| 631 | // cycle through + translate into limitedFields (removing any blanks, or arrays (e.g. externalSources)) |
| 632 | // we also have to remake a 'faux' data (removing blanks for tags etc.) for the post-update updates |
| 633 | foreach ( $dbData as $k => $v ) { |
| 634 | |
| 635 | $intV = (int) $v; |
| 636 | |
| 637 | // only add if valuenot empty |
| 638 | if ( ! is_array( $v ) && ! empty( $v ) && $v != '' && $v !== 0 && $v !== -1 && $intV !== -1 ) { |
| 639 | |
| 640 | // add to update arr |
| 641 | $limitedData[] = array( |
| 642 | 'key' => 'zbser_' . $k, // we have to add zbser_ here because translating from data -> limited fields |
| 643 | 'val' => $v, |
| 644 | 'type' => $this->getTypeStr( 'zbser_' . $k ), |
| 645 | ); |
| 646 | |
| 647 | // add to remade $data for post-update updates |
| 648 | $data[ $k ] = $v; |
| 649 | |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | // copy over |
| 654 | $limitedFields = $limitedData; |
| 655 | |
| 656 | } // / if ID |
| 657 | |
| 658 | } // / if do_not_update_blanks |
| 659 | |
| 660 | #} ========= / OVERRIDE SETTING (Deny blank overrides) =========== |
| 661 | |
| 662 | #} ========= BUILD DATA =========== |
| 663 | |
| 664 | $update = false; |
| 665 | $dataArr = array(); |
| 666 | $typeArr = array(); |
| 667 | |
| 668 | if ( is_array( $limitedFields ) ) { |
| 669 | |
| 670 | // LIMITED FIELDS |
| 671 | $update = true; |
| 672 | |
| 673 | // cycle through |
| 674 | foreach ( $limitedFields as $field ) { |
| 675 | |
| 676 | // some weird case where getting empties, so added check |
| 677 | if ( ! empty( $field['key'] ) ) { |
| 678 | $dataArr[ $field['key'] ] = $field['val']; |
| 679 | $typeArr[] = $field['type']; |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | // add update time |
| 684 | if ( ! isset( $dataArr['zbser_lastupdated'] ) ) { |
| 685 | $dataArr['zbser_lastupdated'] = time(); |
| 686 | $typeArr[] = '%d'; } |
| 687 | } else { |
| 688 | |
| 689 | // FULL UPDATE/INSERT |
| 690 | |
| 691 | // UPDATE |
| 692 | $dataArr = array( |
| 693 | |
| 694 | // ownership |
| 695 | // no need to update these (as of yet) - can't move teams etc. |
| 696 | // 'zbs_site' => zeroBSCRM_installSite(), |
| 697 | // 'zbs_team' => zeroBSCRM_installTeam(), |
| 698 | // 'zbs_owner' => $owner, |
| 699 | |
| 700 | 'zbser_event' => $data['event'], |
| 701 | 'zbser_remind_at' => $data['remind_at'], |
| 702 | 'zbser_sent' => $data['sent'], |
| 703 | 'zbser_lastupdated' => time(), |
| 704 | |
| 705 | ); |
| 706 | |
| 707 | $typeArr = array( // field data types |
| 708 | // '%d', // site |
| 709 | // '%d', // team |
| 710 | // '%d', // owner |
| 711 | |
| 712 | '%d', |
| 713 | '%d', |
| 714 | '%d', |
| 715 | '%d', |
| 716 | |
| 717 | ); |
| 718 | |
| 719 | if ( ! empty( $id ) && $id > 0 ) { |
| 720 | |
| 721 | // is update |
| 722 | $update = true; |
| 723 | |
| 724 | } else { |
| 725 | |
| 726 | // INSERT (get's few extra :D) |
| 727 | $update = false; |
| 728 | $dataArr['zbs_site'] = zeroBSCRM_site(); |
| 729 | $typeArr[] = '%d'; |
| 730 | $dataArr['zbs_team'] = zeroBSCRM_team(); |
| 731 | $typeArr[] = '%d'; |
| 732 | $dataArr['zbs_owner'] = $owner; |
| 733 | $typeArr[] = '%d'; |
| 734 | if ( isset( $data['created'] ) && ! empty( $data['created'] ) && $data['created'] !== -1 ) { |
| 735 | $dataArr['zbser_created'] = $data['created']; |
| 736 | $typeArr[] = '%d'; |
| 737 | } else { |
| 738 | $dataArr['zbser_created'] = time(); |
| 739 | $typeArr[] = '%d'; |
| 740 | } |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | #} ========= / BUILD DATA =========== |
| 745 | |
| 746 | #} ============================================================ |
| 747 | #} ========= CHECK force_uniques & not_empty & max_len ======== |
| 748 | |
| 749 | // if we're passing limitedFields we skip these, for now |
| 750 | // #v3.1 - would make sense to unique/nonempty check just the limited fields. #gh-145 |
| 751 | if ( ! is_array( $limitedFields ) ) { |
| 752 | |
| 753 | // verify uniques |
| 754 | if ( ! $this->verifyUniqueValues( $data, $id ) ) { |
| 755 | return false; // / fails unique field verify |
| 756 | } |
| 757 | |
| 758 | // verify not_empty |
| 759 | if ( ! $this->verifyNonEmptyValues( $data ) ) { |
| 760 | return false; // / fails empty field verify |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | // whatever we do we check for max_len breaches and abbreviate to avoid wpdb rejections |
| 765 | $dataArr = $this->wpdbChecks( $dataArr ); |
| 766 | |
| 767 | #} ========= / CHECK force_uniques & not_empty ================ |
| 768 | #} ============================================================ |
| 769 | |
| 770 | #} Check if ID present |
| 771 | if ( $update ) { |
| 772 | |
| 773 | #} Attempt update |
| 774 | if ( $wpdb->update( |
| 775 | $ZBSCRM_t['eventreminders'], |
| 776 | $dataArr, |
| 777 | array( // where |
| 778 | 'ID' => $id, |
| 779 | ), |
| 780 | $typeArr, |
| 781 | array( // where data types |
| 782 | '%d', |
| 783 | ) |
| 784 | ) !== false ) { |
| 785 | |
| 786 | // if passing limitedFields instead of data, we ignore the following |
| 787 | // this doesn't work, because data is in args default as arr |
| 788 | // if (isset($data) && is_array($data)){ |
| 789 | // so... |
| 790 | if ( ! isset( $limitedFields ) || ! is_array( $limitedFields ) || $limitedFields == -1 ) { |
| 791 | |
| 792 | } // / if $data |
| 793 | |
| 794 | // Successfully updated - Return id |
| 795 | return $id; |
| 796 | |
| 797 | } else { |
| 798 | |
| 799 | $msg = __( 'DB Update Failed', 'zero-bs-crm' ); |
| 800 | $zbs->DAL->addError( 302, $this->objectType, $msg, $dataArr ); |
| 801 | |
| 802 | // FAILED update |
| 803 | return false; |
| 804 | |
| 805 | } |
| 806 | } else { |
| 807 | |
| 808 | #} No ID - must be an INSERT |
| 809 | if ( $wpdb->insert( |
| 810 | $ZBSCRM_t['eventreminders'], |
| 811 | $dataArr, |
| 812 | $typeArr |
| 813 | ) > 0 ) { |
| 814 | |
| 815 | #} Successfully inserted, lets return new ID |
| 816 | $newID = $wpdb->insert_id; |
| 817 | |
| 818 | return $newID; |
| 819 | |
| 820 | } else { |
| 821 | |
| 822 | $msg = __( 'DB Insert Failed', 'zero-bs-crm' ); |
| 823 | $zbs->DAL->addError( 303, $this->objectType, $msg, $dataArr ); |
| 824 | |
| 825 | #} Failed to Insert |
| 826 | return false; |
| 827 | |
| 828 | } |
| 829 | } |
| 830 | |
| 831 | return false; |
| 832 | } |
| 833 | |
| 834 | /** |
| 835 | * updates sent status for an event reminder |
| 836 | * |
| 837 | * @param int id Event Reminder ID |
| 838 | * @param int Sent Status (-1 = unsent, 1 = sent) |
| 839 | * |
| 840 | * @return bool |
| 841 | */ |
| 842 | public function setSentStatus( $id = -1, $status = -1 ) { |
| 843 | |
| 844 | global $zbs; |
| 845 | |
| 846 | $id = (int) $id; |
| 847 | |
| 848 | if ( $id > 0 && in_array( $status, array( -1, 1 ) ) ) { |
| 849 | |
| 850 | return $this->addUpdateEventreminder( |
| 851 | array( |
| 852 | 'id' => $id, |
| 853 | 'limitedFields' => array( |
| 854 | array( |
| 855 | 'key' => 'zbser_sent', |
| 856 | 'val' => $status, |
| 857 | 'type' => '%d', |
| 858 | ), |
| 859 | ), |
| 860 | ) |
| 861 | ); |
| 862 | |
| 863 | } |
| 864 | |
| 865 | return false; |
| 866 | } |
| 867 | |
| 868 | /** |
| 869 | * deletes a eventreminder object |
| 870 | * |
| 871 | * @param array $args Associative array of arguments |
| 872 | * id |
| 873 | * |
| 874 | * @return int success; |
| 875 | */ |
| 876 | public function deleteEventreminder( $args = array() ) { |
| 877 | |
| 878 | global $ZBSCRM_t, $wpdb, $zbs; |
| 879 | |
| 880 | #} ============ LOAD ARGS ============= |
| 881 | $defaultArgs = array( |
| 882 | |
| 883 | 'id' => -1, |
| 884 | 'saveOrphans' => true, |
| 885 | |
| 886 | ); |
| 887 | foreach ( $defaultArgs as $argK => $argV ) { |
| 888 | $$argK = $argV; |
| 889 | if ( is_array( $args ) && isset( $args[ $argK ] ) ) { |
| 890 | if ( is_array( $args[ $argK ] ) ) { |
| 891 | $newData = $$argK; |
| 892 | if ( ! is_array( $newData ) ) { |
| 893 | $newData = array(); |
| 894 | } foreach ( $args[ $argK ] as $subK => $subV ) { |
| 895 | $newData[ $subK ] = $subV; |
| 896 | }$$argK = $newData; |
| 897 | } else { |
| 898 | $$argK = $args[ $argK ]; } |
| 899 | } |
| 900 | } |
| 901 | #} =========== / LOAD ARGS ============ |
| 902 | |
| 903 | #} Check ID & Delete :) |
| 904 | $id = (int) $id; |
| 905 | if ( ! empty( $id ) && $id > 0 ) { |
| 906 | |
| 907 | // delete orphans? |
| 908 | if ( $saveOrphans === false ) { |
| 909 | |
| 910 | } |
| 911 | |
| 912 | return zeroBSCRM_db2_deleteGeneric( $id, 'eventreminders' ); |
| 913 | |
| 914 | } |
| 915 | |
| 916 | return false; |
| 917 | } |
| 918 | |
| 919 | /** |
| 920 | * deletes all eventreminder objects assigned to event |
| 921 | * |
| 922 | * @param array $args Associative array of arguments |
| 923 | * id |
| 924 | * |
| 925 | * @return int success; |
| 926 | */ |
| 927 | public function deleteEventRemindersForEvent( $args = array() ) { |
| 928 | |
| 929 | global $ZBSCRM_t, $wpdb, $zbs; |
| 930 | |
| 931 | #} ============ LOAD ARGS ============= |
| 932 | $defaultArgs = array( |
| 933 | |
| 934 | 'eventID' => -1, |
| 935 | |
| 936 | ); |
| 937 | foreach ( $defaultArgs as $argK => $argV ) { |
| 938 | $$argK = $argV; |
| 939 | if ( is_array( $args ) && isset( $args[ $argK ] ) ) { |
| 940 | if ( is_array( $args[ $argK ] ) ) { |
| 941 | $newData = $$argK; |
| 942 | if ( ! is_array( $newData ) ) { |
| 943 | $newData = array(); |
| 944 | } foreach ( $args[ $argK ] as $subK => $subV ) { |
| 945 | $newData[ $subK ] = $subV; |
| 946 | }$$argK = $newData; |
| 947 | } else { |
| 948 | $$argK = $args[ $argK ]; } |
| 949 | } |
| 950 | } |
| 951 | #} =========== / LOAD ARGS ============ |
| 952 | |
| 953 | #} Check ID & Delete :) |
| 954 | $eventID = (int) $eventID; |
| 955 | if ( ! empty( $eventID ) && $eventID > 0 ) { |
| 956 | |
| 957 | $reminders = $this->getEventreminders( |
| 958 | array( |
| 959 | 'eventID' => $eventID, |
| 960 | 'perPage' => 1000, |
| 961 | 'ignoreowner' => true, |
| 962 | ) |
| 963 | ); |
| 964 | |
| 965 | $delcount = 0; |
| 966 | if ( is_array( $reminders ) ) { |
| 967 | foreach ( $reminders as $r ) { |
| 968 | |
| 969 | $delcount += $this->deleteEventreminder( array( 'id' => $r['id'] ) ); |
| 970 | |
| 971 | } |
| 972 | } |
| 973 | |
| 974 | return $delcount; |
| 975 | |
| 976 | } |
| 977 | |
| 978 | return false; |
| 979 | } |
| 980 | |
| 981 | /** |
| 982 | * tidy's the object from wp db into clean array |
| 983 | * |
| 984 | * @param array $obj (DB obj) |
| 985 | * |
| 986 | * @return array eventreminder (clean obj) |
| 987 | */ |
| 988 | private function tidy_eventreminder( $obj = false, $withCustomFields = false ) { |
| 989 | |
| 990 | $res = false; |
| 991 | |
| 992 | if ( isset( $obj->ID ) ) { |
| 993 | $res = array(); |
| 994 | $res['id'] = $obj->ID; |
| 995 | /* |
| 996 | `zbs_site` INT NULL DEFAULT NULL, |
| 997 | `zbs_team` INT NULL DEFAULT NULL, |
| 998 | `zbs_owner` INT NOT NULL, |
| 999 | */ |
| 1000 | $res['owner'] = $obj->zbs_owner; |
| 1001 | |
| 1002 | $res['event'] = (int) $obj->zbser_event; |
| 1003 | $res['remind_at'] = (int) $obj->zbser_remind_at; |
| 1004 | $res['sent'] = (int) $obj->zbser_sent; |
| 1005 | $res['created'] = (int) $obj->zbser_created; |
| 1006 | $res['created_date'] = ( isset( $obj->zbser_created ) && $obj->zbser_created > 0 ) ? zeroBSCRM_locale_utsToDatetime( $obj->zbser_created ) : false; |
| 1007 | $res['lastupdated'] = (int) $obj->zbser_lastupdated; |
| 1008 | $res['lastupdated_date'] = ( isset( $obj->zbser_lastupdated ) && $obj->zbser_lastupdated > 0 ) ? zeroBSCRM_locale_utsToDatetime( $obj->zbser_lastupdated ) : false; |
| 1009 | |
| 1010 | // if set: due |
| 1011 | if ( isset( $obj->due ) ) { |
| 1012 | $res['due'] = (int) $obj->due; |
| 1013 | } |
| 1014 | } |
| 1015 | |
| 1016 | return $res; |
| 1017 | } |
| 1018 | |
| 1019 | /** |
| 1020 | * remove any non-db fields from the object |
| 1021 | * basically takes array like array('owner'=>1,'fname'=>'x','fullname'=>'x') |
| 1022 | * and returns array like array('owner'=>1,'fname'=>'x') |
| 1023 | * This does so based on the objectModel! |
| 1024 | * |
| 1025 | * @param array $obj (clean obj) |
| 1026 | * |
| 1027 | * @return array (db ready arr) |
| 1028 | */ |
| 1029 | private function db_ready_eventreminder( $obj = false ) { |
| 1030 | |
| 1031 | // use the generic? (override here if necessary) |
| 1032 | return $this->db_ready_obj( $obj ); |
| 1033 | } |
| 1034 | |
| 1035 | // =========== / EVENTREMINDER ======================================================= |
| 1036 | // =============================================================================== |
| 1037 | } // / class |