@@ -35,7 +35,7 @@ discard block |
||
| 35 | 35 | * @param EEM_Attendee $attendee_model |
| 36 | 36 | */ |
| 37 | 37 | public function __construct(EEM_Attendee $attendee_model ) { |
| 38 | - $this->attendee_model = $attendee_model; |
|
| 38 | + $this->attendee_model = $attendee_model; |
|
| 39 | 39 | } |
| 40 | 40 | |
| 41 | 41 | |
@@ -83,7 +83,7 @@ discard block |
||
| 83 | 83 | private function findExistingAttendee( EE_Registration $registration, array $attendee_data ) { |
| 84 | 84 | $existing_attendee = null; |
| 85 | 85 | // does this attendee already exist in the db ? |
| 86 | - // we're searching using a combination of first name, last name, AND email address |
|
| 86 | + // we're searching using a combination of first name, last name, AND email address |
|
| 87 | 87 | $ATT_fname = ! empty( $attendee_data['ATT_fname'] ) |
| 88 | 88 | ? $attendee_data['ATT_fname'] |
| 89 | 89 | : ''; |
@@ -124,8 +124,8 @@ discard block |
||
| 124 | 124 | */ |
| 125 | 125 | private function updateExistingAttendeeData( EE_Attendee $existing_attendee, array $attendee_data ) { |
| 126 | 126 | // first remove fname, lname, and email from attendee data |
| 127 | - // because these properties will be exactly the same as the returned attendee object, |
|
| 128 | - // since they were used in the query to get the attendee object in the first place |
|
| 127 | + // because these properties will be exactly the same as the returned attendee object, |
|
| 128 | + // since they were used in the query to get the attendee object in the first place |
|
| 129 | 129 | $dont_set = array( 'ATT_fname', 'ATT_lname', 'ATT_email' ); |
| 130 | 130 | // now loop thru what's left and add to attendee CPT |
| 131 | 131 | foreach ( $attendee_data as $property_name => $property_value ) { |
@@ -9,7 +9,7 @@ discard block |
||
| 9 | 9 | use EventEspresso\core\services\commands\CommandHandler; |
| 10 | 10 | use EventEspresso\core\services\commands\CommandInterface; |
| 11 | 11 | |
| 12 | -defined( 'EVENT_ESPRESSO_VERSION' ) || exit; |
|
| 12 | +defined('EVENT_ESPRESSO_VERSION') || exit; |
|
| 13 | 13 | |
| 14 | 14 | |
| 15 | 15 | |
@@ -34,7 +34,7 @@ discard block |
||
| 34 | 34 | /** |
| 35 | 35 | * @param EEM_Attendee $attendee_model |
| 36 | 36 | */ |
| 37 | - public function __construct(EEM_Attendee $attendee_model ) { |
|
| 37 | + public function __construct(EEM_Attendee $attendee_model) { |
|
| 38 | 38 | $this->attendee_model = $attendee_model; |
| 39 | 39 | } |
| 40 | 40 | |
@@ -46,10 +46,10 @@ discard block |
||
| 46 | 46 | * @throws EE_Error |
| 47 | 47 | * @throws InvalidEntityException |
| 48 | 48 | */ |
| 49 | - public function handle( CommandInterface $command ) { |
|
| 49 | + public function handle(CommandInterface $command) { |
|
| 50 | 50 | /** @var CreateAttendeeCommand $command */ |
| 51 | - if ( ! $command instanceof CreateAttendeeCommand ) { |
|
| 52 | - throw new InvalidEntityException( get_class( $command ), 'CreateAttendeeCommand' ); |
|
| 51 | + if ( ! $command instanceof CreateAttendeeCommand) { |
|
| 52 | + throw new InvalidEntityException(get_class($command), 'CreateAttendeeCommand'); |
|
| 53 | 53 | } |
| 54 | 54 | // have we met before? |
| 55 | 55 | $attendee = $this->findExistingAttendee( |
@@ -57,7 +57,7 @@ discard block |
||
| 57 | 57 | $command->attendeeDetails() |
| 58 | 58 | ); |
| 59 | 59 | // did we find an already existing record for this attendee ? |
| 60 | - if ( $attendee instanceof EE_Attendee ) { |
|
| 60 | + if ($attendee instanceof EE_Attendee) { |
|
| 61 | 61 | $attendee = $this->updateExistingAttendeeData( |
| 62 | 62 | $attendee, |
| 63 | 63 | $command->attendeeDetails() |
@@ -80,21 +80,21 @@ discard block |
||
| 80 | 80 | * @param array $attendee_data |
| 81 | 81 | * @return EE_Attendee |
| 82 | 82 | */ |
| 83 | - private function findExistingAttendee( EE_Registration $registration, array $attendee_data ) { |
|
| 83 | + private function findExistingAttendee(EE_Registration $registration, array $attendee_data) { |
|
| 84 | 84 | $existing_attendee = null; |
| 85 | 85 | // does this attendee already exist in the db ? |
| 86 | 86 | // we're searching using a combination of first name, last name, AND email address |
| 87 | - $ATT_fname = ! empty( $attendee_data['ATT_fname'] ) |
|
| 87 | + $ATT_fname = ! empty($attendee_data['ATT_fname']) |
|
| 88 | 88 | ? $attendee_data['ATT_fname'] |
| 89 | 89 | : ''; |
| 90 | - $ATT_lname = ! empty( $attendee_data['ATT_lname'] ) |
|
| 90 | + $ATT_lname = ! empty($attendee_data['ATT_lname']) |
|
| 91 | 91 | ? $attendee_data['ATT_lname'] |
| 92 | 92 | : ''; |
| 93 | - $ATT_email = ! empty( $attendee_data['ATT_email'] ) |
|
| 93 | + $ATT_email = ! empty($attendee_data['ATT_email']) |
|
| 94 | 94 | ? $attendee_data['ATT_email'] |
| 95 | 95 | : ''; |
| 96 | 96 | // but only if those have values |
| 97 | - if ( $ATT_fname && $ATT_lname && $ATT_email ) { |
|
| 97 | + if ($ATT_fname && $ATT_lname && $ATT_email) { |
|
| 98 | 98 | $existing_attendee = $this->attendee_model->find_existing_attendee( |
| 99 | 99 | array( |
| 100 | 100 | 'ATT_fname' => $ATT_fname, |
@@ -122,18 +122,18 @@ discard block |
||
| 122 | 122 | * @return EE_Attendee |
| 123 | 123 | * @throws EE_Error |
| 124 | 124 | */ |
| 125 | - private function updateExistingAttendeeData( EE_Attendee $existing_attendee, array $attendee_data ) { |
|
| 125 | + private function updateExistingAttendeeData(EE_Attendee $existing_attendee, array $attendee_data) { |
|
| 126 | 126 | // first remove fname, lname, and email from attendee data |
| 127 | 127 | // because these properties will be exactly the same as the returned attendee object, |
| 128 | 128 | // since they were used in the query to get the attendee object in the first place |
| 129 | - $dont_set = array( 'ATT_fname', 'ATT_lname', 'ATT_email' ); |
|
| 129 | + $dont_set = array('ATT_fname', 'ATT_lname', 'ATT_email'); |
|
| 130 | 130 | // now loop thru what's left and add to attendee CPT |
| 131 | - foreach ( $attendee_data as $property_name => $property_value ) { |
|
| 131 | + foreach ($attendee_data as $property_name => $property_value) { |
|
| 132 | 132 | if ( |
| 133 | - ! in_array( $property_name, $dont_set, true ) |
|
| 134 | - && $this->attendee_model->has_field( $property_name ) |
|
| 133 | + ! in_array($property_name, $dont_set, true) |
|
| 134 | + && $this->attendee_model->has_field($property_name) |
|
| 135 | 135 | ) { |
| 136 | - $existing_attendee->set( $property_name, $property_value ); |
|
| 136 | + $existing_attendee->set($property_name, $property_value); |
|
| 137 | 137 | } |
| 138 | 138 | } |
| 139 | 139 | // better save that now |
@@ -151,11 +151,11 @@ discard block |
||
| 151 | 151 | * @return EE_Attendee |
| 152 | 152 | * @throws EE_Error |
| 153 | 153 | */ |
| 154 | - private function createNewAttendee( EE_Registration $registration, array $attendee_data ) { |
|
| 154 | + private function createNewAttendee(EE_Registration $registration, array $attendee_data) { |
|
| 155 | 155 | // create new attendee object |
| 156 | - $new_attendee = EE_Attendee::new_instance( $attendee_data ); |
|
| 156 | + $new_attendee = EE_Attendee::new_instance($attendee_data); |
|
| 157 | 157 | // set author to event creator |
| 158 | - $new_attendee->set( 'ATT_author', $registration->event()->wp_user() ); |
|
| 158 | + $new_attendee->set('ATT_author', $registration->event()->wp_user()); |
|
| 159 | 159 | $new_attendee->save(); |
| 160 | 160 | return $new_attendee; |
| 161 | 161 | } |
@@ -39,8 +39,8 @@ |
||
| 39 | 39 | * @param EE_Transaction $transaction |
| 40 | 40 | * @param EE_Ticket $ticket |
| 41 | 41 | * @param EE_Line_Item $ticket_line_item |
| 42 | - * @param $reg_count |
|
| 43 | - * @param $reg_group_size |
|
| 42 | + * @param integer $reg_count |
|
| 43 | + * @param integer $reg_group_size |
|
| 44 | 44 | * @param string $reg_status |
| 45 | 45 | * @return EE_Registration |
| 46 | 46 | * @throws OutOfRangeException |
@@ -17,7 +17,7 @@ discard block |
||
| 17 | 17 | use OutOfRangeException; |
| 18 | 18 | |
| 19 | 19 | if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
| 20 | - exit('No direct script access allowed'); |
|
| 20 | + exit('No direct script access allowed'); |
|
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | |
@@ -41,54 +41,54 @@ discard block |
||
| 41 | 41 | * @param EE_Line_Item $ticket_line_item |
| 42 | 42 | * @param $reg_count |
| 43 | 43 | * @param $reg_group_size |
| 44 | - * @param string $reg_status |
|
| 45 | - * @return EE_Registration |
|
| 44 | + * @param string $reg_status |
|
| 45 | + * @return EE_Registration |
|
| 46 | 46 | * @throws OutOfRangeException |
| 47 | 47 | * @throws EE_Error |
| 48 | 48 | * @throws UnexpectedEntityException |
| 49 | 49 | */ |
| 50 | - public function create( |
|
| 51 | - EE_Event $event, |
|
| 52 | - EE_Transaction $transaction, |
|
| 53 | - EE_Ticket $ticket, |
|
| 54 | - EE_Line_Item $ticket_line_item, |
|
| 55 | - $reg_count, |
|
| 56 | - $reg_group_size, |
|
| 57 | - $reg_status = EEM_Registration::status_id_incomplete |
|
| 58 | - ) { |
|
| 59 | - $registrations = $transaction->registrations(); |
|
| 60 | - $reg_count = $reg_count ? $reg_count : count($registrations) + 1; |
|
| 61 | - $reg_url_link = new RegUrlLink($reg_count, $ticket_line_item); |
|
| 62 | - $reg_code = new RegCode($reg_url_link, $transaction, $ticket); |
|
| 63 | - // generate new EE_Registration |
|
| 64 | - $registration = EE_Registration::new_instance( |
|
| 65 | - array( |
|
| 66 | - 'EVT_ID' => $event->ID(), |
|
| 67 | - 'TXN_ID' => $transaction->ID(), |
|
| 68 | - 'TKT_ID' => $ticket->ID(), |
|
| 69 | - 'STS_ID' => $reg_status, |
|
| 70 | - 'REG_final_price' => $this->resolveFinalPrice($transaction, $ticket, $ticket_line_item), |
|
| 71 | - 'REG_session' => EE_Registry::instance()->SSN->id(), |
|
| 72 | - 'REG_count' => $reg_count, |
|
| 73 | - 'REG_group_size' => $reg_group_size ? $reg_group_size : $this->incrementRegCount($registrations), |
|
| 74 | - 'REG_url_link' => $reg_url_link, |
|
| 75 | - 'REG_code' => $reg_code, |
|
| 76 | - ) |
|
| 77 | - ); |
|
| 78 | - if ( ! $registration instanceof EE_Registration) { |
|
| 79 | - throw new UnexpectedEntityException($registration, 'EE_Registration'); |
|
| 80 | - } |
|
| 81 | - // save registration so that we have an ID |
|
| 82 | - $registration->save(); |
|
| 83 | - // track reservation on reg but don't adjust ticket and datetime reserved counts |
|
| 84 | - // because that is done as soon as the tickets are added/removed from the cart |
|
| 85 | - $registration->reserve_ticket(); |
|
| 86 | - $registration->_add_relation_to($event, 'Event', array(), $event->ID()); |
|
| 87 | - $registration->_add_relation_to($ticket, 'Ticket', array(), $ticket->ID()); |
|
| 88 | - $transaction->_add_relation_to($registration, 'Registration', array(), $registration->ID()); |
|
| 89 | - $registration->save(); |
|
| 90 | - return $registration; |
|
| 91 | - } |
|
| 50 | + public function create( |
|
| 51 | + EE_Event $event, |
|
| 52 | + EE_Transaction $transaction, |
|
| 53 | + EE_Ticket $ticket, |
|
| 54 | + EE_Line_Item $ticket_line_item, |
|
| 55 | + $reg_count, |
|
| 56 | + $reg_group_size, |
|
| 57 | + $reg_status = EEM_Registration::status_id_incomplete |
|
| 58 | + ) { |
|
| 59 | + $registrations = $transaction->registrations(); |
|
| 60 | + $reg_count = $reg_count ? $reg_count : count($registrations) + 1; |
|
| 61 | + $reg_url_link = new RegUrlLink($reg_count, $ticket_line_item); |
|
| 62 | + $reg_code = new RegCode($reg_url_link, $transaction, $ticket); |
|
| 63 | + // generate new EE_Registration |
|
| 64 | + $registration = EE_Registration::new_instance( |
|
| 65 | + array( |
|
| 66 | + 'EVT_ID' => $event->ID(), |
|
| 67 | + 'TXN_ID' => $transaction->ID(), |
|
| 68 | + 'TKT_ID' => $ticket->ID(), |
|
| 69 | + 'STS_ID' => $reg_status, |
|
| 70 | + 'REG_final_price' => $this->resolveFinalPrice($transaction, $ticket, $ticket_line_item), |
|
| 71 | + 'REG_session' => EE_Registry::instance()->SSN->id(), |
|
| 72 | + 'REG_count' => $reg_count, |
|
| 73 | + 'REG_group_size' => $reg_group_size ? $reg_group_size : $this->incrementRegCount($registrations), |
|
| 74 | + 'REG_url_link' => $reg_url_link, |
|
| 75 | + 'REG_code' => $reg_code, |
|
| 76 | + ) |
|
| 77 | + ); |
|
| 78 | + if ( ! $registration instanceof EE_Registration) { |
|
| 79 | + throw new UnexpectedEntityException($registration, 'EE_Registration'); |
|
| 80 | + } |
|
| 81 | + // save registration so that we have an ID |
|
| 82 | + $registration->save(); |
|
| 83 | + // track reservation on reg but don't adjust ticket and datetime reserved counts |
|
| 84 | + // because that is done as soon as the tickets are added/removed from the cart |
|
| 85 | + $registration->reserve_ticket(); |
|
| 86 | + $registration->_add_relation_to($event, 'Event', array(), $event->ID()); |
|
| 87 | + $registration->_add_relation_to($ticket, 'Ticket', array(), $ticket->ID()); |
|
| 88 | + $transaction->_add_relation_to($registration, 'Registration', array(), $registration->ID()); |
|
| 89 | + $registration->save(); |
|
| 90 | + return $registration; |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | 93 | |
| 94 | 94 | |
@@ -100,40 +100,40 @@ discard block |
||
| 100 | 100 | * @throws EE_Error |
| 101 | 101 | * @throws OutOfRangeException |
| 102 | 102 | */ |
| 103 | - protected function resolveFinalPrice( |
|
| 104 | - EE_Transaction $transaction, |
|
| 105 | - EE_Ticket $ticket, |
|
| 106 | - EE_Line_Item $ticket_line_item |
|
| 107 | - ) { |
|
| 108 | - $final_price = EEH_Line_Item::calculate_final_price_for_ticket_line_item( |
|
| 109 | - $transaction->total_line_item(), |
|
| 110 | - $ticket_line_item |
|
| 111 | - ); |
|
| 112 | - $final_price = $final_price !== null ? $final_price : $ticket->get_ticket_total_with_taxes(); |
|
| 113 | - return (float)$final_price; |
|
| 114 | - } |
|
| 103 | + protected function resolveFinalPrice( |
|
| 104 | + EE_Transaction $transaction, |
|
| 105 | + EE_Ticket $ticket, |
|
| 106 | + EE_Line_Item $ticket_line_item |
|
| 107 | + ) { |
|
| 108 | + $final_price = EEH_Line_Item::calculate_final_price_for_ticket_line_item( |
|
| 109 | + $transaction->total_line_item(), |
|
| 110 | + $ticket_line_item |
|
| 111 | + ); |
|
| 112 | + $final_price = $final_price !== null ? $final_price : $ticket->get_ticket_total_with_taxes(); |
|
| 113 | + return (float)$final_price; |
|
| 114 | + } |
|
| 115 | 115 | |
| 116 | 116 | |
| 117 | 117 | |
| 118 | - /** |
|
| 119 | - * @param EE_Registration[] $registrations |
|
| 120 | - * @param boolean $update_existing_registrations |
|
| 121 | - * @return int |
|
| 122 | - * @throws EE_Error |
|
| 123 | - */ |
|
| 124 | - protected function incrementRegCount(array $registrations, $update_existing_registrations = true) |
|
| 125 | - { |
|
| 126 | - $new_reg_count = count($registrations) + 1; |
|
| 127 | - if ($update_existing_registrations) { |
|
| 128 | - foreach ($registrations as $registration) { |
|
| 129 | - if ($registration instanceof EE_Registration) { |
|
| 130 | - $registration->set_count($new_reg_count); |
|
| 131 | - $registration->save(); |
|
| 132 | - } |
|
| 133 | - } |
|
| 134 | - } |
|
| 135 | - return $new_reg_count; |
|
| 136 | - } |
|
| 118 | + /** |
|
| 119 | + * @param EE_Registration[] $registrations |
|
| 120 | + * @param boolean $update_existing_registrations |
|
| 121 | + * @return int |
|
| 122 | + * @throws EE_Error |
|
| 123 | + */ |
|
| 124 | + protected function incrementRegCount(array $registrations, $update_existing_registrations = true) |
|
| 125 | + { |
|
| 126 | + $new_reg_count = count($registrations) + 1; |
|
| 127 | + if ($update_existing_registrations) { |
|
| 128 | + foreach ($registrations as $registration) { |
|
| 129 | + if ($registration instanceof EE_Registration) { |
|
| 130 | + $registration->set_count($new_reg_count); |
|
| 131 | + $registration->save(); |
|
| 132 | + } |
|
| 133 | + } |
|
| 134 | + } |
|
| 135 | + return $new_reg_count; |
|
| 136 | + } |
|
| 137 | 137 | |
| 138 | 138 | |
| 139 | 139 | } |
@@ -5,7 +5,7 @@ discard block |
||
| 5 | 5 | use EventEspresso\core\exceptions\UnexpectedEntityException; |
| 6 | 6 | |
| 7 | 7 | if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
| 8 | - exit('No direct script access allowed'); |
|
| 8 | + exit('No direct script access allowed'); |
|
| 9 | 9 | } |
| 10 | 10 | |
| 11 | 11 | |
@@ -21,44 +21,44 @@ discard block |
||
| 21 | 21 | class CreateTicketLineItemService extends DomainService |
| 22 | 22 | { |
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * @param \EE_Transaction $transaction |
|
| 26 | - * @param \EE_Ticket $ticket |
|
| 27 | - * @param int $quantity |
|
| 28 | - * @return \EE_Line_Item |
|
| 29 | - * @throws \EE_Error |
|
| 30 | - * @throws UnexpectedEntityException |
|
| 31 | - */ |
|
| 32 | - public function create( |
|
| 33 | - \EE_Transaction $transaction, |
|
| 34 | - \EE_Ticket $ticket, |
|
| 35 | - $quantity = 1 |
|
| 36 | - ) |
|
| 37 | - { |
|
| 38 | - $total_line_item = $transaction->total_line_item(); |
|
| 39 | - if (! $total_line_item instanceof \EE_Line_Item) { |
|
| 40 | - throw new UnexpectedEntityException($total_line_item, 'EE_Line_Item'); |
|
| 41 | - } |
|
| 42 | - // create new line item for ticket |
|
| 43 | - $ticket_line_item = \EEH_Line_Item::add_ticket_purchase( |
|
| 44 | - $total_line_item, |
|
| 45 | - $ticket, |
|
| 46 | - $quantity |
|
| 47 | - ); |
|
| 48 | - if ( ! $ticket_line_item instanceof \EE_Line_Item) { |
|
| 49 | - throw new UnexpectedEntityException($ticket_line_item, 'EE_Line_Item'); |
|
| 50 | - } |
|
| 51 | - $total_line_item->save_this_and_descendants_to_txn($transaction->ID()); |
|
| 52 | - // apply any applicable promotions that were initially used during registration to new line items |
|
| 53 | - do_action( |
|
| 54 | - 'AHEE__\EventEspresso\core\services\commands\ticket\CreateTicketLineItemCommandHandler__handle__new_ticket_line_item_added', |
|
| 55 | - $total_line_item, |
|
| 56 | - $ticket, |
|
| 57 | - $transaction, |
|
| 58 | - $quantity |
|
| 59 | - ); |
|
| 60 | - return $ticket_line_item; |
|
| 61 | - } |
|
| 24 | + /** |
|
| 25 | + * @param \EE_Transaction $transaction |
|
| 26 | + * @param \EE_Ticket $ticket |
|
| 27 | + * @param int $quantity |
|
| 28 | + * @return \EE_Line_Item |
|
| 29 | + * @throws \EE_Error |
|
| 30 | + * @throws UnexpectedEntityException |
|
| 31 | + */ |
|
| 32 | + public function create( |
|
| 33 | + \EE_Transaction $transaction, |
|
| 34 | + \EE_Ticket $ticket, |
|
| 35 | + $quantity = 1 |
|
| 36 | + ) |
|
| 37 | + { |
|
| 38 | + $total_line_item = $transaction->total_line_item(); |
|
| 39 | + if (! $total_line_item instanceof \EE_Line_Item) { |
|
| 40 | + throw new UnexpectedEntityException($total_line_item, 'EE_Line_Item'); |
|
| 41 | + } |
|
| 42 | + // create new line item for ticket |
|
| 43 | + $ticket_line_item = \EEH_Line_Item::add_ticket_purchase( |
|
| 44 | + $total_line_item, |
|
| 45 | + $ticket, |
|
| 46 | + $quantity |
|
| 47 | + ); |
|
| 48 | + if ( ! $ticket_line_item instanceof \EE_Line_Item) { |
|
| 49 | + throw new UnexpectedEntityException($ticket_line_item, 'EE_Line_Item'); |
|
| 50 | + } |
|
| 51 | + $total_line_item->save_this_and_descendants_to_txn($transaction->ID()); |
|
| 52 | + // apply any applicable promotions that were initially used during registration to new line items |
|
| 53 | + do_action( |
|
| 54 | + 'AHEE__\EventEspresso\core\services\commands\ticket\CreateTicketLineItemCommandHandler__handle__new_ticket_line_item_added', |
|
| 55 | + $total_line_item, |
|
| 56 | + $ticket, |
|
| 57 | + $transaction, |
|
| 58 | + $quantity |
|
| 59 | + ); |
|
| 60 | + return $ticket_line_item; |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | 63 | |
| 64 | 64 | |
@@ -36,7 +36,7 @@ |
||
| 36 | 36 | ) |
| 37 | 37 | { |
| 38 | 38 | $total_line_item = $transaction->total_line_item(); |
| 39 | - if (! $total_line_item instanceof \EE_Line_Item) { |
|
| 39 | + if ( ! $total_line_item instanceof \EE_Line_Item) { |
|
| 40 | 40 | throw new UnexpectedEntityException($total_line_item, 'EE_Line_Item'); |
| 41 | 41 | } |
| 42 | 42 | // create new line item for ticket |
@@ -7,7 +7,7 @@ discard block |
||
| 7 | 7 | use EventEspresso\core\services\commands\CommandInterface; |
| 8 | 8 | |
| 9 | 9 | if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
| 10 | - exit('No direct script access allowed'); |
|
| 10 | + exit('No direct script access allowed'); |
|
| 11 | 11 | } |
| 12 | 12 | |
| 13 | 13 | |
@@ -23,50 +23,50 @@ discard block |
||
| 23 | 23 | class CreateRegistrationCommandHandler extends CommandHandler |
| 24 | 24 | { |
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * @var CreateRegistrationService $registration_service |
|
| 28 | - */ |
|
| 29 | - private $registration_service; |
|
| 26 | + /** |
|
| 27 | + * @var CreateRegistrationService $registration_service |
|
| 28 | + */ |
|
| 29 | + private $registration_service; |
|
| 30 | 30 | |
| 31 | 31 | |
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * Command constructor |
|
| 35 | - * |
|
| 36 | - * @param CreateRegistrationService $registration_service |
|
| 37 | - */ |
|
| 38 | - public function __construct(CreateRegistrationService $registration_service) |
|
| 39 | - { |
|
| 40 | - $this->registration_service = $registration_service; |
|
| 41 | - } |
|
| 33 | + /** |
|
| 34 | + * Command constructor |
|
| 35 | + * |
|
| 36 | + * @param CreateRegistrationService $registration_service |
|
| 37 | + */ |
|
| 38 | + public function __construct(CreateRegistrationService $registration_service) |
|
| 39 | + { |
|
| 40 | + $this->registration_service = $registration_service; |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | 43 | |
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * @param CommandInterface $command |
|
| 47 | - * @return mixed |
|
| 48 | - * @throws \OutOfRangeException |
|
| 49 | - * @throws \EventEspresso\core\exceptions\UnexpectedEntityException |
|
| 50 | - * @throws \EE_Error |
|
| 51 | - * @throws \EventEspresso\core\exceptions\InvalidEntityException |
|
| 52 | - */ |
|
| 53 | - public function handle(CommandInterface $command) |
|
| 54 | - { |
|
| 55 | - /** @var CreateRegistrationCommand $command */ |
|
| 56 | - if ( ! $command instanceof CreateRegistrationCommand) { |
|
| 57 | - throw new InvalidEntityException(get_class($command), 'CreateRegistrationCommand'); |
|
| 58 | - } |
|
| 59 | - // now create a new registration for the ticket |
|
| 60 | - return $this->registration_service->create( |
|
| 61 | - $command->ticket()->get_related_event(), |
|
| 62 | - $command->transaction(), |
|
| 63 | - $command->ticket(), |
|
| 64 | - $command->ticketLineItem(), |
|
| 65 | - $command->regCount(), |
|
| 66 | - $command->regGroupSize(), |
|
| 67 | - $command->regStatus() |
|
| 68 | - ); |
|
| 69 | - } |
|
| 45 | + /** |
|
| 46 | + * @param CommandInterface $command |
|
| 47 | + * @return mixed |
|
| 48 | + * @throws \OutOfRangeException |
|
| 49 | + * @throws \EventEspresso\core\exceptions\UnexpectedEntityException |
|
| 50 | + * @throws \EE_Error |
|
| 51 | + * @throws \EventEspresso\core\exceptions\InvalidEntityException |
|
| 52 | + */ |
|
| 53 | + public function handle(CommandInterface $command) |
|
| 54 | + { |
|
| 55 | + /** @var CreateRegistrationCommand $command */ |
|
| 56 | + if ( ! $command instanceof CreateRegistrationCommand) { |
|
| 57 | + throw new InvalidEntityException(get_class($command), 'CreateRegistrationCommand'); |
|
| 58 | + } |
|
| 59 | + // now create a new registration for the ticket |
|
| 60 | + return $this->registration_service->create( |
|
| 61 | + $command->ticket()->get_related_event(), |
|
| 62 | + $command->transaction(), |
|
| 63 | + $command->ticket(), |
|
| 64 | + $command->ticketLineItem(), |
|
| 65 | + $command->regCount(), |
|
| 66 | + $command->regGroupSize(), |
|
| 67 | + $command->regStatus() |
|
| 68 | + ); |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | 71 | |
| 72 | 72 | |
@@ -1,5 +1,5 @@ discard block |
||
| 1 | 1 | <?php if (! defined('EVENT_ESPRESSO_VERSION')) { |
| 2 | - exit('No direct script access allowed'); |
|
| 2 | + exit('No direct script access allowed'); |
|
| 3 | 3 | } |
| 4 | 4 | |
| 5 | 5 | |
@@ -16,672 +16,672 @@ discard block |
||
| 16 | 16 | abstract class EE_SPCO_Reg_Step |
| 17 | 17 | { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * $_completed - TRUE if this step has fully completed it's duties |
|
| 21 | - * |
|
| 22 | - * @access protected |
|
| 23 | - * @type bool $_completed |
|
| 24 | - */ |
|
| 25 | - protected $_completed = false; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * $_is_current_step - TRUE if this is the current step |
|
| 29 | - * |
|
| 30 | - * @access protected |
|
| 31 | - * @type bool $_is_current_step |
|
| 32 | - */ |
|
| 33 | - protected $_is_current_step = false; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * $_order - when the reg step should be run relative to other steps |
|
| 37 | - * |
|
| 38 | - * @access protected |
|
| 39 | - * @type int $_template |
|
| 40 | - */ |
|
| 41 | - protected $_order = 0; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * $_slug - URL param for this step |
|
| 45 | - * |
|
| 46 | - * @access protected |
|
| 47 | - * @type string $_slug |
|
| 48 | - */ |
|
| 49 | - protected $_slug; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * $_name - Step Name - translatable string |
|
| 53 | - * |
|
| 54 | - * @access protected |
|
| 55 | - * @type string $_slug |
|
| 56 | - */ |
|
| 57 | - protected $_name; |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * $_submit_button_text - translatable string that appears on this step's submit button |
|
| 61 | - * |
|
| 62 | - * @access protected |
|
| 63 | - * @type string $_slug |
|
| 64 | - */ |
|
| 65 | - protected $_submit_button_text; |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * $_template - template name |
|
| 69 | - * |
|
| 70 | - * @access protected |
|
| 71 | - * @type string $_template |
|
| 72 | - */ |
|
| 73 | - protected $_template; |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * $_reg_form_name - the form input name and id attribute |
|
| 77 | - * |
|
| 78 | - * @access protected |
|
| 79 | - * @var string $_reg_form_name |
|
| 80 | - */ |
|
| 81 | - protected $_reg_form_name; |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * $_success_message - text to display upon successful form submission |
|
| 85 | - * |
|
| 86 | - * @access private |
|
| 87 | - * @var string $_success_message |
|
| 88 | - */ |
|
| 89 | - protected $_success_message; |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * $_instructions - a brief description of how to complete the reg step. |
|
| 93 | - * Usually displayed in conjunction with the previous step's success message. |
|
| 94 | - * |
|
| 95 | - * @access private |
|
| 96 | - * @var string $_instructions |
|
| 97 | - */ |
|
| 98 | - protected $_instructions; |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * $_valid_data - the normalized and validated data for this step |
|
| 102 | - * |
|
| 103 | - * @access public |
|
| 104 | - * @var array $_valid_data |
|
| 105 | - */ |
|
| 106 | - protected $_valid_data = array(); |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * $reg_form - the registration form for this step |
|
| 110 | - * |
|
| 111 | - * @access public |
|
| 112 | - * @var EE_Form_Section_Proper $reg_form |
|
| 113 | - */ |
|
| 114 | - public $reg_form; |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * $checkout - EE_Checkout object for handling the properties of the current checkout process |
|
| 118 | - * |
|
| 119 | - * @access public |
|
| 120 | - * @var EE_Checkout $checkout |
|
| 121 | - */ |
|
| 122 | - public $checkout; |
|
| 123 | - |
|
| 124 | - |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * @return void |
|
| 128 | - */ |
|
| 129 | - abstract public function translate_js_strings(); |
|
| 130 | - |
|
| 131 | - |
|
| 19 | + /** |
|
| 20 | + * $_completed - TRUE if this step has fully completed it's duties |
|
| 21 | + * |
|
| 22 | + * @access protected |
|
| 23 | + * @type bool $_completed |
|
| 24 | + */ |
|
| 25 | + protected $_completed = false; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * $_is_current_step - TRUE if this is the current step |
|
| 29 | + * |
|
| 30 | + * @access protected |
|
| 31 | + * @type bool $_is_current_step |
|
| 32 | + */ |
|
| 33 | + protected $_is_current_step = false; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * $_order - when the reg step should be run relative to other steps |
|
| 37 | + * |
|
| 38 | + * @access protected |
|
| 39 | + * @type int $_template |
|
| 40 | + */ |
|
| 41 | + protected $_order = 0; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * $_slug - URL param for this step |
|
| 45 | + * |
|
| 46 | + * @access protected |
|
| 47 | + * @type string $_slug |
|
| 48 | + */ |
|
| 49 | + protected $_slug; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * $_name - Step Name - translatable string |
|
| 53 | + * |
|
| 54 | + * @access protected |
|
| 55 | + * @type string $_slug |
|
| 56 | + */ |
|
| 57 | + protected $_name; |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * $_submit_button_text - translatable string that appears on this step's submit button |
|
| 61 | + * |
|
| 62 | + * @access protected |
|
| 63 | + * @type string $_slug |
|
| 64 | + */ |
|
| 65 | + protected $_submit_button_text; |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * $_template - template name |
|
| 69 | + * |
|
| 70 | + * @access protected |
|
| 71 | + * @type string $_template |
|
| 72 | + */ |
|
| 73 | + protected $_template; |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * $_reg_form_name - the form input name and id attribute |
|
| 77 | + * |
|
| 78 | + * @access protected |
|
| 79 | + * @var string $_reg_form_name |
|
| 80 | + */ |
|
| 81 | + protected $_reg_form_name; |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * $_success_message - text to display upon successful form submission |
|
| 85 | + * |
|
| 86 | + * @access private |
|
| 87 | + * @var string $_success_message |
|
| 88 | + */ |
|
| 89 | + protected $_success_message; |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * $_instructions - a brief description of how to complete the reg step. |
|
| 93 | + * Usually displayed in conjunction with the previous step's success message. |
|
| 94 | + * |
|
| 95 | + * @access private |
|
| 96 | + * @var string $_instructions |
|
| 97 | + */ |
|
| 98 | + protected $_instructions; |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * $_valid_data - the normalized and validated data for this step |
|
| 102 | + * |
|
| 103 | + * @access public |
|
| 104 | + * @var array $_valid_data |
|
| 105 | + */ |
|
| 106 | + protected $_valid_data = array(); |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * $reg_form - the registration form for this step |
|
| 110 | + * |
|
| 111 | + * @access public |
|
| 112 | + * @var EE_Form_Section_Proper $reg_form |
|
| 113 | + */ |
|
| 114 | + public $reg_form; |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * $checkout - EE_Checkout object for handling the properties of the current checkout process |
|
| 118 | + * |
|
| 119 | + * @access public |
|
| 120 | + * @var EE_Checkout $checkout |
|
| 121 | + */ |
|
| 122 | + public $checkout; |
|
| 123 | + |
|
| 124 | + |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * @return void |
|
| 128 | + */ |
|
| 129 | + abstract public function translate_js_strings(); |
|
| 130 | + |
|
| 131 | + |
|
| 132 | 132 | |
| 133 | - /** |
|
| 134 | - * @return void |
|
| 135 | - */ |
|
| 136 | - abstract public function enqueue_styles_and_scripts(); |
|
| 137 | - |
|
| 138 | - |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * @return boolean |
|
| 142 | - */ |
|
| 143 | - abstract public function initialize_reg_step(); |
|
| 144 | - |
|
| 145 | - |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * @return string |
|
| 149 | - */ |
|
| 150 | - abstract public function generate_reg_form(); |
|
| 151 | - |
|
| 152 | - |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * @return boolean |
|
| 156 | - */ |
|
| 157 | - abstract public function process_reg_step(); |
|
| 158 | - |
|
| 159 | - |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * @return boolean |
|
| 163 | - */ |
|
| 164 | - abstract public function update_reg_step(); |
|
| 165 | - |
|
| 166 | - |
|
| 167 | - |
|
| 168 | - /** |
|
| 169 | - * @return boolean |
|
| 170 | - */ |
|
| 171 | - public function completed() |
|
| 172 | - { |
|
| 173 | - return $this->_completed; |
|
| 174 | - } |
|
| 133 | + /** |
|
| 134 | + * @return void |
|
| 135 | + */ |
|
| 136 | + abstract public function enqueue_styles_and_scripts(); |
|
| 137 | + |
|
| 138 | + |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * @return boolean |
|
| 142 | + */ |
|
| 143 | + abstract public function initialize_reg_step(); |
|
| 144 | + |
|
| 145 | + |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * @return string |
|
| 149 | + */ |
|
| 150 | + abstract public function generate_reg_form(); |
|
| 151 | + |
|
| 152 | + |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * @return boolean |
|
| 156 | + */ |
|
| 157 | + abstract public function process_reg_step(); |
|
| 158 | + |
|
| 159 | + |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * @return boolean |
|
| 163 | + */ |
|
| 164 | + abstract public function update_reg_step(); |
|
| 165 | + |
|
| 166 | + |
|
| 167 | + |
|
| 168 | + /** |
|
| 169 | + * @return boolean |
|
| 170 | + */ |
|
| 171 | + public function completed() |
|
| 172 | + { |
|
| 173 | + return $this->_completed; |
|
| 174 | + } |
|
| 175 | 175 | |
| 176 | 176 | |
| 177 | 177 | |
| 178 | - /** |
|
| 179 | - * set_completed - toggles $_completed to TRUE |
|
| 180 | - */ |
|
| 181 | - public function set_completed() |
|
| 182 | - { |
|
| 183 | - // DEBUG LOG |
|
| 184 | - //$this->checkout->log( __CLASS__, __FUNCTION__, __LINE__ ); |
|
| 185 | - $this->_completed = apply_filters('FHEE__EE_SPCO_Reg_Step__set_completed___completed', true, $this); |
|
| 186 | - } |
|
| 178 | + /** |
|
| 179 | + * set_completed - toggles $_completed to TRUE |
|
| 180 | + */ |
|
| 181 | + public function set_completed() |
|
| 182 | + { |
|
| 183 | + // DEBUG LOG |
|
| 184 | + //$this->checkout->log( __CLASS__, __FUNCTION__, __LINE__ ); |
|
| 185 | + $this->_completed = apply_filters('FHEE__EE_SPCO_Reg_Step__set_completed___completed', true, $this); |
|
| 186 | + } |
|
| 187 | 187 | |
| 188 | 188 | |
| 189 | 189 | |
| 190 | - /** |
|
| 191 | - * set_completed - toggles $_completed to FALSE |
|
| 192 | - */ |
|
| 193 | - public function set_not_completed() |
|
| 194 | - { |
|
| 195 | - $this->_completed = false; |
|
| 196 | - } |
|
| 190 | + /** |
|
| 191 | + * set_completed - toggles $_completed to FALSE |
|
| 192 | + */ |
|
| 193 | + public function set_not_completed() |
|
| 194 | + { |
|
| 195 | + $this->_completed = false; |
|
| 196 | + } |
|
| 197 | 197 | |
| 198 | 198 | |
| 199 | 199 | |
| 200 | - /** |
|
| 201 | - * @return string |
|
| 202 | - */ |
|
| 203 | - public function name() |
|
| 204 | - { |
|
| 205 | - return $this->_name; |
|
| 206 | - } |
|
| 200 | + /** |
|
| 201 | + * @return string |
|
| 202 | + */ |
|
| 203 | + public function name() |
|
| 204 | + { |
|
| 205 | + return $this->_name; |
|
| 206 | + } |
|
| 207 | 207 | |
| 208 | 208 | |
| 209 | 209 | |
| 210 | - /** |
|
| 211 | - * @return string |
|
| 212 | - */ |
|
| 213 | - public function slug() |
|
| 214 | - { |
|
| 215 | - return $this->_slug; |
|
| 216 | - } |
|
| 210 | + /** |
|
| 211 | + * @return string |
|
| 212 | + */ |
|
| 213 | + public function slug() |
|
| 214 | + { |
|
| 215 | + return $this->_slug; |
|
| 216 | + } |
|
| 217 | 217 | |
| 218 | 218 | |
| 219 | 219 | |
| 220 | - /** |
|
| 221 | - * submit_button_text |
|
| 222 | - * the text that appears on the reg step form submit button |
|
| 223 | - * |
|
| 224 | - * @return string |
|
| 225 | - */ |
|
| 226 | - public function submit_button_text() |
|
| 227 | - { |
|
| 228 | - return $this->_submit_button_text; |
|
| 229 | - } |
|
| 220 | + /** |
|
| 221 | + * submit_button_text |
|
| 222 | + * the text that appears on the reg step form submit button |
|
| 223 | + * |
|
| 224 | + * @return string |
|
| 225 | + */ |
|
| 226 | + public function submit_button_text() |
|
| 227 | + { |
|
| 228 | + return $this->_submit_button_text; |
|
| 229 | + } |
|
| 230 | 230 | |
| 231 | 231 | |
| 232 | 232 | |
| 233 | - /** |
|
| 234 | - * set_submit_button_text |
|
| 235 | - * sets the text that appears on the reg step form submit button |
|
| 236 | - * |
|
| 237 | - * @param string $submit_button_text |
|
| 238 | - */ |
|
| 239 | - public function set_submit_button_text($submit_button_text = '') |
|
| 240 | - { |
|
| 241 | - if (! empty($submit_button_text)) { |
|
| 242 | - $this->_submit_button_text = $submit_button_text; |
|
| 243 | - } else if ($this->checkout->next_step instanceof EE_SPCO_Reg_Step) { |
|
| 244 | - if ($this->checkout->revisit) { |
|
| 245 | - $this->_submit_button_text = sprintf( |
|
| 246 | - __('Update %s', 'event_espresso'), |
|
| 247 | - $this->checkout->current_step->name() |
|
| 248 | - ); |
|
| 249 | - } else { |
|
| 250 | - $this->_submit_button_text = sprintf( |
|
| 251 | - __('Proceed to %s', 'event_espresso'), |
|
| 252 | - $this->checkout->next_step->name() |
|
| 253 | - ); |
|
| 254 | - } |
|
| 255 | - } |
|
| 256 | - // filters the submit button text |
|
| 257 | - $this->_submit_button_text = apply_filters( |
|
| 258 | - 'FHEE__EE_SPCO_Reg_Step__set_submit_button_text___submit_button_text', |
|
| 259 | - $this->_submit_button_text, |
|
| 260 | - $this->checkout |
|
| 261 | - ); |
|
| 262 | - } |
|
| 233 | + /** |
|
| 234 | + * set_submit_button_text |
|
| 235 | + * sets the text that appears on the reg step form submit button |
|
| 236 | + * |
|
| 237 | + * @param string $submit_button_text |
|
| 238 | + */ |
|
| 239 | + public function set_submit_button_text($submit_button_text = '') |
|
| 240 | + { |
|
| 241 | + if (! empty($submit_button_text)) { |
|
| 242 | + $this->_submit_button_text = $submit_button_text; |
|
| 243 | + } else if ($this->checkout->next_step instanceof EE_SPCO_Reg_Step) { |
|
| 244 | + if ($this->checkout->revisit) { |
|
| 245 | + $this->_submit_button_text = sprintf( |
|
| 246 | + __('Update %s', 'event_espresso'), |
|
| 247 | + $this->checkout->current_step->name() |
|
| 248 | + ); |
|
| 249 | + } else { |
|
| 250 | + $this->_submit_button_text = sprintf( |
|
| 251 | + __('Proceed to %s', 'event_espresso'), |
|
| 252 | + $this->checkout->next_step->name() |
|
| 253 | + ); |
|
| 254 | + } |
|
| 255 | + } |
|
| 256 | + // filters the submit button text |
|
| 257 | + $this->_submit_button_text = apply_filters( |
|
| 258 | + 'FHEE__EE_SPCO_Reg_Step__set_submit_button_text___submit_button_text', |
|
| 259 | + $this->_submit_button_text, |
|
| 260 | + $this->checkout |
|
| 261 | + ); |
|
| 262 | + } |
|
| 263 | 263 | |
| 264 | 264 | |
| 265 | 265 | |
| 266 | - /** |
|
| 267 | - * @param boolean $is_current_step |
|
| 268 | - */ |
|
| 269 | - public function set_is_current_step($is_current_step) |
|
| 270 | - { |
|
| 271 | - $this->_is_current_step = $is_current_step; |
|
| 272 | - } |
|
| 266 | + /** |
|
| 267 | + * @param boolean $is_current_step |
|
| 268 | + */ |
|
| 269 | + public function set_is_current_step($is_current_step) |
|
| 270 | + { |
|
| 271 | + $this->_is_current_step = $is_current_step; |
|
| 272 | + } |
|
| 273 | 273 | |
| 274 | 274 | |
| 275 | 275 | |
| 276 | - /** |
|
| 277 | - * @return boolean |
|
| 278 | - */ |
|
| 279 | - public function is_current_step() |
|
| 280 | - { |
|
| 281 | - return $this->_is_current_step; |
|
| 282 | - } |
|
| 276 | + /** |
|
| 277 | + * @return boolean |
|
| 278 | + */ |
|
| 279 | + public function is_current_step() |
|
| 280 | + { |
|
| 281 | + return $this->_is_current_step; |
|
| 282 | + } |
|
| 283 | 283 | |
| 284 | 284 | |
| 285 | 285 | |
| 286 | - /** |
|
| 287 | - * @return boolean |
|
| 288 | - */ |
|
| 289 | - public function is_final_step() |
|
| 290 | - { |
|
| 291 | - return $this instanceof EE_SPCO_Reg_Step_Finalize_Registration ? true : false; |
|
| 292 | - } |
|
| 286 | + /** |
|
| 287 | + * @return boolean |
|
| 288 | + */ |
|
| 289 | + public function is_final_step() |
|
| 290 | + { |
|
| 291 | + return $this instanceof EE_SPCO_Reg_Step_Finalize_Registration ? true : false; |
|
| 292 | + } |
|
| 293 | 293 | |
| 294 | 294 | |
| 295 | 295 | |
| 296 | - /** |
|
| 297 | - * @param int $order |
|
| 298 | - */ |
|
| 299 | - public function set_order($order) |
|
| 300 | - { |
|
| 301 | - $this->_order = $order; |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - |
|
| 296 | + /** |
|
| 297 | + * @param int $order |
|
| 298 | + */ |
|
| 299 | + public function set_order($order) |
|
| 300 | + { |
|
| 301 | + $this->_order = $order; |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + |
|
| 305 | 305 | |
| 306 | - /** |
|
| 307 | - * @return int |
|
| 308 | - */ |
|
| 309 | - public function order() |
|
| 310 | - { |
|
| 311 | - return $this->_order; |
|
| 312 | - } |
|
| 313 | - |
|
| 314 | - |
|
| 315 | - |
|
| 316 | - /** |
|
| 317 | - * @return string |
|
| 318 | - */ |
|
| 319 | - public function template() |
|
| 320 | - { |
|
| 321 | - return $this->_template; |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - |
|
| 325 | - |
|
| 326 | - /** |
|
| 327 | - * @return string |
|
| 328 | - */ |
|
| 329 | - public function success_message() |
|
| 330 | - { |
|
| 331 | - return $this->_success_message; |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - |
|
| 335 | - |
|
| 336 | - /** |
|
| 337 | - * _set_success_message |
|
| 338 | - * |
|
| 339 | - * @param string $success_message |
|
| 340 | - */ |
|
| 341 | - protected function _set_success_message($success_message) |
|
| 342 | - { |
|
| 343 | - $this->_success_message = $success_message; |
|
| 344 | - } |
|
| 345 | - |
|
| 346 | - |
|
| 347 | - |
|
| 348 | - /** |
|
| 349 | - * _reset_success_message |
|
| 350 | - * |
|
| 351 | - * @return void |
|
| 352 | - */ |
|
| 353 | - protected function _reset_success_message() |
|
| 354 | - { |
|
| 355 | - $this->_success_message = ''; |
|
| 356 | - } |
|
| 306 | + /** |
|
| 307 | + * @return int |
|
| 308 | + */ |
|
| 309 | + public function order() |
|
| 310 | + { |
|
| 311 | + return $this->_order; |
|
| 312 | + } |
|
| 313 | + |
|
| 314 | + |
|
| 315 | + |
|
| 316 | + /** |
|
| 317 | + * @return string |
|
| 318 | + */ |
|
| 319 | + public function template() |
|
| 320 | + { |
|
| 321 | + return $this->_template; |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + |
|
| 325 | + |
|
| 326 | + /** |
|
| 327 | + * @return string |
|
| 328 | + */ |
|
| 329 | + public function success_message() |
|
| 330 | + { |
|
| 331 | + return $this->_success_message; |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + |
|
| 335 | + |
|
| 336 | + /** |
|
| 337 | + * _set_success_message |
|
| 338 | + * |
|
| 339 | + * @param string $success_message |
|
| 340 | + */ |
|
| 341 | + protected function _set_success_message($success_message) |
|
| 342 | + { |
|
| 343 | + $this->_success_message = $success_message; |
|
| 344 | + } |
|
| 345 | + |
|
| 346 | + |
|
| 347 | + |
|
| 348 | + /** |
|
| 349 | + * _reset_success_message |
|
| 350 | + * |
|
| 351 | + * @return void |
|
| 352 | + */ |
|
| 353 | + protected function _reset_success_message() |
|
| 354 | + { |
|
| 355 | + $this->_success_message = ''; |
|
| 356 | + } |
|
| 357 | 357 | |
| 358 | 358 | |
| 359 | 359 | |
| 360 | - /** |
|
| 361 | - * @return string |
|
| 362 | - */ |
|
| 363 | - public function _instructions() |
|
| 364 | - { |
|
| 365 | - return $this->_instructions; |
|
| 366 | - } |
|
| 367 | - |
|
| 368 | - |
|
| 369 | - |
|
| 370 | - /** |
|
| 371 | - * @param string $instructions |
|
| 372 | - */ |
|
| 373 | - public function set_instructions($instructions) |
|
| 374 | - { |
|
| 375 | - $this->_instructions = apply_filters( |
|
| 376 | - 'FHEE__EE_SPCO_Reg_Step__set_instructions__instructions', |
|
| 377 | - $instructions, |
|
| 378 | - $this |
|
| 379 | - ); |
|
| 380 | - } |
|
| 381 | - |
|
| 382 | - |
|
| 383 | - |
|
| 384 | - /** |
|
| 385 | - * @param array $valid_data |
|
| 386 | - */ |
|
| 387 | - public function set_valid_data($valid_data) |
|
| 388 | - { |
|
| 389 | - $this->_valid_data = $valid_data; |
|
| 390 | - } |
|
| 391 | - |
|
| 392 | - |
|
| 393 | - |
|
| 394 | - /** |
|
| 395 | - * @return array |
|
| 396 | - */ |
|
| 397 | - public function valid_data() |
|
| 398 | - { |
|
| 399 | - if (empty($this->_valid_data)) { |
|
| 400 | - $this->_valid_data = $this->reg_form->valid_data(); |
|
| 401 | - } |
|
| 402 | - return $this->_valid_data; |
|
| 403 | - } |
|
| 404 | - |
|
| 405 | - |
|
| 406 | - |
|
| 407 | - /** |
|
| 408 | - * @return string |
|
| 409 | - */ |
|
| 410 | - public function reg_form_name() |
|
| 411 | - { |
|
| 412 | - if (empty($this->_reg_form_name)) { |
|
| 413 | - $this->set_reg_form_name('ee-spco-' . $this->slug() . '-reg-step-form'); |
|
| 414 | - } |
|
| 415 | - return $this->_reg_form_name; |
|
| 416 | - } |
|
| 417 | - |
|
| 418 | - |
|
| 419 | - |
|
| 420 | - /** |
|
| 421 | - * @param string $reg_form_name |
|
| 422 | - */ |
|
| 423 | - protected function set_reg_form_name($reg_form_name) |
|
| 424 | - { |
|
| 425 | - $this->_reg_form_name = $reg_form_name; |
|
| 426 | - } |
|
| 427 | - |
|
| 428 | - |
|
| 429 | - |
|
| 430 | - /** |
|
| 431 | - * reg_step_url |
|
| 432 | - * |
|
| 433 | - * @param string $action |
|
| 434 | - * @return string |
|
| 435 | - */ |
|
| 436 | - public function reg_step_url($action = '') |
|
| 437 | - { |
|
| 438 | - $query_args = array('step' => $this->slug()); |
|
| 439 | - if (! empty($action)) { |
|
| 440 | - $query_args['action'] = $action; |
|
| 441 | - } |
|
| 442 | - // final step has no display |
|
| 443 | - if ($this instanceof EE_SPCO_Reg_Step_Finalize_Registration && $action === 'display_spco_reg_step') { |
|
| 444 | - $query_args['action'] = 'process_reg_step'; |
|
| 445 | - } |
|
| 446 | - if ($this->checkout->revisit) { |
|
| 447 | - $query_args['revisit'] = true; |
|
| 448 | - } |
|
| 449 | - if ($this->checkout->reg_url_link) { |
|
| 450 | - $query_args['e_reg_url_link'] = $this->checkout->reg_url_link; |
|
| 451 | - } |
|
| 452 | - return add_query_arg($query_args, $this->checkout->reg_page_base_url); |
|
| 453 | - } |
|
| 454 | - |
|
| 455 | - |
|
| 456 | - |
|
| 457 | - /** |
|
| 458 | - * creates the default hidden inputs section |
|
| 459 | - * |
|
| 460 | - * @return EE_Form_Section_Proper |
|
| 461 | - * @throws \EE_Error |
|
| 462 | - */ |
|
| 463 | - public function reg_step_hidden_inputs() |
|
| 464 | - { |
|
| 465 | - // hidden inputs for admin registrations |
|
| 466 | - if ($this->checkout->admin_request) { |
|
| 467 | - return new EE_Form_Section_Proper( |
|
| 468 | - array( |
|
| 469 | - 'layout_strategy' => new EE_Div_Per_Section_Layout(), |
|
| 470 | - 'html_id' => 'ee-' . $this->slug() . '-hidden-inputs', |
|
| 471 | - 'subsections' => array( |
|
| 472 | - 'next_step' => new EE_Fixed_Hidden_Input( |
|
| 473 | - array( |
|
| 474 | - 'html_name' => 'next_step', |
|
| 475 | - 'html_id' => 'spco-' . $this->slug() . '-next-step', |
|
| 476 | - 'default' => $this->checkout->next_step instanceof EE_SPCO_Reg_Step |
|
| 477 | - ? $this->checkout->next_step->slug() |
|
| 478 | - : '', |
|
| 479 | - ) |
|
| 480 | - ), |
|
| 481 | - ), |
|
| 482 | - ) |
|
| 483 | - ); |
|
| 484 | - } else { |
|
| 485 | - $default_form_action = apply_filters( |
|
| 486 | - 'FHEE__EE_SPCO_Reg_Step__reg_step_hidden_inputs__default_form_action', |
|
| 487 | - empty($this->checkout->reg_url_link) |
|
| 488 | - ? 'process_reg_step' |
|
| 489 | - : 'update_reg_step', |
|
| 490 | - $this |
|
| 491 | - ); |
|
| 492 | - // hidden inputs for frontend registrations |
|
| 493 | - return new EE_Form_Section_Proper( |
|
| 494 | - array( |
|
| 495 | - 'layout_strategy' => new EE_Div_Per_Section_Layout(), |
|
| 496 | - 'html_id' => 'ee-' . $this->slug() . '-hidden-inputs', |
|
| 497 | - 'subsections' => array( |
|
| 498 | - 'action' => new EE_Fixed_Hidden_Input( |
|
| 499 | - array( |
|
| 500 | - 'html_name' => 'action', |
|
| 501 | - 'html_id' => 'spco-' . $this->slug() . '-action', |
|
| 502 | - 'default' => $default_form_action, |
|
| 503 | - ) |
|
| 504 | - ), |
|
| 505 | - 'next_step' => new EE_Fixed_Hidden_Input( |
|
| 506 | - array( |
|
| 507 | - 'html_name' => 'next_step', |
|
| 508 | - 'html_id' => 'spco-' . $this->slug() . '-next-step', |
|
| 509 | - 'default' => $this->checkout->next_step instanceof EE_SPCO_Reg_Step |
|
| 510 | - ? $this->checkout->next_step->slug() |
|
| 511 | - : '', |
|
| 512 | - ) |
|
| 513 | - ), |
|
| 514 | - 'e_reg_url_link' => new EE_Fixed_Hidden_Input( |
|
| 515 | - array( |
|
| 516 | - 'html_name' => 'e_reg_url_link', |
|
| 517 | - 'html_id' => 'spco-reg_url_link', |
|
| 518 | - 'default' => $this->checkout->reg_url_link, |
|
| 519 | - ) |
|
| 520 | - ), |
|
| 521 | - 'revisit' => new EE_Fixed_Hidden_Input( |
|
| 522 | - array( |
|
| 523 | - 'html_name' => 'revisit', |
|
| 524 | - 'html_id' => 'spco-revisit', |
|
| 525 | - 'default' => $this->checkout->revisit, |
|
| 526 | - ) |
|
| 527 | - ), |
|
| 528 | - ), |
|
| 529 | - ) |
|
| 530 | - ); |
|
| 531 | - } |
|
| 532 | - } |
|
| 533 | - |
|
| 534 | - |
|
| 535 | - |
|
| 536 | - /** |
|
| 537 | - * generate_reg_form_for_actions |
|
| 538 | - * |
|
| 539 | - * @param array $actions |
|
| 540 | - * @return void |
|
| 541 | - */ |
|
| 542 | - public function generate_reg_form_for_actions($actions = array()) |
|
| 543 | - { |
|
| 544 | - $actions = array_merge( |
|
| 545 | - array( |
|
| 546 | - 'generate_reg_form', |
|
| 547 | - 'display_spco_reg_step', |
|
| 548 | - 'process_reg_step', |
|
| 549 | - 'update_reg_step', |
|
| 550 | - ), |
|
| 551 | - $actions |
|
| 552 | - ); |
|
| 553 | - $this->checkout->generate_reg_form = in_array($this->checkout->action, $actions, true) ? true : false; |
|
| 554 | - } |
|
| 555 | - |
|
| 556 | - |
|
| 557 | - |
|
| 558 | - /** |
|
| 559 | - * @return string |
|
| 560 | - * @throws \EE_Error |
|
| 561 | - */ |
|
| 562 | - public function display_reg_form() |
|
| 563 | - { |
|
| 564 | - $html = ''; |
|
| 565 | - if ($this->reg_form instanceof EE_Form_Section_Proper) { |
|
| 566 | - $html .= ! $this->checkout->admin_request ? $this->reg_form->form_open($this->reg_step_url()) : ''; |
|
| 567 | - if (EE_Registry::instance()->REQ->ajax) { |
|
| 568 | - $this->reg_form->localize_validation_rules(); |
|
| 569 | - $this->checkout->json_response->add_validation_rules(EE_Form_Section_Proper::js_localization()); |
|
| 570 | - } |
|
| 571 | - $html .= $this->reg_form->get_html(); |
|
| 572 | - $html .= ! $this->checkout->admin_request ? $this->reg_step_submit_button() : ''; |
|
| 573 | - $html .= ! $this->checkout->admin_request ? $this->reg_form->form_close() : ''; |
|
| 574 | - } |
|
| 575 | - return $html; |
|
| 576 | - } |
|
| 577 | - |
|
| 578 | - |
|
| 579 | - |
|
| 580 | - /** |
|
| 581 | - * div_class - returns nothing for current step, but a css class of "hidden" for others |
|
| 582 | - * |
|
| 583 | - * @return string |
|
| 584 | - * @throws \EE_Error |
|
| 585 | - */ |
|
| 586 | - public function reg_step_submit_button() |
|
| 587 | - { |
|
| 588 | - if (! $this->checkout->next_step instanceof EE_SPCO_Reg_Step) { |
|
| 589 | - return ''; |
|
| 590 | - } |
|
| 591 | - ob_start(); |
|
| 592 | - do_action( |
|
| 593 | - 'AHEE__before_spco_whats_next_buttons', |
|
| 594 | - $this->slug(), |
|
| 595 | - $this->checkout->next_step->slug(), |
|
| 596 | - $this->checkout |
|
| 597 | - ); |
|
| 598 | - $html = ob_get_clean(); |
|
| 599 | - // generate submit button |
|
| 600 | - $sbmt_btn = new EE_Submit_Input(array( |
|
| 601 | - 'html_name' => 'spco-go-to-step-' . $this->checkout->next_step->slug(), |
|
| 602 | - 'html_id' => 'spco-go-to-step-' . $this->checkout->next_step->slug(), |
|
| 603 | - 'html_class' => 'spco-next-step-btn', |
|
| 604 | - 'other_html_attributes' => ' rel="' . $this->slug() . '"', |
|
| 605 | - 'default' => $this->submit_button_text(), |
|
| 606 | - )); |
|
| 607 | - $sbmt_btn->set_button_css_attributes(true, 'large'); |
|
| 608 | - $sbmt_btn_html = $sbmt_btn->get_html_for_input(); |
|
| 609 | - $html .= EEH_HTML::div( |
|
| 610 | - apply_filters('FHEE__EE_SPCO_Reg_Step__reg_step_submit_button__sbmt_btn_html', $sbmt_btn_html, $this), |
|
| 611 | - 'spco-' . $this->slug() . '-whats-next-buttons-dv', |
|
| 612 | - 'spco-whats-next-buttons' |
|
| 613 | - ); |
|
| 614 | - return $html; |
|
| 615 | - } |
|
| 616 | - |
|
| 617 | - |
|
| 618 | - |
|
| 619 | - /** |
|
| 620 | - * div_class - returns nothing for current step, but a css class of "hidden" for others |
|
| 621 | - * |
|
| 622 | - * @return string |
|
| 623 | - */ |
|
| 624 | - public function div_class() |
|
| 625 | - { |
|
| 626 | - return $this->is_current_step() ? '' : ' hidden'; |
|
| 627 | - } |
|
| 628 | - |
|
| 629 | - |
|
| 630 | - |
|
| 631 | - /** |
|
| 632 | - * div_class - returns a css class of "hidden" for current step, but nothing for others |
|
| 633 | - * |
|
| 634 | - * @return string |
|
| 635 | - */ |
|
| 636 | - public function edit_lnk_url() |
|
| 637 | - { |
|
| 638 | - return add_query_arg(array('step' => $this->slug()), $this->checkout->reg_page_base_url); |
|
| 639 | - } |
|
| 640 | - |
|
| 641 | - |
|
| 642 | - |
|
| 643 | - /** |
|
| 644 | - * div_class - returns a css class of "hidden" for current step, but nothing for others |
|
| 645 | - * |
|
| 646 | - * @return string |
|
| 647 | - */ |
|
| 648 | - public function edit_link_class() |
|
| 649 | - { |
|
| 650 | - return $this->is_current_step() ? ' hidden' : ''; |
|
| 651 | - } |
|
| 652 | - |
|
| 653 | - |
|
| 654 | - |
|
| 655 | - /** |
|
| 656 | - * update_checkout with changes that have been made to the cart |
|
| 657 | - * |
|
| 658 | - * @return void |
|
| 659 | - * @throws \EE_Error |
|
| 660 | - */ |
|
| 661 | - public function update_checkout() |
|
| 662 | - { |
|
| 663 | - // grab the cart grand total and reset TXN total |
|
| 664 | - $this->checkout->transaction->set_total($this->checkout->cart->get_cart_grand_total()); |
|
| 665 | - $this->checkout->stash_transaction_and_checkout(); |
|
| 666 | - } |
|
| 667 | - |
|
| 668 | - |
|
| 669 | - |
|
| 670 | - |
|
| 671 | - |
|
| 672 | - /** |
|
| 673 | - * __sleep |
|
| 674 | - * to conserve db space, let's remove the reg_form and the EE_Checkout object from EE_SPCO_Reg_Step objects upon |
|
| 675 | - * serialization EE_Checkout will handle the reimplementation of itself upon waking, but we won't bother with the |
|
| 676 | - * reg form, because if needed, it will be regenerated anyways |
|
| 677 | - * |
|
| 678 | - * @return array |
|
| 679 | - */ |
|
| 680 | - public function __sleep() |
|
| 681 | - { |
|
| 682 | - // remove the reg form and the checkout |
|
| 683 | - return array_diff(array_keys(get_object_vars($this)), array('reg_form', 'checkout')); |
|
| 684 | - } |
|
| 360 | + /** |
|
| 361 | + * @return string |
|
| 362 | + */ |
|
| 363 | + public function _instructions() |
|
| 364 | + { |
|
| 365 | + return $this->_instructions; |
|
| 366 | + } |
|
| 367 | + |
|
| 368 | + |
|
| 369 | + |
|
| 370 | + /** |
|
| 371 | + * @param string $instructions |
|
| 372 | + */ |
|
| 373 | + public function set_instructions($instructions) |
|
| 374 | + { |
|
| 375 | + $this->_instructions = apply_filters( |
|
| 376 | + 'FHEE__EE_SPCO_Reg_Step__set_instructions__instructions', |
|
| 377 | + $instructions, |
|
| 378 | + $this |
|
| 379 | + ); |
|
| 380 | + } |
|
| 381 | + |
|
| 382 | + |
|
| 383 | + |
|
| 384 | + /** |
|
| 385 | + * @param array $valid_data |
|
| 386 | + */ |
|
| 387 | + public function set_valid_data($valid_data) |
|
| 388 | + { |
|
| 389 | + $this->_valid_data = $valid_data; |
|
| 390 | + } |
|
| 391 | + |
|
| 392 | + |
|
| 393 | + |
|
| 394 | + /** |
|
| 395 | + * @return array |
|
| 396 | + */ |
|
| 397 | + public function valid_data() |
|
| 398 | + { |
|
| 399 | + if (empty($this->_valid_data)) { |
|
| 400 | + $this->_valid_data = $this->reg_form->valid_data(); |
|
| 401 | + } |
|
| 402 | + return $this->_valid_data; |
|
| 403 | + } |
|
| 404 | + |
|
| 405 | + |
|
| 406 | + |
|
| 407 | + /** |
|
| 408 | + * @return string |
|
| 409 | + */ |
|
| 410 | + public function reg_form_name() |
|
| 411 | + { |
|
| 412 | + if (empty($this->_reg_form_name)) { |
|
| 413 | + $this->set_reg_form_name('ee-spco-' . $this->slug() . '-reg-step-form'); |
|
| 414 | + } |
|
| 415 | + return $this->_reg_form_name; |
|
| 416 | + } |
|
| 417 | + |
|
| 418 | + |
|
| 419 | + |
|
| 420 | + /** |
|
| 421 | + * @param string $reg_form_name |
|
| 422 | + */ |
|
| 423 | + protected function set_reg_form_name($reg_form_name) |
|
| 424 | + { |
|
| 425 | + $this->_reg_form_name = $reg_form_name; |
|
| 426 | + } |
|
| 427 | + |
|
| 428 | + |
|
| 429 | + |
|
| 430 | + /** |
|
| 431 | + * reg_step_url |
|
| 432 | + * |
|
| 433 | + * @param string $action |
|
| 434 | + * @return string |
|
| 435 | + */ |
|
| 436 | + public function reg_step_url($action = '') |
|
| 437 | + { |
|
| 438 | + $query_args = array('step' => $this->slug()); |
|
| 439 | + if (! empty($action)) { |
|
| 440 | + $query_args['action'] = $action; |
|
| 441 | + } |
|
| 442 | + // final step has no display |
|
| 443 | + if ($this instanceof EE_SPCO_Reg_Step_Finalize_Registration && $action === 'display_spco_reg_step') { |
|
| 444 | + $query_args['action'] = 'process_reg_step'; |
|
| 445 | + } |
|
| 446 | + if ($this->checkout->revisit) { |
|
| 447 | + $query_args['revisit'] = true; |
|
| 448 | + } |
|
| 449 | + if ($this->checkout->reg_url_link) { |
|
| 450 | + $query_args['e_reg_url_link'] = $this->checkout->reg_url_link; |
|
| 451 | + } |
|
| 452 | + return add_query_arg($query_args, $this->checkout->reg_page_base_url); |
|
| 453 | + } |
|
| 454 | + |
|
| 455 | + |
|
| 456 | + |
|
| 457 | + /** |
|
| 458 | + * creates the default hidden inputs section |
|
| 459 | + * |
|
| 460 | + * @return EE_Form_Section_Proper |
|
| 461 | + * @throws \EE_Error |
|
| 462 | + */ |
|
| 463 | + public function reg_step_hidden_inputs() |
|
| 464 | + { |
|
| 465 | + // hidden inputs for admin registrations |
|
| 466 | + if ($this->checkout->admin_request) { |
|
| 467 | + return new EE_Form_Section_Proper( |
|
| 468 | + array( |
|
| 469 | + 'layout_strategy' => new EE_Div_Per_Section_Layout(), |
|
| 470 | + 'html_id' => 'ee-' . $this->slug() . '-hidden-inputs', |
|
| 471 | + 'subsections' => array( |
|
| 472 | + 'next_step' => new EE_Fixed_Hidden_Input( |
|
| 473 | + array( |
|
| 474 | + 'html_name' => 'next_step', |
|
| 475 | + 'html_id' => 'spco-' . $this->slug() . '-next-step', |
|
| 476 | + 'default' => $this->checkout->next_step instanceof EE_SPCO_Reg_Step |
|
| 477 | + ? $this->checkout->next_step->slug() |
|
| 478 | + : '', |
|
| 479 | + ) |
|
| 480 | + ), |
|
| 481 | + ), |
|
| 482 | + ) |
|
| 483 | + ); |
|
| 484 | + } else { |
|
| 485 | + $default_form_action = apply_filters( |
|
| 486 | + 'FHEE__EE_SPCO_Reg_Step__reg_step_hidden_inputs__default_form_action', |
|
| 487 | + empty($this->checkout->reg_url_link) |
|
| 488 | + ? 'process_reg_step' |
|
| 489 | + : 'update_reg_step', |
|
| 490 | + $this |
|
| 491 | + ); |
|
| 492 | + // hidden inputs for frontend registrations |
|
| 493 | + return new EE_Form_Section_Proper( |
|
| 494 | + array( |
|
| 495 | + 'layout_strategy' => new EE_Div_Per_Section_Layout(), |
|
| 496 | + 'html_id' => 'ee-' . $this->slug() . '-hidden-inputs', |
|
| 497 | + 'subsections' => array( |
|
| 498 | + 'action' => new EE_Fixed_Hidden_Input( |
|
| 499 | + array( |
|
| 500 | + 'html_name' => 'action', |
|
| 501 | + 'html_id' => 'spco-' . $this->slug() . '-action', |
|
| 502 | + 'default' => $default_form_action, |
|
| 503 | + ) |
|
| 504 | + ), |
|
| 505 | + 'next_step' => new EE_Fixed_Hidden_Input( |
|
| 506 | + array( |
|
| 507 | + 'html_name' => 'next_step', |
|
| 508 | + 'html_id' => 'spco-' . $this->slug() . '-next-step', |
|
| 509 | + 'default' => $this->checkout->next_step instanceof EE_SPCO_Reg_Step |
|
| 510 | + ? $this->checkout->next_step->slug() |
|
| 511 | + : '', |
|
| 512 | + ) |
|
| 513 | + ), |
|
| 514 | + 'e_reg_url_link' => new EE_Fixed_Hidden_Input( |
|
| 515 | + array( |
|
| 516 | + 'html_name' => 'e_reg_url_link', |
|
| 517 | + 'html_id' => 'spco-reg_url_link', |
|
| 518 | + 'default' => $this->checkout->reg_url_link, |
|
| 519 | + ) |
|
| 520 | + ), |
|
| 521 | + 'revisit' => new EE_Fixed_Hidden_Input( |
|
| 522 | + array( |
|
| 523 | + 'html_name' => 'revisit', |
|
| 524 | + 'html_id' => 'spco-revisit', |
|
| 525 | + 'default' => $this->checkout->revisit, |
|
| 526 | + ) |
|
| 527 | + ), |
|
| 528 | + ), |
|
| 529 | + ) |
|
| 530 | + ); |
|
| 531 | + } |
|
| 532 | + } |
|
| 533 | + |
|
| 534 | + |
|
| 535 | + |
|
| 536 | + /** |
|
| 537 | + * generate_reg_form_for_actions |
|
| 538 | + * |
|
| 539 | + * @param array $actions |
|
| 540 | + * @return void |
|
| 541 | + */ |
|
| 542 | + public function generate_reg_form_for_actions($actions = array()) |
|
| 543 | + { |
|
| 544 | + $actions = array_merge( |
|
| 545 | + array( |
|
| 546 | + 'generate_reg_form', |
|
| 547 | + 'display_spco_reg_step', |
|
| 548 | + 'process_reg_step', |
|
| 549 | + 'update_reg_step', |
|
| 550 | + ), |
|
| 551 | + $actions |
|
| 552 | + ); |
|
| 553 | + $this->checkout->generate_reg_form = in_array($this->checkout->action, $actions, true) ? true : false; |
|
| 554 | + } |
|
| 555 | + |
|
| 556 | + |
|
| 557 | + |
|
| 558 | + /** |
|
| 559 | + * @return string |
|
| 560 | + * @throws \EE_Error |
|
| 561 | + */ |
|
| 562 | + public function display_reg_form() |
|
| 563 | + { |
|
| 564 | + $html = ''; |
|
| 565 | + if ($this->reg_form instanceof EE_Form_Section_Proper) { |
|
| 566 | + $html .= ! $this->checkout->admin_request ? $this->reg_form->form_open($this->reg_step_url()) : ''; |
|
| 567 | + if (EE_Registry::instance()->REQ->ajax) { |
|
| 568 | + $this->reg_form->localize_validation_rules(); |
|
| 569 | + $this->checkout->json_response->add_validation_rules(EE_Form_Section_Proper::js_localization()); |
|
| 570 | + } |
|
| 571 | + $html .= $this->reg_form->get_html(); |
|
| 572 | + $html .= ! $this->checkout->admin_request ? $this->reg_step_submit_button() : ''; |
|
| 573 | + $html .= ! $this->checkout->admin_request ? $this->reg_form->form_close() : ''; |
|
| 574 | + } |
|
| 575 | + return $html; |
|
| 576 | + } |
|
| 577 | + |
|
| 578 | + |
|
| 579 | + |
|
| 580 | + /** |
|
| 581 | + * div_class - returns nothing for current step, but a css class of "hidden" for others |
|
| 582 | + * |
|
| 583 | + * @return string |
|
| 584 | + * @throws \EE_Error |
|
| 585 | + */ |
|
| 586 | + public function reg_step_submit_button() |
|
| 587 | + { |
|
| 588 | + if (! $this->checkout->next_step instanceof EE_SPCO_Reg_Step) { |
|
| 589 | + return ''; |
|
| 590 | + } |
|
| 591 | + ob_start(); |
|
| 592 | + do_action( |
|
| 593 | + 'AHEE__before_spco_whats_next_buttons', |
|
| 594 | + $this->slug(), |
|
| 595 | + $this->checkout->next_step->slug(), |
|
| 596 | + $this->checkout |
|
| 597 | + ); |
|
| 598 | + $html = ob_get_clean(); |
|
| 599 | + // generate submit button |
|
| 600 | + $sbmt_btn = new EE_Submit_Input(array( |
|
| 601 | + 'html_name' => 'spco-go-to-step-' . $this->checkout->next_step->slug(), |
|
| 602 | + 'html_id' => 'spco-go-to-step-' . $this->checkout->next_step->slug(), |
|
| 603 | + 'html_class' => 'spco-next-step-btn', |
|
| 604 | + 'other_html_attributes' => ' rel="' . $this->slug() . '"', |
|
| 605 | + 'default' => $this->submit_button_text(), |
|
| 606 | + )); |
|
| 607 | + $sbmt_btn->set_button_css_attributes(true, 'large'); |
|
| 608 | + $sbmt_btn_html = $sbmt_btn->get_html_for_input(); |
|
| 609 | + $html .= EEH_HTML::div( |
|
| 610 | + apply_filters('FHEE__EE_SPCO_Reg_Step__reg_step_submit_button__sbmt_btn_html', $sbmt_btn_html, $this), |
|
| 611 | + 'spco-' . $this->slug() . '-whats-next-buttons-dv', |
|
| 612 | + 'spco-whats-next-buttons' |
|
| 613 | + ); |
|
| 614 | + return $html; |
|
| 615 | + } |
|
| 616 | + |
|
| 617 | + |
|
| 618 | + |
|
| 619 | + /** |
|
| 620 | + * div_class - returns nothing for current step, but a css class of "hidden" for others |
|
| 621 | + * |
|
| 622 | + * @return string |
|
| 623 | + */ |
|
| 624 | + public function div_class() |
|
| 625 | + { |
|
| 626 | + return $this->is_current_step() ? '' : ' hidden'; |
|
| 627 | + } |
|
| 628 | + |
|
| 629 | + |
|
| 630 | + |
|
| 631 | + /** |
|
| 632 | + * div_class - returns a css class of "hidden" for current step, but nothing for others |
|
| 633 | + * |
|
| 634 | + * @return string |
|
| 635 | + */ |
|
| 636 | + public function edit_lnk_url() |
|
| 637 | + { |
|
| 638 | + return add_query_arg(array('step' => $this->slug()), $this->checkout->reg_page_base_url); |
|
| 639 | + } |
|
| 640 | + |
|
| 641 | + |
|
| 642 | + |
|
| 643 | + /** |
|
| 644 | + * div_class - returns a css class of "hidden" for current step, but nothing for others |
|
| 645 | + * |
|
| 646 | + * @return string |
|
| 647 | + */ |
|
| 648 | + public function edit_link_class() |
|
| 649 | + { |
|
| 650 | + return $this->is_current_step() ? ' hidden' : ''; |
|
| 651 | + } |
|
| 652 | + |
|
| 653 | + |
|
| 654 | + |
|
| 655 | + /** |
|
| 656 | + * update_checkout with changes that have been made to the cart |
|
| 657 | + * |
|
| 658 | + * @return void |
|
| 659 | + * @throws \EE_Error |
|
| 660 | + */ |
|
| 661 | + public function update_checkout() |
|
| 662 | + { |
|
| 663 | + // grab the cart grand total and reset TXN total |
|
| 664 | + $this->checkout->transaction->set_total($this->checkout->cart->get_cart_grand_total()); |
|
| 665 | + $this->checkout->stash_transaction_and_checkout(); |
|
| 666 | + } |
|
| 667 | + |
|
| 668 | + |
|
| 669 | + |
|
| 670 | + |
|
| 671 | + |
|
| 672 | + /** |
|
| 673 | + * __sleep |
|
| 674 | + * to conserve db space, let's remove the reg_form and the EE_Checkout object from EE_SPCO_Reg_Step objects upon |
|
| 675 | + * serialization EE_Checkout will handle the reimplementation of itself upon waking, but we won't bother with the |
|
| 676 | + * reg form, because if needed, it will be regenerated anyways |
|
| 677 | + * |
|
| 678 | + * @return array |
|
| 679 | + */ |
|
| 680 | + public function __sleep() |
|
| 681 | + { |
|
| 682 | + // remove the reg form and the checkout |
|
| 683 | + return array_diff(array_keys(get_object_vars($this)), array('reg_form', 'checkout')); |
|
| 684 | + } |
|
| 685 | 685 | |
| 686 | 686 | |
| 687 | 687 | |
@@ -1,4 +1,4 @@ discard block |
||
| 1 | -<?php if (! defined('EVENT_ESPRESSO_VERSION')) { |
|
| 1 | +<?php if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
| 2 | 2 | exit('No direct script access allowed'); |
| 3 | 3 | } |
| 4 | 4 | |
@@ -238,7 +238,7 @@ discard block |
||
| 238 | 238 | */ |
| 239 | 239 | public function set_submit_button_text($submit_button_text = '') |
| 240 | 240 | { |
| 241 | - if (! empty($submit_button_text)) { |
|
| 241 | + if ( ! empty($submit_button_text)) { |
|
| 242 | 242 | $this->_submit_button_text = $submit_button_text; |
| 243 | 243 | } else if ($this->checkout->next_step instanceof EE_SPCO_Reg_Step) { |
| 244 | 244 | if ($this->checkout->revisit) { |
@@ -410,7 +410,7 @@ discard block |
||
| 410 | 410 | public function reg_form_name() |
| 411 | 411 | { |
| 412 | 412 | if (empty($this->_reg_form_name)) { |
| 413 | - $this->set_reg_form_name('ee-spco-' . $this->slug() . '-reg-step-form'); |
|
| 413 | + $this->set_reg_form_name('ee-spco-'.$this->slug().'-reg-step-form'); |
|
| 414 | 414 | } |
| 415 | 415 | return $this->_reg_form_name; |
| 416 | 416 | } |
@@ -436,7 +436,7 @@ discard block |
||
| 436 | 436 | public function reg_step_url($action = '') |
| 437 | 437 | { |
| 438 | 438 | $query_args = array('step' => $this->slug()); |
| 439 | - if (! empty($action)) { |
|
| 439 | + if ( ! empty($action)) { |
|
| 440 | 440 | $query_args['action'] = $action; |
| 441 | 441 | } |
| 442 | 442 | // final step has no display |
@@ -467,12 +467,12 @@ discard block |
||
| 467 | 467 | return new EE_Form_Section_Proper( |
| 468 | 468 | array( |
| 469 | 469 | 'layout_strategy' => new EE_Div_Per_Section_Layout(), |
| 470 | - 'html_id' => 'ee-' . $this->slug() . '-hidden-inputs', |
|
| 470 | + 'html_id' => 'ee-'.$this->slug().'-hidden-inputs', |
|
| 471 | 471 | 'subsections' => array( |
| 472 | 472 | 'next_step' => new EE_Fixed_Hidden_Input( |
| 473 | 473 | array( |
| 474 | 474 | 'html_name' => 'next_step', |
| 475 | - 'html_id' => 'spco-' . $this->slug() . '-next-step', |
|
| 475 | + 'html_id' => 'spco-'.$this->slug().'-next-step', |
|
| 476 | 476 | 'default' => $this->checkout->next_step instanceof EE_SPCO_Reg_Step |
| 477 | 477 | ? $this->checkout->next_step->slug() |
| 478 | 478 | : '', |
@@ -493,19 +493,19 @@ discard block |
||
| 493 | 493 | return new EE_Form_Section_Proper( |
| 494 | 494 | array( |
| 495 | 495 | 'layout_strategy' => new EE_Div_Per_Section_Layout(), |
| 496 | - 'html_id' => 'ee-' . $this->slug() . '-hidden-inputs', |
|
| 496 | + 'html_id' => 'ee-'.$this->slug().'-hidden-inputs', |
|
| 497 | 497 | 'subsections' => array( |
| 498 | 498 | 'action' => new EE_Fixed_Hidden_Input( |
| 499 | 499 | array( |
| 500 | 500 | 'html_name' => 'action', |
| 501 | - 'html_id' => 'spco-' . $this->slug() . '-action', |
|
| 501 | + 'html_id' => 'spco-'.$this->slug().'-action', |
|
| 502 | 502 | 'default' => $default_form_action, |
| 503 | 503 | ) |
| 504 | 504 | ), |
| 505 | 505 | 'next_step' => new EE_Fixed_Hidden_Input( |
| 506 | 506 | array( |
| 507 | 507 | 'html_name' => 'next_step', |
| 508 | - 'html_id' => 'spco-' . $this->slug() . '-next-step', |
|
| 508 | + 'html_id' => 'spco-'.$this->slug().'-next-step', |
|
| 509 | 509 | 'default' => $this->checkout->next_step instanceof EE_SPCO_Reg_Step |
| 510 | 510 | ? $this->checkout->next_step->slug() |
| 511 | 511 | : '', |
@@ -585,7 +585,7 @@ discard block |
||
| 585 | 585 | */ |
| 586 | 586 | public function reg_step_submit_button() |
| 587 | 587 | { |
| 588 | - if (! $this->checkout->next_step instanceof EE_SPCO_Reg_Step) { |
|
| 588 | + if ( ! $this->checkout->next_step instanceof EE_SPCO_Reg_Step) { |
|
| 589 | 589 | return ''; |
| 590 | 590 | } |
| 591 | 591 | ob_start(); |
@@ -598,17 +598,17 @@ discard block |
||
| 598 | 598 | $html = ob_get_clean(); |
| 599 | 599 | // generate submit button |
| 600 | 600 | $sbmt_btn = new EE_Submit_Input(array( |
| 601 | - 'html_name' => 'spco-go-to-step-' . $this->checkout->next_step->slug(), |
|
| 602 | - 'html_id' => 'spco-go-to-step-' . $this->checkout->next_step->slug(), |
|
| 601 | + 'html_name' => 'spco-go-to-step-'.$this->checkout->next_step->slug(), |
|
| 602 | + 'html_id' => 'spco-go-to-step-'.$this->checkout->next_step->slug(), |
|
| 603 | 603 | 'html_class' => 'spco-next-step-btn', |
| 604 | - 'other_html_attributes' => ' rel="' . $this->slug() . '"', |
|
| 604 | + 'other_html_attributes' => ' rel="'.$this->slug().'"', |
|
| 605 | 605 | 'default' => $this->submit_button_text(), |
| 606 | 606 | )); |
| 607 | 607 | $sbmt_btn->set_button_css_attributes(true, 'large'); |
| 608 | 608 | $sbmt_btn_html = $sbmt_btn->get_html_for_input(); |
| 609 | 609 | $html .= EEH_HTML::div( |
| 610 | 610 | apply_filters('FHEE__EE_SPCO_Reg_Step__reg_step_submit_button__sbmt_btn_html', $sbmt_btn_html, $this), |
| 611 | - 'spco-' . $this->slug() . '-whats-next-buttons-dv', |
|
| 611 | + 'spco-'.$this->slug().'-whats-next-buttons-dv', |
|
| 612 | 612 | 'spco-whats-next-buttons' |
| 613 | 613 | ); |
| 614 | 614 | return $html; |
@@ -13,7 +13,7 @@ discard block |
||
| 13 | 13 | use EventEspresso\core\services\commands\CommandRequiresCapCheckInterface; |
| 14 | 14 | |
| 15 | 15 | if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
| 16 | - exit('No direct script access allowed'); |
|
| 16 | + exit('No direct script access allowed'); |
|
| 17 | 17 | } |
| 18 | 18 | |
| 19 | 19 | |
@@ -29,162 +29,162 @@ discard block |
||
| 29 | 29 | class CreateRegistrationCommand extends Command implements CommandRequiresCapCheckInterface |
| 30 | 30 | { |
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * @var EE_Transaction $transaction |
|
| 34 | - */ |
|
| 35 | - private $transaction; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * @var EE_Ticket $ticket |
|
| 39 | - */ |
|
| 40 | - private $ticket; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * @var EE_Line_Item $ticket_line_item |
|
| 44 | - */ |
|
| 45 | - private $ticket_line_item; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * @var int $reg_count |
|
| 49 | - */ |
|
| 50 | - private $reg_count; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * @var int $reg_group_size |
|
| 54 | - */ |
|
| 55 | - private $reg_group_size; |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * @var string $reg_status |
|
| 59 | - */ |
|
| 60 | - private $reg_status; |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * @var EE_Registration $registration |
|
| 64 | - */ |
|
| 65 | - protected $registration; |
|
| 66 | - |
|
| 67 | - |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * CreateRegistrationCommand constructor. |
|
| 71 | - * |
|
| 72 | - * @param EE_Transaction $transaction |
|
| 73 | - * @param EE_Line_Item $ticket_line_item |
|
| 74 | - * @param int $reg_count |
|
| 75 | - * @param int $reg_group_size |
|
| 76 | - * @param string $reg_status |
|
| 77 | - * @throws InvalidEntityException |
|
| 78 | - */ |
|
| 79 | - public function __construct( |
|
| 80 | - EE_Transaction $transaction, |
|
| 81 | - EE_Line_Item $ticket_line_item, |
|
| 82 | - $reg_count = 1, |
|
| 83 | - $reg_group_size = 0, |
|
| 84 | - $reg_status = EEM_Registration::status_id_incomplete |
|
| 85 | - ) { |
|
| 86 | - // grab the related ticket object for this line_item |
|
| 87 | - $this->ticket = $ticket_line_item->ticket(); |
|
| 88 | - if ( ! $this->ticket instanceof EE_Ticket) { |
|
| 89 | - throw new InvalidEntityException( |
|
| 90 | - is_object($this->ticket) ? get_class($this->ticket) : gettype($this->ticket), |
|
| 91 | - 'EE_Ticket', |
|
| 92 | - sprintf( |
|
| 93 | - __('Line item %s did not contain a valid ticket', 'event_espresso'), |
|
| 94 | - $ticket_line_item->ID() |
|
| 95 | - ) |
|
| 96 | - ); |
|
| 97 | - } |
|
| 98 | - $this->transaction = $transaction; |
|
| 99 | - $this->ticket_line_item = $ticket_line_item; |
|
| 100 | - $this->reg_count = absint($reg_count); |
|
| 101 | - $this->reg_group_size = absint($reg_group_size); |
|
| 102 | - $this->reg_status = $reg_status; |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * @return \EventEspresso\core\domain\services\capabilities\CapCheckInterface |
|
| 109 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 110 | - */ |
|
| 111 | - public function getCapCheck() |
|
| 112 | - { |
|
| 113 | - if ( ! $this->cap_check instanceof CapCheckInterface) { |
|
| 114 | - return new CapCheck('ee_edit_registrations', 'create_new_registration'); |
|
| 115 | - } |
|
| 116 | - return $this->cap_check; |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * @return EE_Transaction |
|
| 123 | - */ |
|
| 124 | - public function transaction() |
|
| 125 | - { |
|
| 126 | - return $this->transaction; |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * @return EE_Ticket |
|
| 133 | - */ |
|
| 134 | - public function ticket() |
|
| 135 | - { |
|
| 136 | - return $this->ticket; |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - |
|
| 140 | - |
|
| 141 | - /** |
|
| 142 | - * @return EE_Line_Item |
|
| 143 | - */ |
|
| 144 | - public function ticketLineItem() |
|
| 145 | - { |
|
| 146 | - return $this->ticket_line_item; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * @return int |
|
| 153 | - */ |
|
| 154 | - public function regCount() |
|
| 155 | - { |
|
| 156 | - return $this->reg_count; |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * @return int |
|
| 163 | - */ |
|
| 164 | - public function regGroupSize() |
|
| 165 | - { |
|
| 166 | - return $this->reg_group_size; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - |
|
| 170 | - |
|
| 171 | - /** |
|
| 172 | - * @return string |
|
| 173 | - */ |
|
| 174 | - public function regStatus() |
|
| 175 | - { |
|
| 176 | - return $this->reg_status; |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * @return EE_Registration |
|
| 183 | - */ |
|
| 184 | - public function registration() |
|
| 185 | - { |
|
| 186 | - return $this->registration; |
|
| 187 | - } |
|
| 32 | + /** |
|
| 33 | + * @var EE_Transaction $transaction |
|
| 34 | + */ |
|
| 35 | + private $transaction; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * @var EE_Ticket $ticket |
|
| 39 | + */ |
|
| 40 | + private $ticket; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * @var EE_Line_Item $ticket_line_item |
|
| 44 | + */ |
|
| 45 | + private $ticket_line_item; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * @var int $reg_count |
|
| 49 | + */ |
|
| 50 | + private $reg_count; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * @var int $reg_group_size |
|
| 54 | + */ |
|
| 55 | + private $reg_group_size; |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * @var string $reg_status |
|
| 59 | + */ |
|
| 60 | + private $reg_status; |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * @var EE_Registration $registration |
|
| 64 | + */ |
|
| 65 | + protected $registration; |
|
| 66 | + |
|
| 67 | + |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * CreateRegistrationCommand constructor. |
|
| 71 | + * |
|
| 72 | + * @param EE_Transaction $transaction |
|
| 73 | + * @param EE_Line_Item $ticket_line_item |
|
| 74 | + * @param int $reg_count |
|
| 75 | + * @param int $reg_group_size |
|
| 76 | + * @param string $reg_status |
|
| 77 | + * @throws InvalidEntityException |
|
| 78 | + */ |
|
| 79 | + public function __construct( |
|
| 80 | + EE_Transaction $transaction, |
|
| 81 | + EE_Line_Item $ticket_line_item, |
|
| 82 | + $reg_count = 1, |
|
| 83 | + $reg_group_size = 0, |
|
| 84 | + $reg_status = EEM_Registration::status_id_incomplete |
|
| 85 | + ) { |
|
| 86 | + // grab the related ticket object for this line_item |
|
| 87 | + $this->ticket = $ticket_line_item->ticket(); |
|
| 88 | + if ( ! $this->ticket instanceof EE_Ticket) { |
|
| 89 | + throw new InvalidEntityException( |
|
| 90 | + is_object($this->ticket) ? get_class($this->ticket) : gettype($this->ticket), |
|
| 91 | + 'EE_Ticket', |
|
| 92 | + sprintf( |
|
| 93 | + __('Line item %s did not contain a valid ticket', 'event_espresso'), |
|
| 94 | + $ticket_line_item->ID() |
|
| 95 | + ) |
|
| 96 | + ); |
|
| 97 | + } |
|
| 98 | + $this->transaction = $transaction; |
|
| 99 | + $this->ticket_line_item = $ticket_line_item; |
|
| 100 | + $this->reg_count = absint($reg_count); |
|
| 101 | + $this->reg_group_size = absint($reg_group_size); |
|
| 102 | + $this->reg_status = $reg_status; |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * @return \EventEspresso\core\domain\services\capabilities\CapCheckInterface |
|
| 109 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 110 | + */ |
|
| 111 | + public function getCapCheck() |
|
| 112 | + { |
|
| 113 | + if ( ! $this->cap_check instanceof CapCheckInterface) { |
|
| 114 | + return new CapCheck('ee_edit_registrations', 'create_new_registration'); |
|
| 115 | + } |
|
| 116 | + return $this->cap_check; |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * @return EE_Transaction |
|
| 123 | + */ |
|
| 124 | + public function transaction() |
|
| 125 | + { |
|
| 126 | + return $this->transaction; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * @return EE_Ticket |
|
| 133 | + */ |
|
| 134 | + public function ticket() |
|
| 135 | + { |
|
| 136 | + return $this->ticket; |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + |
|
| 140 | + |
|
| 141 | + /** |
|
| 142 | + * @return EE_Line_Item |
|
| 143 | + */ |
|
| 144 | + public function ticketLineItem() |
|
| 145 | + { |
|
| 146 | + return $this->ticket_line_item; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * @return int |
|
| 153 | + */ |
|
| 154 | + public function regCount() |
|
| 155 | + { |
|
| 156 | + return $this->reg_count; |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * @return int |
|
| 163 | + */ |
|
| 164 | + public function regGroupSize() |
|
| 165 | + { |
|
| 166 | + return $this->reg_group_size; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + |
|
| 170 | + |
|
| 171 | + /** |
|
| 172 | + * @return string |
|
| 173 | + */ |
|
| 174 | + public function regStatus() |
|
| 175 | + { |
|
| 176 | + return $this->reg_status; |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * @return EE_Registration |
|
| 183 | + */ |
|
| 184 | + public function registration() |
|
| 185 | + { |
|
| 186 | + return $this->registration; |
|
| 187 | + } |
|
| 188 | 188 | |
| 189 | 189 | |
| 190 | 190 | |
@@ -7,7 +7,7 @@ discard block |
||
| 7 | 7 | use InvalidArgumentException; |
| 8 | 8 | |
| 9 | 9 | if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
| 10 | - exit('No direct script access allowed'); |
|
| 10 | + exit('No direct script access allowed'); |
|
| 11 | 11 | } |
| 12 | 12 | |
| 13 | 13 | |
@@ -23,103 +23,103 @@ discard block |
||
| 23 | 23 | class RegUrlLink |
| 24 | 24 | { |
| 25 | 25 | |
| 26 | - /* |
|
| 26 | + /* |
|
| 27 | 27 | * @var string $reg_url_link |
| 28 | 28 | */ |
| 29 | - private $reg_url_link; |
|
| 30 | - |
|
| 31 | - |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * @param string $reg_url_link |
|
| 35 | - * @return RegUrlLink |
|
| 36 | - * @throws InvalidArgumentException |
|
| 37 | - */ |
|
| 38 | - public static function fromRegUrlLinkString($reg_url_link) |
|
| 39 | - { |
|
| 40 | - if (empty($reg_url_link) || ! is_string($reg_url_link)) { |
|
| 41 | - throw new InvalidArgumentException( |
|
| 42 | - __( |
|
| 43 | - 'You must supply a valid non-empty string to generate a reg_url_link.', |
|
| 44 | - 'event_espresso' |
|
| 45 | - ) |
|
| 46 | - ); |
|
| 47 | - } |
|
| 48 | - return new RegUrlLink(1, '', $reg_url_link); |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * @param EE_Registration $registration |
|
| 55 | - * @return RegUrlLink |
|
| 56 | - * @throws EntityNotFoundException |
|
| 57 | - * @throws EE_Error |
|
| 58 | - * @throws InvalidArgumentException |
|
| 59 | - */ |
|
| 60 | - public static function fromRegistration(EE_Registration $registration) |
|
| 61 | - { |
|
| 62 | - return new RegUrlLink( |
|
| 63 | - $registration->count(), |
|
| 64 | - $registration->ticket_line_item() |
|
| 65 | - ); |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * CreateRegUrlLinkCommand constructor. |
|
| 72 | - * |
|
| 73 | - * @param int $reg_count |
|
| 74 | - * @param mixed $base_code |
|
| 75 | - * @param string $reg_url_link |
|
| 76 | - * @throws InvalidArgumentException |
|
| 77 | - */ |
|
| 78 | - public function __construct( |
|
| 79 | - $reg_count = 1, |
|
| 80 | - $base_code = '', |
|
| 81 | - $reg_url_link = '' |
|
| 82 | - ) { |
|
| 83 | - if ( ! empty($reg_url_link) && is_string($reg_url_link)) { |
|
| 84 | - $this->reg_url_link = apply_filters( |
|
| 85 | - 'FHEE__\EventEspresso\core\domain\entities\RegUrlLink__construct__reg_url_link', |
|
| 86 | - $reg_url_link, |
|
| 87 | - $reg_count, |
|
| 88 | - $base_code, |
|
| 89 | - $reg_url_link |
|
| 90 | - ); |
|
| 91 | - return; |
|
| 92 | - } |
|
| 93 | - $reg_count = max(1, absint($reg_count)); |
|
| 94 | - $base_code = $base_code instanceof \EE_Line_Item ? $base_code->code() : $base_code; |
|
| 95 | - if (empty($base_code) || ! is_string($base_code)) { |
|
| 96 | - throw new InvalidArgumentException( |
|
| 97 | - __( |
|
| 98 | - 'You must supply a valid EE_Line_Item or a non-empty string to generate a reg_url_link.', |
|
| 99 | - 'event_espresso' |
|
| 100 | - ) |
|
| 101 | - ); |
|
| 102 | - } |
|
| 103 | - $this->reg_url_link = (string) apply_filters( |
|
| 104 | - 'FHEE__\EventEspresso\core\domain\entities\RegUrlLink__construct__reg_url_link', |
|
| 105 | - $reg_count . '-' . md5($base_code . microtime()), |
|
| 106 | - $reg_count, |
|
| 107 | - $base_code, |
|
| 108 | - $reg_url_link |
|
| 109 | - ); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * Return the object as a string |
|
| 116 | - * |
|
| 117 | - * @return string |
|
| 118 | - */ |
|
| 119 | - public function __toString() |
|
| 120 | - { |
|
| 121 | - return $this->reg_url_link; |
|
| 122 | - } |
|
| 29 | + private $reg_url_link; |
|
| 30 | + |
|
| 31 | + |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * @param string $reg_url_link |
|
| 35 | + * @return RegUrlLink |
|
| 36 | + * @throws InvalidArgumentException |
|
| 37 | + */ |
|
| 38 | + public static function fromRegUrlLinkString($reg_url_link) |
|
| 39 | + { |
|
| 40 | + if (empty($reg_url_link) || ! is_string($reg_url_link)) { |
|
| 41 | + throw new InvalidArgumentException( |
|
| 42 | + __( |
|
| 43 | + 'You must supply a valid non-empty string to generate a reg_url_link.', |
|
| 44 | + 'event_espresso' |
|
| 45 | + ) |
|
| 46 | + ); |
|
| 47 | + } |
|
| 48 | + return new RegUrlLink(1, '', $reg_url_link); |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * @param EE_Registration $registration |
|
| 55 | + * @return RegUrlLink |
|
| 56 | + * @throws EntityNotFoundException |
|
| 57 | + * @throws EE_Error |
|
| 58 | + * @throws InvalidArgumentException |
|
| 59 | + */ |
|
| 60 | + public static function fromRegistration(EE_Registration $registration) |
|
| 61 | + { |
|
| 62 | + return new RegUrlLink( |
|
| 63 | + $registration->count(), |
|
| 64 | + $registration->ticket_line_item() |
|
| 65 | + ); |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * CreateRegUrlLinkCommand constructor. |
|
| 72 | + * |
|
| 73 | + * @param int $reg_count |
|
| 74 | + * @param mixed $base_code |
|
| 75 | + * @param string $reg_url_link |
|
| 76 | + * @throws InvalidArgumentException |
|
| 77 | + */ |
|
| 78 | + public function __construct( |
|
| 79 | + $reg_count = 1, |
|
| 80 | + $base_code = '', |
|
| 81 | + $reg_url_link = '' |
|
| 82 | + ) { |
|
| 83 | + if ( ! empty($reg_url_link) && is_string($reg_url_link)) { |
|
| 84 | + $this->reg_url_link = apply_filters( |
|
| 85 | + 'FHEE__\EventEspresso\core\domain\entities\RegUrlLink__construct__reg_url_link', |
|
| 86 | + $reg_url_link, |
|
| 87 | + $reg_count, |
|
| 88 | + $base_code, |
|
| 89 | + $reg_url_link |
|
| 90 | + ); |
|
| 91 | + return; |
|
| 92 | + } |
|
| 93 | + $reg_count = max(1, absint($reg_count)); |
|
| 94 | + $base_code = $base_code instanceof \EE_Line_Item ? $base_code->code() : $base_code; |
|
| 95 | + if (empty($base_code) || ! is_string($base_code)) { |
|
| 96 | + throw new InvalidArgumentException( |
|
| 97 | + __( |
|
| 98 | + 'You must supply a valid EE_Line_Item or a non-empty string to generate a reg_url_link.', |
|
| 99 | + 'event_espresso' |
|
| 100 | + ) |
|
| 101 | + ); |
|
| 102 | + } |
|
| 103 | + $this->reg_url_link = (string) apply_filters( |
|
| 104 | + 'FHEE__\EventEspresso\core\domain\entities\RegUrlLink__construct__reg_url_link', |
|
| 105 | + $reg_count . '-' . md5($base_code . microtime()), |
|
| 106 | + $reg_count, |
|
| 107 | + $base_code, |
|
| 108 | + $reg_url_link |
|
| 109 | + ); |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * Return the object as a string |
|
| 116 | + * |
|
| 117 | + * @return string |
|
| 118 | + */ |
|
| 119 | + public function __toString() |
|
| 120 | + { |
|
| 121 | + return $this->reg_url_link; |
|
| 122 | + } |
|
| 123 | 123 | } |
| 124 | 124 | // End of file RegUrlLink.php |
| 125 | 125 | // Location: /RegUrlLink.php |
| 126 | 126 | \ No newline at end of file |
@@ -102,7 +102,7 @@ |
||
| 102 | 102 | } |
| 103 | 103 | $this->reg_url_link = (string) apply_filters( |
| 104 | 104 | 'FHEE__\EventEspresso\core\domain\entities\RegUrlLink__construct__reg_url_link', |
| 105 | - $reg_count . '-' . md5($base_code . microtime()), |
|
| 105 | + $reg_count.'-'.md5($base_code.microtime()), |
|
| 106 | 106 | $reg_count, |
| 107 | 107 | $base_code, |
| 108 | 108 | $reg_url_link |
@@ -2,7 +2,7 @@ discard block |
||
| 2 | 2 | use EventEspresso\core\libraries\form_sections\strategies\filter\FormHtmlFilter; |
| 3 | 3 | |
| 4 | 4 | if (! defined('EVENT_ESPRESSO_VERSION')) { |
| 5 | - exit('No direct script access allowed'); |
|
| 5 | + exit('No direct script access allowed'); |
|
| 6 | 6 | } |
| 7 | 7 | |
| 8 | 8 | |
@@ -19,480 +19,480 @@ discard block |
||
| 19 | 19 | abstract class EE_Form_Section_Base |
| 20 | 20 | { |
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * the URL the form is submitted to |
|
| 24 | - * |
|
| 25 | - * @var string |
|
| 26 | - */ |
|
| 27 | - protected $_action; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * POST (default) or GET |
|
| 31 | - * |
|
| 32 | - * @var string |
|
| 33 | - */ |
|
| 34 | - protected $_method; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * html_id and html_name are derived from this by default |
|
| 38 | - * |
|
| 39 | - * @var string |
|
| 40 | - */ |
|
| 41 | - protected $_name; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * $_html_id |
|
| 45 | - * @var string |
|
| 46 | - */ |
|
| 47 | - protected $_html_id; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * $_html_class |
|
| 51 | - * @var string |
|
| 52 | - */ |
|
| 53 | - protected $_html_class; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * $_html_style |
|
| 57 | - * @var string |
|
| 58 | - */ |
|
| 59 | - protected $_html_style; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * $_other_html_attributes |
|
| 63 | - * @var string |
|
| 64 | - */ |
|
| 65 | - protected $_other_html_attributes; |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * The form section of which this form section is a part |
|
| 69 | - * |
|
| 70 | - * @var EE_Form_Section_Proper |
|
| 71 | - */ |
|
| 72 | - protected $_parent_section; |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * flag indicating that _construct_finalize has been called. |
|
| 76 | - * If it hasn't been called and we try to use functions which require it, we call it |
|
| 77 | - * with no parameters. But normally, _construct_finalize should be called by the instantiating class |
|
| 78 | - * |
|
| 79 | - * @var boolean |
|
| 80 | - */ |
|
| 81 | - protected $_construction_finalized; |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * Strategy for parsing the form HTML upon display |
|
| 85 | - * |
|
| 86 | - * @var FormHtmlFilter |
|
| 87 | - */ |
|
| 88 | - protected $_form_html_filter; |
|
| 89 | - |
|
| 90 | - |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * @param array $options_array { |
|
| 94 | - * @type $name string the name for this form section, if you want to explicitly define it |
|
| 95 | - * } |
|
| 96 | - */ |
|
| 97 | - public function __construct($options_array = array()) |
|
| 98 | - { |
|
| 99 | - // used by display strategies |
|
| 100 | - // assign incoming values to properties |
|
| 101 | - foreach ($options_array as $key => $value) { |
|
| 102 | - $key = '_' . $key; |
|
| 103 | - if (property_exists($this, $key) && empty($this->{$key})) { |
|
| 104 | - $this->{$key} = $value; |
|
| 105 | - } |
|
| 106 | - } |
|
| 107 | - // set parser which allows the form section's rendered HTML to be filtered |
|
| 108 | - if (isset($options_array['form_html_filter']) && $options_array['form_html_filter'] instanceof FormHtmlFilter) { |
|
| 109 | - $this->_form_html_filter = $options_array['form_html_filter']; |
|
| 110 | - } |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * @param $parent_form_section |
|
| 117 | - * @param $name |
|
| 118 | - * @throws \EE_Error |
|
| 119 | - */ |
|
| 120 | - protected function _construct_finalize($parent_form_section, $name) |
|
| 121 | - { |
|
| 122 | - $this->_construction_finalized = true; |
|
| 123 | - $this->_parent_section = $parent_form_section; |
|
| 124 | - if ($name !== null) { |
|
| 125 | - $this->_name = $name; |
|
| 126 | - } |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * make sure construction finalized was called, otherwise children might not be ready |
|
| 133 | - * |
|
| 134 | - * @return void |
|
| 135 | - * @throws \EE_Error |
|
| 136 | - */ |
|
| 137 | - public function ensure_construct_finalized_called() |
|
| 138 | - { |
|
| 139 | - if (! $this->_construction_finalized) { |
|
| 140 | - $this->_construct_finalize($this->_parent_section, $this->_name); |
|
| 141 | - } |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * @return string |
|
| 148 | - */ |
|
| 149 | - public function action() |
|
| 150 | - { |
|
| 151 | - return $this->_action; |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * @param string $action |
|
| 158 | - */ |
|
| 159 | - public function set_action($action) |
|
| 160 | - { |
|
| 161 | - $this->_action = $action; |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * @return string |
|
| 168 | - */ |
|
| 169 | - public function method() |
|
| 170 | - { |
|
| 171 | - return ! empty($this->_method) ? $this->_method : 'POST'; |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - |
|
| 175 | - |
|
| 176 | - /** |
|
| 177 | - * @param string $method |
|
| 178 | - */ |
|
| 179 | - public function set_method($method) |
|
| 180 | - { |
|
| 181 | - switch ($method) { |
|
| 182 | - case 'get' : |
|
| 183 | - case 'GET' : |
|
| 184 | - $this->_method = 'GET'; |
|
| 185 | - break; |
|
| 186 | - default : |
|
| 187 | - $this->_method = 'POST'; |
|
| 188 | - } |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * Sets the html_id to its default value, if none was specified in the constructor. |
|
| 195 | - * Calculation involves using the name and the parent's html id |
|
| 196 | - * return void |
|
| 197 | - * |
|
| 198 | - * @throws \EE_Error |
|
| 199 | - */ |
|
| 200 | - protected function _set_default_html_id_if_empty() |
|
| 201 | - { |
|
| 202 | - if (! $this->_html_id) { |
|
| 203 | - if ($this->_parent_section && $this->_parent_section instanceof EE_Form_Section_Proper) { |
|
| 204 | - $this->_html_id = $this->_parent_section->html_id() |
|
| 205 | - . '-' |
|
| 206 | - . $this->_prep_name_for_html_id($this->name()); |
|
| 207 | - } else { |
|
| 208 | - $this->_html_id = $this->_prep_name_for_html_id($this->name()); |
|
| 209 | - } |
|
| 210 | - } |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - |
|
| 214 | - |
|
| 215 | - /** |
|
| 216 | - * _prep_name_for_html_id |
|
| 217 | - * |
|
| 218 | - * @param $name |
|
| 219 | - * @return string |
|
| 220 | - */ |
|
| 221 | - private function _prep_name_for_html_id($name) |
|
| 222 | - { |
|
| 223 | - return sanitize_key(str_replace(array(' ', ' ', '_'), '-', $name)); |
|
| 224 | - } |
|
| 22 | + /** |
|
| 23 | + * the URL the form is submitted to |
|
| 24 | + * |
|
| 25 | + * @var string |
|
| 26 | + */ |
|
| 27 | + protected $_action; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * POST (default) or GET |
|
| 31 | + * |
|
| 32 | + * @var string |
|
| 33 | + */ |
|
| 34 | + protected $_method; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * html_id and html_name are derived from this by default |
|
| 38 | + * |
|
| 39 | + * @var string |
|
| 40 | + */ |
|
| 41 | + protected $_name; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * $_html_id |
|
| 45 | + * @var string |
|
| 46 | + */ |
|
| 47 | + protected $_html_id; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * $_html_class |
|
| 51 | + * @var string |
|
| 52 | + */ |
|
| 53 | + protected $_html_class; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * $_html_style |
|
| 57 | + * @var string |
|
| 58 | + */ |
|
| 59 | + protected $_html_style; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * $_other_html_attributes |
|
| 63 | + * @var string |
|
| 64 | + */ |
|
| 65 | + protected $_other_html_attributes; |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * The form section of which this form section is a part |
|
| 69 | + * |
|
| 70 | + * @var EE_Form_Section_Proper |
|
| 71 | + */ |
|
| 72 | + protected $_parent_section; |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * flag indicating that _construct_finalize has been called. |
|
| 76 | + * If it hasn't been called and we try to use functions which require it, we call it |
|
| 77 | + * with no parameters. But normally, _construct_finalize should be called by the instantiating class |
|
| 78 | + * |
|
| 79 | + * @var boolean |
|
| 80 | + */ |
|
| 81 | + protected $_construction_finalized; |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * Strategy for parsing the form HTML upon display |
|
| 85 | + * |
|
| 86 | + * @var FormHtmlFilter |
|
| 87 | + */ |
|
| 88 | + protected $_form_html_filter; |
|
| 89 | + |
|
| 90 | + |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * @param array $options_array { |
|
| 94 | + * @type $name string the name for this form section, if you want to explicitly define it |
|
| 95 | + * } |
|
| 96 | + */ |
|
| 97 | + public function __construct($options_array = array()) |
|
| 98 | + { |
|
| 99 | + // used by display strategies |
|
| 100 | + // assign incoming values to properties |
|
| 101 | + foreach ($options_array as $key => $value) { |
|
| 102 | + $key = '_' . $key; |
|
| 103 | + if (property_exists($this, $key) && empty($this->{$key})) { |
|
| 104 | + $this->{$key} = $value; |
|
| 105 | + } |
|
| 106 | + } |
|
| 107 | + // set parser which allows the form section's rendered HTML to be filtered |
|
| 108 | + if (isset($options_array['form_html_filter']) && $options_array['form_html_filter'] instanceof FormHtmlFilter) { |
|
| 109 | + $this->_form_html_filter = $options_array['form_html_filter']; |
|
| 110 | + } |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * @param $parent_form_section |
|
| 117 | + * @param $name |
|
| 118 | + * @throws \EE_Error |
|
| 119 | + */ |
|
| 120 | + protected function _construct_finalize($parent_form_section, $name) |
|
| 121 | + { |
|
| 122 | + $this->_construction_finalized = true; |
|
| 123 | + $this->_parent_section = $parent_form_section; |
|
| 124 | + if ($name !== null) { |
|
| 125 | + $this->_name = $name; |
|
| 126 | + } |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * make sure construction finalized was called, otherwise children might not be ready |
|
| 133 | + * |
|
| 134 | + * @return void |
|
| 135 | + * @throws \EE_Error |
|
| 136 | + */ |
|
| 137 | + public function ensure_construct_finalized_called() |
|
| 138 | + { |
|
| 139 | + if (! $this->_construction_finalized) { |
|
| 140 | + $this->_construct_finalize($this->_parent_section, $this->_name); |
|
| 141 | + } |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * @return string |
|
| 148 | + */ |
|
| 149 | + public function action() |
|
| 150 | + { |
|
| 151 | + return $this->_action; |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * @param string $action |
|
| 158 | + */ |
|
| 159 | + public function set_action($action) |
|
| 160 | + { |
|
| 161 | + $this->_action = $action; |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * @return string |
|
| 168 | + */ |
|
| 169 | + public function method() |
|
| 170 | + { |
|
| 171 | + return ! empty($this->_method) ? $this->_method : 'POST'; |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + |
|
| 175 | + |
|
| 176 | + /** |
|
| 177 | + * @param string $method |
|
| 178 | + */ |
|
| 179 | + public function set_method($method) |
|
| 180 | + { |
|
| 181 | + switch ($method) { |
|
| 182 | + case 'get' : |
|
| 183 | + case 'GET' : |
|
| 184 | + $this->_method = 'GET'; |
|
| 185 | + break; |
|
| 186 | + default : |
|
| 187 | + $this->_method = 'POST'; |
|
| 188 | + } |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * Sets the html_id to its default value, if none was specified in the constructor. |
|
| 195 | + * Calculation involves using the name and the parent's html id |
|
| 196 | + * return void |
|
| 197 | + * |
|
| 198 | + * @throws \EE_Error |
|
| 199 | + */ |
|
| 200 | + protected function _set_default_html_id_if_empty() |
|
| 201 | + { |
|
| 202 | + if (! $this->_html_id) { |
|
| 203 | + if ($this->_parent_section && $this->_parent_section instanceof EE_Form_Section_Proper) { |
|
| 204 | + $this->_html_id = $this->_parent_section->html_id() |
|
| 205 | + . '-' |
|
| 206 | + . $this->_prep_name_for_html_id($this->name()); |
|
| 207 | + } else { |
|
| 208 | + $this->_html_id = $this->_prep_name_for_html_id($this->name()); |
|
| 209 | + } |
|
| 210 | + } |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + |
|
| 214 | + |
|
| 215 | + /** |
|
| 216 | + * _prep_name_for_html_id |
|
| 217 | + * |
|
| 218 | + * @param $name |
|
| 219 | + * @return string |
|
| 220 | + */ |
|
| 221 | + private function _prep_name_for_html_id($name) |
|
| 222 | + { |
|
| 223 | + return sanitize_key(str_replace(array(' ', ' ', '_'), '-', $name)); |
|
| 224 | + } |
|
| 225 | 225 | |
| 226 | 226 | |
| 227 | 227 | |
| 228 | - /** |
|
| 229 | - * Returns the HTML, JS, and CSS necessary to display this form section on a page. |
|
| 230 | - * Note however, it's recommended that you instead call enqueue_js on the "wp_enqueue_scripts" action, |
|
| 231 | - * and call get_html when you want to output the html. Calling get_html_and_js after |
|
| 232 | - * "wp_enqueue_scripts" has already fired seems to work for now, but is contrary |
|
| 233 | - * to the instructions on https://developer.wordpress.org/reference/functions/wp_enqueue_script/ |
|
| 234 | - * and so might stop working anytime. |
|
| 235 | - * |
|
| 236 | - * @return string |
|
| 237 | - */ |
|
| 238 | - public function get_html_and_js() |
|
| 239 | - { |
|
| 240 | - return $this->get_html(); |
|
| 241 | - } |
|
| 228 | + /** |
|
| 229 | + * Returns the HTML, JS, and CSS necessary to display this form section on a page. |
|
| 230 | + * Note however, it's recommended that you instead call enqueue_js on the "wp_enqueue_scripts" action, |
|
| 231 | + * and call get_html when you want to output the html. Calling get_html_and_js after |
|
| 232 | + * "wp_enqueue_scripts" has already fired seems to work for now, but is contrary |
|
| 233 | + * to the instructions on https://developer.wordpress.org/reference/functions/wp_enqueue_script/ |
|
| 234 | + * and so might stop working anytime. |
|
| 235 | + * |
|
| 236 | + * @return string |
|
| 237 | + */ |
|
| 238 | + public function get_html_and_js() |
|
| 239 | + { |
|
| 240 | + return $this->get_html(); |
|
| 241 | + } |
|
| 242 | 242 | |
| 243 | 243 | |
| 244 | 244 | |
| 245 | - /** |
|
| 246 | - * Gets the HTML for displaying this form section |
|
| 247 | - * |
|
| 248 | - * @return string |
|
| 249 | - */ |
|
| 250 | - public abstract function get_html(); |
|
| 245 | + /** |
|
| 246 | + * Gets the HTML for displaying this form section |
|
| 247 | + * |
|
| 248 | + * @return string |
|
| 249 | + */ |
|
| 250 | + public abstract function get_html(); |
|
| 251 | 251 | |
| 252 | 252 | |
| 253 | 253 | |
| 254 | - /** |
|
| 255 | - * @param bool $add_pound_sign |
|
| 256 | - * @return string |
|
| 257 | - */ |
|
| 258 | - public function html_id($add_pound_sign = false) |
|
| 259 | - { |
|
| 260 | - $this->_set_default_html_id_if_empty(); |
|
| 261 | - return $add_pound_sign ? '#' . $this->_html_id : $this->_html_id; |
|
| 262 | - } |
|
| 254 | + /** |
|
| 255 | + * @param bool $add_pound_sign |
|
| 256 | + * @return string |
|
| 257 | + */ |
|
| 258 | + public function html_id($add_pound_sign = false) |
|
| 259 | + { |
|
| 260 | + $this->_set_default_html_id_if_empty(); |
|
| 261 | + return $add_pound_sign ? '#' . $this->_html_id : $this->_html_id; |
|
| 262 | + } |
|
| 263 | 263 | |
| 264 | 264 | |
| 265 | - |
|
| 266 | - /** |
|
| 267 | - * @return string |
|
| 268 | - */ |
|
| 269 | - public function html_class() |
|
| 270 | - { |
|
| 271 | - return $this->_html_class; |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - |
|
| 275 | - |
|
| 276 | - /** |
|
| 277 | - * @return string |
|
| 278 | - */ |
|
| 279 | - public function html_style() |
|
| 280 | - { |
|
| 281 | - return $this->_html_style; |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - |
|
| 285 | - |
|
| 286 | - /** |
|
| 287 | - * @param mixed $html_class |
|
| 288 | - */ |
|
| 289 | - public function set_html_class($html_class) |
|
| 290 | - { |
|
| 291 | - $this->_html_class = $html_class; |
|
| 292 | - } |
|
| 293 | - |
|
| 294 | - |
|
| 295 | - |
|
| 296 | - /** |
|
| 297 | - * @param mixed $html_id |
|
| 298 | - */ |
|
| 299 | - public function set_html_id($html_id) |
|
| 300 | - { |
|
| 301 | - $this->_html_id = $html_id; |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - |
|
| 305 | - |
|
| 306 | - /** |
|
| 307 | - * @param mixed $html_style |
|
| 308 | - */ |
|
| 309 | - public function set_html_style($html_style) |
|
| 310 | - { |
|
| 311 | - $this->_html_style = $html_style; |
|
| 312 | - } |
|
| 313 | - |
|
| 314 | - |
|
| 315 | - |
|
| 316 | - /** |
|
| 317 | - * @param string $other_html_attributes |
|
| 318 | - */ |
|
| 319 | - public function set_other_html_attributes($other_html_attributes) |
|
| 320 | - { |
|
| 321 | - $this->_other_html_attributes = $other_html_attributes; |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - |
|
| 325 | - |
|
| 326 | - /** |
|
| 327 | - * @return string |
|
| 328 | - */ |
|
| 329 | - public function other_html_attributes() |
|
| 330 | - { |
|
| 331 | - return $this->_other_html_attributes; |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - |
|
| 335 | - |
|
| 336 | - /** |
|
| 337 | - * Gets the name of the form section. This is not the same as the HTML name. |
|
| 338 | - * |
|
| 339 | - * @throws EE_Error |
|
| 340 | - * @return string |
|
| 341 | - */ |
|
| 342 | - public function name() |
|
| 343 | - { |
|
| 344 | - if (! $this->_construction_finalized) { |
|
| 345 | - throw new EE_Error(sprintf(__('You cannot use the form section\s name until _construct_finalize has been called on it (when we set the name). It was called on a form section of type \'s\'', |
|
| 346 | - 'event_espresso'), get_class($this))); |
|
| 347 | - } |
|
| 348 | - return $this->_name; |
|
| 349 | - } |
|
| 350 | - |
|
| 351 | - |
|
| 352 | - |
|
| 353 | - /** |
|
| 354 | - * Gets the parent section |
|
| 355 | - * |
|
| 356 | - * @return EE_Form_Section_Proper |
|
| 357 | - */ |
|
| 358 | - public function parent_section() |
|
| 359 | - { |
|
| 360 | - return $this->_parent_section; |
|
| 361 | - } |
|
| 362 | - |
|
| 363 | - |
|
| 364 | - |
|
| 365 | - /** |
|
| 366 | - * returns HTML for generating the opening form HTML tag (<form>) |
|
| 367 | - * |
|
| 368 | - * @param string $action the URL the form is submitted to |
|
| 369 | - * @param string $method POST (default) or GET |
|
| 370 | - * @param string $other_attributes anything else added to the form open tag, MUST BE VALID HTML |
|
| 371 | - * @return string |
|
| 372 | - */ |
|
| 373 | - public function form_open($action = '', $method = '', $other_attributes = '') |
|
| 374 | - { |
|
| 375 | - if (! empty($action)) { |
|
| 376 | - $this->set_action($action); |
|
| 377 | - } |
|
| 378 | - if (! empty($method)) { |
|
| 379 | - $this->set_method($method); |
|
| 380 | - } |
|
| 381 | - $html = EEH_HTML::nl(1, 'form') . '<form'; |
|
| 382 | - $html .= $this->html_id() !== '' ? ' id="' . $this->get_html_id_for_form($this->html_id()) . '"' : ''; |
|
| 383 | - $html .= ' action="' . $this->action() . '"'; |
|
| 384 | - $html .= ' method="' . $this->method() . '"'; |
|
| 385 | - $html .= $other_attributes . '>'; |
|
| 386 | - return $html; |
|
| 387 | - } |
|
| 388 | - |
|
| 389 | - |
|
| 390 | - |
|
| 391 | - /** |
|
| 392 | - * ensures that html id for form either ends in "-form" or "-frm" |
|
| 393 | - * so that id doesn't conflict/collide with other elements |
|
| 394 | - * |
|
| 395 | - * @param string $html_id |
|
| 396 | - * @return string |
|
| 397 | - */ |
|
| 398 | - protected function get_html_id_for_form($html_id) |
|
| 399 | - { |
|
| 400 | - $strlen = strlen($html_id); |
|
| 401 | - $html_id = strpos($html_id, '-form') === $strlen-5 || strpos($html_id, '-frm') === $strlen - 4 |
|
| 402 | - ? $html_id |
|
| 403 | - : $html_id . '-frm'; |
|
| 404 | - return $html_id; |
|
| 405 | - } |
|
| 406 | - |
|
| 407 | - |
|
| 408 | - |
|
| 409 | - /** |
|
| 410 | - * returns HTML for generating the closing form HTML tag (</form>) |
|
| 411 | - * |
|
| 412 | - * @return string |
|
| 413 | - */ |
|
| 414 | - public function form_close() |
|
| 415 | - { |
|
| 416 | - return EEH_HTML::nl(-1, 'form') |
|
| 417 | - . '</form>' |
|
| 418 | - . EEH_HTML::nl() |
|
| 419 | - . '<!-- end of ee-' |
|
| 420 | - . $this->html_id() |
|
| 421 | - . '-form -->' |
|
| 422 | - . EEH_HTML::nl(); |
|
| 423 | - } |
|
| 424 | - |
|
| 425 | - |
|
| 426 | - |
|
| 427 | - /** |
|
| 428 | - * enqueues JS (and CSS) for the form (ie immediately call wp_enqueue_script and |
|
| 429 | - * wp_enqueue_style; the scripts could have optionally been registered earlier) |
|
| 430 | - * Default does nothing, but child classes can override |
|
| 431 | - * |
|
| 432 | - * @return void |
|
| 433 | - */ |
|
| 434 | - public function enqueue_js() |
|
| 435 | - { |
|
| 436 | - //defaults to enqueue NO js or css |
|
| 437 | - } |
|
| 438 | - |
|
| 439 | - |
|
| 440 | - |
|
| 441 | - /** |
|
| 442 | - * Adds any extra data needed by js. Eventually we'll call wp_localize_script |
|
| 443 | - * with it, and it will be on each form section's 'other_data' property. |
|
| 444 | - * By default nothing is added, but child classes can extend this method to add something. |
|
| 445 | - * Eg, if you have an input that will cause a modal dialog to appear, |
|
| 446 | - * here you could add an entry like 'modal_dialog_inputs' to this array |
|
| 447 | - * to map between the input's html ID and the modal dialogue's ID, so that |
|
| 448 | - * your JS code will know where to find the modal dialog when the input is pressed. |
|
| 449 | - * Eg $form_other_js_data['modal_dialog_inputs']['some-input-id']='modal-dialog-id'; |
|
| 450 | - * |
|
| 451 | - * @param array $form_other_js_data |
|
| 452 | - * @return array |
|
| 453 | - */ |
|
| 454 | - public function get_other_js_data($form_other_js_data = array()) |
|
| 455 | - { |
|
| 456 | - return $form_other_js_data; |
|
| 457 | - } |
|
| 458 | - |
|
| 459 | - |
|
| 460 | - |
|
| 461 | - /** |
|
| 462 | - * This isn't just the name of an input, it's a path pointing to an input. The |
|
| 463 | - * path is similar to a folder path: slash (/) means to descend into a subsection, |
|
| 464 | - * dot-dot-slash (../) means to ascend into the parent section. |
|
| 465 | - * After a series of slashes and dot-dot-slashes, there should be the name of an input, |
|
| 466 | - * which will be returned. |
|
| 467 | - * Eg, if you want the related input to be conditional on a sibling input name 'foobar' |
|
| 468 | - * just use 'foobar'. If you want it to be conditional on an aunt/uncle input name |
|
| 469 | - * 'baz', use '../baz'. If you want it to be conditional on a cousin input, |
|
| 470 | - * the child of 'baz_section' named 'baz_child', use '../baz_section/baz_child'. |
|
| 471 | - * Etc |
|
| 472 | - * |
|
| 473 | - * @param string|false $form_section_path we accept false also because substr( '../', '../' ) = false |
|
| 474 | - * @return EE_Form_Section_Base |
|
| 475 | - */ |
|
| 476 | - public function find_section_from_path($form_section_path) |
|
| 477 | - { |
|
| 478 | - if (strpos($form_section_path, '/') === 0) { |
|
| 479 | - $form_section_path = substr($form_section_path, strlen('/')); |
|
| 480 | - } |
|
| 481 | - if (empty($form_section_path)) { |
|
| 482 | - return $this; |
|
| 483 | - } |
|
| 484 | - if (strpos($form_section_path, '../') === 0) { |
|
| 485 | - $parent = $this->parent_section(); |
|
| 486 | - $form_section_path = substr($form_section_path, strlen('../')); |
|
| 487 | - if ($parent instanceof EE_Form_Section_Base) { |
|
| 488 | - return $parent->find_section_from_path($form_section_path); |
|
| 489 | - } elseif (empty($form_section_path)) { |
|
| 490 | - return $this; |
|
| 491 | - } |
|
| 492 | - } |
|
| 493 | - //couldn't find it using simple parent following |
|
| 494 | - return null; |
|
| 495 | - } |
|
| 265 | + |
|
| 266 | + /** |
|
| 267 | + * @return string |
|
| 268 | + */ |
|
| 269 | + public function html_class() |
|
| 270 | + { |
|
| 271 | + return $this->_html_class; |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + |
|
| 275 | + |
|
| 276 | + /** |
|
| 277 | + * @return string |
|
| 278 | + */ |
|
| 279 | + public function html_style() |
|
| 280 | + { |
|
| 281 | + return $this->_html_style; |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + |
|
| 285 | + |
|
| 286 | + /** |
|
| 287 | + * @param mixed $html_class |
|
| 288 | + */ |
|
| 289 | + public function set_html_class($html_class) |
|
| 290 | + { |
|
| 291 | + $this->_html_class = $html_class; |
|
| 292 | + } |
|
| 293 | + |
|
| 294 | + |
|
| 295 | + |
|
| 296 | + /** |
|
| 297 | + * @param mixed $html_id |
|
| 298 | + */ |
|
| 299 | + public function set_html_id($html_id) |
|
| 300 | + { |
|
| 301 | + $this->_html_id = $html_id; |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + |
|
| 305 | + |
|
| 306 | + /** |
|
| 307 | + * @param mixed $html_style |
|
| 308 | + */ |
|
| 309 | + public function set_html_style($html_style) |
|
| 310 | + { |
|
| 311 | + $this->_html_style = $html_style; |
|
| 312 | + } |
|
| 313 | + |
|
| 314 | + |
|
| 315 | + |
|
| 316 | + /** |
|
| 317 | + * @param string $other_html_attributes |
|
| 318 | + */ |
|
| 319 | + public function set_other_html_attributes($other_html_attributes) |
|
| 320 | + { |
|
| 321 | + $this->_other_html_attributes = $other_html_attributes; |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + |
|
| 325 | + |
|
| 326 | + /** |
|
| 327 | + * @return string |
|
| 328 | + */ |
|
| 329 | + public function other_html_attributes() |
|
| 330 | + { |
|
| 331 | + return $this->_other_html_attributes; |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + |
|
| 335 | + |
|
| 336 | + /** |
|
| 337 | + * Gets the name of the form section. This is not the same as the HTML name. |
|
| 338 | + * |
|
| 339 | + * @throws EE_Error |
|
| 340 | + * @return string |
|
| 341 | + */ |
|
| 342 | + public function name() |
|
| 343 | + { |
|
| 344 | + if (! $this->_construction_finalized) { |
|
| 345 | + throw new EE_Error(sprintf(__('You cannot use the form section\s name until _construct_finalize has been called on it (when we set the name). It was called on a form section of type \'s\'', |
|
| 346 | + 'event_espresso'), get_class($this))); |
|
| 347 | + } |
|
| 348 | + return $this->_name; |
|
| 349 | + } |
|
| 350 | + |
|
| 351 | + |
|
| 352 | + |
|
| 353 | + /** |
|
| 354 | + * Gets the parent section |
|
| 355 | + * |
|
| 356 | + * @return EE_Form_Section_Proper |
|
| 357 | + */ |
|
| 358 | + public function parent_section() |
|
| 359 | + { |
|
| 360 | + return $this->_parent_section; |
|
| 361 | + } |
|
| 362 | + |
|
| 363 | + |
|
| 364 | + |
|
| 365 | + /** |
|
| 366 | + * returns HTML for generating the opening form HTML tag (<form>) |
|
| 367 | + * |
|
| 368 | + * @param string $action the URL the form is submitted to |
|
| 369 | + * @param string $method POST (default) or GET |
|
| 370 | + * @param string $other_attributes anything else added to the form open tag, MUST BE VALID HTML |
|
| 371 | + * @return string |
|
| 372 | + */ |
|
| 373 | + public function form_open($action = '', $method = '', $other_attributes = '') |
|
| 374 | + { |
|
| 375 | + if (! empty($action)) { |
|
| 376 | + $this->set_action($action); |
|
| 377 | + } |
|
| 378 | + if (! empty($method)) { |
|
| 379 | + $this->set_method($method); |
|
| 380 | + } |
|
| 381 | + $html = EEH_HTML::nl(1, 'form') . '<form'; |
|
| 382 | + $html .= $this->html_id() !== '' ? ' id="' . $this->get_html_id_for_form($this->html_id()) . '"' : ''; |
|
| 383 | + $html .= ' action="' . $this->action() . '"'; |
|
| 384 | + $html .= ' method="' . $this->method() . '"'; |
|
| 385 | + $html .= $other_attributes . '>'; |
|
| 386 | + return $html; |
|
| 387 | + } |
|
| 388 | + |
|
| 389 | + |
|
| 390 | + |
|
| 391 | + /** |
|
| 392 | + * ensures that html id for form either ends in "-form" or "-frm" |
|
| 393 | + * so that id doesn't conflict/collide with other elements |
|
| 394 | + * |
|
| 395 | + * @param string $html_id |
|
| 396 | + * @return string |
|
| 397 | + */ |
|
| 398 | + protected function get_html_id_for_form($html_id) |
|
| 399 | + { |
|
| 400 | + $strlen = strlen($html_id); |
|
| 401 | + $html_id = strpos($html_id, '-form') === $strlen-5 || strpos($html_id, '-frm') === $strlen - 4 |
|
| 402 | + ? $html_id |
|
| 403 | + : $html_id . '-frm'; |
|
| 404 | + return $html_id; |
|
| 405 | + } |
|
| 406 | + |
|
| 407 | + |
|
| 408 | + |
|
| 409 | + /** |
|
| 410 | + * returns HTML for generating the closing form HTML tag (</form>) |
|
| 411 | + * |
|
| 412 | + * @return string |
|
| 413 | + */ |
|
| 414 | + public function form_close() |
|
| 415 | + { |
|
| 416 | + return EEH_HTML::nl(-1, 'form') |
|
| 417 | + . '</form>' |
|
| 418 | + . EEH_HTML::nl() |
|
| 419 | + . '<!-- end of ee-' |
|
| 420 | + . $this->html_id() |
|
| 421 | + . '-form -->' |
|
| 422 | + . EEH_HTML::nl(); |
|
| 423 | + } |
|
| 424 | + |
|
| 425 | + |
|
| 426 | + |
|
| 427 | + /** |
|
| 428 | + * enqueues JS (and CSS) for the form (ie immediately call wp_enqueue_script and |
|
| 429 | + * wp_enqueue_style; the scripts could have optionally been registered earlier) |
|
| 430 | + * Default does nothing, but child classes can override |
|
| 431 | + * |
|
| 432 | + * @return void |
|
| 433 | + */ |
|
| 434 | + public function enqueue_js() |
|
| 435 | + { |
|
| 436 | + //defaults to enqueue NO js or css |
|
| 437 | + } |
|
| 438 | + |
|
| 439 | + |
|
| 440 | + |
|
| 441 | + /** |
|
| 442 | + * Adds any extra data needed by js. Eventually we'll call wp_localize_script |
|
| 443 | + * with it, and it will be on each form section's 'other_data' property. |
|
| 444 | + * By default nothing is added, but child classes can extend this method to add something. |
|
| 445 | + * Eg, if you have an input that will cause a modal dialog to appear, |
|
| 446 | + * here you could add an entry like 'modal_dialog_inputs' to this array |
|
| 447 | + * to map between the input's html ID and the modal dialogue's ID, so that |
|
| 448 | + * your JS code will know where to find the modal dialog when the input is pressed. |
|
| 449 | + * Eg $form_other_js_data['modal_dialog_inputs']['some-input-id']='modal-dialog-id'; |
|
| 450 | + * |
|
| 451 | + * @param array $form_other_js_data |
|
| 452 | + * @return array |
|
| 453 | + */ |
|
| 454 | + public function get_other_js_data($form_other_js_data = array()) |
|
| 455 | + { |
|
| 456 | + return $form_other_js_data; |
|
| 457 | + } |
|
| 458 | + |
|
| 459 | + |
|
| 460 | + |
|
| 461 | + /** |
|
| 462 | + * This isn't just the name of an input, it's a path pointing to an input. The |
|
| 463 | + * path is similar to a folder path: slash (/) means to descend into a subsection, |
|
| 464 | + * dot-dot-slash (../) means to ascend into the parent section. |
|
| 465 | + * After a series of slashes and dot-dot-slashes, there should be the name of an input, |
|
| 466 | + * which will be returned. |
|
| 467 | + * Eg, if you want the related input to be conditional on a sibling input name 'foobar' |
|
| 468 | + * just use 'foobar'. If you want it to be conditional on an aunt/uncle input name |
|
| 469 | + * 'baz', use '../baz'. If you want it to be conditional on a cousin input, |
|
| 470 | + * the child of 'baz_section' named 'baz_child', use '../baz_section/baz_child'. |
|
| 471 | + * Etc |
|
| 472 | + * |
|
| 473 | + * @param string|false $form_section_path we accept false also because substr( '../', '../' ) = false |
|
| 474 | + * @return EE_Form_Section_Base |
|
| 475 | + */ |
|
| 476 | + public function find_section_from_path($form_section_path) |
|
| 477 | + { |
|
| 478 | + if (strpos($form_section_path, '/') === 0) { |
|
| 479 | + $form_section_path = substr($form_section_path, strlen('/')); |
|
| 480 | + } |
|
| 481 | + if (empty($form_section_path)) { |
|
| 482 | + return $this; |
|
| 483 | + } |
|
| 484 | + if (strpos($form_section_path, '../') === 0) { |
|
| 485 | + $parent = $this->parent_section(); |
|
| 486 | + $form_section_path = substr($form_section_path, strlen('../')); |
|
| 487 | + if ($parent instanceof EE_Form_Section_Base) { |
|
| 488 | + return $parent->find_section_from_path($form_section_path); |
|
| 489 | + } elseif (empty($form_section_path)) { |
|
| 490 | + return $this; |
|
| 491 | + } |
|
| 492 | + } |
|
| 493 | + //couldn't find it using simple parent following |
|
| 494 | + return null; |
|
| 495 | + } |
|
| 496 | 496 | |
| 497 | 497 | |
| 498 | 498 | } |
@@ -1,7 +1,7 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | use EventEspresso\core\libraries\form_sections\strategies\filter\FormHtmlFilter; |
| 3 | 3 | |
| 4 | -if (! defined('EVENT_ESPRESSO_VERSION')) { |
|
| 4 | +if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
| 5 | 5 | exit('No direct script access allowed'); |
| 6 | 6 | } |
| 7 | 7 | |
@@ -99,7 +99,7 @@ discard block |
||
| 99 | 99 | // used by display strategies |
| 100 | 100 | // assign incoming values to properties |
| 101 | 101 | foreach ($options_array as $key => $value) { |
| 102 | - $key = '_' . $key; |
|
| 102 | + $key = '_'.$key; |
|
| 103 | 103 | if (property_exists($this, $key) && empty($this->{$key})) { |
| 104 | 104 | $this->{$key} = $value; |
| 105 | 105 | } |
@@ -136,7 +136,7 @@ discard block |
||
| 136 | 136 | */ |
| 137 | 137 | public function ensure_construct_finalized_called() |
| 138 | 138 | { |
| 139 | - if (! $this->_construction_finalized) { |
|
| 139 | + if ( ! $this->_construction_finalized) { |
|
| 140 | 140 | $this->_construct_finalize($this->_parent_section, $this->_name); |
| 141 | 141 | } |
| 142 | 142 | } |
@@ -199,7 +199,7 @@ discard block |
||
| 199 | 199 | */ |
| 200 | 200 | protected function _set_default_html_id_if_empty() |
| 201 | 201 | { |
| 202 | - if (! $this->_html_id) { |
|
| 202 | + if ( ! $this->_html_id) { |
|
| 203 | 203 | if ($this->_parent_section && $this->_parent_section instanceof EE_Form_Section_Proper) { |
| 204 | 204 | $this->_html_id = $this->_parent_section->html_id() |
| 205 | 205 | . '-' |
@@ -258,7 +258,7 @@ discard block |
||
| 258 | 258 | public function html_id($add_pound_sign = false) |
| 259 | 259 | { |
| 260 | 260 | $this->_set_default_html_id_if_empty(); |
| 261 | - return $add_pound_sign ? '#' . $this->_html_id : $this->_html_id; |
|
| 261 | + return $add_pound_sign ? '#'.$this->_html_id : $this->_html_id; |
|
| 262 | 262 | } |
| 263 | 263 | |
| 264 | 264 | |
@@ -341,7 +341,7 @@ discard block |
||
| 341 | 341 | */ |
| 342 | 342 | public function name() |
| 343 | 343 | { |
| 344 | - if (! $this->_construction_finalized) { |
|
| 344 | + if ( ! $this->_construction_finalized) { |
|
| 345 | 345 | throw new EE_Error(sprintf(__('You cannot use the form section\s name until _construct_finalize has been called on it (when we set the name). It was called on a form section of type \'s\'', |
| 346 | 346 | 'event_espresso'), get_class($this))); |
| 347 | 347 | } |
@@ -372,17 +372,17 @@ discard block |
||
| 372 | 372 | */ |
| 373 | 373 | public function form_open($action = '', $method = '', $other_attributes = '') |
| 374 | 374 | { |
| 375 | - if (! empty($action)) { |
|
| 375 | + if ( ! empty($action)) { |
|
| 376 | 376 | $this->set_action($action); |
| 377 | 377 | } |
| 378 | - if (! empty($method)) { |
|
| 378 | + if ( ! empty($method)) { |
|
| 379 | 379 | $this->set_method($method); |
| 380 | 380 | } |
| 381 | - $html = EEH_HTML::nl(1, 'form') . '<form'; |
|
| 382 | - $html .= $this->html_id() !== '' ? ' id="' . $this->get_html_id_for_form($this->html_id()) . '"' : ''; |
|
| 383 | - $html .= ' action="' . $this->action() . '"'; |
|
| 384 | - $html .= ' method="' . $this->method() . '"'; |
|
| 385 | - $html .= $other_attributes . '>'; |
|
| 381 | + $html = EEH_HTML::nl(1, 'form').'<form'; |
|
| 382 | + $html .= $this->html_id() !== '' ? ' id="'.$this->get_html_id_for_form($this->html_id()).'"' : ''; |
|
| 383 | + $html .= ' action="'.$this->action().'"'; |
|
| 384 | + $html .= ' method="'.$this->method().'"'; |
|
| 385 | + $html .= $other_attributes.'>'; |
|
| 386 | 386 | return $html; |
| 387 | 387 | } |
| 388 | 388 | |
@@ -398,9 +398,9 @@ discard block |
||
| 398 | 398 | protected function get_html_id_for_form($html_id) |
| 399 | 399 | { |
| 400 | 400 | $strlen = strlen($html_id); |
| 401 | - $html_id = strpos($html_id, '-form') === $strlen-5 || strpos($html_id, '-frm') === $strlen - 4 |
|
| 401 | + $html_id = strpos($html_id, '-form') === $strlen - 5 || strpos($html_id, '-frm') === $strlen - 4 |
|
| 402 | 402 | ? $html_id |
| 403 | - : $html_id . '-frm'; |
|
| 403 | + : $html_id.'-frm'; |
|
| 404 | 404 | return $html_id; |
| 405 | 405 | } |
| 406 | 406 | |
@@ -609,7 +609,7 @@ discard block |
||
| 609 | 609 | * |
| 610 | 610 | * @param \EE_Datetime_Field $datetime_field |
| 611 | 611 | * @param bool $pretty |
| 612 | - * @param null $date_or_time |
|
| 612 | + * @param string|null $date_or_time |
|
| 613 | 613 | * @return void |
| 614 | 614 | * @throws \EE_Error |
| 615 | 615 | */ |
@@ -946,7 +946,7 @@ discard block |
||
| 946 | 946 | * |
| 947 | 947 | * @param null $field_to_order_by What field is being used as the reference point. |
| 948 | 948 | * @param array $query_params Any additional conditions on the query. |
| 949 | - * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
|
| 949 | + * @param string $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
|
| 950 | 950 | * you can indicate just the columns you want returned |
| 951 | 951 | * @return array|EE_Base_Class |
| 952 | 952 | * @throws \EE_Error |
@@ -971,7 +971,7 @@ discard block |
||
| 971 | 971 | * |
| 972 | 972 | * @param null $field_to_order_by What field is being used as the reference point. |
| 973 | 973 | * @param array $query_params Any additional conditions on the query. |
| 974 | - * @param null $columns_to_select If left null, then an EE_Base_Class object is returned, otherwise |
|
| 974 | + * @param string $columns_to_select If left null, then an EE_Base_Class object is returned, otherwise |
|
| 975 | 975 | * you can indicate just the column you want returned |
| 976 | 976 | * @return array|EE_Base_Class |
| 977 | 977 | * @throws \EE_Error |
@@ -1044,7 +1044,7 @@ discard block |
||
| 1044 | 1044 | * This method simply returns the RAW unprocessed value for the given property in this class |
| 1045 | 1045 | * |
| 1046 | 1046 | * @param string $field_name A valid fieldname |
| 1047 | - * @return mixed Whatever the raw value stored on the property is. |
|
| 1047 | + * @return integer|null Whatever the raw value stored on the property is. |
|
| 1048 | 1048 | * @throws EE_Error if fieldSettings is misconfigured or the field doesn't exist. |
| 1049 | 1049 | */ |
| 1050 | 1050 | public function get_raw($field_name) |
@@ -1321,7 +1321,7 @@ discard block |
||
| 1321 | 1321 | * sets the time on a datetime property |
| 1322 | 1322 | * |
| 1323 | 1323 | * @access protected |
| 1324 | - * @param string|Datetime $time a valid time string for php datetime functions (or DateTime object) |
|
| 1324 | + * @param string $time a valid time string for php datetime functions (or DateTime object) |
|
| 1325 | 1325 | * @param string $fieldname the name of the field the time is being set on (must match a EE_Datetime_Field) |
| 1326 | 1326 | * @throws \EE_Error |
| 1327 | 1327 | */ |
@@ -1336,7 +1336,7 @@ discard block |
||
| 1336 | 1336 | * sets the date on a datetime property |
| 1337 | 1337 | * |
| 1338 | 1338 | * @access protected |
| 1339 | - * @param string|DateTime $date a valid date string for php datetime functions ( or DateTime object) |
|
| 1339 | + * @param string $date a valid date string for php datetime functions ( or DateTime object) |
|
| 1340 | 1340 | * @param string $fieldname the name of the field the date is being set on (must match a EE_Datetime_Field) |
| 1341 | 1341 | * @throws \EE_Error |
| 1342 | 1342 | */ |
@@ -1399,6 +1399,7 @@ discard block |
||
| 1399 | 1399 | * @param mixed (array|string) $args This is the arguments that will be passed to the callback. |
| 1400 | 1400 | * @param string $prepend You can include something to prepend on the timestamp |
| 1401 | 1401 | * @param string $append You can include something to append on the timestamp |
| 1402 | + * @param string $args |
|
| 1402 | 1403 | * @throws EE_Error |
| 1403 | 1404 | * @return string timestamp |
| 1404 | 1405 | */ |
@@ -1790,7 +1791,7 @@ discard block |
||
| 1790 | 1791 | * |
| 1791 | 1792 | * @param array $props_n_values incoming array of properties and their values |
| 1792 | 1793 | * @param string $classname the classname of the child class |
| 1793 | - * @param null $timezone |
|
| 1794 | + * @param string|null $timezone |
|
| 1794 | 1795 | * @param array $date_formats incoming date_formats in an array where the first value is the |
| 1795 | 1796 | * date_format and the second value is the time format |
| 1796 | 1797 | * @return mixed (EE_Base_Class|bool) |
@@ -1868,7 +1869,7 @@ discard block |
||
| 1868 | 1869 | * Gets the model instance (eg instance of EEM_Attendee) given its classname (eg EE_Attendee) |
| 1869 | 1870 | * |
| 1870 | 1871 | * @param string $model_classname |
| 1871 | - * @param null $timezone |
|
| 1872 | + * @param string|null $timezone |
|
| 1872 | 1873 | * @return EEM_Base |
| 1873 | 1874 | */ |
| 1874 | 1875 | protected static function _get_model_instance_with_name($model_classname, $timezone = null) |
@@ -2367,7 +2368,7 @@ discard block |
||
| 2367 | 2368 | * |
| 2368 | 2369 | * @param string $meta_key |
| 2369 | 2370 | * @param mixed $meta_value |
| 2370 | - * @param mixed $previous_value |
|
| 2371 | + * @param boolean $previous_value |
|
| 2371 | 2372 | * @return bool|int # of records updated (or BOOLEAN if we actually ended up inserting the extra meta row) |
| 2372 | 2373 | * @throws \EE_Error |
| 2373 | 2374 | * NOTE: if the values haven't changed, returns 0 |
@@ -1,5 +1,5 @@ discard block |
||
| 1 | 1 | <?php if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
| 2 | - exit('No direct script access allowed'); |
|
| 2 | + exit('No direct script access allowed'); |
|
| 3 | 3 | } |
| 4 | 4 | do_action('AHEE_log', __FILE__, ' FILE LOADED', ''); |
| 5 | 5 | |
@@ -25,2674 +25,2674 @@ discard block |
||
| 25 | 25 | abstract class EE_Base_Class |
| 26 | 26 | { |
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * This is an array of the original properties and values provided during construction |
|
| 30 | - * of this model object. (keys are model field names, values are their values). |
|
| 31 | - * This list is important to remember so that when we are merging data from the db, we know |
|
| 32 | - * which values to override and which to not override. |
|
| 33 | - * |
|
| 34 | - * @var array |
|
| 35 | - */ |
|
| 36 | - protected $_props_n_values_provided_in_constructor; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * Timezone |
|
| 40 | - * This gets set by the "set_timezone()" method so that we know what timezone incoming strings|timestamps are in. |
|
| 41 | - * This can also be used before a get to set what timezone you want strings coming out of the object to be in. NOT |
|
| 42 | - * all EE_Base_Class child classes use this property but any that use a EE_Datetime_Field data type will have |
|
| 43 | - * access to it. |
|
| 44 | - * |
|
| 45 | - * @var string |
|
| 46 | - */ |
|
| 47 | - protected $_timezone; |
|
| 48 | - |
|
| 49 | - |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * date format |
|
| 53 | - * pattern or format for displaying dates |
|
| 54 | - * |
|
| 55 | - * @var string $_dt_frmt |
|
| 56 | - */ |
|
| 57 | - protected $_dt_frmt; |
|
| 58 | - |
|
| 59 | - |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * time format |
|
| 63 | - * pattern or format for displaying time |
|
| 64 | - * |
|
| 65 | - * @var string $_tm_frmt |
|
| 66 | - */ |
|
| 67 | - protected $_tm_frmt; |
|
| 68 | - |
|
| 69 | - |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * This property is for holding a cached array of object properties indexed by property name as the key. |
|
| 73 | - * The purpose of this is for setting a cache on properties that may have calculated values after a |
|
| 74 | - * prepare_for_get. That way the cache can be checked first and the calculated property returned instead of having |
|
| 75 | - * to recalculate. Used by _set_cached_property() and _get_cached_property() methods. |
|
| 76 | - * |
|
| 77 | - * @var array |
|
| 78 | - */ |
|
| 79 | - protected $_cached_properties = array(); |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * An array containing keys of the related model, and values are either an array of related mode objects or a |
|
| 83 | - * single |
|
| 84 | - * related model object. see the model's _model_relations. The keys should match those specified. And if the |
|
| 85 | - * relation is of type EE_Belongs_To (or one of its children), then there should only be ONE related model object, |
|
| 86 | - * all others have an array) |
|
| 87 | - * |
|
| 88 | - * @var array |
|
| 89 | - */ |
|
| 90 | - protected $_model_relations = array(); |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * Array where keys are field names (see the model's _fields property) and values are their values. To see what |
|
| 94 | - * their types should be, look at what that field object returns on its prepare_for_get and prepare_for_set methods) |
|
| 95 | - * |
|
| 96 | - * @var array |
|
| 97 | - */ |
|
| 98 | - protected $_fields = array(); |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * @var boolean indicating whether or not this model object is intended to ever be saved |
|
| 102 | - * For example, we might create model objects intended to only be used for the duration |
|
| 103 | - * of this request and to be thrown away, and if they were accidentally saved |
|
| 104 | - * it would be a bug. |
|
| 105 | - */ |
|
| 106 | - protected $_allow_persist = true; |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * @var boolean indicating whether or not this model object's properties have changed since construction |
|
| 110 | - */ |
|
| 111 | - protected $_has_changes = false; |
|
| 112 | - |
|
| 113 | - |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * basic constructor for Event Espresso classes, performs any necessary initialization, and verifies it's children |
|
| 117 | - * play nice |
|
| 118 | - * |
|
| 119 | - * @param array $fieldValues where each key is a field (ie, array key in the 2nd |
|
| 120 | - * layer of the model's _fields array, (eg, EVT_ID, |
|
| 121 | - * TXN_amount, QST_name, etc) and values are their values |
|
| 122 | - * @param boolean $bydb a flag for setting if the class is instantiated by the |
|
| 123 | - * corresponding db model or not. |
|
| 124 | - * @param string $timezone indicate what timezone you want any datetime fields to |
|
| 125 | - * be in when instantiating a EE_Base_Class object. |
|
| 126 | - * @param array $date_formats An array of date formats to set on construct where first |
|
| 127 | - * value is the date_format and second value is the time |
|
| 128 | - * format. |
|
| 129 | - * @throws EE_Error |
|
| 130 | - */ |
|
| 131 | - protected function __construct($fieldValues = array(), $bydb = false, $timezone = '', $date_formats = array()) |
|
| 132 | - { |
|
| 133 | - $className = get_class($this); |
|
| 134 | - do_action("AHEE__{$className}__construct", $this, $fieldValues); |
|
| 135 | - $model = $this->get_model(); |
|
| 136 | - $model_fields = $model->field_settings(false); |
|
| 137 | - // ensure $fieldValues is an array |
|
| 138 | - $fieldValues = is_array($fieldValues) ? $fieldValues : array($fieldValues); |
|
| 139 | - // EEH_Debug_Tools::printr( $fieldValues, '$fieldValues <br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span>', 'auto' ); |
|
| 140 | - // verify client code has not passed any invalid field names |
|
| 141 | - foreach ($fieldValues as $field_name => $field_value) { |
|
| 142 | - if ( ! isset($model_fields[$field_name])) { |
|
| 143 | - throw new EE_Error(sprintf(__("Invalid field (%s) passed to constructor of %s. Allowed fields are :%s", |
|
| 144 | - "event_espresso"), $field_name, get_class($this), implode(", ", array_keys($model_fields)))); |
|
| 145 | - } |
|
| 146 | - } |
|
| 147 | - // EEH_Debug_Tools::printr( $model_fields, '$model_fields <br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span>', 'auto' ); |
|
| 148 | - $this->_timezone = EEH_DTT_Helper::get_valid_timezone_string($timezone); |
|
| 149 | - if ( ! empty($date_formats) && is_array($date_formats)) { |
|
| 150 | - list($this->_dt_frmt, $this->_tm_frmt) = $date_formats; |
|
| 151 | - } else { |
|
| 152 | - //set default formats for date and time |
|
| 153 | - $this->_dt_frmt = (string)get_option('date_format', 'Y-m-d'); |
|
| 154 | - $this->_tm_frmt = (string)get_option('time_format', 'g:i a'); |
|
| 155 | - } |
|
| 156 | - //if db model is instantiating |
|
| 157 | - if ($bydb) { |
|
| 158 | - //client code has indicated these field values are from the database |
|
| 159 | - foreach ($model_fields as $fieldName => $field) { |
|
| 160 | - $this->set_from_db($fieldName, isset($fieldValues[$fieldName]) ? $fieldValues[$fieldName] : null); |
|
| 161 | - } |
|
| 162 | - } else { |
|
| 163 | - //we're constructing a brand |
|
| 164 | - //new instance of the model object. Generally, this means we'll need to do more field validation |
|
| 165 | - foreach ($model_fields as $fieldName => $field) { |
|
| 166 | - $this->set($fieldName, isset($fieldValues[$fieldName]) ? $fieldValues[$fieldName] : null, true); |
|
| 167 | - } |
|
| 168 | - } |
|
| 169 | - //remember what values were passed to this constructor |
|
| 170 | - $this->_props_n_values_provided_in_constructor = $fieldValues; |
|
| 171 | - //remember in entity mapper |
|
| 172 | - if ( ! $bydb && $model->has_primary_key_field() && $this->ID()) { |
|
| 173 | - $model->add_to_entity_map($this); |
|
| 174 | - } |
|
| 175 | - //setup all the relations |
|
| 176 | - foreach ($this->get_model()->relation_settings() as $relation_name => $relation_obj) { |
|
| 177 | - if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 178 | - $this->_model_relations[$relation_name] = null; |
|
| 179 | - } else { |
|
| 180 | - $this->_model_relations[$relation_name] = array(); |
|
| 181 | - } |
|
| 182 | - } |
|
| 183 | - /** |
|
| 184 | - * Action done at the end of each model object construction |
|
| 185 | - * |
|
| 186 | - * @param EE_Base_Class $this the model object just created |
|
| 187 | - */ |
|
| 188 | - do_action('AHEE__EE_Base_Class__construct__finished', $this); |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * Gets whether or not this model object is allowed to persist/be saved to the database. |
|
| 195 | - * |
|
| 196 | - * @return boolean |
|
| 197 | - */ |
|
| 198 | - public function allow_persist() |
|
| 199 | - { |
|
| 200 | - return $this->_allow_persist; |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - |
|
| 204 | - |
|
| 205 | - /** |
|
| 206 | - * Sets whether or not this model object should be allowed to be saved to the DB. |
|
| 207 | - * Normally once this is set to FALSE you wouldn't set it back to TRUE, unless |
|
| 208 | - * you got new information that somehow made you change your mind. |
|
| 209 | - * |
|
| 210 | - * @param boolean $allow_persist |
|
| 211 | - * @return boolean |
|
| 212 | - */ |
|
| 213 | - public function set_allow_persist($allow_persist) |
|
| 214 | - { |
|
| 215 | - return $this->_allow_persist = $allow_persist; |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - |
|
| 219 | - |
|
| 220 | - /** |
|
| 221 | - * Gets the field's original value when this object was constructed during this request. |
|
| 222 | - * This can be helpful when determining if a model object has changed or not |
|
| 223 | - * |
|
| 224 | - * @param string $field_name |
|
| 225 | - * @return mixed|null |
|
| 226 | - * @throws \EE_Error |
|
| 227 | - */ |
|
| 228 | - public function get_original($field_name) |
|
| 229 | - { |
|
| 230 | - if (isset($this->_props_n_values_provided_in_constructor[$field_name]) |
|
| 231 | - && $field_settings = $this->get_model()->field_settings_for($field_name) |
|
| 232 | - ) { |
|
| 233 | - return $field_settings->prepare_for_get($this->_props_n_values_provided_in_constructor[$field_name]); |
|
| 234 | - } else { |
|
| 235 | - return null; |
|
| 236 | - } |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - |
|
| 240 | - |
|
| 241 | - /** |
|
| 242 | - * @param EE_Base_Class $obj |
|
| 243 | - * @return string |
|
| 244 | - */ |
|
| 245 | - public function get_class($obj) |
|
| 246 | - { |
|
| 247 | - return get_class($obj); |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - |
|
| 251 | - |
|
| 252 | - /** |
|
| 253 | - * Overrides parent because parent expects old models. |
|
| 254 | - * This also doesn't do any validation, and won't work for serialized arrays |
|
| 255 | - * |
|
| 256 | - * @param string $field_name |
|
| 257 | - * @param mixed $field_value |
|
| 258 | - * @param bool $use_default |
|
| 259 | - * @throws \EE_Error |
|
| 260 | - */ |
|
| 261 | - public function set($field_name, $field_value, $use_default = false) |
|
| 262 | - { |
|
| 263 | - // if not using default and nothing has changed, and object has already been setup (has ID), |
|
| 264 | - // then don't do anything |
|
| 265 | - if ( |
|
| 266 | - ! $use_default |
|
| 267 | - && $this->_fields[$field_name] === $field_value |
|
| 268 | - && $this->ID() |
|
| 269 | - ) { |
|
| 270 | - return; |
|
| 271 | - } |
|
| 272 | - $this->_has_changes = true; |
|
| 273 | - $field_obj = $this->get_model()->field_settings_for($field_name); |
|
| 274 | - if ($field_obj instanceof EE_Model_Field_Base) { |
|
| 275 | - // if ( method_exists( $field_obj, 'set_timezone' )) { |
|
| 276 | - if ($field_obj instanceof EE_Datetime_Field) { |
|
| 277 | - $field_obj->set_timezone($this->_timezone); |
|
| 278 | - $field_obj->set_date_format($this->_dt_frmt); |
|
| 279 | - $field_obj->set_time_format($this->_tm_frmt); |
|
| 280 | - } |
|
| 281 | - $holder_of_value = $field_obj->prepare_for_set($field_value); |
|
| 282 | - //should the value be null? |
|
| 283 | - if (($field_value === null || $holder_of_value === null || $holder_of_value === '') && $use_default) { |
|
| 284 | - $this->_fields[$field_name] = $field_obj->get_default_value(); |
|
| 285 | - /** |
|
| 286 | - * To save having to refactor all the models, if a default value is used for a |
|
| 287 | - * EE_Datetime_Field, and that value is not null nor is it a DateTime |
|
| 288 | - * object. Then let's do a set again to ensure that it becomes a DateTime |
|
| 289 | - * object. |
|
| 290 | - * |
|
| 291 | - * @since 4.6.10+ |
|
| 292 | - */ |
|
| 293 | - if ( |
|
| 294 | - $field_obj instanceof EE_Datetime_Field |
|
| 295 | - && $this->_fields[$field_name] !== null |
|
| 296 | - && ! $this->_fields[$field_name] instanceof DateTime |
|
| 297 | - ) { |
|
| 298 | - empty($this->_fields[$field_name]) |
|
| 299 | - ? $this->set($field_name, time()) |
|
| 300 | - : $this->set($field_name, $this->_fields[$field_name]); |
|
| 301 | - } |
|
| 302 | - } else { |
|
| 303 | - $this->_fields[$field_name] = $holder_of_value; |
|
| 304 | - } |
|
| 305 | - //if we're not in the constructor... |
|
| 306 | - //now check if what we set was a primary key |
|
| 307 | - if ( |
|
| 308 | - //note: props_n_values_provided_in_constructor is only set at the END of the constructor |
|
| 309 | - $this->_props_n_values_provided_in_constructor |
|
| 310 | - && $field_value |
|
| 311 | - && $field_name === self::_get_primary_key_name(get_class($this)) |
|
| 312 | - ) { |
|
| 313 | - //if so, we want all this object's fields to be filled either with |
|
| 314 | - //what we've explicitly set on this model |
|
| 315 | - //or what we have in the db |
|
| 316 | - // echo "setting primary key!"; |
|
| 317 | - $fields_on_model = self::_get_model(get_class($this))->field_settings(); |
|
| 318 | - $obj_in_db = self::_get_model(get_class($this))->get_one_by_ID($field_value); |
|
| 319 | - foreach ($fields_on_model as $field_obj) { |
|
| 320 | - if ( ! array_key_exists($field_obj->get_name(), $this->_props_n_values_provided_in_constructor) |
|
| 321 | - && $field_obj->get_name() !== $field_name |
|
| 322 | - ) { |
|
| 323 | - $this->set($field_obj->get_name(), $obj_in_db->get($field_obj->get_name())); |
|
| 324 | - } |
|
| 325 | - } |
|
| 326 | - //oh this model object has an ID? well make sure its in the entity mapper |
|
| 327 | - $this->get_model()->add_to_entity_map($this); |
|
| 328 | - } |
|
| 329 | - //let's unset any cache for this field_name from the $_cached_properties property. |
|
| 330 | - $this->_clear_cached_property($field_name); |
|
| 331 | - } else { |
|
| 332 | - throw new EE_Error(sprintf(__("A valid EE_Model_Field_Base could not be found for the given field name: %s", |
|
| 333 | - "event_espresso"), $field_name)); |
|
| 334 | - } |
|
| 335 | - } |
|
| 336 | - |
|
| 337 | - |
|
| 338 | - |
|
| 339 | - /** |
|
| 340 | - * This sets the field value on the db column if it exists for the given $column_name or |
|
| 341 | - * saves it to EE_Extra_Meta if the given $column_name does not match a db column. |
|
| 342 | - * |
|
| 343 | - * @see EE_message::get_column_value for related documentation on the necessity of this method. |
|
| 344 | - * @param string $field_name Must be the exact column name. |
|
| 345 | - * @param mixed $field_value The value to set. |
|
| 346 | - * @return int|bool @see EE_Base_Class::update_extra_meta() for return docs. |
|
| 347 | - * @throws \EE_Error |
|
| 348 | - */ |
|
| 349 | - public function set_field_or_extra_meta($field_name, $field_value) |
|
| 350 | - { |
|
| 351 | - if ($this->get_model()->has_field($field_name)) { |
|
| 352 | - $this->set($field_name, $field_value); |
|
| 353 | - return true; |
|
| 354 | - } else { |
|
| 355 | - //ensure this object is saved first so that extra meta can be properly related. |
|
| 356 | - $this->save(); |
|
| 357 | - return $this->update_extra_meta($field_name, $field_value); |
|
| 358 | - } |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - |
|
| 362 | - |
|
| 363 | - /** |
|
| 364 | - * This retrieves the value of the db column set on this class or if that's not present |
|
| 365 | - * it will attempt to retrieve from extra_meta if found. |
|
| 366 | - * Example Usage: |
|
| 367 | - * Via EE_Message child class: |
|
| 368 | - * Due to the dynamic nature of the EE_messages system, EE_messengers will always have a "to", |
|
| 369 | - * "from", "subject", and "content" field (as represented in the EE_Message schema), however they may |
|
| 370 | - * also have additional main fields specific to the messenger. The system accommodates those extra |
|
| 371 | - * fields through the EE_Extra_Meta table. This method allows for EE_messengers to retrieve the |
|
| 372 | - * value for those extra fields dynamically via the EE_message object. |
|
| 373 | - * |
|
| 374 | - * @param string $field_name expecting the fully qualified field name. |
|
| 375 | - * @return mixed|null value for the field if found. null if not found. |
|
| 376 | - * @throws \EE_Error |
|
| 377 | - */ |
|
| 378 | - public function get_field_or_extra_meta($field_name) |
|
| 379 | - { |
|
| 380 | - if ($this->get_model()->has_field($field_name)) { |
|
| 381 | - $column_value = $this->get($field_name); |
|
| 382 | - } else { |
|
| 383 | - //This isn't a column in the main table, let's see if it is in the extra meta. |
|
| 384 | - $column_value = $this->get_extra_meta($field_name, true, null); |
|
| 385 | - } |
|
| 386 | - return $column_value; |
|
| 387 | - } |
|
| 388 | - |
|
| 389 | - |
|
| 390 | - |
|
| 391 | - /** |
|
| 392 | - * See $_timezone property for description of what the timezone property is for. This SETS the timezone internally |
|
| 393 | - * for being able to reference what timezone we are running conversions on when converting TO the internal timezone |
|
| 394 | - * (UTC Unix Timestamp) for the object OR when converting FROM the internal timezone (UTC Unix Timestamp). This is |
|
| 395 | - * available to all child classes that may be using the EE_Datetime_Field for a field data type. |
|
| 396 | - * |
|
| 397 | - * @access public |
|
| 398 | - * @param string $timezone A valid timezone string as described by @link http://www.php.net/manual/en/timezones.php |
|
| 399 | - * @return void |
|
| 400 | - * @throws \EE_Error |
|
| 401 | - */ |
|
| 402 | - public function set_timezone($timezone = '') |
|
| 403 | - { |
|
| 404 | - $this->_timezone = EEH_DTT_Helper::get_valid_timezone_string($timezone); |
|
| 405 | - //make sure we clear all cached properties because they won't be relevant now |
|
| 406 | - $this->_clear_cached_properties(); |
|
| 407 | - //make sure we update field settings and the date for all EE_Datetime_Fields |
|
| 408 | - $model_fields = $this->get_model()->field_settings(false); |
|
| 409 | - foreach ($model_fields as $field_name => $field_obj) { |
|
| 410 | - if ($field_obj instanceof EE_Datetime_Field) { |
|
| 411 | - $field_obj->set_timezone($this->_timezone); |
|
| 412 | - if (isset($this->_fields[$field_name]) && $this->_fields[$field_name] instanceof DateTime) { |
|
| 413 | - $this->_fields[$field_name]->setTimezone(new DateTimeZone($this->_timezone)); |
|
| 414 | - } |
|
| 415 | - } |
|
| 416 | - } |
|
| 417 | - } |
|
| 418 | - |
|
| 419 | - |
|
| 420 | - |
|
| 421 | - /** |
|
| 422 | - * This just returns whatever is set for the current timezone. |
|
| 423 | - * |
|
| 424 | - * @access public |
|
| 425 | - * @return string timezone string |
|
| 426 | - */ |
|
| 427 | - public function get_timezone() |
|
| 428 | - { |
|
| 429 | - return $this->_timezone; |
|
| 430 | - } |
|
| 431 | - |
|
| 432 | - |
|
| 433 | - |
|
| 434 | - /** |
|
| 435 | - * This sets the internal date format to what is sent in to be used as the new default for the class |
|
| 436 | - * internally instead of wp set date format options |
|
| 437 | - * |
|
| 438 | - * @since 4.6 |
|
| 439 | - * @param string $format should be a format recognizable by PHP date() functions. |
|
| 440 | - */ |
|
| 441 | - public function set_date_format($format) |
|
| 442 | - { |
|
| 443 | - $this->_dt_frmt = $format; |
|
| 444 | - //clear cached_properties because they won't be relevant now. |
|
| 445 | - $this->_clear_cached_properties(); |
|
| 446 | - } |
|
| 447 | - |
|
| 448 | - |
|
| 449 | - |
|
| 450 | - /** |
|
| 451 | - * This sets the internal time format string to what is sent in to be used as the new default for the |
|
| 452 | - * class internally instead of wp set time format options. |
|
| 453 | - * |
|
| 454 | - * @since 4.6 |
|
| 455 | - * @param string $format should be a format recognizable by PHP date() functions. |
|
| 456 | - */ |
|
| 457 | - public function set_time_format($format) |
|
| 458 | - { |
|
| 459 | - $this->_tm_frmt = $format; |
|
| 460 | - //clear cached_properties because they won't be relevant now. |
|
| 461 | - $this->_clear_cached_properties(); |
|
| 462 | - } |
|
| 463 | - |
|
| 464 | - |
|
| 465 | - |
|
| 466 | - /** |
|
| 467 | - * This returns the current internal set format for the date and time formats. |
|
| 468 | - * |
|
| 469 | - * @param bool $full if true (default), then return the full format. Otherwise will return an array |
|
| 470 | - * where the first value is the date format and the second value is the time format. |
|
| 471 | - * @return mixed string|array |
|
| 472 | - */ |
|
| 473 | - public function get_format($full = true) |
|
| 474 | - { |
|
| 475 | - return $full ? $this->_dt_frmt . ' ' . $this->_tm_frmt : array($this->_dt_frmt, $this->_tm_frmt); |
|
| 476 | - } |
|
| 477 | - |
|
| 478 | - |
|
| 479 | - |
|
| 480 | - /** |
|
| 481 | - * cache |
|
| 482 | - * stores the passed model object on the current model object. |
|
| 483 | - * In certain circumstances, we can use this cached model object instead of querying for another one entirely. |
|
| 484 | - * |
|
| 485 | - * @param string $relationName one of the keys in the _model_relations array on the model. Eg |
|
| 486 | - * 'Registration' associated with this model object |
|
| 487 | - * @param EE_Base_Class $object_to_cache that has a relation to this model object. (Eg, if this is a Transaction, |
|
| 488 | - * that could be a payment or a registration) |
|
| 489 | - * @param null $cache_id a string or number that will be used as the key for any Belongs_To_Many |
|
| 490 | - * items which will be stored in an array on this object |
|
| 491 | - * @throws EE_Error |
|
| 492 | - * @return mixed index into cache, or just TRUE if the relation is of type Belongs_To (because there's only one |
|
| 493 | - * related thing, no array) |
|
| 494 | - */ |
|
| 495 | - public function cache($relationName = '', $object_to_cache = null, $cache_id = null) |
|
| 496 | - { |
|
| 497 | - // its entirely possible that there IS no related object yet in which case there is nothing to cache. |
|
| 498 | - if ( ! $object_to_cache instanceof EE_Base_Class) { |
|
| 499 | - return false; |
|
| 500 | - } |
|
| 501 | - // also get "how" the object is related, or throw an error |
|
| 502 | - if ( ! $relationship_to_model = $this->get_model()->related_settings_for($relationName)) { |
|
| 503 | - throw new EE_Error(sprintf(__('There is no relationship to %s on a %s. Cannot cache it', 'event_espresso'), |
|
| 504 | - $relationName, get_class($this))); |
|
| 505 | - } |
|
| 506 | - // how many things are related ? |
|
| 507 | - if ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
|
| 508 | - // if it's a "belongs to" relationship, then there's only one related model object eg, if this is a registration, there's only 1 attendee for it |
|
| 509 | - // so for these model objects just set it to be cached |
|
| 510 | - $this->_model_relations[$relationName] = $object_to_cache; |
|
| 511 | - $return = true; |
|
| 512 | - } else { |
|
| 513 | - // otherwise, this is the "many" side of a one to many relationship, so we'll add the object to the array of related objects for that type. |
|
| 514 | - // eg: if this is an event, there are many registrations for that event, so we cache the registrations in an array |
|
| 515 | - if ( ! is_array($this->_model_relations[$relationName])) { |
|
| 516 | - // if for some reason, the cached item is a model object, then stick that in the array, otherwise start with an empty array |
|
| 517 | - $this->_model_relations[$relationName] = $this->_model_relations[$relationName] instanceof EE_Base_Class |
|
| 518 | - ? array($this->_model_relations[$relationName]) : array(); |
|
| 519 | - } |
|
| 520 | - // first check for a cache_id which is normally empty |
|
| 521 | - if ( ! empty($cache_id)) { |
|
| 522 | - // if the cache_id exists, then it means we are purposely trying to cache this with a known key that can then be used to retrieve the object later on |
|
| 523 | - $this->_model_relations[$relationName][$cache_id] = $object_to_cache; |
|
| 524 | - $return = $cache_id; |
|
| 525 | - } elseif ($object_to_cache->ID()) { |
|
| 526 | - // OR the cached object originally came from the db, so let's just use it's PK for an ID |
|
| 527 | - $this->_model_relations[$relationName][$object_to_cache->ID()] = $object_to_cache; |
|
| 528 | - $return = $object_to_cache->ID(); |
|
| 529 | - } else { |
|
| 530 | - // OR it's a new object with no ID, so just throw it in the array with an auto-incremented ID |
|
| 531 | - $this->_model_relations[$relationName][] = $object_to_cache; |
|
| 532 | - // move the internal pointer to the end of the array |
|
| 533 | - end($this->_model_relations[$relationName]); |
|
| 534 | - // and grab the key so that we can return it |
|
| 535 | - $return = key($this->_model_relations[$relationName]); |
|
| 536 | - } |
|
| 537 | - } |
|
| 538 | - return $return; |
|
| 539 | - } |
|
| 540 | - |
|
| 541 | - |
|
| 542 | - |
|
| 543 | - /** |
|
| 544 | - * For adding an item to the cached_properties property. |
|
| 545 | - * |
|
| 546 | - * @access protected |
|
| 547 | - * @param string $fieldname the property item the corresponding value is for. |
|
| 548 | - * @param mixed $value The value we are caching. |
|
| 549 | - * @param string|null $cache_type |
|
| 550 | - * @return void |
|
| 551 | - * @throws \EE_Error |
|
| 552 | - */ |
|
| 553 | - protected function _set_cached_property($fieldname, $value, $cache_type = null) |
|
| 554 | - { |
|
| 555 | - //first make sure this property exists |
|
| 556 | - $this->get_model()->field_settings_for($fieldname); |
|
| 557 | - $cache_type = empty($cache_type) ? 'standard' : $cache_type; |
|
| 558 | - $this->_cached_properties[$fieldname][$cache_type] = $value; |
|
| 559 | - } |
|
| 560 | - |
|
| 561 | - |
|
| 562 | - |
|
| 563 | - /** |
|
| 564 | - * This returns the value cached property if it exists OR the actual property value if the cache doesn't exist. |
|
| 565 | - * This also SETS the cache if we return the actual property! |
|
| 566 | - * |
|
| 567 | - * @param string $fieldname the name of the property we're trying to retrieve |
|
| 568 | - * @param bool $pretty |
|
| 569 | - * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
| 570 | - * (in cases where the same property may be used for different outputs |
|
| 571 | - * - i.e. datetime, money etc.) |
|
| 572 | - * It can also accept certain pre-defined "schema" strings |
|
| 573 | - * to define how to output the property. |
|
| 574 | - * see the field's prepare_for_pretty_echoing for what strings can be used |
|
| 575 | - * @return mixed whatever the value for the property is we're retrieving |
|
| 576 | - * @throws \EE_Error |
|
| 577 | - */ |
|
| 578 | - protected function _get_cached_property($fieldname, $pretty = false, $extra_cache_ref = null) |
|
| 579 | - { |
|
| 580 | - //verify the field exists |
|
| 581 | - $this->get_model()->field_settings_for($fieldname); |
|
| 582 | - $cache_type = $pretty ? 'pretty' : 'standard'; |
|
| 583 | - $cache_type .= ! empty($extra_cache_ref) ? '_' . $extra_cache_ref : ''; |
|
| 584 | - if (isset($this->_cached_properties[$fieldname][$cache_type])) { |
|
| 585 | - return $this->_cached_properties[$fieldname][$cache_type]; |
|
| 586 | - } |
|
| 587 | - $field_obj = $this->get_model()->field_settings_for($fieldname); |
|
| 588 | - if ($field_obj instanceof EE_Model_Field_Base) { |
|
| 589 | - // If this is an EE_Datetime_Field we need to make sure timezone, formats, and output are correct |
|
| 590 | - if ($field_obj instanceof EE_Datetime_Field) { |
|
| 591 | - $this->_prepare_datetime_field($field_obj, $pretty, $extra_cache_ref); |
|
| 592 | - } |
|
| 593 | - if ( ! isset($this->_fields[$fieldname])) { |
|
| 594 | - $this->_fields[$fieldname] = null; |
|
| 595 | - } |
|
| 596 | - $value = $pretty |
|
| 597 | - ? $field_obj->prepare_for_pretty_echoing($this->_fields[$fieldname], $extra_cache_ref) |
|
| 598 | - : $field_obj->prepare_for_get($this->_fields[$fieldname]); |
|
| 599 | - $this->_set_cached_property($fieldname, $value, $cache_type); |
|
| 600 | - return $value; |
|
| 601 | - } |
|
| 602 | - return null; |
|
| 603 | - } |
|
| 604 | - |
|
| 605 | - |
|
| 606 | - |
|
| 607 | - /** |
|
| 608 | - * set timezone, formats, and output for EE_Datetime_Field objects |
|
| 609 | - * |
|
| 610 | - * @param \EE_Datetime_Field $datetime_field |
|
| 611 | - * @param bool $pretty |
|
| 612 | - * @param null $date_or_time |
|
| 613 | - * @return void |
|
| 614 | - * @throws \EE_Error |
|
| 615 | - */ |
|
| 616 | - protected function _prepare_datetime_field( |
|
| 617 | - EE_Datetime_Field $datetime_field, |
|
| 618 | - $pretty = false, |
|
| 619 | - $date_or_time = null |
|
| 620 | - ) { |
|
| 621 | - $datetime_field->set_timezone($this->_timezone); |
|
| 622 | - $datetime_field->set_date_format($this->_dt_frmt, $pretty); |
|
| 623 | - $datetime_field->set_time_format($this->_tm_frmt, $pretty); |
|
| 624 | - //set the output returned |
|
| 625 | - switch ($date_or_time) { |
|
| 626 | - case 'D' : |
|
| 627 | - $datetime_field->set_date_time_output('date'); |
|
| 628 | - break; |
|
| 629 | - case 'T' : |
|
| 630 | - $datetime_field->set_date_time_output('time'); |
|
| 631 | - break; |
|
| 632 | - default : |
|
| 633 | - $datetime_field->set_date_time_output(); |
|
| 634 | - } |
|
| 635 | - } |
|
| 636 | - |
|
| 637 | - |
|
| 638 | - |
|
| 639 | - /** |
|
| 640 | - * This just takes care of clearing out the cached_properties |
|
| 641 | - * |
|
| 642 | - * @return void |
|
| 643 | - */ |
|
| 644 | - protected function _clear_cached_properties() |
|
| 645 | - { |
|
| 646 | - $this->_cached_properties = array(); |
|
| 647 | - } |
|
| 648 | - |
|
| 649 | - |
|
| 650 | - |
|
| 651 | - /** |
|
| 652 | - * This just clears out ONE property if it exists in the cache |
|
| 653 | - * |
|
| 654 | - * @param string $property_name the property to remove if it exists (from the _cached_properties array) |
|
| 655 | - * @return void |
|
| 656 | - */ |
|
| 657 | - protected function _clear_cached_property($property_name) |
|
| 658 | - { |
|
| 659 | - if (isset($this->_cached_properties[$property_name])) { |
|
| 660 | - unset($this->_cached_properties[$property_name]); |
|
| 661 | - } |
|
| 662 | - } |
|
| 663 | - |
|
| 664 | - |
|
| 665 | - |
|
| 666 | - /** |
|
| 667 | - * Ensures that this related thing is a model object. |
|
| 668 | - * |
|
| 669 | - * @param mixed $object_or_id EE_base_Class/int/string either a related model object, or its ID |
|
| 670 | - * @param string $model_name name of the related thing, eg 'Attendee', |
|
| 671 | - * @return EE_Base_Class |
|
| 672 | - * @throws \EE_Error |
|
| 673 | - */ |
|
| 674 | - protected function ensure_related_thing_is_model_obj($object_or_id, $model_name) |
|
| 675 | - { |
|
| 676 | - $other_model_instance = self::_get_model_instance_with_name( |
|
| 677 | - self::_get_model_classname($model_name), |
|
| 678 | - $this->_timezone |
|
| 679 | - ); |
|
| 680 | - return $other_model_instance->ensure_is_obj($object_or_id); |
|
| 681 | - } |
|
| 682 | - |
|
| 683 | - |
|
| 684 | - |
|
| 685 | - /** |
|
| 686 | - * Forgets the cached model of the given relation Name. So the next time we request it, |
|
| 687 | - * we will fetch it again from the database. (Handy if you know it's changed somehow). |
|
| 688 | - * If a specific object is supplied, and the relationship to it is either a HasMany or HABTM, |
|
| 689 | - * then only remove that one object from our cached array. Otherwise, clear the entire list |
|
| 690 | - * |
|
| 691 | - * @param string $relationName one of the keys in the _model_relations array on the model. |
|
| 692 | - * Eg 'Registration' |
|
| 693 | - * @param mixed $object_to_remove_or_index_into_array or an index into the array of cached things, or NULL |
|
| 694 | - * if you intend to use $clear_all = TRUE, or the relation only |
|
| 695 | - * has 1 object anyways (ie, it's a BelongsToRelation) |
|
| 696 | - * @param bool $clear_all This flags clearing the entire cache relation property if |
|
| 697 | - * this is HasMany or HABTM. |
|
| 698 | - * @throws EE_Error |
|
| 699 | - * @return EE_Base_Class | boolean from which was cleared from the cache, or true if we requested to remove a |
|
| 700 | - * relation from all |
|
| 701 | - */ |
|
| 702 | - public function clear_cache($relationName, $object_to_remove_or_index_into_array = null, $clear_all = false) |
|
| 703 | - { |
|
| 704 | - $relationship_to_model = $this->get_model()->related_settings_for($relationName); |
|
| 705 | - $index_in_cache = ''; |
|
| 706 | - if ( ! $relationship_to_model) { |
|
| 707 | - throw new EE_Error( |
|
| 708 | - sprintf( |
|
| 709 | - __("There is no relationship to %s on a %s. Cannot clear that cache", 'event_espresso'), |
|
| 710 | - $relationName, |
|
| 711 | - get_class($this) |
|
| 712 | - ) |
|
| 713 | - ); |
|
| 714 | - } |
|
| 715 | - if ($clear_all) { |
|
| 716 | - $obj_removed = true; |
|
| 717 | - $this->_model_relations[$relationName] = null; |
|
| 718 | - } elseif ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
|
| 719 | - $obj_removed = $this->_model_relations[$relationName]; |
|
| 720 | - $this->_model_relations[$relationName] = null; |
|
| 721 | - } else { |
|
| 722 | - if ($object_to_remove_or_index_into_array instanceof EE_Base_Class |
|
| 723 | - && $object_to_remove_or_index_into_array->ID() |
|
| 724 | - ) { |
|
| 725 | - $index_in_cache = $object_to_remove_or_index_into_array->ID(); |
|
| 726 | - if (is_array($this->_model_relations[$relationName]) |
|
| 727 | - && ! isset($this->_model_relations[$relationName][$index_in_cache]) |
|
| 728 | - ) { |
|
| 729 | - $index_found_at = null; |
|
| 730 | - //find this object in the array even though it has a different key |
|
| 731 | - foreach ($this->_model_relations[$relationName] as $index => $obj) { |
|
| 732 | - if ( |
|
| 733 | - $obj instanceof EE_Base_Class |
|
| 734 | - && ( |
|
| 735 | - $obj == $object_to_remove_or_index_into_array |
|
| 736 | - || $obj->ID() === $object_to_remove_or_index_into_array->ID() |
|
| 737 | - ) |
|
| 738 | - ) { |
|
| 739 | - $index_found_at = $index; |
|
| 740 | - break; |
|
| 741 | - } |
|
| 742 | - } |
|
| 743 | - if ($index_found_at) { |
|
| 744 | - $index_in_cache = $index_found_at; |
|
| 745 | - } else { |
|
| 746 | - //it wasn't found. huh. well obviously it doesn't need to be removed from teh cache |
|
| 747 | - //if it wasn't in it to begin with. So we're done |
|
| 748 | - return $object_to_remove_or_index_into_array; |
|
| 749 | - } |
|
| 750 | - } |
|
| 751 | - } elseif ($object_to_remove_or_index_into_array instanceof EE_Base_Class) { |
|
| 752 | - //so they provided a model object, but it's not yet saved to the DB... so let's go hunting for it! |
|
| 753 | - foreach ($this->get_all_from_cache($relationName) as $index => $potentially_obj_we_want) { |
|
| 754 | - if ($potentially_obj_we_want == $object_to_remove_or_index_into_array) { |
|
| 755 | - $index_in_cache = $index; |
|
| 756 | - } |
|
| 757 | - } |
|
| 758 | - } else { |
|
| 759 | - $index_in_cache = $object_to_remove_or_index_into_array; |
|
| 760 | - } |
|
| 761 | - //supposedly we've found it. But it could just be that the client code |
|
| 762 | - //provided a bad index/object |
|
| 763 | - if ( |
|
| 764 | - isset( |
|
| 765 | - $this->_model_relations[$relationName], |
|
| 766 | - $this->_model_relations[$relationName][$index_in_cache] |
|
| 767 | - ) |
|
| 768 | - ) { |
|
| 769 | - $obj_removed = $this->_model_relations[$relationName][$index_in_cache]; |
|
| 770 | - unset($this->_model_relations[$relationName][$index_in_cache]); |
|
| 771 | - } else { |
|
| 772 | - //that thing was never cached anyways. |
|
| 773 | - $obj_removed = null; |
|
| 774 | - } |
|
| 775 | - } |
|
| 776 | - return $obj_removed; |
|
| 777 | - } |
|
| 778 | - |
|
| 779 | - |
|
| 780 | - |
|
| 781 | - /** |
|
| 782 | - * update_cache_after_object_save |
|
| 783 | - * Allows a cached item to have it's cache ID (within the array of cached items) reset using the new ID it has |
|
| 784 | - * obtained after being saved to the db |
|
| 785 | - * |
|
| 786 | - * @param string $relationName - the type of object that is cached |
|
| 787 | - * @param \EE_Base_Class $newly_saved_object - the newly saved object to be re-cached |
|
| 788 | - * @param string $current_cache_id - the ID that was used when originally caching the object |
|
| 789 | - * @return boolean TRUE on success, FALSE on fail |
|
| 790 | - * @throws \EE_Error |
|
| 791 | - */ |
|
| 792 | - public function update_cache_after_object_save( |
|
| 793 | - $relationName, |
|
| 794 | - EE_Base_Class $newly_saved_object, |
|
| 795 | - $current_cache_id = '' |
|
| 796 | - ) { |
|
| 797 | - // verify that incoming object is of the correct type |
|
| 798 | - $obj_class = 'EE_' . $relationName; |
|
| 799 | - if ($newly_saved_object instanceof $obj_class) { |
|
| 800 | - /* @type EE_Base_Class $newly_saved_object */ |
|
| 801 | - // now get the type of relation |
|
| 802 | - $relationship_to_model = $this->get_model()->related_settings_for($relationName); |
|
| 803 | - // if this is a 1:1 relationship |
|
| 804 | - if ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
|
| 805 | - // then just replace the cached object with the newly saved object |
|
| 806 | - $this->_model_relations[$relationName] = $newly_saved_object; |
|
| 807 | - return true; |
|
| 808 | - // or if it's some kind of sordid feral polyamorous relationship... |
|
| 809 | - } elseif (is_array($this->_model_relations[$relationName]) |
|
| 810 | - && isset($this->_model_relations[$relationName][$current_cache_id]) |
|
| 811 | - ) { |
|
| 812 | - // then remove the current cached item |
|
| 813 | - unset($this->_model_relations[$relationName][$current_cache_id]); |
|
| 814 | - // and cache the newly saved object using it's new ID |
|
| 815 | - $this->_model_relations[$relationName][$newly_saved_object->ID()] = $newly_saved_object; |
|
| 816 | - return true; |
|
| 817 | - } |
|
| 818 | - } |
|
| 819 | - return false; |
|
| 820 | - } |
|
| 821 | - |
|
| 822 | - |
|
| 823 | - |
|
| 824 | - /** |
|
| 825 | - * Fetches a single EE_Base_Class on that relation. (If the relation is of type |
|
| 826 | - * BelongsTo, it will only ever have 1 object. However, other relations could have an array of objects) |
|
| 827 | - * |
|
| 828 | - * @param string $relationName |
|
| 829 | - * @return EE_Base_Class |
|
| 830 | - */ |
|
| 831 | - public function get_one_from_cache($relationName) |
|
| 832 | - { |
|
| 833 | - $cached_array_or_object = isset($this->_model_relations[$relationName]) ? $this->_model_relations[$relationName] |
|
| 834 | - : null; |
|
| 835 | - if (is_array($cached_array_or_object)) { |
|
| 836 | - return array_shift($cached_array_or_object); |
|
| 837 | - } else { |
|
| 838 | - return $cached_array_or_object; |
|
| 839 | - } |
|
| 840 | - } |
|
| 841 | - |
|
| 842 | - |
|
| 843 | - |
|
| 844 | - /** |
|
| 845 | - * Fetches a single EE_Base_Class on that relation. (If the relation is of type |
|
| 846 | - * BelongsTo, it will only ever have 1 object. However, other relations could have an array of objects) |
|
| 847 | - * |
|
| 848 | - * @param string $relationName |
|
| 849 | - * @throws \EE_Error |
|
| 850 | - * @return EE_Base_Class[] NOT necessarily indexed by primary keys |
|
| 851 | - */ |
|
| 852 | - public function get_all_from_cache($relationName) |
|
| 853 | - { |
|
| 854 | - $objects = isset($this->_model_relations[$relationName]) ? $this->_model_relations[$relationName] : array(); |
|
| 855 | - // if the result is not an array, but exists, make it an array |
|
| 856 | - $objects = is_array($objects) ? $objects : array($objects); |
|
| 857 | - //bugfix for https://events.codebasehq.com/projects/event-espresso/tickets/7143 |
|
| 858 | - //basically, if this model object was stored in the session, and these cached model objects |
|
| 859 | - //already have IDs, let's make sure they're in their model's entity mapper |
|
| 860 | - //otherwise we will have duplicates next time we call |
|
| 861 | - // EE_Registry::instance()->load_model( $relationName )->get_one_by_ID( $result->ID() ); |
|
| 862 | - $model = EE_Registry::instance()->load_model($relationName); |
|
| 863 | - foreach ($objects as $model_object) { |
|
| 864 | - if ($model instanceof EEM_Base && $model_object instanceof EE_Base_Class) { |
|
| 865 | - //ensure its in the map if it has an ID; otherwise it will be added to the map when its saved |
|
| 866 | - if ($model_object->ID()) { |
|
| 867 | - $model->add_to_entity_map($model_object); |
|
| 868 | - } |
|
| 869 | - } else { |
|
| 870 | - throw new EE_Error( |
|
| 871 | - sprintf( |
|
| 872 | - __( |
|
| 873 | - 'Error retrieving related model objects. Either $1%s is not a model or $2%s is not a model object', |
|
| 874 | - 'event_espresso' |
|
| 875 | - ), |
|
| 876 | - $relationName, |
|
| 877 | - gettype($model_object) |
|
| 878 | - ) |
|
| 879 | - ); |
|
| 880 | - } |
|
| 881 | - } |
|
| 882 | - return $objects; |
|
| 883 | - } |
|
| 884 | - |
|
| 885 | - |
|
| 886 | - |
|
| 887 | - /** |
|
| 888 | - * Returns the next x number of EE_Base_Class objects in sequence from this object as found in the database |
|
| 889 | - * matching the given query conditions. |
|
| 890 | - * |
|
| 891 | - * @param null $field_to_order_by What field is being used as the reference point. |
|
| 892 | - * @param int $limit How many objects to return. |
|
| 893 | - * @param array $query_params Any additional conditions on the query. |
|
| 894 | - * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
|
| 895 | - * you can indicate just the columns you want returned |
|
| 896 | - * @return array|EE_Base_Class[] |
|
| 897 | - * @throws \EE_Error |
|
| 898 | - */ |
|
| 899 | - public function next_x($field_to_order_by = null, $limit = 1, $query_params = array(), $columns_to_select = null) |
|
| 900 | - { |
|
| 901 | - $field = empty($field_to_order_by) && $this->get_model()->has_primary_key_field() |
|
| 902 | - ? $this->get_model()->get_primary_key_field()->get_name() |
|
| 903 | - : $field_to_order_by; |
|
| 904 | - $current_value = ! empty($field) ? $this->get($field) : null; |
|
| 905 | - if (empty($field) || empty($current_value)) { |
|
| 906 | - return array(); |
|
| 907 | - } |
|
| 908 | - return $this->get_model()->next_x($current_value, $field, $limit, $query_params, $columns_to_select); |
|
| 909 | - } |
|
| 910 | - |
|
| 911 | - |
|
| 912 | - |
|
| 913 | - /** |
|
| 914 | - * Returns the previous x number of EE_Base_Class objects in sequence from this object as found in the database |
|
| 915 | - * matching the given query conditions. |
|
| 916 | - * |
|
| 917 | - * @param null $field_to_order_by What field is being used as the reference point. |
|
| 918 | - * @param int $limit How many objects to return. |
|
| 919 | - * @param array $query_params Any additional conditions on the query. |
|
| 920 | - * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
|
| 921 | - * you can indicate just the columns you want returned |
|
| 922 | - * @return array|EE_Base_Class[] |
|
| 923 | - * @throws \EE_Error |
|
| 924 | - */ |
|
| 925 | - public function previous_x( |
|
| 926 | - $field_to_order_by = null, |
|
| 927 | - $limit = 1, |
|
| 928 | - $query_params = array(), |
|
| 929 | - $columns_to_select = null |
|
| 930 | - ) { |
|
| 931 | - $field = empty($field_to_order_by) && $this->get_model()->has_primary_key_field() |
|
| 932 | - ? $this->get_model()->get_primary_key_field()->get_name() |
|
| 933 | - : $field_to_order_by; |
|
| 934 | - $current_value = ! empty($field) ? $this->get($field) : null; |
|
| 935 | - if (empty($field) || empty($current_value)) { |
|
| 936 | - return array(); |
|
| 937 | - } |
|
| 938 | - return $this->get_model()->previous_x($current_value, $field, $limit, $query_params, $columns_to_select); |
|
| 939 | - } |
|
| 940 | - |
|
| 941 | - |
|
| 942 | - |
|
| 943 | - /** |
|
| 944 | - * Returns the next EE_Base_Class object in sequence from this object as found in the database |
|
| 945 | - * matching the given query conditions. |
|
| 946 | - * |
|
| 947 | - * @param null $field_to_order_by What field is being used as the reference point. |
|
| 948 | - * @param array $query_params Any additional conditions on the query. |
|
| 949 | - * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
|
| 950 | - * you can indicate just the columns you want returned |
|
| 951 | - * @return array|EE_Base_Class |
|
| 952 | - * @throws \EE_Error |
|
| 953 | - */ |
|
| 954 | - public function next($field_to_order_by = null, $query_params = array(), $columns_to_select = null) |
|
| 955 | - { |
|
| 956 | - $field = empty($field_to_order_by) && $this->get_model()->has_primary_key_field() |
|
| 957 | - ? $this->get_model()->get_primary_key_field()->get_name() |
|
| 958 | - : $field_to_order_by; |
|
| 959 | - $current_value = ! empty($field) ? $this->get($field) : null; |
|
| 960 | - if (empty($field) || empty($current_value)) { |
|
| 961 | - return array(); |
|
| 962 | - } |
|
| 963 | - return $this->get_model()->next($current_value, $field, $query_params, $columns_to_select); |
|
| 964 | - } |
|
| 965 | - |
|
| 966 | - |
|
| 967 | - |
|
| 968 | - /** |
|
| 969 | - * Returns the previous EE_Base_Class object in sequence from this object as found in the database |
|
| 970 | - * matching the given query conditions. |
|
| 971 | - * |
|
| 972 | - * @param null $field_to_order_by What field is being used as the reference point. |
|
| 973 | - * @param array $query_params Any additional conditions on the query. |
|
| 974 | - * @param null $columns_to_select If left null, then an EE_Base_Class object is returned, otherwise |
|
| 975 | - * you can indicate just the column you want returned |
|
| 976 | - * @return array|EE_Base_Class |
|
| 977 | - * @throws \EE_Error |
|
| 978 | - */ |
|
| 979 | - public function previous($field_to_order_by = null, $query_params = array(), $columns_to_select = null) |
|
| 980 | - { |
|
| 981 | - $field = empty($field_to_order_by) && $this->get_model()->has_primary_key_field() |
|
| 982 | - ? $this->get_model()->get_primary_key_field()->get_name() |
|
| 983 | - : $field_to_order_by; |
|
| 984 | - $current_value = ! empty($field) ? $this->get($field) : null; |
|
| 985 | - if (empty($field) || empty($current_value)) { |
|
| 986 | - return array(); |
|
| 987 | - } |
|
| 988 | - return $this->get_model()->previous($current_value, $field, $query_params, $columns_to_select); |
|
| 989 | - } |
|
| 990 | - |
|
| 991 | - |
|
| 992 | - |
|
| 993 | - /** |
|
| 994 | - * Overrides parent because parent expects old models. |
|
| 995 | - * This also doesn't do any validation, and won't work for serialized arrays |
|
| 996 | - * |
|
| 997 | - * @param string $field_name |
|
| 998 | - * @param mixed $field_value_from_db |
|
| 999 | - * @throws \EE_Error |
|
| 1000 | - */ |
|
| 1001 | - public function set_from_db($field_name, $field_value_from_db) |
|
| 1002 | - { |
|
| 1003 | - $field_obj = $this->get_model()->field_settings_for($field_name); |
|
| 1004 | - if ($field_obj instanceof EE_Model_Field_Base) { |
|
| 1005 | - //you would think the DB has no NULLs for non-null label fields right? wrong! |
|
| 1006 | - //eg, a CPT model object could have an entry in the posts table, but no |
|
| 1007 | - //entry in the meta table. Meaning that all its columns in the meta table |
|
| 1008 | - //are null! yikes! so when we find one like that, use defaults for its meta columns |
|
| 1009 | - if ($field_value_from_db === null) { |
|
| 1010 | - if ($field_obj->is_nullable()) { |
|
| 1011 | - //if the field allows nulls, then let it be null |
|
| 1012 | - $field_value = null; |
|
| 1013 | - } else { |
|
| 1014 | - $field_value = $field_obj->get_default_value(); |
|
| 1015 | - } |
|
| 1016 | - } else { |
|
| 1017 | - $field_value = $field_obj->prepare_for_set_from_db($field_value_from_db); |
|
| 1018 | - } |
|
| 1019 | - $this->_fields[$field_name] = $field_value; |
|
| 1020 | - $this->_clear_cached_property($field_name); |
|
| 1021 | - } |
|
| 1022 | - } |
|
| 1023 | - |
|
| 1024 | - |
|
| 1025 | - |
|
| 1026 | - /** |
|
| 1027 | - * verifies that the specified field is of the correct type |
|
| 1028 | - * |
|
| 1029 | - * @param string $field_name |
|
| 1030 | - * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
| 1031 | - * (in cases where the same property may be used for different outputs |
|
| 1032 | - * - i.e. datetime, money etc.) |
|
| 1033 | - * @return mixed |
|
| 1034 | - * @throws \EE_Error |
|
| 1035 | - */ |
|
| 1036 | - public function get($field_name, $extra_cache_ref = null) |
|
| 1037 | - { |
|
| 1038 | - return $this->_get_cached_property($field_name, false, $extra_cache_ref); |
|
| 1039 | - } |
|
| 1040 | - |
|
| 1041 | - |
|
| 1042 | - |
|
| 1043 | - /** |
|
| 1044 | - * This method simply returns the RAW unprocessed value for the given property in this class |
|
| 1045 | - * |
|
| 1046 | - * @param string $field_name A valid fieldname |
|
| 1047 | - * @return mixed Whatever the raw value stored on the property is. |
|
| 1048 | - * @throws EE_Error if fieldSettings is misconfigured or the field doesn't exist. |
|
| 1049 | - */ |
|
| 1050 | - public function get_raw($field_name) |
|
| 1051 | - { |
|
| 1052 | - $field_settings = $this->get_model()->field_settings_for($field_name); |
|
| 1053 | - return $field_settings instanceof EE_Datetime_Field && $this->_fields[$field_name] instanceof DateTime |
|
| 1054 | - ? $this->_fields[$field_name]->format('U') |
|
| 1055 | - : $this->_fields[$field_name]; |
|
| 1056 | - } |
|
| 1057 | - |
|
| 1058 | - |
|
| 1059 | - |
|
| 1060 | - /** |
|
| 1061 | - * This is used to return the internal DateTime object used for a field that is a |
|
| 1062 | - * EE_Datetime_Field. |
|
| 1063 | - * |
|
| 1064 | - * @param string $field_name The field name retrieving the DateTime object. |
|
| 1065 | - * @return mixed null | false | DateTime If the requested field is NOT a EE_Datetime_Field then |
|
| 1066 | - * @throws \EE_Error |
|
| 1067 | - * an error is set and false returned. If the field IS an |
|
| 1068 | - * EE_Datetime_Field and but the field value is null, then |
|
| 1069 | - * just null is returned (because that indicates that likely |
|
| 1070 | - * this field is nullable). |
|
| 1071 | - */ |
|
| 1072 | - public function get_DateTime_object($field_name) |
|
| 1073 | - { |
|
| 1074 | - $field_settings = $this->get_model()->field_settings_for($field_name); |
|
| 1075 | - if ( ! $field_settings instanceof EE_Datetime_Field) { |
|
| 1076 | - EE_Error::add_error( |
|
| 1077 | - sprintf( |
|
| 1078 | - __( |
|
| 1079 | - 'The field %s is not an EE_Datetime_Field field. There is no DateTime object stored on this field type.', |
|
| 1080 | - 'event_espresso' |
|
| 1081 | - ), |
|
| 1082 | - $field_name |
|
| 1083 | - ), |
|
| 1084 | - __FILE__, |
|
| 1085 | - __FUNCTION__, |
|
| 1086 | - __LINE__ |
|
| 1087 | - ); |
|
| 1088 | - return false; |
|
| 1089 | - } |
|
| 1090 | - return $this->_fields[$field_name]; |
|
| 1091 | - } |
|
| 1092 | - |
|
| 1093 | - |
|
| 1094 | - |
|
| 1095 | - /** |
|
| 1096 | - * To be used in template to immediately echo out the value, and format it for output. |
|
| 1097 | - * Eg, should call stripslashes and whatnot before echoing |
|
| 1098 | - * |
|
| 1099 | - * @param string $field_name the name of the field as it appears in the DB |
|
| 1100 | - * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
| 1101 | - * (in cases where the same property may be used for different outputs |
|
| 1102 | - * - i.e. datetime, money etc.) |
|
| 1103 | - * @return void |
|
| 1104 | - * @throws \EE_Error |
|
| 1105 | - */ |
|
| 1106 | - public function e($field_name, $extra_cache_ref = null) |
|
| 1107 | - { |
|
| 1108 | - echo $this->get_pretty($field_name, $extra_cache_ref); |
|
| 1109 | - } |
|
| 1110 | - |
|
| 1111 | - |
|
| 1112 | - |
|
| 1113 | - /** |
|
| 1114 | - * Exactly like e(), echoes out the field, but sets its schema to 'form_input', so that it |
|
| 1115 | - * can be easily used as the value of form input. |
|
| 1116 | - * |
|
| 1117 | - * @param string $field_name |
|
| 1118 | - * @return void |
|
| 1119 | - * @throws \EE_Error |
|
| 1120 | - */ |
|
| 1121 | - public function f($field_name) |
|
| 1122 | - { |
|
| 1123 | - $this->e($field_name, 'form_input'); |
|
| 1124 | - } |
|
| 1125 | - |
|
| 1126 | - |
|
| 1127 | - |
|
| 1128 | - /** |
|
| 1129 | - * Gets a pretty view of the field's value. $extra_cache_ref can specify different formats for this. |
|
| 1130 | - * The $extra_cache_ref will be passed to the model field's prepare_for_pretty_echoing, so consult the field's class |
|
| 1131 | - * to see what options are available. |
|
| 1132 | - * @param string $field_name |
|
| 1133 | - * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
| 1134 | - * (in cases where the same property may be used for different outputs |
|
| 1135 | - * - i.e. datetime, money etc.) |
|
| 1136 | - * @return mixed |
|
| 1137 | - * @throws \EE_Error |
|
| 1138 | - */ |
|
| 1139 | - public function get_pretty($field_name, $extra_cache_ref = null) |
|
| 1140 | - { |
|
| 1141 | - return $this->_get_cached_property($field_name, true, $extra_cache_ref); |
|
| 1142 | - } |
|
| 1143 | - |
|
| 1144 | - |
|
| 1145 | - |
|
| 1146 | - /** |
|
| 1147 | - * This simply returns the datetime for the given field name |
|
| 1148 | - * Note: this protected function is called by the wrapper get_date or get_time or get_datetime functions |
|
| 1149 | - * (and the equivalent e_date, e_time, e_datetime). |
|
| 1150 | - * |
|
| 1151 | - * @access protected |
|
| 1152 | - * @param string $field_name Field on the instantiated EE_Base_Class child object |
|
| 1153 | - * @param string $dt_frmt valid datetime format used for date |
|
| 1154 | - * (if '' then we just use the default on the field, |
|
| 1155 | - * if NULL we use the last-used format) |
|
| 1156 | - * @param string $tm_frmt Same as above except this is for time format |
|
| 1157 | - * @param string $date_or_time if NULL then both are returned, otherwise "D" = only date and "T" = only time. |
|
| 1158 | - * @param boolean $echo Whether the dtt is echoing using pretty echoing or just returned using vanilla get |
|
| 1159 | - * @return string|bool|EE_Error string on success, FALSE on fail, or EE_Error Exception is thrown |
|
| 1160 | - * if field is not a valid dtt field, or void if echoing |
|
| 1161 | - * @throws \EE_Error |
|
| 1162 | - */ |
|
| 1163 | - protected function _get_datetime($field_name, $dt_frmt = '', $tm_frmt = '', $date_or_time = '', $echo = false) |
|
| 1164 | - { |
|
| 1165 | - // clear cached property |
|
| 1166 | - $this->_clear_cached_property($field_name); |
|
| 1167 | - //reset format properties because they are used in get() |
|
| 1168 | - $this->_dt_frmt = $dt_frmt !== '' ? $dt_frmt : $this->_dt_frmt; |
|
| 1169 | - $this->_tm_frmt = $tm_frmt !== '' ? $tm_frmt : $this->_tm_frmt; |
|
| 1170 | - if ($echo) { |
|
| 1171 | - $this->e($field_name, $date_or_time); |
|
| 1172 | - return ''; |
|
| 1173 | - } |
|
| 1174 | - return $this->get($field_name, $date_or_time); |
|
| 1175 | - } |
|
| 1176 | - |
|
| 1177 | - |
|
| 1178 | - |
|
| 1179 | - /** |
|
| 1180 | - * below are wrapper functions for the various datetime outputs that can be obtained for JUST returning the date |
|
| 1181 | - * portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
|
| 1182 | - * other echoes the pretty value for dtt) |
|
| 1183 | - * |
|
| 1184 | - * @param string $field_name name of model object datetime field holding the value |
|
| 1185 | - * @param string $format format for the date returned (if NULL we use default in dt_frmt property) |
|
| 1186 | - * @return string datetime value formatted |
|
| 1187 | - * @throws \EE_Error |
|
| 1188 | - */ |
|
| 1189 | - public function get_date($field_name, $format = '') |
|
| 1190 | - { |
|
| 1191 | - return $this->_get_datetime($field_name, $format, null, 'D'); |
|
| 1192 | - } |
|
| 1193 | - |
|
| 1194 | - |
|
| 1195 | - |
|
| 1196 | - /** |
|
| 1197 | - * @param $field_name |
|
| 1198 | - * @param string $format |
|
| 1199 | - * @throws \EE_Error |
|
| 1200 | - */ |
|
| 1201 | - public function e_date($field_name, $format = '') |
|
| 1202 | - { |
|
| 1203 | - $this->_get_datetime($field_name, $format, null, 'D', true); |
|
| 1204 | - } |
|
| 1205 | - |
|
| 1206 | - |
|
| 1207 | - |
|
| 1208 | - /** |
|
| 1209 | - * below are wrapper functions for the various datetime outputs that can be obtained for JUST returning the time |
|
| 1210 | - * portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
|
| 1211 | - * other echoes the pretty value for dtt) |
|
| 1212 | - * |
|
| 1213 | - * @param string $field_name name of model object datetime field holding the value |
|
| 1214 | - * @param string $format format for the time returned ( if NULL we use default in tm_frmt property) |
|
| 1215 | - * @return string datetime value formatted |
|
| 1216 | - * @throws \EE_Error |
|
| 1217 | - */ |
|
| 1218 | - public function get_time($field_name, $format = '') |
|
| 1219 | - { |
|
| 1220 | - return $this->_get_datetime($field_name, null, $format, 'T'); |
|
| 1221 | - } |
|
| 1222 | - |
|
| 1223 | - |
|
| 1224 | - |
|
| 1225 | - /** |
|
| 1226 | - * @param $field_name |
|
| 1227 | - * @param string $format |
|
| 1228 | - * @throws \EE_Error |
|
| 1229 | - */ |
|
| 1230 | - public function e_time($field_name, $format = '') |
|
| 1231 | - { |
|
| 1232 | - $this->_get_datetime($field_name, null, $format, 'T', true); |
|
| 1233 | - } |
|
| 1234 | - |
|
| 1235 | - |
|
| 1236 | - |
|
| 1237 | - /** |
|
| 1238 | - * below are wrapper functions for the various datetime outputs that can be obtained for returning the date AND |
|
| 1239 | - * time portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
|
| 1240 | - * other echoes the pretty value for dtt) |
|
| 1241 | - * |
|
| 1242 | - * @param string $field_name name of model object datetime field holding the value |
|
| 1243 | - * @param string $dt_frmt format for the date returned (if NULL we use default in dt_frmt property) |
|
| 1244 | - * @param string $tm_frmt format for the time returned (if NULL we use default in tm_frmt property) |
|
| 1245 | - * @return string datetime value formatted |
|
| 1246 | - * @throws \EE_Error |
|
| 1247 | - */ |
|
| 1248 | - public function get_datetime($field_name, $dt_frmt = '', $tm_frmt = '') |
|
| 1249 | - { |
|
| 1250 | - return $this->_get_datetime($field_name, $dt_frmt, $tm_frmt); |
|
| 1251 | - } |
|
| 1252 | - |
|
| 1253 | - |
|
| 1254 | - |
|
| 1255 | - /** |
|
| 1256 | - * @param string $field_name |
|
| 1257 | - * @param string $dt_frmt |
|
| 1258 | - * @param string $tm_frmt |
|
| 1259 | - * @throws \EE_Error |
|
| 1260 | - */ |
|
| 1261 | - public function e_datetime($field_name, $dt_frmt = '', $tm_frmt = '') |
|
| 1262 | - { |
|
| 1263 | - $this->_get_datetime($field_name, $dt_frmt, $tm_frmt, null, true); |
|
| 1264 | - } |
|
| 1265 | - |
|
| 1266 | - |
|
| 1267 | - |
|
| 1268 | - /** |
|
| 1269 | - * Get the i8ln value for a date using the WordPress @see date_i18n function. |
|
| 1270 | - * |
|
| 1271 | - * @param string $field_name The EE_Datetime_Field reference for the date being retrieved. |
|
| 1272 | - * @param string $format PHP valid date/time string format. If none is provided then the internal set format |
|
| 1273 | - * on the object will be used. |
|
| 1274 | - * @return string Date and time string in set locale or false if no field exists for the given |
|
| 1275 | - * @throws \EE_Error |
|
| 1276 | - * field name. |
|
| 1277 | - */ |
|
| 1278 | - public function get_i18n_datetime($field_name, $format = '') |
|
| 1279 | - { |
|
| 1280 | - $format = empty($format) ? $this->_dt_frmt . ' ' . $this->_tm_frmt : $format; |
|
| 1281 | - return date_i18n( |
|
| 1282 | - $format, |
|
| 1283 | - EEH_DTT_Helper::get_timestamp_with_offset($this->get_raw($field_name), $this->_timezone) |
|
| 1284 | - ); |
|
| 1285 | - } |
|
| 1286 | - |
|
| 1287 | - |
|
| 1288 | - |
|
| 1289 | - /** |
|
| 1290 | - * This method validates whether the given field name is a valid field on the model object as well as it is of a |
|
| 1291 | - * type EE_Datetime_Field. On success there will be returned the field settings. On fail an EE_Error exception is |
|
| 1292 | - * thrown. |
|
| 1293 | - * |
|
| 1294 | - * @param string $field_name The field name being checked |
|
| 1295 | - * @throws EE_Error |
|
| 1296 | - * @return EE_Datetime_Field |
|
| 1297 | - */ |
|
| 1298 | - protected function _get_dtt_field_settings($field_name) |
|
| 1299 | - { |
|
| 1300 | - $field = $this->get_model()->field_settings_for($field_name); |
|
| 1301 | - //check if field is dtt |
|
| 1302 | - if ($field instanceof EE_Datetime_Field) { |
|
| 1303 | - return $field; |
|
| 1304 | - } else { |
|
| 1305 | - throw new EE_Error(sprintf(__('The field name "%s" has been requested for the EE_Base_Class datetime functions and it is not a valid EE_Datetime_Field. Please check the spelling of the field and make sure it has been setup as a EE_Datetime_Field in the %s model constructor', |
|
| 1306 | - 'event_espresso'), $field_name, self::_get_model_classname(get_class($this)))); |
|
| 1307 | - } |
|
| 1308 | - } |
|
| 1309 | - |
|
| 1310 | - |
|
| 1311 | - |
|
| 1312 | - |
|
| 1313 | - /** |
|
| 1314 | - * NOTE ABOUT BELOW: |
|
| 1315 | - * These convenience date and time setters are for setting date and time independently. In other words you might |
|
| 1316 | - * want to change the time on a datetime_field but leave the date the same (or vice versa). IF on the other hand |
|
| 1317 | - * you want to set both date and time at the same time, you can just use the models default set($fieldname,$value) |
|
| 1318 | - * method and make sure you send the entire datetime value for setting. |
|
| 1319 | - */ |
|
| 1320 | - /** |
|
| 1321 | - * sets the time on a datetime property |
|
| 1322 | - * |
|
| 1323 | - * @access protected |
|
| 1324 | - * @param string|Datetime $time a valid time string for php datetime functions (or DateTime object) |
|
| 1325 | - * @param string $fieldname the name of the field the time is being set on (must match a EE_Datetime_Field) |
|
| 1326 | - * @throws \EE_Error |
|
| 1327 | - */ |
|
| 1328 | - protected function _set_time_for($time, $fieldname) |
|
| 1329 | - { |
|
| 1330 | - $this->_set_date_time('T', $time, $fieldname); |
|
| 1331 | - } |
|
| 1332 | - |
|
| 1333 | - |
|
| 1334 | - |
|
| 1335 | - /** |
|
| 1336 | - * sets the date on a datetime property |
|
| 1337 | - * |
|
| 1338 | - * @access protected |
|
| 1339 | - * @param string|DateTime $date a valid date string for php datetime functions ( or DateTime object) |
|
| 1340 | - * @param string $fieldname the name of the field the date is being set on (must match a EE_Datetime_Field) |
|
| 1341 | - * @throws \EE_Error |
|
| 1342 | - */ |
|
| 1343 | - protected function _set_date_for($date, $fieldname) |
|
| 1344 | - { |
|
| 1345 | - $this->_set_date_time('D', $date, $fieldname); |
|
| 1346 | - } |
|
| 1347 | - |
|
| 1348 | - |
|
| 1349 | - |
|
| 1350 | - /** |
|
| 1351 | - * This takes care of setting a date or time independently on a given model object property. This method also |
|
| 1352 | - * verifies that the given fieldname matches a model object property and is for a EE_Datetime_Field field |
|
| 1353 | - * |
|
| 1354 | - * @access protected |
|
| 1355 | - * @param string $what "T" for time, 'B' for both, 'D' for Date. |
|
| 1356 | - * @param string|DateTime $datetime_value A valid Date or Time string (or DateTime object) |
|
| 1357 | - * @param string $fieldname the name of the field the date OR time is being set on (must match a |
|
| 1358 | - * EE_Datetime_Field property) |
|
| 1359 | - * @throws \EE_Error |
|
| 1360 | - */ |
|
| 1361 | - protected function _set_date_time($what = 'T', $datetime_value, $fieldname) |
|
| 1362 | - { |
|
| 1363 | - $field = $this->_get_dtt_field_settings($fieldname); |
|
| 1364 | - $field->set_timezone($this->_timezone); |
|
| 1365 | - $field->set_date_format($this->_dt_frmt); |
|
| 1366 | - $field->set_time_format($this->_tm_frmt); |
|
| 1367 | - switch ($what) { |
|
| 1368 | - case 'T' : |
|
| 1369 | - $this->_fields[$fieldname] = $field->prepare_for_set_with_new_time( |
|
| 1370 | - $datetime_value, |
|
| 1371 | - $this->_fields[$fieldname] |
|
| 1372 | - ); |
|
| 1373 | - break; |
|
| 1374 | - case 'D' : |
|
| 1375 | - $this->_fields[$fieldname] = $field->prepare_for_set_with_new_date( |
|
| 1376 | - $datetime_value, |
|
| 1377 | - $this->_fields[$fieldname] |
|
| 1378 | - ); |
|
| 1379 | - break; |
|
| 1380 | - case 'B' : |
|
| 1381 | - $this->_fields[$fieldname] = $field->prepare_for_set($datetime_value); |
|
| 1382 | - break; |
|
| 1383 | - } |
|
| 1384 | - $this->_clear_cached_property($fieldname); |
|
| 1385 | - } |
|
| 1386 | - |
|
| 1387 | - |
|
| 1388 | - |
|
| 1389 | - /** |
|
| 1390 | - * This will return a timestamp for the website timezone but ONLY when the current website timezone is different |
|
| 1391 | - * than the timezone set for the website. NOTE, this currently only works well with methods that return values. If |
|
| 1392 | - * you use it with methods that echo values the $_timestamp property may not get reset to its original value and |
|
| 1393 | - * that could lead to some unexpected results! |
|
| 1394 | - * |
|
| 1395 | - * @access public |
|
| 1396 | - * @param string $field_name This is the name of the field on the object that contains the date/time |
|
| 1397 | - * value being returned. |
|
| 1398 | - * @param string $callback must match a valid method in this class (defaults to get_datetime) |
|
| 1399 | - * @param mixed (array|string) $args This is the arguments that will be passed to the callback. |
|
| 1400 | - * @param string $prepend You can include something to prepend on the timestamp |
|
| 1401 | - * @param string $append You can include something to append on the timestamp |
|
| 1402 | - * @throws EE_Error |
|
| 1403 | - * @return string timestamp |
|
| 1404 | - */ |
|
| 1405 | - public function display_in_my_timezone( |
|
| 1406 | - $field_name, |
|
| 1407 | - $callback = 'get_datetime', |
|
| 1408 | - $args = null, |
|
| 1409 | - $prepend = '', |
|
| 1410 | - $append = '' |
|
| 1411 | - ) { |
|
| 1412 | - $timezone = EEH_DTT_Helper::get_timezone(); |
|
| 1413 | - if ($timezone === $this->_timezone) { |
|
| 1414 | - return ''; |
|
| 1415 | - } |
|
| 1416 | - $original_timezone = $this->_timezone; |
|
| 1417 | - $this->set_timezone($timezone); |
|
| 1418 | - $fn = (array)$field_name; |
|
| 1419 | - $args = array_merge($fn, (array)$args); |
|
| 1420 | - if ( ! method_exists($this, $callback)) { |
|
| 1421 | - throw new EE_Error( |
|
| 1422 | - sprintf( |
|
| 1423 | - __( |
|
| 1424 | - 'The method named "%s" given as the callback param in "display_in_my_timezone" does not exist. Please check your spelling', |
|
| 1425 | - 'event_espresso' |
|
| 1426 | - ), |
|
| 1427 | - $callback |
|
| 1428 | - ) |
|
| 1429 | - ); |
|
| 1430 | - } |
|
| 1431 | - $args = (array)$args; |
|
| 1432 | - $return = $prepend . call_user_func_array(array($this, $callback), $args) . $append; |
|
| 1433 | - $this->set_timezone($original_timezone); |
|
| 1434 | - return $return; |
|
| 1435 | - } |
|
| 1436 | - |
|
| 1437 | - |
|
| 1438 | - |
|
| 1439 | - /** |
|
| 1440 | - * Deletes this model object. |
|
| 1441 | - * This calls the `EE_Base_Class::_delete` method. Child classes wishing to change default behaviour should |
|
| 1442 | - * override |
|
| 1443 | - * `EE_Base_Class::_delete` NOT this class. |
|
| 1444 | - * |
|
| 1445 | - * @return boolean | int |
|
| 1446 | - * @throws \EE_Error |
|
| 1447 | - */ |
|
| 1448 | - public function delete() |
|
| 1449 | - { |
|
| 1450 | - /** |
|
| 1451 | - * Called just before the `EE_Base_Class::_delete` method call. |
|
| 1452 | - * Note: `EE_Base_Class::_delete` might be overridden by child classes so any client code hooking into these actions |
|
| 1453 | - * should be aware that `_delete` may not always result in a permanent delete. For example, `EE_Soft_Delete_Base_Class::_delete` |
|
| 1454 | - * soft deletes (trash) the object and does not permanently delete it. |
|
| 1455 | - * |
|
| 1456 | - * @param EE_Base_Class $model_object about to be 'deleted' |
|
| 1457 | - */ |
|
| 1458 | - do_action('AHEE__EE_Base_Class__delete__before', $this); |
|
| 1459 | - $result = $this->_delete(); |
|
| 1460 | - /** |
|
| 1461 | - * Called just after the `EE_Base_Class::_delete` method call. |
|
| 1462 | - * Note: `EE_Base_Class::_delete` might be overridden by child classes so any client code hooking into these actions |
|
| 1463 | - * should be aware that `_delete` may not always result in a permanent delete. For example `EE_Soft_Base_Class::_delete` |
|
| 1464 | - * soft deletes (trash) the object and does not permanently delete it. |
|
| 1465 | - * |
|
| 1466 | - * @param EE_Base_Class $model_object that was just 'deleted' |
|
| 1467 | - * @param boolean $result |
|
| 1468 | - */ |
|
| 1469 | - do_action('AHEE__EE_Base_Class__delete__end', $this, $result); |
|
| 1470 | - return $result; |
|
| 1471 | - } |
|
| 1472 | - |
|
| 1473 | - |
|
| 1474 | - |
|
| 1475 | - /** |
|
| 1476 | - * Calls the specific delete method for the instantiated class. |
|
| 1477 | - * This method is called by the public `EE_Base_Class::delete` method. Any child classes desiring to override |
|
| 1478 | - * default functionality for "delete" (which is to call `permanently_delete`) should override this method NOT |
|
| 1479 | - * `EE_Base_Class::delete` |
|
| 1480 | - * |
|
| 1481 | - * @return bool|int |
|
| 1482 | - * @throws \EE_Error |
|
| 1483 | - */ |
|
| 1484 | - protected function _delete() |
|
| 1485 | - { |
|
| 1486 | - return $this->delete_permanently(); |
|
| 1487 | - } |
|
| 1488 | - |
|
| 1489 | - |
|
| 1490 | - |
|
| 1491 | - /** |
|
| 1492 | - * Deletes this model object permanently from db (but keep in mind related models my block the delete and return an |
|
| 1493 | - * error) |
|
| 1494 | - * |
|
| 1495 | - * @return bool | int |
|
| 1496 | - * @throws \EE_Error |
|
| 1497 | - */ |
|
| 1498 | - public function delete_permanently() |
|
| 1499 | - { |
|
| 1500 | - /** |
|
| 1501 | - * Called just before HARD deleting a model object |
|
| 1502 | - * |
|
| 1503 | - * @param EE_Base_Class $model_object about to be 'deleted' |
|
| 1504 | - */ |
|
| 1505 | - do_action('AHEE__EE_Base_Class__delete_permanently__before', $this); |
|
| 1506 | - $model = $this->get_model(); |
|
| 1507 | - $result = $model->delete_permanently_by_ID($this->ID()); |
|
| 1508 | - $this->refresh_cache_of_related_objects(); |
|
| 1509 | - /** |
|
| 1510 | - * Called just after HARD deleting a model object |
|
| 1511 | - * |
|
| 1512 | - * @param EE_Base_Class $model_object that was just 'deleted' |
|
| 1513 | - * @param boolean $result |
|
| 1514 | - */ |
|
| 1515 | - do_action('AHEE__EE_Base_Class__delete_permanently__end', $this, $result); |
|
| 1516 | - return $result; |
|
| 1517 | - } |
|
| 1518 | - |
|
| 1519 | - |
|
| 1520 | - |
|
| 1521 | - /** |
|
| 1522 | - * When this model object is deleted, it may still be cached on related model objects. This clears the cache of |
|
| 1523 | - * related model objects |
|
| 1524 | - * |
|
| 1525 | - * @throws \EE_Error |
|
| 1526 | - */ |
|
| 1527 | - public function refresh_cache_of_related_objects() |
|
| 1528 | - { |
|
| 1529 | - foreach ($this->get_model()->relation_settings() as $relation_name => $relation_obj) { |
|
| 1530 | - if ( ! empty($this->_model_relations[$relation_name])) { |
|
| 1531 | - $related_objects = $this->_model_relations[$relation_name]; |
|
| 1532 | - if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 1533 | - //this relation only stores a single model object, not an array |
|
| 1534 | - //but let's make it consistent |
|
| 1535 | - $related_objects = array($related_objects); |
|
| 1536 | - } |
|
| 1537 | - foreach ($related_objects as $related_object) { |
|
| 1538 | - //only refresh their cache if they're in memory |
|
| 1539 | - if ($related_object instanceof EE_Base_Class) { |
|
| 1540 | - $related_object->clear_cache($this->get_model()->get_this_model_name(), $this); |
|
| 1541 | - } |
|
| 1542 | - } |
|
| 1543 | - } |
|
| 1544 | - } |
|
| 1545 | - } |
|
| 1546 | - |
|
| 1547 | - |
|
| 1548 | - |
|
| 1549 | - /** |
|
| 1550 | - * Saves this object to the database. An array may be supplied to set some values on this |
|
| 1551 | - * object just before saving. |
|
| 1552 | - * |
|
| 1553 | - * @access public |
|
| 1554 | - * @param array $set_cols_n_values keys are field names, values are their new values, |
|
| 1555 | - * if provided during the save() method (often client code will change the fields' |
|
| 1556 | - * values before calling save) |
|
| 1557 | - * @throws \EE_Error |
|
| 1558 | - * @return int , 1 on a successful update, the ID of the new entry on insert; 0 on failure or if the model object |
|
| 1559 | - * isn't allowed to persist (as determined by EE_Base_Class::allow_persist()) |
|
| 1560 | - */ |
|
| 1561 | - public function save($set_cols_n_values = array()) |
|
| 1562 | - { |
|
| 1563 | - /** |
|
| 1564 | - * Filters the fields we're about to save on the model object |
|
| 1565 | - * |
|
| 1566 | - * @param array $set_cols_n_values |
|
| 1567 | - * @param EE_Base_Class $model_object |
|
| 1568 | - */ |
|
| 1569 | - $set_cols_n_values = (array)apply_filters('FHEE__EE_Base_Class__save__set_cols_n_values', $set_cols_n_values, |
|
| 1570 | - $this); |
|
| 1571 | - //set attributes as provided in $set_cols_n_values |
|
| 1572 | - foreach ($set_cols_n_values as $column => $value) { |
|
| 1573 | - $this->set($column, $value); |
|
| 1574 | - } |
|
| 1575 | - // no changes ? then don't do anything |
|
| 1576 | - if (! $this->_has_changes && $this->ID() && $this->get_model()->get_primary_key_field()->is_auto_increment()) { |
|
| 1577 | - return 0; |
|
| 1578 | - } |
|
| 1579 | - /** |
|
| 1580 | - * Saving a model object. |
|
| 1581 | - * Before we perform a save, this action is fired. |
|
| 1582 | - * |
|
| 1583 | - * @param EE_Base_Class $model_object the model object about to be saved. |
|
| 1584 | - */ |
|
| 1585 | - do_action('AHEE__EE_Base_Class__save__begin', $this); |
|
| 1586 | - if ( ! $this->allow_persist()) { |
|
| 1587 | - return 0; |
|
| 1588 | - } |
|
| 1589 | - //now get current attribute values |
|
| 1590 | - $save_cols_n_values = $this->_fields; |
|
| 1591 | - //if the object already has an ID, update it. Otherwise, insert it |
|
| 1592 | - //also: change the assumption about values passed to the model NOT being prepare dby the model object. They have been |
|
| 1593 | - $old_assumption_concerning_value_preparation = $this->get_model() |
|
| 1594 | - ->get_assumption_concerning_values_already_prepared_by_model_object(); |
|
| 1595 | - $this->get_model()->assume_values_already_prepared_by_model_object(true); |
|
| 1596 | - //does this model have an autoincrement PK? |
|
| 1597 | - if ($this->get_model()->has_primary_key_field()) { |
|
| 1598 | - if ($this->get_model()->get_primary_key_field()->is_auto_increment()) { |
|
| 1599 | - //ok check if it's set, if so: update; if not, insert |
|
| 1600 | - if ( ! empty($save_cols_n_values[self::_get_primary_key_name(get_class($this))])) { |
|
| 1601 | - $results = $this->get_model()->update_by_ID($save_cols_n_values, $this->ID()); |
|
| 1602 | - } else { |
|
| 1603 | - unset($save_cols_n_values[self::_get_primary_key_name(get_class($this))]); |
|
| 1604 | - $results = $this->get_model()->insert($save_cols_n_values); |
|
| 1605 | - if ($results) { |
|
| 1606 | - //if successful, set the primary key |
|
| 1607 | - //but don't use the normal SET method, because it will check if |
|
| 1608 | - //an item with the same ID exists in the mapper & db, then |
|
| 1609 | - //will find it in the db (because we just added it) and THAT object |
|
| 1610 | - //will get added to the mapper before we can add this one! |
|
| 1611 | - //but if we just avoid using the SET method, all that headache can be avoided |
|
| 1612 | - $pk_field_name = self::_get_primary_key_name(get_class($this)); |
|
| 1613 | - $this->_fields[$pk_field_name] = $results; |
|
| 1614 | - $this->_clear_cached_property($pk_field_name); |
|
| 1615 | - $this->get_model()->add_to_entity_map($this); |
|
| 1616 | - $this->_update_cached_related_model_objs_fks(); |
|
| 1617 | - } |
|
| 1618 | - } |
|
| 1619 | - } else {//PK is NOT auto-increment |
|
| 1620 | - //so check if one like it already exists in the db |
|
| 1621 | - if ($this->get_model()->exists_by_ID($this->ID())) { |
|
| 1622 | - if (WP_DEBUG && ! $this->in_entity_map()) { |
|
| 1623 | - throw new EE_Error( |
|
| 1624 | - sprintf( |
|
| 1625 | - __('Using a model object %1$s that is NOT in the entity map, can lead to unexpected errors. You should either: %4$s 1. Put it in the entity mapper by calling %2$s %4$s 2. Discard this model object and use what is in the entity mapper %4$s 3. Fetch from the database using %3$s', |
|
| 1626 | - 'event_espresso'), |
|
| 1627 | - get_class($this), |
|
| 1628 | - get_class($this->get_model()) . '::instance()->add_to_entity_map()', |
|
| 1629 | - get_class($this->get_model()) . '::instance()->get_one_by_ID()', |
|
| 1630 | - '<br />' |
|
| 1631 | - ) |
|
| 1632 | - ); |
|
| 1633 | - } |
|
| 1634 | - $results = $this->get_model()->update_by_ID($save_cols_n_values, $this->ID()); |
|
| 1635 | - } else { |
|
| 1636 | - $results = $this->get_model()->insert($save_cols_n_values); |
|
| 1637 | - $this->_update_cached_related_model_objs_fks(); |
|
| 1638 | - } |
|
| 1639 | - } |
|
| 1640 | - } else {//there is NO primary key |
|
| 1641 | - $already_in_db = false; |
|
| 1642 | - foreach ($this->get_model()->unique_indexes() as $index) { |
|
| 1643 | - $uniqueness_where_params = array_intersect_key($save_cols_n_values, $index->fields()); |
|
| 1644 | - if ($this->get_model()->exists(array($uniqueness_where_params))) { |
|
| 1645 | - $already_in_db = true; |
|
| 1646 | - } |
|
| 1647 | - } |
|
| 1648 | - if ($already_in_db) { |
|
| 1649 | - $combined_pk_fields_n_values = array_intersect_key($save_cols_n_values, |
|
| 1650 | - $this->get_model()->get_combined_primary_key_fields()); |
|
| 1651 | - $results = $this->get_model()->update($save_cols_n_values, $combined_pk_fields_n_values); |
|
| 1652 | - } else { |
|
| 1653 | - $results = $this->get_model()->insert($save_cols_n_values); |
|
| 1654 | - } |
|
| 1655 | - } |
|
| 1656 | - //restore the old assumption about values being prepared by the model object |
|
| 1657 | - $this->get_model() |
|
| 1658 | - ->assume_values_already_prepared_by_model_object($old_assumption_concerning_value_preparation); |
|
| 1659 | - /** |
|
| 1660 | - * After saving the model object this action is called |
|
| 1661 | - * |
|
| 1662 | - * @param EE_Base_Class $model_object which was just saved |
|
| 1663 | - * @param boolean|int $results if it were updated, TRUE or FALSE; if it were newly inserted |
|
| 1664 | - * the new ID (or 0 if an error occurred and it wasn't updated) |
|
| 1665 | - */ |
|
| 1666 | - do_action('AHEE__EE_Base_Class__save__end', $this, $results); |
|
| 1667 | - $this->_has_changes = false; |
|
| 1668 | - return $results; |
|
| 1669 | - } |
|
| 1670 | - |
|
| 1671 | - |
|
| 1672 | - |
|
| 1673 | - /** |
|
| 1674 | - * Updates the foreign key on related models objects pointing to this to have this model object's ID |
|
| 1675 | - * as their foreign key. If the cached related model objects already exist in the db, saves them (so that the DB |
|
| 1676 | - * is consistent) Especially useful in case we JUST added this model object ot the database and we want to let its |
|
| 1677 | - * cached relations with foreign keys to it know about that change. Eg: we've created a transaction but haven't |
|
| 1678 | - * saved it to the db. We also create a registration and don't save it to the DB, but we DO cache it on the |
|
| 1679 | - * transaction. Now, when we save the transaction, the registration's TXN_ID will be automatically updated, whether |
|
| 1680 | - * or not they exist in the DB (if they do, their DB records will be automatically updated) |
|
| 1681 | - * |
|
| 1682 | - * @return void |
|
| 1683 | - * @throws \EE_Error |
|
| 1684 | - */ |
|
| 1685 | - protected function _update_cached_related_model_objs_fks() |
|
| 1686 | - { |
|
| 1687 | - foreach ($this->get_model()->relation_settings() as $relation_name => $relation_obj) { |
|
| 1688 | - if ($relation_obj instanceof EE_Has_Many_Relation) { |
|
| 1689 | - foreach ($this->get_all_from_cache($relation_name) as $related_model_obj_in_cache) { |
|
| 1690 | - $fk_to_this = $related_model_obj_in_cache->get_model()->get_foreign_key_to( |
|
| 1691 | - $this->get_model()->get_this_model_name() |
|
| 1692 | - ); |
|
| 1693 | - $related_model_obj_in_cache->set($fk_to_this->get_name(), $this->ID()); |
|
| 1694 | - if ($related_model_obj_in_cache->ID()) { |
|
| 1695 | - $related_model_obj_in_cache->save(); |
|
| 1696 | - } |
|
| 1697 | - } |
|
| 1698 | - } |
|
| 1699 | - } |
|
| 1700 | - } |
|
| 1701 | - |
|
| 1702 | - |
|
| 1703 | - |
|
| 1704 | - /** |
|
| 1705 | - * Saves this model object and its NEW cached relations to the database. |
|
| 1706 | - * (Meaning, for now, IT DOES NOT WORK if the cached items already exist in the DB. |
|
| 1707 | - * In order for that to work, we would need to mark model objects as dirty/clean... |
|
| 1708 | - * because otherwise, there's a potential for infinite looping of saving |
|
| 1709 | - * Saves the cached related model objects, and ensures the relation between them |
|
| 1710 | - * and this object and properly setup |
|
| 1711 | - * |
|
| 1712 | - * @return int ID of new model object on save; 0 on failure+ |
|
| 1713 | - * @throws \EE_Error |
|
| 1714 | - */ |
|
| 1715 | - public function save_new_cached_related_model_objs() |
|
| 1716 | - { |
|
| 1717 | - //make sure this has been saved |
|
| 1718 | - if ( ! $this->ID()) { |
|
| 1719 | - $id = $this->save(); |
|
| 1720 | - } else { |
|
| 1721 | - $id = $this->ID(); |
|
| 1722 | - } |
|
| 1723 | - //now save all the NEW cached model objects (ie they don't exist in the DB) |
|
| 1724 | - foreach ($this->get_model()->relation_settings() as $relationName => $relationObj) { |
|
| 1725 | - if ($this->_model_relations[$relationName]) { |
|
| 1726 | - //is this a relation where we should expect just ONE related object (ie, EE_Belongs_To_relation) |
|
| 1727 | - //or MANY related objects (ie, EE_HABTM_Relation or EE_Has_Many_Relation)? |
|
| 1728 | - if ($relationObj instanceof EE_Belongs_To_Relation) { |
|
| 1729 | - //add a relation to that relation type (which saves the appropriate thing in the process) |
|
| 1730 | - //but ONLY if it DOES NOT exist in the DB |
|
| 1731 | - /* @var $related_model_obj EE_Base_Class */ |
|
| 1732 | - $related_model_obj = $this->_model_relations[$relationName]; |
|
| 1733 | - // if( ! $related_model_obj->ID()){ |
|
| 1734 | - $this->_add_relation_to($related_model_obj, $relationName); |
|
| 1735 | - $related_model_obj->save_new_cached_related_model_objs(); |
|
| 1736 | - // } |
|
| 1737 | - } else { |
|
| 1738 | - foreach ($this->_model_relations[$relationName] as $related_model_obj) { |
|
| 1739 | - //add a relation to that relation type (which saves the appropriate thing in the process) |
|
| 1740 | - //but ONLY if it DOES NOT exist in the DB |
|
| 1741 | - // if( ! $related_model_obj->ID()){ |
|
| 1742 | - $this->_add_relation_to($related_model_obj, $relationName); |
|
| 1743 | - $related_model_obj->save_new_cached_related_model_objs(); |
|
| 1744 | - // } |
|
| 1745 | - } |
|
| 1746 | - } |
|
| 1747 | - } |
|
| 1748 | - } |
|
| 1749 | - return $id; |
|
| 1750 | - } |
|
| 1751 | - |
|
| 1752 | - |
|
| 1753 | - |
|
| 1754 | - /** |
|
| 1755 | - * for getting a model while instantiated. |
|
| 1756 | - * |
|
| 1757 | - * @return \EEM_Base | \EEM_CPT_Base |
|
| 1758 | - */ |
|
| 1759 | - public function get_model() |
|
| 1760 | - { |
|
| 1761 | - $modelName = self::_get_model_classname(get_class($this)); |
|
| 1762 | - return self::_get_model_instance_with_name($modelName, $this->_timezone); |
|
| 1763 | - } |
|
| 1764 | - |
|
| 1765 | - |
|
| 1766 | - |
|
| 1767 | - /** |
|
| 1768 | - * @param $props_n_values |
|
| 1769 | - * @param $classname |
|
| 1770 | - * @return mixed bool|EE_Base_Class|EEM_CPT_Base |
|
| 1771 | - * @throws \EE_Error |
|
| 1772 | - */ |
|
| 1773 | - protected static function _get_object_from_entity_mapper($props_n_values, $classname) |
|
| 1774 | - { |
|
| 1775 | - //TODO: will not work for Term_Relationships because they have no PK! |
|
| 1776 | - $primary_id_ref = self::_get_primary_key_name($classname); |
|
| 1777 | - if (array_key_exists($primary_id_ref, $props_n_values) && ! empty($props_n_values[$primary_id_ref])) { |
|
| 1778 | - $id = $props_n_values[$primary_id_ref]; |
|
| 1779 | - return self::_get_model($classname)->get_from_entity_map($id); |
|
| 1780 | - } |
|
| 1781 | - return false; |
|
| 1782 | - } |
|
| 1783 | - |
|
| 1784 | - |
|
| 1785 | - |
|
| 1786 | - /** |
|
| 1787 | - * This is called by child static "new_instance" method and we'll check to see if there is an existing db entry for |
|
| 1788 | - * the primary key (if present in incoming values). If there is a key in the incoming array that matches the |
|
| 1789 | - * primary key for the model AND it is not null, then we check the db. If there's a an object we return it. If not |
|
| 1790 | - * we return false. |
|
| 1791 | - * |
|
| 1792 | - * @param array $props_n_values incoming array of properties and their values |
|
| 1793 | - * @param string $classname the classname of the child class |
|
| 1794 | - * @param null $timezone |
|
| 1795 | - * @param array $date_formats incoming date_formats in an array where the first value is the |
|
| 1796 | - * date_format and the second value is the time format |
|
| 1797 | - * @return mixed (EE_Base_Class|bool) |
|
| 1798 | - * @throws \EE_Error |
|
| 1799 | - */ |
|
| 1800 | - protected static function _check_for_object($props_n_values, $classname, $timezone = null, $date_formats = array()) |
|
| 1801 | - { |
|
| 1802 | - $existing = null; |
|
| 1803 | - if (self::_get_model($classname)->has_primary_key_field()) { |
|
| 1804 | - $primary_id_ref = self::_get_primary_key_name($classname); |
|
| 1805 | - if (array_key_exists($primary_id_ref, $props_n_values) |
|
| 1806 | - && ! empty($props_n_values[$primary_id_ref]) |
|
| 1807 | - ) { |
|
| 1808 | - $existing = self::_get_model($classname, $timezone)->get_one_by_ID( |
|
| 1809 | - $props_n_values[$primary_id_ref] |
|
| 1810 | - ); |
|
| 1811 | - } |
|
| 1812 | - } elseif (self::_get_model($classname, $timezone)->has_all_combined_primary_key_fields($props_n_values)) { |
|
| 1813 | - //no primary key on this model, but there's still a matching item in the DB |
|
| 1814 | - $existing = self::_get_model($classname, $timezone)->get_one_by_ID( |
|
| 1815 | - self::_get_model($classname, $timezone)->get_index_primary_key_string($props_n_values) |
|
| 1816 | - ); |
|
| 1817 | - } |
|
| 1818 | - if ($existing) { |
|
| 1819 | - //set date formats if present before setting values |
|
| 1820 | - if ( ! empty($date_formats) && is_array($date_formats)) { |
|
| 1821 | - $existing->set_date_format($date_formats[0]); |
|
| 1822 | - $existing->set_time_format($date_formats[1]); |
|
| 1823 | - } else { |
|
| 1824 | - //set default formats for date and time |
|
| 1825 | - $existing->set_date_format(get_option('date_format')); |
|
| 1826 | - $existing->set_time_format(get_option('time_format')); |
|
| 1827 | - } |
|
| 1828 | - foreach ($props_n_values as $property => $field_value) { |
|
| 1829 | - $existing->set($property, $field_value); |
|
| 1830 | - } |
|
| 1831 | - return $existing; |
|
| 1832 | - } else { |
|
| 1833 | - return false; |
|
| 1834 | - } |
|
| 1835 | - } |
|
| 1836 | - |
|
| 1837 | - |
|
| 1838 | - |
|
| 1839 | - /** |
|
| 1840 | - * Gets the EEM_*_Model for this class |
|
| 1841 | - * |
|
| 1842 | - * @access public now, as this is more convenient |
|
| 1843 | - * @param $classname |
|
| 1844 | - * @param null $timezone |
|
| 1845 | - * @throws EE_Error |
|
| 1846 | - * @return EEM_Base |
|
| 1847 | - */ |
|
| 1848 | - protected static function _get_model($classname, $timezone = null) |
|
| 1849 | - { |
|
| 1850 | - //find model for this class |
|
| 1851 | - if ( ! $classname) { |
|
| 1852 | - throw new EE_Error( |
|
| 1853 | - sprintf( |
|
| 1854 | - __( |
|
| 1855 | - "What were you thinking calling _get_model(%s)?? You need to specify the class name", |
|
| 1856 | - "event_espresso" |
|
| 1857 | - ), |
|
| 1858 | - $classname |
|
| 1859 | - ) |
|
| 1860 | - ); |
|
| 1861 | - } |
|
| 1862 | - $modelName = self::_get_model_classname($classname); |
|
| 1863 | - return self::_get_model_instance_with_name($modelName, $timezone); |
|
| 1864 | - } |
|
| 1865 | - |
|
| 1866 | - |
|
| 1867 | - |
|
| 1868 | - /** |
|
| 1869 | - * Gets the model instance (eg instance of EEM_Attendee) given its classname (eg EE_Attendee) |
|
| 1870 | - * |
|
| 1871 | - * @param string $model_classname |
|
| 1872 | - * @param null $timezone |
|
| 1873 | - * @return EEM_Base |
|
| 1874 | - */ |
|
| 1875 | - protected static function _get_model_instance_with_name($model_classname, $timezone = null) |
|
| 1876 | - { |
|
| 1877 | - $model_classname = str_replace('EEM_', '', $model_classname); |
|
| 1878 | - $model = EE_Registry::instance()->load_model($model_classname); |
|
| 1879 | - $model->set_timezone($timezone); |
|
| 1880 | - return $model; |
|
| 1881 | - } |
|
| 1882 | - |
|
| 1883 | - |
|
| 1884 | - |
|
| 1885 | - /** |
|
| 1886 | - * If a model name is provided (eg Registration), gets the model classname for that model. |
|
| 1887 | - * Also works if a model class's classname is provided (eg EE_Registration). |
|
| 1888 | - * |
|
| 1889 | - * @param null $model_name |
|
| 1890 | - * @return string like EEM_Attendee |
|
| 1891 | - */ |
|
| 1892 | - private static function _get_model_classname($model_name = null) |
|
| 1893 | - { |
|
| 1894 | - if (strpos($model_name, "EE_") === 0) { |
|
| 1895 | - $model_classname = str_replace("EE_", "EEM_", $model_name); |
|
| 1896 | - } else { |
|
| 1897 | - $model_classname = "EEM_" . $model_name; |
|
| 1898 | - } |
|
| 1899 | - return $model_classname; |
|
| 1900 | - } |
|
| 1901 | - |
|
| 1902 | - |
|
| 1903 | - |
|
| 1904 | - /** |
|
| 1905 | - * returns the name of the primary key attribute |
|
| 1906 | - * |
|
| 1907 | - * @param null $classname |
|
| 1908 | - * @throws EE_Error |
|
| 1909 | - * @return string |
|
| 1910 | - */ |
|
| 1911 | - protected static function _get_primary_key_name($classname = null) |
|
| 1912 | - { |
|
| 1913 | - if ( ! $classname) { |
|
| 1914 | - throw new EE_Error( |
|
| 1915 | - sprintf( |
|
| 1916 | - __("What were you thinking calling _get_primary_key_name(%s)", "event_espresso"), |
|
| 1917 | - $classname |
|
| 1918 | - ) |
|
| 1919 | - ); |
|
| 1920 | - } |
|
| 1921 | - return self::_get_model($classname)->get_primary_key_field()->get_name(); |
|
| 1922 | - } |
|
| 1923 | - |
|
| 1924 | - |
|
| 1925 | - |
|
| 1926 | - /** |
|
| 1927 | - * Gets the value of the primary key. |
|
| 1928 | - * If the object hasn't yet been saved, it should be whatever the model field's default was |
|
| 1929 | - * (eg, if this were the EE_Event class, look at the primary key field on EEM_Event and see what its default value |
|
| 1930 | - * is. Usually defaults for integer primary keys are 0; string primary keys are usually NULL). |
|
| 1931 | - * |
|
| 1932 | - * @return mixed, if the primary key is of type INT it'll be an int. Otherwise it could be a string |
|
| 1933 | - * @throws \EE_Error |
|
| 1934 | - */ |
|
| 1935 | - public function ID() |
|
| 1936 | - { |
|
| 1937 | - //now that we know the name of the variable, use a variable variable to get its value and return its |
|
| 1938 | - if ($this->get_model()->has_primary_key_field()) { |
|
| 1939 | - return $this->_fields[self::_get_primary_key_name(get_class($this))]; |
|
| 1940 | - } else { |
|
| 1941 | - return $this->get_model()->get_index_primary_key_string($this->_fields); |
|
| 1942 | - } |
|
| 1943 | - } |
|
| 1944 | - |
|
| 1945 | - |
|
| 1946 | - |
|
| 1947 | - /** |
|
| 1948 | - * Adds a relationship to the specified EE_Base_Class object, given the relationship's name. Eg, if the current |
|
| 1949 | - * model is related to a group of events, the $relationName should be 'Event', and should be a key in the EE |
|
| 1950 | - * Model's $_model_relations array. If this model object doesn't exist in the DB, just caches the related thing |
|
| 1951 | - * |
|
| 1952 | - * @param mixed $otherObjectModelObjectOrID EE_Base_Class or the ID of the other object |
|
| 1953 | - * @param string $relationName eg 'Events','Question',etc. |
|
| 1954 | - * an attendee to a group, you also want to specify which role they |
|
| 1955 | - * will have in that group. So you would use this parameter to |
|
| 1956 | - * specify array('role-column-name'=>'role-id') |
|
| 1957 | - * @param array $extra_join_model_fields_n_values You can optionally include an array of key=>value pairs that |
|
| 1958 | - * allow you to further constrict the relation to being added. |
|
| 1959 | - * However, keep in mind that the columns (keys) given must match a |
|
| 1960 | - * column on the JOIN table and currently only the HABTM models |
|
| 1961 | - * accept these additional conditions. Also remember that if an |
|
| 1962 | - * exact match isn't found for these extra cols/val pairs, then a |
|
| 1963 | - * NEW row is created in the join table. |
|
| 1964 | - * @param null $cache_id |
|
| 1965 | - * @throws EE_Error |
|
| 1966 | - * @return EE_Base_Class the object the relation was added to |
|
| 1967 | - */ |
|
| 1968 | - public function _add_relation_to( |
|
| 1969 | - $otherObjectModelObjectOrID, |
|
| 1970 | - $relationName, |
|
| 1971 | - $extra_join_model_fields_n_values = array(), |
|
| 1972 | - $cache_id = null |
|
| 1973 | - ) { |
|
| 1974 | - //if this thing exists in the DB, save the relation to the DB |
|
| 1975 | - if ($this->ID()) { |
|
| 1976 | - $otherObject = $this->get_model() |
|
| 1977 | - ->add_relationship_to($this, $otherObjectModelObjectOrID, $relationName, |
|
| 1978 | - $extra_join_model_fields_n_values); |
|
| 1979 | - //clear cache so future get_many_related and get_first_related() return new results. |
|
| 1980 | - $this->clear_cache($relationName, $otherObject, true); |
|
| 1981 | - if ($otherObject instanceof EE_Base_Class) { |
|
| 1982 | - $otherObject->clear_cache($this->get_model()->get_this_model_name(), $this); |
|
| 1983 | - } |
|
| 1984 | - } else { |
|
| 1985 | - //this thing doesn't exist in the DB, so just cache it |
|
| 1986 | - if ( ! $otherObjectModelObjectOrID instanceof EE_Base_Class) { |
|
| 1987 | - throw new EE_Error(sprintf( |
|
| 1988 | - __('Before a model object is saved to the database, calls to _add_relation_to must be passed an actual object, not just an ID. You provided %s as the model object to a %s', |
|
| 1989 | - 'event_espresso'), |
|
| 1990 | - $otherObjectModelObjectOrID, |
|
| 1991 | - get_class($this) |
|
| 1992 | - )); |
|
| 1993 | - } else { |
|
| 1994 | - $otherObject = $otherObjectModelObjectOrID; |
|
| 1995 | - } |
|
| 1996 | - $this->cache($relationName, $otherObjectModelObjectOrID, $cache_id); |
|
| 1997 | - } |
|
| 1998 | - if ($otherObject instanceof EE_Base_Class) { |
|
| 1999 | - //fix the reciprocal relation too |
|
| 2000 | - if ($otherObject->ID()) { |
|
| 2001 | - //its saved so assumed relations exist in the DB, so we can just |
|
| 2002 | - //clear the cache so future queries use the updated info in the DB |
|
| 2003 | - $otherObject->clear_cache($this->get_model()->get_this_model_name(), null, true); |
|
| 2004 | - } else { |
|
| 2005 | - //it's not saved, so it caches relations like this |
|
| 2006 | - $otherObject->cache($this->get_model()->get_this_model_name(), $this); |
|
| 2007 | - } |
|
| 2008 | - } |
|
| 2009 | - return $otherObject; |
|
| 2010 | - } |
|
| 2011 | - |
|
| 2012 | - |
|
| 2013 | - |
|
| 2014 | - /** |
|
| 2015 | - * Removes a relationship to the specified EE_Base_Class object, given the relationships' name. Eg, if the current |
|
| 2016 | - * model is related to a group of events, the $relationName should be 'Events', and should be a key in the EE |
|
| 2017 | - * Model's $_model_relations array. If this model object doesn't exist in the DB, just removes the related thing |
|
| 2018 | - * from the cache |
|
| 2019 | - * |
|
| 2020 | - * @param mixed $otherObjectModelObjectOrID |
|
| 2021 | - * EE_Base_Class or the ID of the other object, OR an array key into the cache if this isn't saved |
|
| 2022 | - * to the DB yet |
|
| 2023 | - * @param string $relationName |
|
| 2024 | - * @param array $where_query |
|
| 2025 | - * You can optionally include an array of key=>value pairs that allow you to further constrict the |
|
| 2026 | - * relation to being added. However, keep in mind that the columns (keys) given must match a column |
|
| 2027 | - * on the JOIN table and currently only the HABTM models accept these additional conditions. Also |
|
| 2028 | - * remember that if an exact match isn't found for these extra cols/val pairs, then a NEW row is |
|
| 2029 | - * created in the join table. |
|
| 2030 | - * @return EE_Base_Class the relation was removed from |
|
| 2031 | - * @throws \EE_Error |
|
| 2032 | - */ |
|
| 2033 | - public function _remove_relation_to($otherObjectModelObjectOrID, $relationName, $where_query = array()) |
|
| 2034 | - { |
|
| 2035 | - if ($this->ID()) { |
|
| 2036 | - //if this exists in the DB, save the relation change to the DB too |
|
| 2037 | - $otherObject = $this->get_model() |
|
| 2038 | - ->remove_relationship_to($this, $otherObjectModelObjectOrID, $relationName, |
|
| 2039 | - $where_query); |
|
| 2040 | - $this->clear_cache($relationName, $otherObject); |
|
| 2041 | - } else { |
|
| 2042 | - //this doesn't exist in the DB, just remove it from the cache |
|
| 2043 | - $otherObject = $this->clear_cache($relationName, $otherObjectModelObjectOrID); |
|
| 2044 | - } |
|
| 2045 | - if ($otherObject instanceof EE_Base_Class) { |
|
| 2046 | - $otherObject->clear_cache($this->get_model()->get_this_model_name(), $this); |
|
| 2047 | - } |
|
| 2048 | - return $otherObject; |
|
| 2049 | - } |
|
| 2050 | - |
|
| 2051 | - |
|
| 2052 | - |
|
| 2053 | - /** |
|
| 2054 | - * Removes ALL the related things for the $relationName. |
|
| 2055 | - * |
|
| 2056 | - * @param string $relationName |
|
| 2057 | - * @param array $where_query_params like EEM_Base::get_all's $query_params[0] (where conditions) |
|
| 2058 | - * @return EE_Base_Class |
|
| 2059 | - * @throws \EE_Error |
|
| 2060 | - */ |
|
| 2061 | - public function _remove_relations($relationName, $where_query_params = array()) |
|
| 2062 | - { |
|
| 2063 | - if ($this->ID()) { |
|
| 2064 | - //if this exists in the DB, save the relation change to the DB too |
|
| 2065 | - $otherObjects = $this->get_model()->remove_relations($this, $relationName, $where_query_params); |
|
| 2066 | - $this->clear_cache($relationName, null, true); |
|
| 2067 | - } else { |
|
| 2068 | - //this doesn't exist in the DB, just remove it from the cache |
|
| 2069 | - $otherObjects = $this->clear_cache($relationName, null, true); |
|
| 2070 | - } |
|
| 2071 | - if (is_array($otherObjects)) { |
|
| 2072 | - foreach ($otherObjects as $otherObject) { |
|
| 2073 | - $otherObject->clear_cache($this->get_model()->get_this_model_name(), $this); |
|
| 2074 | - } |
|
| 2075 | - } |
|
| 2076 | - return $otherObjects; |
|
| 2077 | - } |
|
| 2078 | - |
|
| 2079 | - |
|
| 2080 | - |
|
| 2081 | - /** |
|
| 2082 | - * Gets all the related model objects of the specified type. Eg, if the current class if |
|
| 2083 | - * EE_Event, you could call $this->get_many_related('Registration') to get an array of all the |
|
| 2084 | - * EE_Registration objects which related to this event. Note: by default, we remove the "default query params" |
|
| 2085 | - * because we want to get even deleted items etc. |
|
| 2086 | - * |
|
| 2087 | - * @param string $relationName key in the model's _model_relations array |
|
| 2088 | - * @param array $query_params like EEM_Base::get_all |
|
| 2089 | - * @return EE_Base_Class[] Results not necessarily indexed by IDs, because some results might not have primary keys |
|
| 2090 | - * @throws \EE_Error |
|
| 2091 | - * or might not be saved yet. Consider using EEM_Base::get_IDs() on these results if |
|
| 2092 | - * you want IDs |
|
| 2093 | - */ |
|
| 2094 | - public function get_many_related($relationName, $query_params = array()) |
|
| 2095 | - { |
|
| 2096 | - if ($this->ID()) { |
|
| 2097 | - //this exists in the DB, so get the related things from either the cache or the DB |
|
| 2098 | - //if there are query parameters, forget about caching the related model objects. |
|
| 2099 | - if ($query_params) { |
|
| 2100 | - $related_model_objects = $this->get_model()->get_all_related($this, $relationName, $query_params); |
|
| 2101 | - } else { |
|
| 2102 | - //did we already cache the result of this query? |
|
| 2103 | - $cached_results = $this->get_all_from_cache($relationName); |
|
| 2104 | - if ( ! $cached_results) { |
|
| 2105 | - $related_model_objects = $this->get_model()->get_all_related($this, $relationName, $query_params); |
|
| 2106 | - //if no query parameters were passed, then we got all the related model objects |
|
| 2107 | - //for that relation. We can cache them then. |
|
| 2108 | - foreach ($related_model_objects as $related_model_object) { |
|
| 2109 | - $this->cache($relationName, $related_model_object); |
|
| 2110 | - } |
|
| 2111 | - } else { |
|
| 2112 | - $related_model_objects = $cached_results; |
|
| 2113 | - } |
|
| 2114 | - } |
|
| 2115 | - } else { |
|
| 2116 | - //this doesn't exist in the DB, so just get the related things from the cache |
|
| 2117 | - $related_model_objects = $this->get_all_from_cache($relationName); |
|
| 2118 | - } |
|
| 2119 | - return $related_model_objects; |
|
| 2120 | - } |
|
| 2121 | - |
|
| 2122 | - |
|
| 2123 | - |
|
| 2124 | - /** |
|
| 2125 | - * Instead of getting the related model objects, simply counts them. Ignores default_where_conditions by default, |
|
| 2126 | - * unless otherwise specified in the $query_params |
|
| 2127 | - * |
|
| 2128 | - * @param string $relation_name model_name like 'Event', or 'Registration' |
|
| 2129 | - * @param array $query_params like EEM_Base::get_all's |
|
| 2130 | - * @param string $field_to_count name of field to count by. By default, uses primary key |
|
| 2131 | - * @param bool $distinct if we want to only count the distinct values for the column then you can trigger |
|
| 2132 | - * that by the setting $distinct to TRUE; |
|
| 2133 | - * @return int |
|
| 2134 | - */ |
|
| 2135 | - public function count_related($relation_name, $query_params = array(), $field_to_count = null, $distinct = false) |
|
| 2136 | - { |
|
| 2137 | - return $this->get_model()->count_related($this, $relation_name, $query_params, $field_to_count, $distinct); |
|
| 2138 | - } |
|
| 2139 | - |
|
| 2140 | - |
|
| 2141 | - |
|
| 2142 | - /** |
|
| 2143 | - * Instead of getting the related model objects, simply sums up the values of the specified field. |
|
| 2144 | - * Note: ignores default_where_conditions by default, unless otherwise specified in the $query_params |
|
| 2145 | - * |
|
| 2146 | - * @param string $relation_name model_name like 'Event', or 'Registration' |
|
| 2147 | - * @param array $query_params like EEM_Base::get_all's |
|
| 2148 | - * @param string $field_to_sum name of field to count by. |
|
| 2149 | - * By default, uses primary key (which doesn't make much sense, so you should probably |
|
| 2150 | - * change it) |
|
| 2151 | - * @return int |
|
| 2152 | - */ |
|
| 2153 | - public function sum_related($relation_name, $query_params = array(), $field_to_sum = null) |
|
| 2154 | - { |
|
| 2155 | - return $this->get_model()->sum_related($this, $relation_name, $query_params, $field_to_sum); |
|
| 2156 | - } |
|
| 2157 | - |
|
| 2158 | - |
|
| 2159 | - |
|
| 2160 | - /** |
|
| 2161 | - * Gets the first (ie, one) related model object of the specified type. |
|
| 2162 | - * |
|
| 2163 | - * @param string $relationName key in the model's _model_relations array |
|
| 2164 | - * @param array $query_params like EEM_Base::get_all |
|
| 2165 | - * @return EE_Base_Class (not an array, a single object) |
|
| 2166 | - * @throws \EE_Error |
|
| 2167 | - */ |
|
| 2168 | - public function get_first_related($relationName, $query_params = array()) |
|
| 2169 | - { |
|
| 2170 | - if ($this->ID()) {//this exists in the DB, get from the cache OR the DB |
|
| 2171 | - //if they've provided some query parameters, don't bother trying to cache the result |
|
| 2172 | - //also make sure we're not caching the result of get_first_related |
|
| 2173 | - //on a relation which should have an array of objects (because the cache might have an array of objects) |
|
| 2174 | - if ($query_params |
|
| 2175 | - || ! $this->get_model()->related_settings_for($relationName) |
|
| 2176 | - instanceof |
|
| 2177 | - EE_Belongs_To_Relation |
|
| 2178 | - ) { |
|
| 2179 | - $related_model_object = $this->get_model()->get_first_related($this, $relationName, $query_params); |
|
| 2180 | - } else { |
|
| 2181 | - //first, check if we've already cached the result of this query |
|
| 2182 | - $cached_result = $this->get_one_from_cache($relationName); |
|
| 2183 | - if ( ! $cached_result) { |
|
| 2184 | - $related_model_object = $this->get_model()->get_first_related($this, $relationName, $query_params); |
|
| 2185 | - $this->cache($relationName, $related_model_object); |
|
| 2186 | - } else { |
|
| 2187 | - $related_model_object = $cached_result; |
|
| 2188 | - } |
|
| 2189 | - } |
|
| 2190 | - } else { |
|
| 2191 | - $related_model_object = null; |
|
| 2192 | - //this doesn't exist in the Db, but maybe the relation is of type belongs to, and so the related thing might |
|
| 2193 | - if ($this->get_model()->related_settings_for($relationName) instanceof EE_Belongs_To_Relation) { |
|
| 2194 | - $related_model_object = $this->get_model()->get_first_related($this, $relationName, $query_params); |
|
| 2195 | - } |
|
| 2196 | - //this doesn't exist in the DB and apparently the thing it belongs to doesn't either, just get what's cached on this object |
|
| 2197 | - if ( ! $related_model_object) { |
|
| 2198 | - $related_model_object = $this->get_one_from_cache($relationName); |
|
| 2199 | - } |
|
| 2200 | - } |
|
| 2201 | - return $related_model_object; |
|
| 2202 | - } |
|
| 2203 | - |
|
| 2204 | - |
|
| 2205 | - |
|
| 2206 | - /** |
|
| 2207 | - * Does a delete on all related objects of type $relationName and removes |
|
| 2208 | - * the current model object's relation to them. If they can't be deleted (because |
|
| 2209 | - * of blocking related model objects) does nothing. If the related model objects are |
|
| 2210 | - * soft-deletable, they will be soft-deleted regardless of related blocking model objects. |
|
| 2211 | - * If this model object doesn't exist yet in the DB, just removes its related things |
|
| 2212 | - * |
|
| 2213 | - * @param string $relationName |
|
| 2214 | - * @param array $query_params like EEM_Base::get_all's |
|
| 2215 | - * @return int how many deleted |
|
| 2216 | - * @throws \EE_Error |
|
| 2217 | - */ |
|
| 2218 | - public function delete_related($relationName, $query_params = array()) |
|
| 2219 | - { |
|
| 2220 | - if ($this->ID()) { |
|
| 2221 | - $count = $this->get_model()->delete_related($this, $relationName, $query_params); |
|
| 2222 | - } else { |
|
| 2223 | - $count = count($this->get_all_from_cache($relationName)); |
|
| 2224 | - $this->clear_cache($relationName, null, true); |
|
| 2225 | - } |
|
| 2226 | - return $count; |
|
| 2227 | - } |
|
| 2228 | - |
|
| 2229 | - |
|
| 2230 | - |
|
| 2231 | - /** |
|
| 2232 | - * Does a hard delete (ie, removes the DB row) on all related objects of type $relationName and removes |
|
| 2233 | - * the current model object's relation to them. If they can't be deleted (because |
|
| 2234 | - * of blocking related model objects) just does a soft delete on it instead, if possible. |
|
| 2235 | - * If the related thing isn't a soft-deletable model object, this function is identical |
|
| 2236 | - * to delete_related(). If this model object doesn't exist in the DB, just remove its related things |
|
| 2237 | - * |
|
| 2238 | - * @param string $relationName |
|
| 2239 | - * @param array $query_params like EEM_Base::get_all's |
|
| 2240 | - * @return int how many deleted (including those soft deleted) |
|
| 2241 | - * @throws \EE_Error |
|
| 2242 | - */ |
|
| 2243 | - public function delete_related_permanently($relationName, $query_params = array()) |
|
| 2244 | - { |
|
| 2245 | - if ($this->ID()) { |
|
| 2246 | - $count = $this->get_model()->delete_related_permanently($this, $relationName, $query_params); |
|
| 2247 | - } else { |
|
| 2248 | - $count = count($this->get_all_from_cache($relationName)); |
|
| 2249 | - } |
|
| 2250 | - $this->clear_cache($relationName, null, true); |
|
| 2251 | - return $count; |
|
| 2252 | - } |
|
| 2253 | - |
|
| 2254 | - |
|
| 2255 | - |
|
| 2256 | - /** |
|
| 2257 | - * is_set |
|
| 2258 | - * Just a simple utility function children can use for checking if property exists |
|
| 2259 | - * |
|
| 2260 | - * @access public |
|
| 2261 | - * @param string $field_name property to check |
|
| 2262 | - * @return bool TRUE if existing,FALSE if not. |
|
| 2263 | - */ |
|
| 2264 | - public function is_set($field_name) |
|
| 2265 | - { |
|
| 2266 | - return isset($this->_fields[$field_name]); |
|
| 2267 | - } |
|
| 2268 | - |
|
| 2269 | - |
|
| 2270 | - |
|
| 2271 | - /** |
|
| 2272 | - * Just a simple utility function children can use for checking if property (or properties) exists and throwing an |
|
| 2273 | - * EE_Error exception if they don't |
|
| 2274 | - * |
|
| 2275 | - * @param mixed (string|array) $properties properties to check |
|
| 2276 | - * @throws EE_Error |
|
| 2277 | - * @return bool TRUE if existing, throw EE_Error if not. |
|
| 2278 | - */ |
|
| 2279 | - protected function _property_exists($properties) |
|
| 2280 | - { |
|
| 2281 | - foreach ((array)$properties as $property_name) { |
|
| 2282 | - //first make sure this property exists |
|
| 2283 | - if ( ! $this->_fields[$property_name]) { |
|
| 2284 | - throw new EE_Error( |
|
| 2285 | - sprintf( |
|
| 2286 | - __( |
|
| 2287 | - 'Trying to retrieve a non-existent property (%s). Double check the spelling please', |
|
| 2288 | - 'event_espresso' |
|
| 2289 | - ), |
|
| 2290 | - $property_name |
|
| 2291 | - ) |
|
| 2292 | - ); |
|
| 2293 | - } |
|
| 2294 | - } |
|
| 2295 | - return true; |
|
| 2296 | - } |
|
| 2297 | - |
|
| 2298 | - |
|
| 2299 | - |
|
| 2300 | - /** |
|
| 2301 | - * This simply returns an array of model fields for this object |
|
| 2302 | - * |
|
| 2303 | - * @return array |
|
| 2304 | - * @throws \EE_Error |
|
| 2305 | - */ |
|
| 2306 | - public function model_field_array() |
|
| 2307 | - { |
|
| 2308 | - $fields = $this->get_model()->field_settings(false); |
|
| 2309 | - $properties = array(); |
|
| 2310 | - //remove prepended underscore |
|
| 2311 | - foreach ($fields as $field_name => $settings) { |
|
| 2312 | - $properties[$field_name] = $this->get($field_name); |
|
| 2313 | - } |
|
| 2314 | - return $properties; |
|
| 2315 | - } |
|
| 2316 | - |
|
| 2317 | - |
|
| 2318 | - |
|
| 2319 | - /** |
|
| 2320 | - * Very handy general function to allow for plugins to extend any child of EE_Base_Class. |
|
| 2321 | - * If a method is called on a child of EE_Base_Class that doesn't exist, this function is called |
|
| 2322 | - * (http://www.garfieldtech.com/blog/php-magic-call) and passed the method's name and arguments. Instead of |
|
| 2323 | - * requiring a plugin to extend the EE_Base_Class (which works fine is there's only 1 plugin, but when will that |
|
| 2324 | - * happen?) they can add a hook onto 'filters_hook_espresso__{className}__{methodName}' (eg, |
|
| 2325 | - * filters_hook_espresso__EE_Answer__my_great_function) and accepts 2 arguments: the object on which the function |
|
| 2326 | - * was called, and an array of the original arguments passed to the function. Whatever their callback function |
|
| 2327 | - * returns will be returned by this function. Example: in functions.php (or in a plugin): |
|
| 2328 | - * add_filter('FHEE__EE_Answer__my_callback','my_callback',10,3); function |
|
| 2329 | - * my_callback($previousReturnValue,EE_Base_Class $object,$argsArray){ |
|
| 2330 | - * $returnString= "you called my_callback! and passed args:".implode(",",$argsArray); |
|
| 2331 | - * return $previousReturnValue.$returnString; |
|
| 2332 | - * } |
|
| 2333 | - * require('EE_Answer.class.php'); |
|
| 2334 | - * $answer= EE_Answer::new_instance(array('REG_ID' => 2,'QST_ID' => 3,'ANS_value' => The answer is 42')); |
|
| 2335 | - * echo $answer->my_callback('monkeys',100); |
|
| 2336 | - * //will output "you called my_callback! and passed args:monkeys,100" |
|
| 2337 | - * |
|
| 2338 | - * @param string $methodName name of method which was called on a child of EE_Base_Class, but which |
|
| 2339 | - * @param array $args array of original arguments passed to the function |
|
| 2340 | - * @throws EE_Error |
|
| 2341 | - * @return mixed whatever the plugin which calls add_filter decides |
|
| 2342 | - */ |
|
| 2343 | - public function __call($methodName, $args) |
|
| 2344 | - { |
|
| 2345 | - $className = get_class($this); |
|
| 2346 | - $tagName = "FHEE__{$className}__{$methodName}"; |
|
| 2347 | - if ( ! has_filter($tagName)) { |
|
| 2348 | - throw new EE_Error( |
|
| 2349 | - sprintf( |
|
| 2350 | - __( |
|
| 2351 | - "Method %s on class %s does not exist! You can create one with the following code in functions.php or in a plugin: add_filter('%s','my_callback',10,3);function my_callback(\$previousReturnValue,EE_Base_Class \$object, \$argsArray){/*function body*/return \$whatever;}", |
|
| 2352 | - "event_espresso" |
|
| 2353 | - ), |
|
| 2354 | - $methodName, |
|
| 2355 | - $className, |
|
| 2356 | - $tagName |
|
| 2357 | - ) |
|
| 2358 | - ); |
|
| 2359 | - } |
|
| 2360 | - return apply_filters($tagName, null, $this, $args); |
|
| 2361 | - } |
|
| 2362 | - |
|
| 2363 | - |
|
| 2364 | - |
|
| 2365 | - /** |
|
| 2366 | - * Similar to insert_post_meta, adds a record in the Extra_Meta model's table with the given key and value. |
|
| 2367 | - * A $previous_value can be specified in case there are many meta rows with the same key |
|
| 2368 | - * |
|
| 2369 | - * @param string $meta_key |
|
| 2370 | - * @param mixed $meta_value |
|
| 2371 | - * @param mixed $previous_value |
|
| 2372 | - * @return bool|int # of records updated (or BOOLEAN if we actually ended up inserting the extra meta row) |
|
| 2373 | - * @throws \EE_Error |
|
| 2374 | - * NOTE: if the values haven't changed, returns 0 |
|
| 2375 | - */ |
|
| 2376 | - public function update_extra_meta($meta_key, $meta_value, $previous_value = null) |
|
| 2377 | - { |
|
| 2378 | - $query_params = array( |
|
| 2379 | - array( |
|
| 2380 | - 'EXM_key' => $meta_key, |
|
| 2381 | - 'OBJ_ID' => $this->ID(), |
|
| 2382 | - 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2383 | - ), |
|
| 2384 | - ); |
|
| 2385 | - if ($previous_value !== null) { |
|
| 2386 | - $query_params[0]['EXM_value'] = $meta_value; |
|
| 2387 | - } |
|
| 2388 | - $existing_rows_like_that = EEM_Extra_Meta::instance()->get_all($query_params); |
|
| 2389 | - if ( ! $existing_rows_like_that) { |
|
| 2390 | - return $this->add_extra_meta($meta_key, $meta_value); |
|
| 2391 | - } |
|
| 2392 | - foreach ($existing_rows_like_that as $existing_row) { |
|
| 2393 | - $existing_row->save(array('EXM_value' => $meta_value)); |
|
| 2394 | - } |
|
| 2395 | - return count($existing_rows_like_that); |
|
| 2396 | - } |
|
| 2397 | - |
|
| 2398 | - |
|
| 2399 | - |
|
| 2400 | - /** |
|
| 2401 | - * Adds a new extra meta record. If $unique is set to TRUE, we'll first double-check |
|
| 2402 | - * no other extra meta for this model object have the same key. Returns TRUE if the |
|
| 2403 | - * extra meta row was entered, false if not |
|
| 2404 | - * |
|
| 2405 | - * @param string $meta_key |
|
| 2406 | - * @param string $meta_value |
|
| 2407 | - * @param boolean $unique |
|
| 2408 | - * @return boolean |
|
| 2409 | - * @throws \EE_Error |
|
| 2410 | - */ |
|
| 2411 | - public function add_extra_meta($meta_key, $meta_value, $unique = false) |
|
| 2412 | - { |
|
| 2413 | - if ($unique) { |
|
| 2414 | - $existing_extra_meta = EEM_Extra_Meta::instance()->get_one( |
|
| 2415 | - array( |
|
| 2416 | - array( |
|
| 2417 | - 'EXM_key' => $meta_key, |
|
| 2418 | - 'OBJ_ID' => $this->ID(), |
|
| 2419 | - 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2420 | - ), |
|
| 2421 | - ) |
|
| 2422 | - ); |
|
| 2423 | - if ($existing_extra_meta) { |
|
| 2424 | - return false; |
|
| 2425 | - } |
|
| 2426 | - } |
|
| 2427 | - $new_extra_meta = EE_Extra_Meta::new_instance( |
|
| 2428 | - array( |
|
| 2429 | - 'EXM_key' => $meta_key, |
|
| 2430 | - 'EXM_value' => $meta_value, |
|
| 2431 | - 'OBJ_ID' => $this->ID(), |
|
| 2432 | - 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2433 | - ) |
|
| 2434 | - ); |
|
| 2435 | - $new_extra_meta->save(); |
|
| 2436 | - return true; |
|
| 2437 | - } |
|
| 2438 | - |
|
| 2439 | - |
|
| 2440 | - |
|
| 2441 | - /** |
|
| 2442 | - * Deletes all the extra meta rows for this record as specified by key. If $meta_value |
|
| 2443 | - * is specified, only deletes extra meta records with that value. |
|
| 2444 | - * |
|
| 2445 | - * @param string $meta_key |
|
| 2446 | - * @param string $meta_value |
|
| 2447 | - * @return int number of extra meta rows deleted |
|
| 2448 | - * @throws \EE_Error |
|
| 2449 | - */ |
|
| 2450 | - public function delete_extra_meta($meta_key, $meta_value = null) |
|
| 2451 | - { |
|
| 2452 | - $query_params = array( |
|
| 2453 | - array( |
|
| 2454 | - 'EXM_key' => $meta_key, |
|
| 2455 | - 'OBJ_ID' => $this->ID(), |
|
| 2456 | - 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2457 | - ), |
|
| 2458 | - ); |
|
| 2459 | - if ($meta_value !== null) { |
|
| 2460 | - $query_params[0]['EXM_value'] = $meta_value; |
|
| 2461 | - } |
|
| 2462 | - return EEM_Extra_Meta::instance()->delete($query_params); |
|
| 2463 | - } |
|
| 2464 | - |
|
| 2465 | - |
|
| 2466 | - |
|
| 2467 | - /** |
|
| 2468 | - * Gets the extra meta with the given meta key. If you specify "single" we just return 1, otherwise |
|
| 2469 | - * an array of everything found. Requires that this model actually have a relation of type EE_Has_Many_Any_Relation. |
|
| 2470 | - * You can specify $default is case you haven't found the extra meta |
|
| 2471 | - * |
|
| 2472 | - * @param string $meta_key |
|
| 2473 | - * @param boolean $single |
|
| 2474 | - * @param mixed $default if we don't find anything, what should we return? |
|
| 2475 | - * @return mixed single value if $single; array if ! $single |
|
| 2476 | - * @throws \EE_Error |
|
| 2477 | - */ |
|
| 2478 | - public function get_extra_meta($meta_key, $single = false, $default = null) |
|
| 2479 | - { |
|
| 2480 | - if ($single) { |
|
| 2481 | - $result = $this->get_first_related('Extra_Meta', array(array('EXM_key' => $meta_key))); |
|
| 2482 | - if ($result instanceof EE_Extra_Meta) { |
|
| 2483 | - return $result->value(); |
|
| 2484 | - } else { |
|
| 2485 | - return $default; |
|
| 2486 | - } |
|
| 2487 | - } else { |
|
| 2488 | - $results = $this->get_many_related('Extra_Meta', array(array('EXM_key' => $meta_key))); |
|
| 2489 | - if ($results) { |
|
| 2490 | - $values = array(); |
|
| 2491 | - foreach ($results as $result) { |
|
| 2492 | - if ($result instanceof EE_Extra_Meta) { |
|
| 2493 | - $values[$result->ID()] = $result->value(); |
|
| 2494 | - } |
|
| 2495 | - } |
|
| 2496 | - return $values; |
|
| 2497 | - } else { |
|
| 2498 | - return $default; |
|
| 2499 | - } |
|
| 2500 | - } |
|
| 2501 | - } |
|
| 2502 | - |
|
| 2503 | - |
|
| 2504 | - |
|
| 2505 | - /** |
|
| 2506 | - * Returns a simple array of all the extra meta associated with this model object. |
|
| 2507 | - * If $one_of_each_key is true (Default), it will be an array of simple key-value pairs, keys being the |
|
| 2508 | - * extra meta's key, and teh value being its value. However, if there are duplicate extra meta rows with |
|
| 2509 | - * the same key, only one will be used. (eg array('foo'=>'bar','monkey'=>123)) |
|
| 2510 | - * If $one_of_each_key is false, it will return an array with the top-level keys being |
|
| 2511 | - * the extra meta keys, but their values are also arrays, which have the extra-meta's ID as their sub-key, and |
|
| 2512 | - * finally the extra meta's value as each sub-value. (eg |
|
| 2513 | - * array('foo'=>array(1=>'bar',2=>'bill'),'monkey'=>array(3=>123))) |
|
| 2514 | - * |
|
| 2515 | - * @param boolean $one_of_each_key |
|
| 2516 | - * @return array |
|
| 2517 | - * @throws \EE_Error |
|
| 2518 | - */ |
|
| 2519 | - public function all_extra_meta_array($one_of_each_key = true) |
|
| 2520 | - { |
|
| 2521 | - $return_array = array(); |
|
| 2522 | - if ($one_of_each_key) { |
|
| 2523 | - $extra_meta_objs = $this->get_many_related('Extra_Meta', array('group_by' => 'EXM_key')); |
|
| 2524 | - foreach ($extra_meta_objs as $extra_meta_obj) { |
|
| 2525 | - if ($extra_meta_obj instanceof EE_Extra_Meta) { |
|
| 2526 | - $return_array[$extra_meta_obj->key()] = $extra_meta_obj->value(); |
|
| 2527 | - } |
|
| 2528 | - } |
|
| 2529 | - } else { |
|
| 2530 | - $extra_meta_objs = $this->get_many_related('Extra_Meta'); |
|
| 2531 | - foreach ($extra_meta_objs as $extra_meta_obj) { |
|
| 2532 | - if ($extra_meta_obj instanceof EE_Extra_Meta) { |
|
| 2533 | - if ( ! isset($return_array[$extra_meta_obj->key()])) { |
|
| 2534 | - $return_array[$extra_meta_obj->key()] = array(); |
|
| 2535 | - } |
|
| 2536 | - $return_array[$extra_meta_obj->key()][$extra_meta_obj->ID()] = $extra_meta_obj->value(); |
|
| 2537 | - } |
|
| 2538 | - } |
|
| 2539 | - } |
|
| 2540 | - return $return_array; |
|
| 2541 | - } |
|
| 2542 | - |
|
| 2543 | - |
|
| 2544 | - |
|
| 2545 | - /** |
|
| 2546 | - * Gets a pretty nice displayable nice for this model object. Often overridden |
|
| 2547 | - * |
|
| 2548 | - * @return string |
|
| 2549 | - * @throws \EE_Error |
|
| 2550 | - */ |
|
| 2551 | - public function name() |
|
| 2552 | - { |
|
| 2553 | - //find a field that's not a text field |
|
| 2554 | - $field_we_can_use = $this->get_model()->get_a_field_of_type('EE_Text_Field_Base'); |
|
| 2555 | - if ($field_we_can_use) { |
|
| 2556 | - return $this->get($field_we_can_use->get_name()); |
|
| 2557 | - } else { |
|
| 2558 | - $first_few_properties = $this->model_field_array(); |
|
| 2559 | - $first_few_properties = array_slice($first_few_properties, 0, 3); |
|
| 2560 | - $name_parts = array(); |
|
| 2561 | - foreach ($first_few_properties as $name => $value) { |
|
| 2562 | - $name_parts[] = "$name:$value"; |
|
| 2563 | - } |
|
| 2564 | - return implode(",", $name_parts); |
|
| 2565 | - } |
|
| 2566 | - } |
|
| 2567 | - |
|
| 2568 | - |
|
| 2569 | - |
|
| 2570 | - /** |
|
| 2571 | - * in_entity_map |
|
| 2572 | - * Checks if this model object has been proven to already be in the entity map |
|
| 2573 | - * |
|
| 2574 | - * @return boolean |
|
| 2575 | - * @throws \EE_Error |
|
| 2576 | - */ |
|
| 2577 | - public function in_entity_map() |
|
| 2578 | - { |
|
| 2579 | - if ($this->ID() && $this->get_model()->get_from_entity_map($this->ID()) === $this) { |
|
| 2580 | - //well, if we looked, did we find it in the entity map? |
|
| 2581 | - return true; |
|
| 2582 | - } else { |
|
| 2583 | - return false; |
|
| 2584 | - } |
|
| 2585 | - } |
|
| 2586 | - |
|
| 2587 | - |
|
| 2588 | - |
|
| 2589 | - /** |
|
| 2590 | - * refresh_from_db |
|
| 2591 | - * Makes sure the fields and values on this model object are in-sync with what's in the database. |
|
| 2592 | - * |
|
| 2593 | - * @throws EE_Error if this model object isn't in the entity mapper (because then you should |
|
| 2594 | - * just use what's in the entity mapper and refresh it) and WP_DEBUG is TRUE |
|
| 2595 | - */ |
|
| 2596 | - public function refresh_from_db() |
|
| 2597 | - { |
|
| 2598 | - if ($this->ID() && $this->in_entity_map()) { |
|
| 2599 | - $this->get_model()->refresh_entity_map_from_db($this->ID()); |
|
| 2600 | - } else { |
|
| 2601 | - //if it doesn't have ID, you shouldn't be asking to refresh it from teh database (because its not in the database) |
|
| 2602 | - //if it has an ID but it's not in the map, and you're asking me to refresh it |
|
| 2603 | - //that's kinda dangerous. You should just use what's in the entity map, or add this to the entity map if there's |
|
| 2604 | - //absolutely nothing in it for this ID |
|
| 2605 | - if (WP_DEBUG) { |
|
| 2606 | - throw new EE_Error( |
|
| 2607 | - sprintf( |
|
| 2608 | - __('Trying to refresh a model object with ID "%1$s" that\'s not in the entity map? First off: you should put it in the entity map by calling %2$s. Second off, if you want what\'s in the database right now, you should just call %3$s yourself and discard this model object.', |
|
| 2609 | - 'event_espresso'), |
|
| 2610 | - $this->ID(), |
|
| 2611 | - get_class($this->get_model()) . '::instance()->add_to_entity_map()', |
|
| 2612 | - get_class($this->get_model()) . '::instance()->refresh_entity_map()' |
|
| 2613 | - ) |
|
| 2614 | - ); |
|
| 2615 | - } |
|
| 2616 | - } |
|
| 2617 | - } |
|
| 2618 | - |
|
| 2619 | - |
|
| 2620 | - |
|
| 2621 | - /** |
|
| 2622 | - * Because some other plugins, like Advanced Cron Manager, expect all objects to have this method |
|
| 2623 | - * (probably a bad assumption they have made, oh well) |
|
| 2624 | - * |
|
| 2625 | - * @return string |
|
| 2626 | - */ |
|
| 2627 | - public function __toString() |
|
| 2628 | - { |
|
| 2629 | - try { |
|
| 2630 | - return sprintf('%s (%s)', $this->name(), $this->ID()); |
|
| 2631 | - } catch (Exception $e) { |
|
| 2632 | - EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__); |
|
| 2633 | - return ''; |
|
| 2634 | - } |
|
| 2635 | - } |
|
| 2636 | - |
|
| 2637 | - |
|
| 2638 | - |
|
| 2639 | - /** |
|
| 2640 | - * Clear related model objects if they're already in the DB, because otherwise when we |
|
| 2641 | - * UN-serialize this model object we'll need to be careful to add them to the entity map. |
|
| 2642 | - * This means if we have made changes to those related model objects, and want to unserialize |
|
| 2643 | - * the this model object on a subsequent request, changes to those related model objects will be lost. |
|
| 2644 | - * Instead, those related model objects should be directly serialized and stored. |
|
| 2645 | - * Eg, the following won't work: |
|
| 2646 | - * $reg = EEM_Registration::instance()->get_one_by_ID( 123 ); |
|
| 2647 | - * $att = $reg->attendee(); |
|
| 2648 | - * $att->set( 'ATT_fname', 'Dirk' ); |
|
| 2649 | - * update_option( 'my_option', serialize( $reg ) ); |
|
| 2650 | - * //END REQUEST |
|
| 2651 | - * //START NEXT REQUEST |
|
| 2652 | - * $reg = get_option( 'my_option' ); |
|
| 2653 | - * $reg->attendee()->save(); |
|
| 2654 | - * And would need to be replace with: |
|
| 2655 | - * $reg = EEM_Registration::instance()->get_one_by_ID( 123 ); |
|
| 2656 | - * $att = $reg->attendee(); |
|
| 2657 | - * $att->set( 'ATT_fname', 'Dirk' ); |
|
| 2658 | - * update_option( 'my_option', serialize( $reg ) ); |
|
| 2659 | - * //END REQUEST |
|
| 2660 | - * //START NEXT REQUEST |
|
| 2661 | - * $att = get_option( 'my_option' ); |
|
| 2662 | - * $att->save(); |
|
| 2663 | - * |
|
| 2664 | - * @return array |
|
| 2665 | - * @throws \EE_Error |
|
| 2666 | - */ |
|
| 2667 | - public function __sleep() |
|
| 2668 | - { |
|
| 2669 | - foreach ($this->get_model()->relation_settings() as $relation_name => $relation_obj) { |
|
| 2670 | - if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 2671 | - $classname = 'EE_' . $this->get_model()->get_this_model_name(); |
|
| 2672 | - if ( |
|
| 2673 | - $this->get_one_from_cache($relation_name) instanceof $classname |
|
| 2674 | - && $this->get_one_from_cache($relation_name)->ID() |
|
| 2675 | - ) { |
|
| 2676 | - $this->clear_cache($relation_name, $this->get_one_from_cache($relation_name)->ID()); |
|
| 2677 | - } |
|
| 2678 | - } |
|
| 2679 | - } |
|
| 2680 | - $this->_props_n_values_provided_in_constructor = array(); |
|
| 2681 | - return array_keys(get_object_vars($this)); |
|
| 2682 | - } |
|
| 2683 | - |
|
| 2684 | - |
|
| 2685 | - |
|
| 2686 | - /** |
|
| 2687 | - * restore _props_n_values_provided_in_constructor |
|
| 2688 | - * PLZ NOTE: this will reset the array to whatever fields values were present prior to serialization, |
|
| 2689 | - * and therefore should NOT be used to determine if state change has occurred since initial construction. |
|
| 2690 | - * At best, you would only be able to detect if state change has occurred during THIS request. |
|
| 2691 | - */ |
|
| 2692 | - public function __wakeup() |
|
| 2693 | - { |
|
| 2694 | - $this->_props_n_values_provided_in_constructor = $this->_fields; |
|
| 2695 | - } |
|
| 28 | + /** |
|
| 29 | + * This is an array of the original properties and values provided during construction |
|
| 30 | + * of this model object. (keys are model field names, values are their values). |
|
| 31 | + * This list is important to remember so that when we are merging data from the db, we know |
|
| 32 | + * which values to override and which to not override. |
|
| 33 | + * |
|
| 34 | + * @var array |
|
| 35 | + */ |
|
| 36 | + protected $_props_n_values_provided_in_constructor; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * Timezone |
|
| 40 | + * This gets set by the "set_timezone()" method so that we know what timezone incoming strings|timestamps are in. |
|
| 41 | + * This can also be used before a get to set what timezone you want strings coming out of the object to be in. NOT |
|
| 42 | + * all EE_Base_Class child classes use this property but any that use a EE_Datetime_Field data type will have |
|
| 43 | + * access to it. |
|
| 44 | + * |
|
| 45 | + * @var string |
|
| 46 | + */ |
|
| 47 | + protected $_timezone; |
|
| 48 | + |
|
| 49 | + |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * date format |
|
| 53 | + * pattern or format for displaying dates |
|
| 54 | + * |
|
| 55 | + * @var string $_dt_frmt |
|
| 56 | + */ |
|
| 57 | + protected $_dt_frmt; |
|
| 58 | + |
|
| 59 | + |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * time format |
|
| 63 | + * pattern or format for displaying time |
|
| 64 | + * |
|
| 65 | + * @var string $_tm_frmt |
|
| 66 | + */ |
|
| 67 | + protected $_tm_frmt; |
|
| 68 | + |
|
| 69 | + |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * This property is for holding a cached array of object properties indexed by property name as the key. |
|
| 73 | + * The purpose of this is for setting a cache on properties that may have calculated values after a |
|
| 74 | + * prepare_for_get. That way the cache can be checked first and the calculated property returned instead of having |
|
| 75 | + * to recalculate. Used by _set_cached_property() and _get_cached_property() methods. |
|
| 76 | + * |
|
| 77 | + * @var array |
|
| 78 | + */ |
|
| 79 | + protected $_cached_properties = array(); |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * An array containing keys of the related model, and values are either an array of related mode objects or a |
|
| 83 | + * single |
|
| 84 | + * related model object. see the model's _model_relations. The keys should match those specified. And if the |
|
| 85 | + * relation is of type EE_Belongs_To (or one of its children), then there should only be ONE related model object, |
|
| 86 | + * all others have an array) |
|
| 87 | + * |
|
| 88 | + * @var array |
|
| 89 | + */ |
|
| 90 | + protected $_model_relations = array(); |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * Array where keys are field names (see the model's _fields property) and values are their values. To see what |
|
| 94 | + * their types should be, look at what that field object returns on its prepare_for_get and prepare_for_set methods) |
|
| 95 | + * |
|
| 96 | + * @var array |
|
| 97 | + */ |
|
| 98 | + protected $_fields = array(); |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * @var boolean indicating whether or not this model object is intended to ever be saved |
|
| 102 | + * For example, we might create model objects intended to only be used for the duration |
|
| 103 | + * of this request and to be thrown away, and if they were accidentally saved |
|
| 104 | + * it would be a bug. |
|
| 105 | + */ |
|
| 106 | + protected $_allow_persist = true; |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * @var boolean indicating whether or not this model object's properties have changed since construction |
|
| 110 | + */ |
|
| 111 | + protected $_has_changes = false; |
|
| 112 | + |
|
| 113 | + |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * basic constructor for Event Espresso classes, performs any necessary initialization, and verifies it's children |
|
| 117 | + * play nice |
|
| 118 | + * |
|
| 119 | + * @param array $fieldValues where each key is a field (ie, array key in the 2nd |
|
| 120 | + * layer of the model's _fields array, (eg, EVT_ID, |
|
| 121 | + * TXN_amount, QST_name, etc) and values are their values |
|
| 122 | + * @param boolean $bydb a flag for setting if the class is instantiated by the |
|
| 123 | + * corresponding db model or not. |
|
| 124 | + * @param string $timezone indicate what timezone you want any datetime fields to |
|
| 125 | + * be in when instantiating a EE_Base_Class object. |
|
| 126 | + * @param array $date_formats An array of date formats to set on construct where first |
|
| 127 | + * value is the date_format and second value is the time |
|
| 128 | + * format. |
|
| 129 | + * @throws EE_Error |
|
| 130 | + */ |
|
| 131 | + protected function __construct($fieldValues = array(), $bydb = false, $timezone = '', $date_formats = array()) |
|
| 132 | + { |
|
| 133 | + $className = get_class($this); |
|
| 134 | + do_action("AHEE__{$className}__construct", $this, $fieldValues); |
|
| 135 | + $model = $this->get_model(); |
|
| 136 | + $model_fields = $model->field_settings(false); |
|
| 137 | + // ensure $fieldValues is an array |
|
| 138 | + $fieldValues = is_array($fieldValues) ? $fieldValues : array($fieldValues); |
|
| 139 | + // EEH_Debug_Tools::printr( $fieldValues, '$fieldValues <br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span>', 'auto' ); |
|
| 140 | + // verify client code has not passed any invalid field names |
|
| 141 | + foreach ($fieldValues as $field_name => $field_value) { |
|
| 142 | + if ( ! isset($model_fields[$field_name])) { |
|
| 143 | + throw new EE_Error(sprintf(__("Invalid field (%s) passed to constructor of %s. Allowed fields are :%s", |
|
| 144 | + "event_espresso"), $field_name, get_class($this), implode(", ", array_keys($model_fields)))); |
|
| 145 | + } |
|
| 146 | + } |
|
| 147 | + // EEH_Debug_Tools::printr( $model_fields, '$model_fields <br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span>', 'auto' ); |
|
| 148 | + $this->_timezone = EEH_DTT_Helper::get_valid_timezone_string($timezone); |
|
| 149 | + if ( ! empty($date_formats) && is_array($date_formats)) { |
|
| 150 | + list($this->_dt_frmt, $this->_tm_frmt) = $date_formats; |
|
| 151 | + } else { |
|
| 152 | + //set default formats for date and time |
|
| 153 | + $this->_dt_frmt = (string)get_option('date_format', 'Y-m-d'); |
|
| 154 | + $this->_tm_frmt = (string)get_option('time_format', 'g:i a'); |
|
| 155 | + } |
|
| 156 | + //if db model is instantiating |
|
| 157 | + if ($bydb) { |
|
| 158 | + //client code has indicated these field values are from the database |
|
| 159 | + foreach ($model_fields as $fieldName => $field) { |
|
| 160 | + $this->set_from_db($fieldName, isset($fieldValues[$fieldName]) ? $fieldValues[$fieldName] : null); |
|
| 161 | + } |
|
| 162 | + } else { |
|
| 163 | + //we're constructing a brand |
|
| 164 | + //new instance of the model object. Generally, this means we'll need to do more field validation |
|
| 165 | + foreach ($model_fields as $fieldName => $field) { |
|
| 166 | + $this->set($fieldName, isset($fieldValues[$fieldName]) ? $fieldValues[$fieldName] : null, true); |
|
| 167 | + } |
|
| 168 | + } |
|
| 169 | + //remember what values were passed to this constructor |
|
| 170 | + $this->_props_n_values_provided_in_constructor = $fieldValues; |
|
| 171 | + //remember in entity mapper |
|
| 172 | + if ( ! $bydb && $model->has_primary_key_field() && $this->ID()) { |
|
| 173 | + $model->add_to_entity_map($this); |
|
| 174 | + } |
|
| 175 | + //setup all the relations |
|
| 176 | + foreach ($this->get_model()->relation_settings() as $relation_name => $relation_obj) { |
|
| 177 | + if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 178 | + $this->_model_relations[$relation_name] = null; |
|
| 179 | + } else { |
|
| 180 | + $this->_model_relations[$relation_name] = array(); |
|
| 181 | + } |
|
| 182 | + } |
|
| 183 | + /** |
|
| 184 | + * Action done at the end of each model object construction |
|
| 185 | + * |
|
| 186 | + * @param EE_Base_Class $this the model object just created |
|
| 187 | + */ |
|
| 188 | + do_action('AHEE__EE_Base_Class__construct__finished', $this); |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * Gets whether or not this model object is allowed to persist/be saved to the database. |
|
| 195 | + * |
|
| 196 | + * @return boolean |
|
| 197 | + */ |
|
| 198 | + public function allow_persist() |
|
| 199 | + { |
|
| 200 | + return $this->_allow_persist; |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + |
|
| 204 | + |
|
| 205 | + /** |
|
| 206 | + * Sets whether or not this model object should be allowed to be saved to the DB. |
|
| 207 | + * Normally once this is set to FALSE you wouldn't set it back to TRUE, unless |
|
| 208 | + * you got new information that somehow made you change your mind. |
|
| 209 | + * |
|
| 210 | + * @param boolean $allow_persist |
|
| 211 | + * @return boolean |
|
| 212 | + */ |
|
| 213 | + public function set_allow_persist($allow_persist) |
|
| 214 | + { |
|
| 215 | + return $this->_allow_persist = $allow_persist; |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + |
|
| 219 | + |
|
| 220 | + /** |
|
| 221 | + * Gets the field's original value when this object was constructed during this request. |
|
| 222 | + * This can be helpful when determining if a model object has changed or not |
|
| 223 | + * |
|
| 224 | + * @param string $field_name |
|
| 225 | + * @return mixed|null |
|
| 226 | + * @throws \EE_Error |
|
| 227 | + */ |
|
| 228 | + public function get_original($field_name) |
|
| 229 | + { |
|
| 230 | + if (isset($this->_props_n_values_provided_in_constructor[$field_name]) |
|
| 231 | + && $field_settings = $this->get_model()->field_settings_for($field_name) |
|
| 232 | + ) { |
|
| 233 | + return $field_settings->prepare_for_get($this->_props_n_values_provided_in_constructor[$field_name]); |
|
| 234 | + } else { |
|
| 235 | + return null; |
|
| 236 | + } |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + |
|
| 240 | + |
|
| 241 | + /** |
|
| 242 | + * @param EE_Base_Class $obj |
|
| 243 | + * @return string |
|
| 244 | + */ |
|
| 245 | + public function get_class($obj) |
|
| 246 | + { |
|
| 247 | + return get_class($obj); |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + |
|
| 251 | + |
|
| 252 | + /** |
|
| 253 | + * Overrides parent because parent expects old models. |
|
| 254 | + * This also doesn't do any validation, and won't work for serialized arrays |
|
| 255 | + * |
|
| 256 | + * @param string $field_name |
|
| 257 | + * @param mixed $field_value |
|
| 258 | + * @param bool $use_default |
|
| 259 | + * @throws \EE_Error |
|
| 260 | + */ |
|
| 261 | + public function set($field_name, $field_value, $use_default = false) |
|
| 262 | + { |
|
| 263 | + // if not using default and nothing has changed, and object has already been setup (has ID), |
|
| 264 | + // then don't do anything |
|
| 265 | + if ( |
|
| 266 | + ! $use_default |
|
| 267 | + && $this->_fields[$field_name] === $field_value |
|
| 268 | + && $this->ID() |
|
| 269 | + ) { |
|
| 270 | + return; |
|
| 271 | + } |
|
| 272 | + $this->_has_changes = true; |
|
| 273 | + $field_obj = $this->get_model()->field_settings_for($field_name); |
|
| 274 | + if ($field_obj instanceof EE_Model_Field_Base) { |
|
| 275 | + // if ( method_exists( $field_obj, 'set_timezone' )) { |
|
| 276 | + if ($field_obj instanceof EE_Datetime_Field) { |
|
| 277 | + $field_obj->set_timezone($this->_timezone); |
|
| 278 | + $field_obj->set_date_format($this->_dt_frmt); |
|
| 279 | + $field_obj->set_time_format($this->_tm_frmt); |
|
| 280 | + } |
|
| 281 | + $holder_of_value = $field_obj->prepare_for_set($field_value); |
|
| 282 | + //should the value be null? |
|
| 283 | + if (($field_value === null || $holder_of_value === null || $holder_of_value === '') && $use_default) { |
|
| 284 | + $this->_fields[$field_name] = $field_obj->get_default_value(); |
|
| 285 | + /** |
|
| 286 | + * To save having to refactor all the models, if a default value is used for a |
|
| 287 | + * EE_Datetime_Field, and that value is not null nor is it a DateTime |
|
| 288 | + * object. Then let's do a set again to ensure that it becomes a DateTime |
|
| 289 | + * object. |
|
| 290 | + * |
|
| 291 | + * @since 4.6.10+ |
|
| 292 | + */ |
|
| 293 | + if ( |
|
| 294 | + $field_obj instanceof EE_Datetime_Field |
|
| 295 | + && $this->_fields[$field_name] !== null |
|
| 296 | + && ! $this->_fields[$field_name] instanceof DateTime |
|
| 297 | + ) { |
|
| 298 | + empty($this->_fields[$field_name]) |
|
| 299 | + ? $this->set($field_name, time()) |
|
| 300 | + : $this->set($field_name, $this->_fields[$field_name]); |
|
| 301 | + } |
|
| 302 | + } else { |
|
| 303 | + $this->_fields[$field_name] = $holder_of_value; |
|
| 304 | + } |
|
| 305 | + //if we're not in the constructor... |
|
| 306 | + //now check if what we set was a primary key |
|
| 307 | + if ( |
|
| 308 | + //note: props_n_values_provided_in_constructor is only set at the END of the constructor |
|
| 309 | + $this->_props_n_values_provided_in_constructor |
|
| 310 | + && $field_value |
|
| 311 | + && $field_name === self::_get_primary_key_name(get_class($this)) |
|
| 312 | + ) { |
|
| 313 | + //if so, we want all this object's fields to be filled either with |
|
| 314 | + //what we've explicitly set on this model |
|
| 315 | + //or what we have in the db |
|
| 316 | + // echo "setting primary key!"; |
|
| 317 | + $fields_on_model = self::_get_model(get_class($this))->field_settings(); |
|
| 318 | + $obj_in_db = self::_get_model(get_class($this))->get_one_by_ID($field_value); |
|
| 319 | + foreach ($fields_on_model as $field_obj) { |
|
| 320 | + if ( ! array_key_exists($field_obj->get_name(), $this->_props_n_values_provided_in_constructor) |
|
| 321 | + && $field_obj->get_name() !== $field_name |
|
| 322 | + ) { |
|
| 323 | + $this->set($field_obj->get_name(), $obj_in_db->get($field_obj->get_name())); |
|
| 324 | + } |
|
| 325 | + } |
|
| 326 | + //oh this model object has an ID? well make sure its in the entity mapper |
|
| 327 | + $this->get_model()->add_to_entity_map($this); |
|
| 328 | + } |
|
| 329 | + //let's unset any cache for this field_name from the $_cached_properties property. |
|
| 330 | + $this->_clear_cached_property($field_name); |
|
| 331 | + } else { |
|
| 332 | + throw new EE_Error(sprintf(__("A valid EE_Model_Field_Base could not be found for the given field name: %s", |
|
| 333 | + "event_espresso"), $field_name)); |
|
| 334 | + } |
|
| 335 | + } |
|
| 336 | + |
|
| 337 | + |
|
| 338 | + |
|
| 339 | + /** |
|
| 340 | + * This sets the field value on the db column if it exists for the given $column_name or |
|
| 341 | + * saves it to EE_Extra_Meta if the given $column_name does not match a db column. |
|
| 342 | + * |
|
| 343 | + * @see EE_message::get_column_value for related documentation on the necessity of this method. |
|
| 344 | + * @param string $field_name Must be the exact column name. |
|
| 345 | + * @param mixed $field_value The value to set. |
|
| 346 | + * @return int|bool @see EE_Base_Class::update_extra_meta() for return docs. |
|
| 347 | + * @throws \EE_Error |
|
| 348 | + */ |
|
| 349 | + public function set_field_or_extra_meta($field_name, $field_value) |
|
| 350 | + { |
|
| 351 | + if ($this->get_model()->has_field($field_name)) { |
|
| 352 | + $this->set($field_name, $field_value); |
|
| 353 | + return true; |
|
| 354 | + } else { |
|
| 355 | + //ensure this object is saved first so that extra meta can be properly related. |
|
| 356 | + $this->save(); |
|
| 357 | + return $this->update_extra_meta($field_name, $field_value); |
|
| 358 | + } |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + |
|
| 362 | + |
|
| 363 | + /** |
|
| 364 | + * This retrieves the value of the db column set on this class or if that's not present |
|
| 365 | + * it will attempt to retrieve from extra_meta if found. |
|
| 366 | + * Example Usage: |
|
| 367 | + * Via EE_Message child class: |
|
| 368 | + * Due to the dynamic nature of the EE_messages system, EE_messengers will always have a "to", |
|
| 369 | + * "from", "subject", and "content" field (as represented in the EE_Message schema), however they may |
|
| 370 | + * also have additional main fields specific to the messenger. The system accommodates those extra |
|
| 371 | + * fields through the EE_Extra_Meta table. This method allows for EE_messengers to retrieve the |
|
| 372 | + * value for those extra fields dynamically via the EE_message object. |
|
| 373 | + * |
|
| 374 | + * @param string $field_name expecting the fully qualified field name. |
|
| 375 | + * @return mixed|null value for the field if found. null if not found. |
|
| 376 | + * @throws \EE_Error |
|
| 377 | + */ |
|
| 378 | + public function get_field_or_extra_meta($field_name) |
|
| 379 | + { |
|
| 380 | + if ($this->get_model()->has_field($field_name)) { |
|
| 381 | + $column_value = $this->get($field_name); |
|
| 382 | + } else { |
|
| 383 | + //This isn't a column in the main table, let's see if it is in the extra meta. |
|
| 384 | + $column_value = $this->get_extra_meta($field_name, true, null); |
|
| 385 | + } |
|
| 386 | + return $column_value; |
|
| 387 | + } |
|
| 388 | + |
|
| 389 | + |
|
| 390 | + |
|
| 391 | + /** |
|
| 392 | + * See $_timezone property for description of what the timezone property is for. This SETS the timezone internally |
|
| 393 | + * for being able to reference what timezone we are running conversions on when converting TO the internal timezone |
|
| 394 | + * (UTC Unix Timestamp) for the object OR when converting FROM the internal timezone (UTC Unix Timestamp). This is |
|
| 395 | + * available to all child classes that may be using the EE_Datetime_Field for a field data type. |
|
| 396 | + * |
|
| 397 | + * @access public |
|
| 398 | + * @param string $timezone A valid timezone string as described by @link http://www.php.net/manual/en/timezones.php |
|
| 399 | + * @return void |
|
| 400 | + * @throws \EE_Error |
|
| 401 | + */ |
|
| 402 | + public function set_timezone($timezone = '') |
|
| 403 | + { |
|
| 404 | + $this->_timezone = EEH_DTT_Helper::get_valid_timezone_string($timezone); |
|
| 405 | + //make sure we clear all cached properties because they won't be relevant now |
|
| 406 | + $this->_clear_cached_properties(); |
|
| 407 | + //make sure we update field settings and the date for all EE_Datetime_Fields |
|
| 408 | + $model_fields = $this->get_model()->field_settings(false); |
|
| 409 | + foreach ($model_fields as $field_name => $field_obj) { |
|
| 410 | + if ($field_obj instanceof EE_Datetime_Field) { |
|
| 411 | + $field_obj->set_timezone($this->_timezone); |
|
| 412 | + if (isset($this->_fields[$field_name]) && $this->_fields[$field_name] instanceof DateTime) { |
|
| 413 | + $this->_fields[$field_name]->setTimezone(new DateTimeZone($this->_timezone)); |
|
| 414 | + } |
|
| 415 | + } |
|
| 416 | + } |
|
| 417 | + } |
|
| 418 | + |
|
| 419 | + |
|
| 420 | + |
|
| 421 | + /** |
|
| 422 | + * This just returns whatever is set for the current timezone. |
|
| 423 | + * |
|
| 424 | + * @access public |
|
| 425 | + * @return string timezone string |
|
| 426 | + */ |
|
| 427 | + public function get_timezone() |
|
| 428 | + { |
|
| 429 | + return $this->_timezone; |
|
| 430 | + } |
|
| 431 | + |
|
| 432 | + |
|
| 433 | + |
|
| 434 | + /** |
|
| 435 | + * This sets the internal date format to what is sent in to be used as the new default for the class |
|
| 436 | + * internally instead of wp set date format options |
|
| 437 | + * |
|
| 438 | + * @since 4.6 |
|
| 439 | + * @param string $format should be a format recognizable by PHP date() functions. |
|
| 440 | + */ |
|
| 441 | + public function set_date_format($format) |
|
| 442 | + { |
|
| 443 | + $this->_dt_frmt = $format; |
|
| 444 | + //clear cached_properties because they won't be relevant now. |
|
| 445 | + $this->_clear_cached_properties(); |
|
| 446 | + } |
|
| 447 | + |
|
| 448 | + |
|
| 449 | + |
|
| 450 | + /** |
|
| 451 | + * This sets the internal time format string to what is sent in to be used as the new default for the |
|
| 452 | + * class internally instead of wp set time format options. |
|
| 453 | + * |
|
| 454 | + * @since 4.6 |
|
| 455 | + * @param string $format should be a format recognizable by PHP date() functions. |
|
| 456 | + */ |
|
| 457 | + public function set_time_format($format) |
|
| 458 | + { |
|
| 459 | + $this->_tm_frmt = $format; |
|
| 460 | + //clear cached_properties because they won't be relevant now. |
|
| 461 | + $this->_clear_cached_properties(); |
|
| 462 | + } |
|
| 463 | + |
|
| 464 | + |
|
| 465 | + |
|
| 466 | + /** |
|
| 467 | + * This returns the current internal set format for the date and time formats. |
|
| 468 | + * |
|
| 469 | + * @param bool $full if true (default), then return the full format. Otherwise will return an array |
|
| 470 | + * where the first value is the date format and the second value is the time format. |
|
| 471 | + * @return mixed string|array |
|
| 472 | + */ |
|
| 473 | + public function get_format($full = true) |
|
| 474 | + { |
|
| 475 | + return $full ? $this->_dt_frmt . ' ' . $this->_tm_frmt : array($this->_dt_frmt, $this->_tm_frmt); |
|
| 476 | + } |
|
| 477 | + |
|
| 478 | + |
|
| 479 | + |
|
| 480 | + /** |
|
| 481 | + * cache |
|
| 482 | + * stores the passed model object on the current model object. |
|
| 483 | + * In certain circumstances, we can use this cached model object instead of querying for another one entirely. |
|
| 484 | + * |
|
| 485 | + * @param string $relationName one of the keys in the _model_relations array on the model. Eg |
|
| 486 | + * 'Registration' associated with this model object |
|
| 487 | + * @param EE_Base_Class $object_to_cache that has a relation to this model object. (Eg, if this is a Transaction, |
|
| 488 | + * that could be a payment or a registration) |
|
| 489 | + * @param null $cache_id a string or number that will be used as the key for any Belongs_To_Many |
|
| 490 | + * items which will be stored in an array on this object |
|
| 491 | + * @throws EE_Error |
|
| 492 | + * @return mixed index into cache, or just TRUE if the relation is of type Belongs_To (because there's only one |
|
| 493 | + * related thing, no array) |
|
| 494 | + */ |
|
| 495 | + public function cache($relationName = '', $object_to_cache = null, $cache_id = null) |
|
| 496 | + { |
|
| 497 | + // its entirely possible that there IS no related object yet in which case there is nothing to cache. |
|
| 498 | + if ( ! $object_to_cache instanceof EE_Base_Class) { |
|
| 499 | + return false; |
|
| 500 | + } |
|
| 501 | + // also get "how" the object is related, or throw an error |
|
| 502 | + if ( ! $relationship_to_model = $this->get_model()->related_settings_for($relationName)) { |
|
| 503 | + throw new EE_Error(sprintf(__('There is no relationship to %s on a %s. Cannot cache it', 'event_espresso'), |
|
| 504 | + $relationName, get_class($this))); |
|
| 505 | + } |
|
| 506 | + // how many things are related ? |
|
| 507 | + if ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
|
| 508 | + // if it's a "belongs to" relationship, then there's only one related model object eg, if this is a registration, there's only 1 attendee for it |
|
| 509 | + // so for these model objects just set it to be cached |
|
| 510 | + $this->_model_relations[$relationName] = $object_to_cache; |
|
| 511 | + $return = true; |
|
| 512 | + } else { |
|
| 513 | + // otherwise, this is the "many" side of a one to many relationship, so we'll add the object to the array of related objects for that type. |
|
| 514 | + // eg: if this is an event, there are many registrations for that event, so we cache the registrations in an array |
|
| 515 | + if ( ! is_array($this->_model_relations[$relationName])) { |
|
| 516 | + // if for some reason, the cached item is a model object, then stick that in the array, otherwise start with an empty array |
|
| 517 | + $this->_model_relations[$relationName] = $this->_model_relations[$relationName] instanceof EE_Base_Class |
|
| 518 | + ? array($this->_model_relations[$relationName]) : array(); |
|
| 519 | + } |
|
| 520 | + // first check for a cache_id which is normally empty |
|
| 521 | + if ( ! empty($cache_id)) { |
|
| 522 | + // if the cache_id exists, then it means we are purposely trying to cache this with a known key that can then be used to retrieve the object later on |
|
| 523 | + $this->_model_relations[$relationName][$cache_id] = $object_to_cache; |
|
| 524 | + $return = $cache_id; |
|
| 525 | + } elseif ($object_to_cache->ID()) { |
|
| 526 | + // OR the cached object originally came from the db, so let's just use it's PK for an ID |
|
| 527 | + $this->_model_relations[$relationName][$object_to_cache->ID()] = $object_to_cache; |
|
| 528 | + $return = $object_to_cache->ID(); |
|
| 529 | + } else { |
|
| 530 | + // OR it's a new object with no ID, so just throw it in the array with an auto-incremented ID |
|
| 531 | + $this->_model_relations[$relationName][] = $object_to_cache; |
|
| 532 | + // move the internal pointer to the end of the array |
|
| 533 | + end($this->_model_relations[$relationName]); |
|
| 534 | + // and grab the key so that we can return it |
|
| 535 | + $return = key($this->_model_relations[$relationName]); |
|
| 536 | + } |
|
| 537 | + } |
|
| 538 | + return $return; |
|
| 539 | + } |
|
| 540 | + |
|
| 541 | + |
|
| 542 | + |
|
| 543 | + /** |
|
| 544 | + * For adding an item to the cached_properties property. |
|
| 545 | + * |
|
| 546 | + * @access protected |
|
| 547 | + * @param string $fieldname the property item the corresponding value is for. |
|
| 548 | + * @param mixed $value The value we are caching. |
|
| 549 | + * @param string|null $cache_type |
|
| 550 | + * @return void |
|
| 551 | + * @throws \EE_Error |
|
| 552 | + */ |
|
| 553 | + protected function _set_cached_property($fieldname, $value, $cache_type = null) |
|
| 554 | + { |
|
| 555 | + //first make sure this property exists |
|
| 556 | + $this->get_model()->field_settings_for($fieldname); |
|
| 557 | + $cache_type = empty($cache_type) ? 'standard' : $cache_type; |
|
| 558 | + $this->_cached_properties[$fieldname][$cache_type] = $value; |
|
| 559 | + } |
|
| 560 | + |
|
| 561 | + |
|
| 562 | + |
|
| 563 | + /** |
|
| 564 | + * This returns the value cached property if it exists OR the actual property value if the cache doesn't exist. |
|
| 565 | + * This also SETS the cache if we return the actual property! |
|
| 566 | + * |
|
| 567 | + * @param string $fieldname the name of the property we're trying to retrieve |
|
| 568 | + * @param bool $pretty |
|
| 569 | + * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
| 570 | + * (in cases where the same property may be used for different outputs |
|
| 571 | + * - i.e. datetime, money etc.) |
|
| 572 | + * It can also accept certain pre-defined "schema" strings |
|
| 573 | + * to define how to output the property. |
|
| 574 | + * see the field's prepare_for_pretty_echoing for what strings can be used |
|
| 575 | + * @return mixed whatever the value for the property is we're retrieving |
|
| 576 | + * @throws \EE_Error |
|
| 577 | + */ |
|
| 578 | + protected function _get_cached_property($fieldname, $pretty = false, $extra_cache_ref = null) |
|
| 579 | + { |
|
| 580 | + //verify the field exists |
|
| 581 | + $this->get_model()->field_settings_for($fieldname); |
|
| 582 | + $cache_type = $pretty ? 'pretty' : 'standard'; |
|
| 583 | + $cache_type .= ! empty($extra_cache_ref) ? '_' . $extra_cache_ref : ''; |
|
| 584 | + if (isset($this->_cached_properties[$fieldname][$cache_type])) { |
|
| 585 | + return $this->_cached_properties[$fieldname][$cache_type]; |
|
| 586 | + } |
|
| 587 | + $field_obj = $this->get_model()->field_settings_for($fieldname); |
|
| 588 | + if ($field_obj instanceof EE_Model_Field_Base) { |
|
| 589 | + // If this is an EE_Datetime_Field we need to make sure timezone, formats, and output are correct |
|
| 590 | + if ($field_obj instanceof EE_Datetime_Field) { |
|
| 591 | + $this->_prepare_datetime_field($field_obj, $pretty, $extra_cache_ref); |
|
| 592 | + } |
|
| 593 | + if ( ! isset($this->_fields[$fieldname])) { |
|
| 594 | + $this->_fields[$fieldname] = null; |
|
| 595 | + } |
|
| 596 | + $value = $pretty |
|
| 597 | + ? $field_obj->prepare_for_pretty_echoing($this->_fields[$fieldname], $extra_cache_ref) |
|
| 598 | + : $field_obj->prepare_for_get($this->_fields[$fieldname]); |
|
| 599 | + $this->_set_cached_property($fieldname, $value, $cache_type); |
|
| 600 | + return $value; |
|
| 601 | + } |
|
| 602 | + return null; |
|
| 603 | + } |
|
| 604 | + |
|
| 605 | + |
|
| 606 | + |
|
| 607 | + /** |
|
| 608 | + * set timezone, formats, and output for EE_Datetime_Field objects |
|
| 609 | + * |
|
| 610 | + * @param \EE_Datetime_Field $datetime_field |
|
| 611 | + * @param bool $pretty |
|
| 612 | + * @param null $date_or_time |
|
| 613 | + * @return void |
|
| 614 | + * @throws \EE_Error |
|
| 615 | + */ |
|
| 616 | + protected function _prepare_datetime_field( |
|
| 617 | + EE_Datetime_Field $datetime_field, |
|
| 618 | + $pretty = false, |
|
| 619 | + $date_or_time = null |
|
| 620 | + ) { |
|
| 621 | + $datetime_field->set_timezone($this->_timezone); |
|
| 622 | + $datetime_field->set_date_format($this->_dt_frmt, $pretty); |
|
| 623 | + $datetime_field->set_time_format($this->_tm_frmt, $pretty); |
|
| 624 | + //set the output returned |
|
| 625 | + switch ($date_or_time) { |
|
| 626 | + case 'D' : |
|
| 627 | + $datetime_field->set_date_time_output('date'); |
|
| 628 | + break; |
|
| 629 | + case 'T' : |
|
| 630 | + $datetime_field->set_date_time_output('time'); |
|
| 631 | + break; |
|
| 632 | + default : |
|
| 633 | + $datetime_field->set_date_time_output(); |
|
| 634 | + } |
|
| 635 | + } |
|
| 636 | + |
|
| 637 | + |
|
| 638 | + |
|
| 639 | + /** |
|
| 640 | + * This just takes care of clearing out the cached_properties |
|
| 641 | + * |
|
| 642 | + * @return void |
|
| 643 | + */ |
|
| 644 | + protected function _clear_cached_properties() |
|
| 645 | + { |
|
| 646 | + $this->_cached_properties = array(); |
|
| 647 | + } |
|
| 648 | + |
|
| 649 | + |
|
| 650 | + |
|
| 651 | + /** |
|
| 652 | + * This just clears out ONE property if it exists in the cache |
|
| 653 | + * |
|
| 654 | + * @param string $property_name the property to remove if it exists (from the _cached_properties array) |
|
| 655 | + * @return void |
|
| 656 | + */ |
|
| 657 | + protected function _clear_cached_property($property_name) |
|
| 658 | + { |
|
| 659 | + if (isset($this->_cached_properties[$property_name])) { |
|
| 660 | + unset($this->_cached_properties[$property_name]); |
|
| 661 | + } |
|
| 662 | + } |
|
| 663 | + |
|
| 664 | + |
|
| 665 | + |
|
| 666 | + /** |
|
| 667 | + * Ensures that this related thing is a model object. |
|
| 668 | + * |
|
| 669 | + * @param mixed $object_or_id EE_base_Class/int/string either a related model object, or its ID |
|
| 670 | + * @param string $model_name name of the related thing, eg 'Attendee', |
|
| 671 | + * @return EE_Base_Class |
|
| 672 | + * @throws \EE_Error |
|
| 673 | + */ |
|
| 674 | + protected function ensure_related_thing_is_model_obj($object_or_id, $model_name) |
|
| 675 | + { |
|
| 676 | + $other_model_instance = self::_get_model_instance_with_name( |
|
| 677 | + self::_get_model_classname($model_name), |
|
| 678 | + $this->_timezone |
|
| 679 | + ); |
|
| 680 | + return $other_model_instance->ensure_is_obj($object_or_id); |
|
| 681 | + } |
|
| 682 | + |
|
| 683 | + |
|
| 684 | + |
|
| 685 | + /** |
|
| 686 | + * Forgets the cached model of the given relation Name. So the next time we request it, |
|
| 687 | + * we will fetch it again from the database. (Handy if you know it's changed somehow). |
|
| 688 | + * If a specific object is supplied, and the relationship to it is either a HasMany or HABTM, |
|
| 689 | + * then only remove that one object from our cached array. Otherwise, clear the entire list |
|
| 690 | + * |
|
| 691 | + * @param string $relationName one of the keys in the _model_relations array on the model. |
|
| 692 | + * Eg 'Registration' |
|
| 693 | + * @param mixed $object_to_remove_or_index_into_array or an index into the array of cached things, or NULL |
|
| 694 | + * if you intend to use $clear_all = TRUE, or the relation only |
|
| 695 | + * has 1 object anyways (ie, it's a BelongsToRelation) |
|
| 696 | + * @param bool $clear_all This flags clearing the entire cache relation property if |
|
| 697 | + * this is HasMany or HABTM. |
|
| 698 | + * @throws EE_Error |
|
| 699 | + * @return EE_Base_Class | boolean from which was cleared from the cache, or true if we requested to remove a |
|
| 700 | + * relation from all |
|
| 701 | + */ |
|
| 702 | + public function clear_cache($relationName, $object_to_remove_or_index_into_array = null, $clear_all = false) |
|
| 703 | + { |
|
| 704 | + $relationship_to_model = $this->get_model()->related_settings_for($relationName); |
|
| 705 | + $index_in_cache = ''; |
|
| 706 | + if ( ! $relationship_to_model) { |
|
| 707 | + throw new EE_Error( |
|
| 708 | + sprintf( |
|
| 709 | + __("There is no relationship to %s on a %s. Cannot clear that cache", 'event_espresso'), |
|
| 710 | + $relationName, |
|
| 711 | + get_class($this) |
|
| 712 | + ) |
|
| 713 | + ); |
|
| 714 | + } |
|
| 715 | + if ($clear_all) { |
|
| 716 | + $obj_removed = true; |
|
| 717 | + $this->_model_relations[$relationName] = null; |
|
| 718 | + } elseif ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
|
| 719 | + $obj_removed = $this->_model_relations[$relationName]; |
|
| 720 | + $this->_model_relations[$relationName] = null; |
|
| 721 | + } else { |
|
| 722 | + if ($object_to_remove_or_index_into_array instanceof EE_Base_Class |
|
| 723 | + && $object_to_remove_or_index_into_array->ID() |
|
| 724 | + ) { |
|
| 725 | + $index_in_cache = $object_to_remove_or_index_into_array->ID(); |
|
| 726 | + if (is_array($this->_model_relations[$relationName]) |
|
| 727 | + && ! isset($this->_model_relations[$relationName][$index_in_cache]) |
|
| 728 | + ) { |
|
| 729 | + $index_found_at = null; |
|
| 730 | + //find this object in the array even though it has a different key |
|
| 731 | + foreach ($this->_model_relations[$relationName] as $index => $obj) { |
|
| 732 | + if ( |
|
| 733 | + $obj instanceof EE_Base_Class |
|
| 734 | + && ( |
|
| 735 | + $obj == $object_to_remove_or_index_into_array |
|
| 736 | + || $obj->ID() === $object_to_remove_or_index_into_array->ID() |
|
| 737 | + ) |
|
| 738 | + ) { |
|
| 739 | + $index_found_at = $index; |
|
| 740 | + break; |
|
| 741 | + } |
|
| 742 | + } |
|
| 743 | + if ($index_found_at) { |
|
| 744 | + $index_in_cache = $index_found_at; |
|
| 745 | + } else { |
|
| 746 | + //it wasn't found. huh. well obviously it doesn't need to be removed from teh cache |
|
| 747 | + //if it wasn't in it to begin with. So we're done |
|
| 748 | + return $object_to_remove_or_index_into_array; |
|
| 749 | + } |
|
| 750 | + } |
|
| 751 | + } elseif ($object_to_remove_or_index_into_array instanceof EE_Base_Class) { |
|
| 752 | + //so they provided a model object, but it's not yet saved to the DB... so let's go hunting for it! |
|
| 753 | + foreach ($this->get_all_from_cache($relationName) as $index => $potentially_obj_we_want) { |
|
| 754 | + if ($potentially_obj_we_want == $object_to_remove_or_index_into_array) { |
|
| 755 | + $index_in_cache = $index; |
|
| 756 | + } |
|
| 757 | + } |
|
| 758 | + } else { |
|
| 759 | + $index_in_cache = $object_to_remove_or_index_into_array; |
|
| 760 | + } |
|
| 761 | + //supposedly we've found it. But it could just be that the client code |
|
| 762 | + //provided a bad index/object |
|
| 763 | + if ( |
|
| 764 | + isset( |
|
| 765 | + $this->_model_relations[$relationName], |
|
| 766 | + $this->_model_relations[$relationName][$index_in_cache] |
|
| 767 | + ) |
|
| 768 | + ) { |
|
| 769 | + $obj_removed = $this->_model_relations[$relationName][$index_in_cache]; |
|
| 770 | + unset($this->_model_relations[$relationName][$index_in_cache]); |
|
| 771 | + } else { |
|
| 772 | + //that thing was never cached anyways. |
|
| 773 | + $obj_removed = null; |
|
| 774 | + } |
|
| 775 | + } |
|
| 776 | + return $obj_removed; |
|
| 777 | + } |
|
| 778 | + |
|
| 779 | + |
|
| 780 | + |
|
| 781 | + /** |
|
| 782 | + * update_cache_after_object_save |
|
| 783 | + * Allows a cached item to have it's cache ID (within the array of cached items) reset using the new ID it has |
|
| 784 | + * obtained after being saved to the db |
|
| 785 | + * |
|
| 786 | + * @param string $relationName - the type of object that is cached |
|
| 787 | + * @param \EE_Base_Class $newly_saved_object - the newly saved object to be re-cached |
|
| 788 | + * @param string $current_cache_id - the ID that was used when originally caching the object |
|
| 789 | + * @return boolean TRUE on success, FALSE on fail |
|
| 790 | + * @throws \EE_Error |
|
| 791 | + */ |
|
| 792 | + public function update_cache_after_object_save( |
|
| 793 | + $relationName, |
|
| 794 | + EE_Base_Class $newly_saved_object, |
|
| 795 | + $current_cache_id = '' |
|
| 796 | + ) { |
|
| 797 | + // verify that incoming object is of the correct type |
|
| 798 | + $obj_class = 'EE_' . $relationName; |
|
| 799 | + if ($newly_saved_object instanceof $obj_class) { |
|
| 800 | + /* @type EE_Base_Class $newly_saved_object */ |
|
| 801 | + // now get the type of relation |
|
| 802 | + $relationship_to_model = $this->get_model()->related_settings_for($relationName); |
|
| 803 | + // if this is a 1:1 relationship |
|
| 804 | + if ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
|
| 805 | + // then just replace the cached object with the newly saved object |
|
| 806 | + $this->_model_relations[$relationName] = $newly_saved_object; |
|
| 807 | + return true; |
|
| 808 | + // or if it's some kind of sordid feral polyamorous relationship... |
|
| 809 | + } elseif (is_array($this->_model_relations[$relationName]) |
|
| 810 | + && isset($this->_model_relations[$relationName][$current_cache_id]) |
|
| 811 | + ) { |
|
| 812 | + // then remove the current cached item |
|
| 813 | + unset($this->_model_relations[$relationName][$current_cache_id]); |
|
| 814 | + // and cache the newly saved object using it's new ID |
|
| 815 | + $this->_model_relations[$relationName][$newly_saved_object->ID()] = $newly_saved_object; |
|
| 816 | + return true; |
|
| 817 | + } |
|
| 818 | + } |
|
| 819 | + return false; |
|
| 820 | + } |
|
| 821 | + |
|
| 822 | + |
|
| 823 | + |
|
| 824 | + /** |
|
| 825 | + * Fetches a single EE_Base_Class on that relation. (If the relation is of type |
|
| 826 | + * BelongsTo, it will only ever have 1 object. However, other relations could have an array of objects) |
|
| 827 | + * |
|
| 828 | + * @param string $relationName |
|
| 829 | + * @return EE_Base_Class |
|
| 830 | + */ |
|
| 831 | + public function get_one_from_cache($relationName) |
|
| 832 | + { |
|
| 833 | + $cached_array_or_object = isset($this->_model_relations[$relationName]) ? $this->_model_relations[$relationName] |
|
| 834 | + : null; |
|
| 835 | + if (is_array($cached_array_or_object)) { |
|
| 836 | + return array_shift($cached_array_or_object); |
|
| 837 | + } else { |
|
| 838 | + return $cached_array_or_object; |
|
| 839 | + } |
|
| 840 | + } |
|
| 841 | + |
|
| 842 | + |
|
| 843 | + |
|
| 844 | + /** |
|
| 845 | + * Fetches a single EE_Base_Class on that relation. (If the relation is of type |
|
| 846 | + * BelongsTo, it will only ever have 1 object. However, other relations could have an array of objects) |
|
| 847 | + * |
|
| 848 | + * @param string $relationName |
|
| 849 | + * @throws \EE_Error |
|
| 850 | + * @return EE_Base_Class[] NOT necessarily indexed by primary keys |
|
| 851 | + */ |
|
| 852 | + public function get_all_from_cache($relationName) |
|
| 853 | + { |
|
| 854 | + $objects = isset($this->_model_relations[$relationName]) ? $this->_model_relations[$relationName] : array(); |
|
| 855 | + // if the result is not an array, but exists, make it an array |
|
| 856 | + $objects = is_array($objects) ? $objects : array($objects); |
|
| 857 | + //bugfix for https://events.codebasehq.com/projects/event-espresso/tickets/7143 |
|
| 858 | + //basically, if this model object was stored in the session, and these cached model objects |
|
| 859 | + //already have IDs, let's make sure they're in their model's entity mapper |
|
| 860 | + //otherwise we will have duplicates next time we call |
|
| 861 | + // EE_Registry::instance()->load_model( $relationName )->get_one_by_ID( $result->ID() ); |
|
| 862 | + $model = EE_Registry::instance()->load_model($relationName); |
|
| 863 | + foreach ($objects as $model_object) { |
|
| 864 | + if ($model instanceof EEM_Base && $model_object instanceof EE_Base_Class) { |
|
| 865 | + //ensure its in the map if it has an ID; otherwise it will be added to the map when its saved |
|
| 866 | + if ($model_object->ID()) { |
|
| 867 | + $model->add_to_entity_map($model_object); |
|
| 868 | + } |
|
| 869 | + } else { |
|
| 870 | + throw new EE_Error( |
|
| 871 | + sprintf( |
|
| 872 | + __( |
|
| 873 | + 'Error retrieving related model objects. Either $1%s is not a model or $2%s is not a model object', |
|
| 874 | + 'event_espresso' |
|
| 875 | + ), |
|
| 876 | + $relationName, |
|
| 877 | + gettype($model_object) |
|
| 878 | + ) |
|
| 879 | + ); |
|
| 880 | + } |
|
| 881 | + } |
|
| 882 | + return $objects; |
|
| 883 | + } |
|
| 884 | + |
|
| 885 | + |
|
| 886 | + |
|
| 887 | + /** |
|
| 888 | + * Returns the next x number of EE_Base_Class objects in sequence from this object as found in the database |
|
| 889 | + * matching the given query conditions. |
|
| 890 | + * |
|
| 891 | + * @param null $field_to_order_by What field is being used as the reference point. |
|
| 892 | + * @param int $limit How many objects to return. |
|
| 893 | + * @param array $query_params Any additional conditions on the query. |
|
| 894 | + * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
|
| 895 | + * you can indicate just the columns you want returned |
|
| 896 | + * @return array|EE_Base_Class[] |
|
| 897 | + * @throws \EE_Error |
|
| 898 | + */ |
|
| 899 | + public function next_x($field_to_order_by = null, $limit = 1, $query_params = array(), $columns_to_select = null) |
|
| 900 | + { |
|
| 901 | + $field = empty($field_to_order_by) && $this->get_model()->has_primary_key_field() |
|
| 902 | + ? $this->get_model()->get_primary_key_field()->get_name() |
|
| 903 | + : $field_to_order_by; |
|
| 904 | + $current_value = ! empty($field) ? $this->get($field) : null; |
|
| 905 | + if (empty($field) || empty($current_value)) { |
|
| 906 | + return array(); |
|
| 907 | + } |
|
| 908 | + return $this->get_model()->next_x($current_value, $field, $limit, $query_params, $columns_to_select); |
|
| 909 | + } |
|
| 910 | + |
|
| 911 | + |
|
| 912 | + |
|
| 913 | + /** |
|
| 914 | + * Returns the previous x number of EE_Base_Class objects in sequence from this object as found in the database |
|
| 915 | + * matching the given query conditions. |
|
| 916 | + * |
|
| 917 | + * @param null $field_to_order_by What field is being used as the reference point. |
|
| 918 | + * @param int $limit How many objects to return. |
|
| 919 | + * @param array $query_params Any additional conditions on the query. |
|
| 920 | + * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
|
| 921 | + * you can indicate just the columns you want returned |
|
| 922 | + * @return array|EE_Base_Class[] |
|
| 923 | + * @throws \EE_Error |
|
| 924 | + */ |
|
| 925 | + public function previous_x( |
|
| 926 | + $field_to_order_by = null, |
|
| 927 | + $limit = 1, |
|
| 928 | + $query_params = array(), |
|
| 929 | + $columns_to_select = null |
|
| 930 | + ) { |
|
| 931 | + $field = empty($field_to_order_by) && $this->get_model()->has_primary_key_field() |
|
| 932 | + ? $this->get_model()->get_primary_key_field()->get_name() |
|
| 933 | + : $field_to_order_by; |
|
| 934 | + $current_value = ! empty($field) ? $this->get($field) : null; |
|
| 935 | + if (empty($field) || empty($current_value)) { |
|
| 936 | + return array(); |
|
| 937 | + } |
|
| 938 | + return $this->get_model()->previous_x($current_value, $field, $limit, $query_params, $columns_to_select); |
|
| 939 | + } |
|
| 940 | + |
|
| 941 | + |
|
| 942 | + |
|
| 943 | + /** |
|
| 944 | + * Returns the next EE_Base_Class object in sequence from this object as found in the database |
|
| 945 | + * matching the given query conditions. |
|
| 946 | + * |
|
| 947 | + * @param null $field_to_order_by What field is being used as the reference point. |
|
| 948 | + * @param array $query_params Any additional conditions on the query. |
|
| 949 | + * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
|
| 950 | + * you can indicate just the columns you want returned |
|
| 951 | + * @return array|EE_Base_Class |
|
| 952 | + * @throws \EE_Error |
|
| 953 | + */ |
|
| 954 | + public function next($field_to_order_by = null, $query_params = array(), $columns_to_select = null) |
|
| 955 | + { |
|
| 956 | + $field = empty($field_to_order_by) && $this->get_model()->has_primary_key_field() |
|
| 957 | + ? $this->get_model()->get_primary_key_field()->get_name() |
|
| 958 | + : $field_to_order_by; |
|
| 959 | + $current_value = ! empty($field) ? $this->get($field) : null; |
|
| 960 | + if (empty($field) || empty($current_value)) { |
|
| 961 | + return array(); |
|
| 962 | + } |
|
| 963 | + return $this->get_model()->next($current_value, $field, $query_params, $columns_to_select); |
|
| 964 | + } |
|
| 965 | + |
|
| 966 | + |
|
| 967 | + |
|
| 968 | + /** |
|
| 969 | + * Returns the previous EE_Base_Class object in sequence from this object as found in the database |
|
| 970 | + * matching the given query conditions. |
|
| 971 | + * |
|
| 972 | + * @param null $field_to_order_by What field is being used as the reference point. |
|
| 973 | + * @param array $query_params Any additional conditions on the query. |
|
| 974 | + * @param null $columns_to_select If left null, then an EE_Base_Class object is returned, otherwise |
|
| 975 | + * you can indicate just the column you want returned |
|
| 976 | + * @return array|EE_Base_Class |
|
| 977 | + * @throws \EE_Error |
|
| 978 | + */ |
|
| 979 | + public function previous($field_to_order_by = null, $query_params = array(), $columns_to_select = null) |
|
| 980 | + { |
|
| 981 | + $field = empty($field_to_order_by) && $this->get_model()->has_primary_key_field() |
|
| 982 | + ? $this->get_model()->get_primary_key_field()->get_name() |
|
| 983 | + : $field_to_order_by; |
|
| 984 | + $current_value = ! empty($field) ? $this->get($field) : null; |
|
| 985 | + if (empty($field) || empty($current_value)) { |
|
| 986 | + return array(); |
|
| 987 | + } |
|
| 988 | + return $this->get_model()->previous($current_value, $field, $query_params, $columns_to_select); |
|
| 989 | + } |
|
| 990 | + |
|
| 991 | + |
|
| 992 | + |
|
| 993 | + /** |
|
| 994 | + * Overrides parent because parent expects old models. |
|
| 995 | + * This also doesn't do any validation, and won't work for serialized arrays |
|
| 996 | + * |
|
| 997 | + * @param string $field_name |
|
| 998 | + * @param mixed $field_value_from_db |
|
| 999 | + * @throws \EE_Error |
|
| 1000 | + */ |
|
| 1001 | + public function set_from_db($field_name, $field_value_from_db) |
|
| 1002 | + { |
|
| 1003 | + $field_obj = $this->get_model()->field_settings_for($field_name); |
|
| 1004 | + if ($field_obj instanceof EE_Model_Field_Base) { |
|
| 1005 | + //you would think the DB has no NULLs for non-null label fields right? wrong! |
|
| 1006 | + //eg, a CPT model object could have an entry in the posts table, but no |
|
| 1007 | + //entry in the meta table. Meaning that all its columns in the meta table |
|
| 1008 | + //are null! yikes! so when we find one like that, use defaults for its meta columns |
|
| 1009 | + if ($field_value_from_db === null) { |
|
| 1010 | + if ($field_obj->is_nullable()) { |
|
| 1011 | + //if the field allows nulls, then let it be null |
|
| 1012 | + $field_value = null; |
|
| 1013 | + } else { |
|
| 1014 | + $field_value = $field_obj->get_default_value(); |
|
| 1015 | + } |
|
| 1016 | + } else { |
|
| 1017 | + $field_value = $field_obj->prepare_for_set_from_db($field_value_from_db); |
|
| 1018 | + } |
|
| 1019 | + $this->_fields[$field_name] = $field_value; |
|
| 1020 | + $this->_clear_cached_property($field_name); |
|
| 1021 | + } |
|
| 1022 | + } |
|
| 1023 | + |
|
| 1024 | + |
|
| 1025 | + |
|
| 1026 | + /** |
|
| 1027 | + * verifies that the specified field is of the correct type |
|
| 1028 | + * |
|
| 1029 | + * @param string $field_name |
|
| 1030 | + * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
| 1031 | + * (in cases where the same property may be used for different outputs |
|
| 1032 | + * - i.e. datetime, money etc.) |
|
| 1033 | + * @return mixed |
|
| 1034 | + * @throws \EE_Error |
|
| 1035 | + */ |
|
| 1036 | + public function get($field_name, $extra_cache_ref = null) |
|
| 1037 | + { |
|
| 1038 | + return $this->_get_cached_property($field_name, false, $extra_cache_ref); |
|
| 1039 | + } |
|
| 1040 | + |
|
| 1041 | + |
|
| 1042 | + |
|
| 1043 | + /** |
|
| 1044 | + * This method simply returns the RAW unprocessed value for the given property in this class |
|
| 1045 | + * |
|
| 1046 | + * @param string $field_name A valid fieldname |
|
| 1047 | + * @return mixed Whatever the raw value stored on the property is. |
|
| 1048 | + * @throws EE_Error if fieldSettings is misconfigured or the field doesn't exist. |
|
| 1049 | + */ |
|
| 1050 | + public function get_raw($field_name) |
|
| 1051 | + { |
|
| 1052 | + $field_settings = $this->get_model()->field_settings_for($field_name); |
|
| 1053 | + return $field_settings instanceof EE_Datetime_Field && $this->_fields[$field_name] instanceof DateTime |
|
| 1054 | + ? $this->_fields[$field_name]->format('U') |
|
| 1055 | + : $this->_fields[$field_name]; |
|
| 1056 | + } |
|
| 1057 | + |
|
| 1058 | + |
|
| 1059 | + |
|
| 1060 | + /** |
|
| 1061 | + * This is used to return the internal DateTime object used for a field that is a |
|
| 1062 | + * EE_Datetime_Field. |
|
| 1063 | + * |
|
| 1064 | + * @param string $field_name The field name retrieving the DateTime object. |
|
| 1065 | + * @return mixed null | false | DateTime If the requested field is NOT a EE_Datetime_Field then |
|
| 1066 | + * @throws \EE_Error |
|
| 1067 | + * an error is set and false returned. If the field IS an |
|
| 1068 | + * EE_Datetime_Field and but the field value is null, then |
|
| 1069 | + * just null is returned (because that indicates that likely |
|
| 1070 | + * this field is nullable). |
|
| 1071 | + */ |
|
| 1072 | + public function get_DateTime_object($field_name) |
|
| 1073 | + { |
|
| 1074 | + $field_settings = $this->get_model()->field_settings_for($field_name); |
|
| 1075 | + if ( ! $field_settings instanceof EE_Datetime_Field) { |
|
| 1076 | + EE_Error::add_error( |
|
| 1077 | + sprintf( |
|
| 1078 | + __( |
|
| 1079 | + 'The field %s is not an EE_Datetime_Field field. There is no DateTime object stored on this field type.', |
|
| 1080 | + 'event_espresso' |
|
| 1081 | + ), |
|
| 1082 | + $field_name |
|
| 1083 | + ), |
|
| 1084 | + __FILE__, |
|
| 1085 | + __FUNCTION__, |
|
| 1086 | + __LINE__ |
|
| 1087 | + ); |
|
| 1088 | + return false; |
|
| 1089 | + } |
|
| 1090 | + return $this->_fields[$field_name]; |
|
| 1091 | + } |
|
| 1092 | + |
|
| 1093 | + |
|
| 1094 | + |
|
| 1095 | + /** |
|
| 1096 | + * To be used in template to immediately echo out the value, and format it for output. |
|
| 1097 | + * Eg, should call stripslashes and whatnot before echoing |
|
| 1098 | + * |
|
| 1099 | + * @param string $field_name the name of the field as it appears in the DB |
|
| 1100 | + * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
| 1101 | + * (in cases where the same property may be used for different outputs |
|
| 1102 | + * - i.e. datetime, money etc.) |
|
| 1103 | + * @return void |
|
| 1104 | + * @throws \EE_Error |
|
| 1105 | + */ |
|
| 1106 | + public function e($field_name, $extra_cache_ref = null) |
|
| 1107 | + { |
|
| 1108 | + echo $this->get_pretty($field_name, $extra_cache_ref); |
|
| 1109 | + } |
|
| 1110 | + |
|
| 1111 | + |
|
| 1112 | + |
|
| 1113 | + /** |
|
| 1114 | + * Exactly like e(), echoes out the field, but sets its schema to 'form_input', so that it |
|
| 1115 | + * can be easily used as the value of form input. |
|
| 1116 | + * |
|
| 1117 | + * @param string $field_name |
|
| 1118 | + * @return void |
|
| 1119 | + * @throws \EE_Error |
|
| 1120 | + */ |
|
| 1121 | + public function f($field_name) |
|
| 1122 | + { |
|
| 1123 | + $this->e($field_name, 'form_input'); |
|
| 1124 | + } |
|
| 1125 | + |
|
| 1126 | + |
|
| 1127 | + |
|
| 1128 | + /** |
|
| 1129 | + * Gets a pretty view of the field's value. $extra_cache_ref can specify different formats for this. |
|
| 1130 | + * The $extra_cache_ref will be passed to the model field's prepare_for_pretty_echoing, so consult the field's class |
|
| 1131 | + * to see what options are available. |
|
| 1132 | + * @param string $field_name |
|
| 1133 | + * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
| 1134 | + * (in cases where the same property may be used for different outputs |
|
| 1135 | + * - i.e. datetime, money etc.) |
|
| 1136 | + * @return mixed |
|
| 1137 | + * @throws \EE_Error |
|
| 1138 | + */ |
|
| 1139 | + public function get_pretty($field_name, $extra_cache_ref = null) |
|
| 1140 | + { |
|
| 1141 | + return $this->_get_cached_property($field_name, true, $extra_cache_ref); |
|
| 1142 | + } |
|
| 1143 | + |
|
| 1144 | + |
|
| 1145 | + |
|
| 1146 | + /** |
|
| 1147 | + * This simply returns the datetime for the given field name |
|
| 1148 | + * Note: this protected function is called by the wrapper get_date or get_time or get_datetime functions |
|
| 1149 | + * (and the equivalent e_date, e_time, e_datetime). |
|
| 1150 | + * |
|
| 1151 | + * @access protected |
|
| 1152 | + * @param string $field_name Field on the instantiated EE_Base_Class child object |
|
| 1153 | + * @param string $dt_frmt valid datetime format used for date |
|
| 1154 | + * (if '' then we just use the default on the field, |
|
| 1155 | + * if NULL we use the last-used format) |
|
| 1156 | + * @param string $tm_frmt Same as above except this is for time format |
|
| 1157 | + * @param string $date_or_time if NULL then both are returned, otherwise "D" = only date and "T" = only time. |
|
| 1158 | + * @param boolean $echo Whether the dtt is echoing using pretty echoing or just returned using vanilla get |
|
| 1159 | + * @return string|bool|EE_Error string on success, FALSE on fail, or EE_Error Exception is thrown |
|
| 1160 | + * if field is not a valid dtt field, or void if echoing |
|
| 1161 | + * @throws \EE_Error |
|
| 1162 | + */ |
|
| 1163 | + protected function _get_datetime($field_name, $dt_frmt = '', $tm_frmt = '', $date_or_time = '', $echo = false) |
|
| 1164 | + { |
|
| 1165 | + // clear cached property |
|
| 1166 | + $this->_clear_cached_property($field_name); |
|
| 1167 | + //reset format properties because they are used in get() |
|
| 1168 | + $this->_dt_frmt = $dt_frmt !== '' ? $dt_frmt : $this->_dt_frmt; |
|
| 1169 | + $this->_tm_frmt = $tm_frmt !== '' ? $tm_frmt : $this->_tm_frmt; |
|
| 1170 | + if ($echo) { |
|
| 1171 | + $this->e($field_name, $date_or_time); |
|
| 1172 | + return ''; |
|
| 1173 | + } |
|
| 1174 | + return $this->get($field_name, $date_or_time); |
|
| 1175 | + } |
|
| 1176 | + |
|
| 1177 | + |
|
| 1178 | + |
|
| 1179 | + /** |
|
| 1180 | + * below are wrapper functions for the various datetime outputs that can be obtained for JUST returning the date |
|
| 1181 | + * portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
|
| 1182 | + * other echoes the pretty value for dtt) |
|
| 1183 | + * |
|
| 1184 | + * @param string $field_name name of model object datetime field holding the value |
|
| 1185 | + * @param string $format format for the date returned (if NULL we use default in dt_frmt property) |
|
| 1186 | + * @return string datetime value formatted |
|
| 1187 | + * @throws \EE_Error |
|
| 1188 | + */ |
|
| 1189 | + public function get_date($field_name, $format = '') |
|
| 1190 | + { |
|
| 1191 | + return $this->_get_datetime($field_name, $format, null, 'D'); |
|
| 1192 | + } |
|
| 1193 | + |
|
| 1194 | + |
|
| 1195 | + |
|
| 1196 | + /** |
|
| 1197 | + * @param $field_name |
|
| 1198 | + * @param string $format |
|
| 1199 | + * @throws \EE_Error |
|
| 1200 | + */ |
|
| 1201 | + public function e_date($field_name, $format = '') |
|
| 1202 | + { |
|
| 1203 | + $this->_get_datetime($field_name, $format, null, 'D', true); |
|
| 1204 | + } |
|
| 1205 | + |
|
| 1206 | + |
|
| 1207 | + |
|
| 1208 | + /** |
|
| 1209 | + * below are wrapper functions for the various datetime outputs that can be obtained for JUST returning the time |
|
| 1210 | + * portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
|
| 1211 | + * other echoes the pretty value for dtt) |
|
| 1212 | + * |
|
| 1213 | + * @param string $field_name name of model object datetime field holding the value |
|
| 1214 | + * @param string $format format for the time returned ( if NULL we use default in tm_frmt property) |
|
| 1215 | + * @return string datetime value formatted |
|
| 1216 | + * @throws \EE_Error |
|
| 1217 | + */ |
|
| 1218 | + public function get_time($field_name, $format = '') |
|
| 1219 | + { |
|
| 1220 | + return $this->_get_datetime($field_name, null, $format, 'T'); |
|
| 1221 | + } |
|
| 1222 | + |
|
| 1223 | + |
|
| 1224 | + |
|
| 1225 | + /** |
|
| 1226 | + * @param $field_name |
|
| 1227 | + * @param string $format |
|
| 1228 | + * @throws \EE_Error |
|
| 1229 | + */ |
|
| 1230 | + public function e_time($field_name, $format = '') |
|
| 1231 | + { |
|
| 1232 | + $this->_get_datetime($field_name, null, $format, 'T', true); |
|
| 1233 | + } |
|
| 1234 | + |
|
| 1235 | + |
|
| 1236 | + |
|
| 1237 | + /** |
|
| 1238 | + * below are wrapper functions for the various datetime outputs that can be obtained for returning the date AND |
|
| 1239 | + * time portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
|
| 1240 | + * other echoes the pretty value for dtt) |
|
| 1241 | + * |
|
| 1242 | + * @param string $field_name name of model object datetime field holding the value |
|
| 1243 | + * @param string $dt_frmt format for the date returned (if NULL we use default in dt_frmt property) |
|
| 1244 | + * @param string $tm_frmt format for the time returned (if NULL we use default in tm_frmt property) |
|
| 1245 | + * @return string datetime value formatted |
|
| 1246 | + * @throws \EE_Error |
|
| 1247 | + */ |
|
| 1248 | + public function get_datetime($field_name, $dt_frmt = '', $tm_frmt = '') |
|
| 1249 | + { |
|
| 1250 | + return $this->_get_datetime($field_name, $dt_frmt, $tm_frmt); |
|
| 1251 | + } |
|
| 1252 | + |
|
| 1253 | + |
|
| 1254 | + |
|
| 1255 | + /** |
|
| 1256 | + * @param string $field_name |
|
| 1257 | + * @param string $dt_frmt |
|
| 1258 | + * @param string $tm_frmt |
|
| 1259 | + * @throws \EE_Error |
|
| 1260 | + */ |
|
| 1261 | + public function e_datetime($field_name, $dt_frmt = '', $tm_frmt = '') |
|
| 1262 | + { |
|
| 1263 | + $this->_get_datetime($field_name, $dt_frmt, $tm_frmt, null, true); |
|
| 1264 | + } |
|
| 1265 | + |
|
| 1266 | + |
|
| 1267 | + |
|
| 1268 | + /** |
|
| 1269 | + * Get the i8ln value for a date using the WordPress @see date_i18n function. |
|
| 1270 | + * |
|
| 1271 | + * @param string $field_name The EE_Datetime_Field reference for the date being retrieved. |
|
| 1272 | + * @param string $format PHP valid date/time string format. If none is provided then the internal set format |
|
| 1273 | + * on the object will be used. |
|
| 1274 | + * @return string Date and time string in set locale or false if no field exists for the given |
|
| 1275 | + * @throws \EE_Error |
|
| 1276 | + * field name. |
|
| 1277 | + */ |
|
| 1278 | + public function get_i18n_datetime($field_name, $format = '') |
|
| 1279 | + { |
|
| 1280 | + $format = empty($format) ? $this->_dt_frmt . ' ' . $this->_tm_frmt : $format; |
|
| 1281 | + return date_i18n( |
|
| 1282 | + $format, |
|
| 1283 | + EEH_DTT_Helper::get_timestamp_with_offset($this->get_raw($field_name), $this->_timezone) |
|
| 1284 | + ); |
|
| 1285 | + } |
|
| 1286 | + |
|
| 1287 | + |
|
| 1288 | + |
|
| 1289 | + /** |
|
| 1290 | + * This method validates whether the given field name is a valid field on the model object as well as it is of a |
|
| 1291 | + * type EE_Datetime_Field. On success there will be returned the field settings. On fail an EE_Error exception is |
|
| 1292 | + * thrown. |
|
| 1293 | + * |
|
| 1294 | + * @param string $field_name The field name being checked |
|
| 1295 | + * @throws EE_Error |
|
| 1296 | + * @return EE_Datetime_Field |
|
| 1297 | + */ |
|
| 1298 | + protected function _get_dtt_field_settings($field_name) |
|
| 1299 | + { |
|
| 1300 | + $field = $this->get_model()->field_settings_for($field_name); |
|
| 1301 | + //check if field is dtt |
|
| 1302 | + if ($field instanceof EE_Datetime_Field) { |
|
| 1303 | + return $field; |
|
| 1304 | + } else { |
|
| 1305 | + throw new EE_Error(sprintf(__('The field name "%s" has been requested for the EE_Base_Class datetime functions and it is not a valid EE_Datetime_Field. Please check the spelling of the field and make sure it has been setup as a EE_Datetime_Field in the %s model constructor', |
|
| 1306 | + 'event_espresso'), $field_name, self::_get_model_classname(get_class($this)))); |
|
| 1307 | + } |
|
| 1308 | + } |
|
| 1309 | + |
|
| 1310 | + |
|
| 1311 | + |
|
| 1312 | + |
|
| 1313 | + /** |
|
| 1314 | + * NOTE ABOUT BELOW: |
|
| 1315 | + * These convenience date and time setters are for setting date and time independently. In other words you might |
|
| 1316 | + * want to change the time on a datetime_field but leave the date the same (or vice versa). IF on the other hand |
|
| 1317 | + * you want to set both date and time at the same time, you can just use the models default set($fieldname,$value) |
|
| 1318 | + * method and make sure you send the entire datetime value for setting. |
|
| 1319 | + */ |
|
| 1320 | + /** |
|
| 1321 | + * sets the time on a datetime property |
|
| 1322 | + * |
|
| 1323 | + * @access protected |
|
| 1324 | + * @param string|Datetime $time a valid time string for php datetime functions (or DateTime object) |
|
| 1325 | + * @param string $fieldname the name of the field the time is being set on (must match a EE_Datetime_Field) |
|
| 1326 | + * @throws \EE_Error |
|
| 1327 | + */ |
|
| 1328 | + protected function _set_time_for($time, $fieldname) |
|
| 1329 | + { |
|
| 1330 | + $this->_set_date_time('T', $time, $fieldname); |
|
| 1331 | + } |
|
| 1332 | + |
|
| 1333 | + |
|
| 1334 | + |
|
| 1335 | + /** |
|
| 1336 | + * sets the date on a datetime property |
|
| 1337 | + * |
|
| 1338 | + * @access protected |
|
| 1339 | + * @param string|DateTime $date a valid date string for php datetime functions ( or DateTime object) |
|
| 1340 | + * @param string $fieldname the name of the field the date is being set on (must match a EE_Datetime_Field) |
|
| 1341 | + * @throws \EE_Error |
|
| 1342 | + */ |
|
| 1343 | + protected function _set_date_for($date, $fieldname) |
|
| 1344 | + { |
|
| 1345 | + $this->_set_date_time('D', $date, $fieldname); |
|
| 1346 | + } |
|
| 1347 | + |
|
| 1348 | + |
|
| 1349 | + |
|
| 1350 | + /** |
|
| 1351 | + * This takes care of setting a date or time independently on a given model object property. This method also |
|
| 1352 | + * verifies that the given fieldname matches a model object property and is for a EE_Datetime_Field field |
|
| 1353 | + * |
|
| 1354 | + * @access protected |
|
| 1355 | + * @param string $what "T" for time, 'B' for both, 'D' for Date. |
|
| 1356 | + * @param string|DateTime $datetime_value A valid Date or Time string (or DateTime object) |
|
| 1357 | + * @param string $fieldname the name of the field the date OR time is being set on (must match a |
|
| 1358 | + * EE_Datetime_Field property) |
|
| 1359 | + * @throws \EE_Error |
|
| 1360 | + */ |
|
| 1361 | + protected function _set_date_time($what = 'T', $datetime_value, $fieldname) |
|
| 1362 | + { |
|
| 1363 | + $field = $this->_get_dtt_field_settings($fieldname); |
|
| 1364 | + $field->set_timezone($this->_timezone); |
|
| 1365 | + $field->set_date_format($this->_dt_frmt); |
|
| 1366 | + $field->set_time_format($this->_tm_frmt); |
|
| 1367 | + switch ($what) { |
|
| 1368 | + case 'T' : |
|
| 1369 | + $this->_fields[$fieldname] = $field->prepare_for_set_with_new_time( |
|
| 1370 | + $datetime_value, |
|
| 1371 | + $this->_fields[$fieldname] |
|
| 1372 | + ); |
|
| 1373 | + break; |
|
| 1374 | + case 'D' : |
|
| 1375 | + $this->_fields[$fieldname] = $field->prepare_for_set_with_new_date( |
|
| 1376 | + $datetime_value, |
|
| 1377 | + $this->_fields[$fieldname] |
|
| 1378 | + ); |
|
| 1379 | + break; |
|
| 1380 | + case 'B' : |
|
| 1381 | + $this->_fields[$fieldname] = $field->prepare_for_set($datetime_value); |
|
| 1382 | + break; |
|
| 1383 | + } |
|
| 1384 | + $this->_clear_cached_property($fieldname); |
|
| 1385 | + } |
|
| 1386 | + |
|
| 1387 | + |
|
| 1388 | + |
|
| 1389 | + /** |
|
| 1390 | + * This will return a timestamp for the website timezone but ONLY when the current website timezone is different |
|
| 1391 | + * than the timezone set for the website. NOTE, this currently only works well with methods that return values. If |
|
| 1392 | + * you use it with methods that echo values the $_timestamp property may not get reset to its original value and |
|
| 1393 | + * that could lead to some unexpected results! |
|
| 1394 | + * |
|
| 1395 | + * @access public |
|
| 1396 | + * @param string $field_name This is the name of the field on the object that contains the date/time |
|
| 1397 | + * value being returned. |
|
| 1398 | + * @param string $callback must match a valid method in this class (defaults to get_datetime) |
|
| 1399 | + * @param mixed (array|string) $args This is the arguments that will be passed to the callback. |
|
| 1400 | + * @param string $prepend You can include something to prepend on the timestamp |
|
| 1401 | + * @param string $append You can include something to append on the timestamp |
|
| 1402 | + * @throws EE_Error |
|
| 1403 | + * @return string timestamp |
|
| 1404 | + */ |
|
| 1405 | + public function display_in_my_timezone( |
|
| 1406 | + $field_name, |
|
| 1407 | + $callback = 'get_datetime', |
|
| 1408 | + $args = null, |
|
| 1409 | + $prepend = '', |
|
| 1410 | + $append = '' |
|
| 1411 | + ) { |
|
| 1412 | + $timezone = EEH_DTT_Helper::get_timezone(); |
|
| 1413 | + if ($timezone === $this->_timezone) { |
|
| 1414 | + return ''; |
|
| 1415 | + } |
|
| 1416 | + $original_timezone = $this->_timezone; |
|
| 1417 | + $this->set_timezone($timezone); |
|
| 1418 | + $fn = (array)$field_name; |
|
| 1419 | + $args = array_merge($fn, (array)$args); |
|
| 1420 | + if ( ! method_exists($this, $callback)) { |
|
| 1421 | + throw new EE_Error( |
|
| 1422 | + sprintf( |
|
| 1423 | + __( |
|
| 1424 | + 'The method named "%s" given as the callback param in "display_in_my_timezone" does not exist. Please check your spelling', |
|
| 1425 | + 'event_espresso' |
|
| 1426 | + ), |
|
| 1427 | + $callback |
|
| 1428 | + ) |
|
| 1429 | + ); |
|
| 1430 | + } |
|
| 1431 | + $args = (array)$args; |
|
| 1432 | + $return = $prepend . call_user_func_array(array($this, $callback), $args) . $append; |
|
| 1433 | + $this->set_timezone($original_timezone); |
|
| 1434 | + return $return; |
|
| 1435 | + } |
|
| 1436 | + |
|
| 1437 | + |
|
| 1438 | + |
|
| 1439 | + /** |
|
| 1440 | + * Deletes this model object. |
|
| 1441 | + * This calls the `EE_Base_Class::_delete` method. Child classes wishing to change default behaviour should |
|
| 1442 | + * override |
|
| 1443 | + * `EE_Base_Class::_delete` NOT this class. |
|
| 1444 | + * |
|
| 1445 | + * @return boolean | int |
|
| 1446 | + * @throws \EE_Error |
|
| 1447 | + */ |
|
| 1448 | + public function delete() |
|
| 1449 | + { |
|
| 1450 | + /** |
|
| 1451 | + * Called just before the `EE_Base_Class::_delete` method call. |
|
| 1452 | + * Note: `EE_Base_Class::_delete` might be overridden by child classes so any client code hooking into these actions |
|
| 1453 | + * should be aware that `_delete` may not always result in a permanent delete. For example, `EE_Soft_Delete_Base_Class::_delete` |
|
| 1454 | + * soft deletes (trash) the object and does not permanently delete it. |
|
| 1455 | + * |
|
| 1456 | + * @param EE_Base_Class $model_object about to be 'deleted' |
|
| 1457 | + */ |
|
| 1458 | + do_action('AHEE__EE_Base_Class__delete__before', $this); |
|
| 1459 | + $result = $this->_delete(); |
|
| 1460 | + /** |
|
| 1461 | + * Called just after the `EE_Base_Class::_delete` method call. |
|
| 1462 | + * Note: `EE_Base_Class::_delete` might be overridden by child classes so any client code hooking into these actions |
|
| 1463 | + * should be aware that `_delete` may not always result in a permanent delete. For example `EE_Soft_Base_Class::_delete` |
|
| 1464 | + * soft deletes (trash) the object and does not permanently delete it. |
|
| 1465 | + * |
|
| 1466 | + * @param EE_Base_Class $model_object that was just 'deleted' |
|
| 1467 | + * @param boolean $result |
|
| 1468 | + */ |
|
| 1469 | + do_action('AHEE__EE_Base_Class__delete__end', $this, $result); |
|
| 1470 | + return $result; |
|
| 1471 | + } |
|
| 1472 | + |
|
| 1473 | + |
|
| 1474 | + |
|
| 1475 | + /** |
|
| 1476 | + * Calls the specific delete method for the instantiated class. |
|
| 1477 | + * This method is called by the public `EE_Base_Class::delete` method. Any child classes desiring to override |
|
| 1478 | + * default functionality for "delete" (which is to call `permanently_delete`) should override this method NOT |
|
| 1479 | + * `EE_Base_Class::delete` |
|
| 1480 | + * |
|
| 1481 | + * @return bool|int |
|
| 1482 | + * @throws \EE_Error |
|
| 1483 | + */ |
|
| 1484 | + protected function _delete() |
|
| 1485 | + { |
|
| 1486 | + return $this->delete_permanently(); |
|
| 1487 | + } |
|
| 1488 | + |
|
| 1489 | + |
|
| 1490 | + |
|
| 1491 | + /** |
|
| 1492 | + * Deletes this model object permanently from db (but keep in mind related models my block the delete and return an |
|
| 1493 | + * error) |
|
| 1494 | + * |
|
| 1495 | + * @return bool | int |
|
| 1496 | + * @throws \EE_Error |
|
| 1497 | + */ |
|
| 1498 | + public function delete_permanently() |
|
| 1499 | + { |
|
| 1500 | + /** |
|
| 1501 | + * Called just before HARD deleting a model object |
|
| 1502 | + * |
|
| 1503 | + * @param EE_Base_Class $model_object about to be 'deleted' |
|
| 1504 | + */ |
|
| 1505 | + do_action('AHEE__EE_Base_Class__delete_permanently__before', $this); |
|
| 1506 | + $model = $this->get_model(); |
|
| 1507 | + $result = $model->delete_permanently_by_ID($this->ID()); |
|
| 1508 | + $this->refresh_cache_of_related_objects(); |
|
| 1509 | + /** |
|
| 1510 | + * Called just after HARD deleting a model object |
|
| 1511 | + * |
|
| 1512 | + * @param EE_Base_Class $model_object that was just 'deleted' |
|
| 1513 | + * @param boolean $result |
|
| 1514 | + */ |
|
| 1515 | + do_action('AHEE__EE_Base_Class__delete_permanently__end', $this, $result); |
|
| 1516 | + return $result; |
|
| 1517 | + } |
|
| 1518 | + |
|
| 1519 | + |
|
| 1520 | + |
|
| 1521 | + /** |
|
| 1522 | + * When this model object is deleted, it may still be cached on related model objects. This clears the cache of |
|
| 1523 | + * related model objects |
|
| 1524 | + * |
|
| 1525 | + * @throws \EE_Error |
|
| 1526 | + */ |
|
| 1527 | + public function refresh_cache_of_related_objects() |
|
| 1528 | + { |
|
| 1529 | + foreach ($this->get_model()->relation_settings() as $relation_name => $relation_obj) { |
|
| 1530 | + if ( ! empty($this->_model_relations[$relation_name])) { |
|
| 1531 | + $related_objects = $this->_model_relations[$relation_name]; |
|
| 1532 | + if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 1533 | + //this relation only stores a single model object, not an array |
|
| 1534 | + //but let's make it consistent |
|
| 1535 | + $related_objects = array($related_objects); |
|
| 1536 | + } |
|
| 1537 | + foreach ($related_objects as $related_object) { |
|
| 1538 | + //only refresh their cache if they're in memory |
|
| 1539 | + if ($related_object instanceof EE_Base_Class) { |
|
| 1540 | + $related_object->clear_cache($this->get_model()->get_this_model_name(), $this); |
|
| 1541 | + } |
|
| 1542 | + } |
|
| 1543 | + } |
|
| 1544 | + } |
|
| 1545 | + } |
|
| 1546 | + |
|
| 1547 | + |
|
| 1548 | + |
|
| 1549 | + /** |
|
| 1550 | + * Saves this object to the database. An array may be supplied to set some values on this |
|
| 1551 | + * object just before saving. |
|
| 1552 | + * |
|
| 1553 | + * @access public |
|
| 1554 | + * @param array $set_cols_n_values keys are field names, values are their new values, |
|
| 1555 | + * if provided during the save() method (often client code will change the fields' |
|
| 1556 | + * values before calling save) |
|
| 1557 | + * @throws \EE_Error |
|
| 1558 | + * @return int , 1 on a successful update, the ID of the new entry on insert; 0 on failure or if the model object |
|
| 1559 | + * isn't allowed to persist (as determined by EE_Base_Class::allow_persist()) |
|
| 1560 | + */ |
|
| 1561 | + public function save($set_cols_n_values = array()) |
|
| 1562 | + { |
|
| 1563 | + /** |
|
| 1564 | + * Filters the fields we're about to save on the model object |
|
| 1565 | + * |
|
| 1566 | + * @param array $set_cols_n_values |
|
| 1567 | + * @param EE_Base_Class $model_object |
|
| 1568 | + */ |
|
| 1569 | + $set_cols_n_values = (array)apply_filters('FHEE__EE_Base_Class__save__set_cols_n_values', $set_cols_n_values, |
|
| 1570 | + $this); |
|
| 1571 | + //set attributes as provided in $set_cols_n_values |
|
| 1572 | + foreach ($set_cols_n_values as $column => $value) { |
|
| 1573 | + $this->set($column, $value); |
|
| 1574 | + } |
|
| 1575 | + // no changes ? then don't do anything |
|
| 1576 | + if (! $this->_has_changes && $this->ID() && $this->get_model()->get_primary_key_field()->is_auto_increment()) { |
|
| 1577 | + return 0; |
|
| 1578 | + } |
|
| 1579 | + /** |
|
| 1580 | + * Saving a model object. |
|
| 1581 | + * Before we perform a save, this action is fired. |
|
| 1582 | + * |
|
| 1583 | + * @param EE_Base_Class $model_object the model object about to be saved. |
|
| 1584 | + */ |
|
| 1585 | + do_action('AHEE__EE_Base_Class__save__begin', $this); |
|
| 1586 | + if ( ! $this->allow_persist()) { |
|
| 1587 | + return 0; |
|
| 1588 | + } |
|
| 1589 | + //now get current attribute values |
|
| 1590 | + $save_cols_n_values = $this->_fields; |
|
| 1591 | + //if the object already has an ID, update it. Otherwise, insert it |
|
| 1592 | + //also: change the assumption about values passed to the model NOT being prepare dby the model object. They have been |
|
| 1593 | + $old_assumption_concerning_value_preparation = $this->get_model() |
|
| 1594 | + ->get_assumption_concerning_values_already_prepared_by_model_object(); |
|
| 1595 | + $this->get_model()->assume_values_already_prepared_by_model_object(true); |
|
| 1596 | + //does this model have an autoincrement PK? |
|
| 1597 | + if ($this->get_model()->has_primary_key_field()) { |
|
| 1598 | + if ($this->get_model()->get_primary_key_field()->is_auto_increment()) { |
|
| 1599 | + //ok check if it's set, if so: update; if not, insert |
|
| 1600 | + if ( ! empty($save_cols_n_values[self::_get_primary_key_name(get_class($this))])) { |
|
| 1601 | + $results = $this->get_model()->update_by_ID($save_cols_n_values, $this->ID()); |
|
| 1602 | + } else { |
|
| 1603 | + unset($save_cols_n_values[self::_get_primary_key_name(get_class($this))]); |
|
| 1604 | + $results = $this->get_model()->insert($save_cols_n_values); |
|
| 1605 | + if ($results) { |
|
| 1606 | + //if successful, set the primary key |
|
| 1607 | + //but don't use the normal SET method, because it will check if |
|
| 1608 | + //an item with the same ID exists in the mapper & db, then |
|
| 1609 | + //will find it in the db (because we just added it) and THAT object |
|
| 1610 | + //will get added to the mapper before we can add this one! |
|
| 1611 | + //but if we just avoid using the SET method, all that headache can be avoided |
|
| 1612 | + $pk_field_name = self::_get_primary_key_name(get_class($this)); |
|
| 1613 | + $this->_fields[$pk_field_name] = $results; |
|
| 1614 | + $this->_clear_cached_property($pk_field_name); |
|
| 1615 | + $this->get_model()->add_to_entity_map($this); |
|
| 1616 | + $this->_update_cached_related_model_objs_fks(); |
|
| 1617 | + } |
|
| 1618 | + } |
|
| 1619 | + } else {//PK is NOT auto-increment |
|
| 1620 | + //so check if one like it already exists in the db |
|
| 1621 | + if ($this->get_model()->exists_by_ID($this->ID())) { |
|
| 1622 | + if (WP_DEBUG && ! $this->in_entity_map()) { |
|
| 1623 | + throw new EE_Error( |
|
| 1624 | + sprintf( |
|
| 1625 | + __('Using a model object %1$s that is NOT in the entity map, can lead to unexpected errors. You should either: %4$s 1. Put it in the entity mapper by calling %2$s %4$s 2. Discard this model object and use what is in the entity mapper %4$s 3. Fetch from the database using %3$s', |
|
| 1626 | + 'event_espresso'), |
|
| 1627 | + get_class($this), |
|
| 1628 | + get_class($this->get_model()) . '::instance()->add_to_entity_map()', |
|
| 1629 | + get_class($this->get_model()) . '::instance()->get_one_by_ID()', |
|
| 1630 | + '<br />' |
|
| 1631 | + ) |
|
| 1632 | + ); |
|
| 1633 | + } |
|
| 1634 | + $results = $this->get_model()->update_by_ID($save_cols_n_values, $this->ID()); |
|
| 1635 | + } else { |
|
| 1636 | + $results = $this->get_model()->insert($save_cols_n_values); |
|
| 1637 | + $this->_update_cached_related_model_objs_fks(); |
|
| 1638 | + } |
|
| 1639 | + } |
|
| 1640 | + } else {//there is NO primary key |
|
| 1641 | + $already_in_db = false; |
|
| 1642 | + foreach ($this->get_model()->unique_indexes() as $index) { |
|
| 1643 | + $uniqueness_where_params = array_intersect_key($save_cols_n_values, $index->fields()); |
|
| 1644 | + if ($this->get_model()->exists(array($uniqueness_where_params))) { |
|
| 1645 | + $already_in_db = true; |
|
| 1646 | + } |
|
| 1647 | + } |
|
| 1648 | + if ($already_in_db) { |
|
| 1649 | + $combined_pk_fields_n_values = array_intersect_key($save_cols_n_values, |
|
| 1650 | + $this->get_model()->get_combined_primary_key_fields()); |
|
| 1651 | + $results = $this->get_model()->update($save_cols_n_values, $combined_pk_fields_n_values); |
|
| 1652 | + } else { |
|
| 1653 | + $results = $this->get_model()->insert($save_cols_n_values); |
|
| 1654 | + } |
|
| 1655 | + } |
|
| 1656 | + //restore the old assumption about values being prepared by the model object |
|
| 1657 | + $this->get_model() |
|
| 1658 | + ->assume_values_already_prepared_by_model_object($old_assumption_concerning_value_preparation); |
|
| 1659 | + /** |
|
| 1660 | + * After saving the model object this action is called |
|
| 1661 | + * |
|
| 1662 | + * @param EE_Base_Class $model_object which was just saved |
|
| 1663 | + * @param boolean|int $results if it were updated, TRUE or FALSE; if it were newly inserted |
|
| 1664 | + * the new ID (or 0 if an error occurred and it wasn't updated) |
|
| 1665 | + */ |
|
| 1666 | + do_action('AHEE__EE_Base_Class__save__end', $this, $results); |
|
| 1667 | + $this->_has_changes = false; |
|
| 1668 | + return $results; |
|
| 1669 | + } |
|
| 1670 | + |
|
| 1671 | + |
|
| 1672 | + |
|
| 1673 | + /** |
|
| 1674 | + * Updates the foreign key on related models objects pointing to this to have this model object's ID |
|
| 1675 | + * as their foreign key. If the cached related model objects already exist in the db, saves them (so that the DB |
|
| 1676 | + * is consistent) Especially useful in case we JUST added this model object ot the database and we want to let its |
|
| 1677 | + * cached relations with foreign keys to it know about that change. Eg: we've created a transaction but haven't |
|
| 1678 | + * saved it to the db. We also create a registration and don't save it to the DB, but we DO cache it on the |
|
| 1679 | + * transaction. Now, when we save the transaction, the registration's TXN_ID will be automatically updated, whether |
|
| 1680 | + * or not they exist in the DB (if they do, their DB records will be automatically updated) |
|
| 1681 | + * |
|
| 1682 | + * @return void |
|
| 1683 | + * @throws \EE_Error |
|
| 1684 | + */ |
|
| 1685 | + protected function _update_cached_related_model_objs_fks() |
|
| 1686 | + { |
|
| 1687 | + foreach ($this->get_model()->relation_settings() as $relation_name => $relation_obj) { |
|
| 1688 | + if ($relation_obj instanceof EE_Has_Many_Relation) { |
|
| 1689 | + foreach ($this->get_all_from_cache($relation_name) as $related_model_obj_in_cache) { |
|
| 1690 | + $fk_to_this = $related_model_obj_in_cache->get_model()->get_foreign_key_to( |
|
| 1691 | + $this->get_model()->get_this_model_name() |
|
| 1692 | + ); |
|
| 1693 | + $related_model_obj_in_cache->set($fk_to_this->get_name(), $this->ID()); |
|
| 1694 | + if ($related_model_obj_in_cache->ID()) { |
|
| 1695 | + $related_model_obj_in_cache->save(); |
|
| 1696 | + } |
|
| 1697 | + } |
|
| 1698 | + } |
|
| 1699 | + } |
|
| 1700 | + } |
|
| 1701 | + |
|
| 1702 | + |
|
| 1703 | + |
|
| 1704 | + /** |
|
| 1705 | + * Saves this model object and its NEW cached relations to the database. |
|
| 1706 | + * (Meaning, for now, IT DOES NOT WORK if the cached items already exist in the DB. |
|
| 1707 | + * In order for that to work, we would need to mark model objects as dirty/clean... |
|
| 1708 | + * because otherwise, there's a potential for infinite looping of saving |
|
| 1709 | + * Saves the cached related model objects, and ensures the relation between them |
|
| 1710 | + * and this object and properly setup |
|
| 1711 | + * |
|
| 1712 | + * @return int ID of new model object on save; 0 on failure+ |
|
| 1713 | + * @throws \EE_Error |
|
| 1714 | + */ |
|
| 1715 | + public function save_new_cached_related_model_objs() |
|
| 1716 | + { |
|
| 1717 | + //make sure this has been saved |
|
| 1718 | + if ( ! $this->ID()) { |
|
| 1719 | + $id = $this->save(); |
|
| 1720 | + } else { |
|
| 1721 | + $id = $this->ID(); |
|
| 1722 | + } |
|
| 1723 | + //now save all the NEW cached model objects (ie they don't exist in the DB) |
|
| 1724 | + foreach ($this->get_model()->relation_settings() as $relationName => $relationObj) { |
|
| 1725 | + if ($this->_model_relations[$relationName]) { |
|
| 1726 | + //is this a relation where we should expect just ONE related object (ie, EE_Belongs_To_relation) |
|
| 1727 | + //or MANY related objects (ie, EE_HABTM_Relation or EE_Has_Many_Relation)? |
|
| 1728 | + if ($relationObj instanceof EE_Belongs_To_Relation) { |
|
| 1729 | + //add a relation to that relation type (which saves the appropriate thing in the process) |
|
| 1730 | + //but ONLY if it DOES NOT exist in the DB |
|
| 1731 | + /* @var $related_model_obj EE_Base_Class */ |
|
| 1732 | + $related_model_obj = $this->_model_relations[$relationName]; |
|
| 1733 | + // if( ! $related_model_obj->ID()){ |
|
| 1734 | + $this->_add_relation_to($related_model_obj, $relationName); |
|
| 1735 | + $related_model_obj->save_new_cached_related_model_objs(); |
|
| 1736 | + // } |
|
| 1737 | + } else { |
|
| 1738 | + foreach ($this->_model_relations[$relationName] as $related_model_obj) { |
|
| 1739 | + //add a relation to that relation type (which saves the appropriate thing in the process) |
|
| 1740 | + //but ONLY if it DOES NOT exist in the DB |
|
| 1741 | + // if( ! $related_model_obj->ID()){ |
|
| 1742 | + $this->_add_relation_to($related_model_obj, $relationName); |
|
| 1743 | + $related_model_obj->save_new_cached_related_model_objs(); |
|
| 1744 | + // } |
|
| 1745 | + } |
|
| 1746 | + } |
|
| 1747 | + } |
|
| 1748 | + } |
|
| 1749 | + return $id; |
|
| 1750 | + } |
|
| 1751 | + |
|
| 1752 | + |
|
| 1753 | + |
|
| 1754 | + /** |
|
| 1755 | + * for getting a model while instantiated. |
|
| 1756 | + * |
|
| 1757 | + * @return \EEM_Base | \EEM_CPT_Base |
|
| 1758 | + */ |
|
| 1759 | + public function get_model() |
|
| 1760 | + { |
|
| 1761 | + $modelName = self::_get_model_classname(get_class($this)); |
|
| 1762 | + return self::_get_model_instance_with_name($modelName, $this->_timezone); |
|
| 1763 | + } |
|
| 1764 | + |
|
| 1765 | + |
|
| 1766 | + |
|
| 1767 | + /** |
|
| 1768 | + * @param $props_n_values |
|
| 1769 | + * @param $classname |
|
| 1770 | + * @return mixed bool|EE_Base_Class|EEM_CPT_Base |
|
| 1771 | + * @throws \EE_Error |
|
| 1772 | + */ |
|
| 1773 | + protected static function _get_object_from_entity_mapper($props_n_values, $classname) |
|
| 1774 | + { |
|
| 1775 | + //TODO: will not work for Term_Relationships because they have no PK! |
|
| 1776 | + $primary_id_ref = self::_get_primary_key_name($classname); |
|
| 1777 | + if (array_key_exists($primary_id_ref, $props_n_values) && ! empty($props_n_values[$primary_id_ref])) { |
|
| 1778 | + $id = $props_n_values[$primary_id_ref]; |
|
| 1779 | + return self::_get_model($classname)->get_from_entity_map($id); |
|
| 1780 | + } |
|
| 1781 | + return false; |
|
| 1782 | + } |
|
| 1783 | + |
|
| 1784 | + |
|
| 1785 | + |
|
| 1786 | + /** |
|
| 1787 | + * This is called by child static "new_instance" method and we'll check to see if there is an existing db entry for |
|
| 1788 | + * the primary key (if present in incoming values). If there is a key in the incoming array that matches the |
|
| 1789 | + * primary key for the model AND it is not null, then we check the db. If there's a an object we return it. If not |
|
| 1790 | + * we return false. |
|
| 1791 | + * |
|
| 1792 | + * @param array $props_n_values incoming array of properties and their values |
|
| 1793 | + * @param string $classname the classname of the child class |
|
| 1794 | + * @param null $timezone |
|
| 1795 | + * @param array $date_formats incoming date_formats in an array where the first value is the |
|
| 1796 | + * date_format and the second value is the time format |
|
| 1797 | + * @return mixed (EE_Base_Class|bool) |
|
| 1798 | + * @throws \EE_Error |
|
| 1799 | + */ |
|
| 1800 | + protected static function _check_for_object($props_n_values, $classname, $timezone = null, $date_formats = array()) |
|
| 1801 | + { |
|
| 1802 | + $existing = null; |
|
| 1803 | + if (self::_get_model($classname)->has_primary_key_field()) { |
|
| 1804 | + $primary_id_ref = self::_get_primary_key_name($classname); |
|
| 1805 | + if (array_key_exists($primary_id_ref, $props_n_values) |
|
| 1806 | + && ! empty($props_n_values[$primary_id_ref]) |
|
| 1807 | + ) { |
|
| 1808 | + $existing = self::_get_model($classname, $timezone)->get_one_by_ID( |
|
| 1809 | + $props_n_values[$primary_id_ref] |
|
| 1810 | + ); |
|
| 1811 | + } |
|
| 1812 | + } elseif (self::_get_model($classname, $timezone)->has_all_combined_primary_key_fields($props_n_values)) { |
|
| 1813 | + //no primary key on this model, but there's still a matching item in the DB |
|
| 1814 | + $existing = self::_get_model($classname, $timezone)->get_one_by_ID( |
|
| 1815 | + self::_get_model($classname, $timezone)->get_index_primary_key_string($props_n_values) |
|
| 1816 | + ); |
|
| 1817 | + } |
|
| 1818 | + if ($existing) { |
|
| 1819 | + //set date formats if present before setting values |
|
| 1820 | + if ( ! empty($date_formats) && is_array($date_formats)) { |
|
| 1821 | + $existing->set_date_format($date_formats[0]); |
|
| 1822 | + $existing->set_time_format($date_formats[1]); |
|
| 1823 | + } else { |
|
| 1824 | + //set default formats for date and time |
|
| 1825 | + $existing->set_date_format(get_option('date_format')); |
|
| 1826 | + $existing->set_time_format(get_option('time_format')); |
|
| 1827 | + } |
|
| 1828 | + foreach ($props_n_values as $property => $field_value) { |
|
| 1829 | + $existing->set($property, $field_value); |
|
| 1830 | + } |
|
| 1831 | + return $existing; |
|
| 1832 | + } else { |
|
| 1833 | + return false; |
|
| 1834 | + } |
|
| 1835 | + } |
|
| 1836 | + |
|
| 1837 | + |
|
| 1838 | + |
|
| 1839 | + /** |
|
| 1840 | + * Gets the EEM_*_Model for this class |
|
| 1841 | + * |
|
| 1842 | + * @access public now, as this is more convenient |
|
| 1843 | + * @param $classname |
|
| 1844 | + * @param null $timezone |
|
| 1845 | + * @throws EE_Error |
|
| 1846 | + * @return EEM_Base |
|
| 1847 | + */ |
|
| 1848 | + protected static function _get_model($classname, $timezone = null) |
|
| 1849 | + { |
|
| 1850 | + //find model for this class |
|
| 1851 | + if ( ! $classname) { |
|
| 1852 | + throw new EE_Error( |
|
| 1853 | + sprintf( |
|
| 1854 | + __( |
|
| 1855 | + "What were you thinking calling _get_model(%s)?? You need to specify the class name", |
|
| 1856 | + "event_espresso" |
|
| 1857 | + ), |
|
| 1858 | + $classname |
|
| 1859 | + ) |
|
| 1860 | + ); |
|
| 1861 | + } |
|
| 1862 | + $modelName = self::_get_model_classname($classname); |
|
| 1863 | + return self::_get_model_instance_with_name($modelName, $timezone); |
|
| 1864 | + } |
|
| 1865 | + |
|
| 1866 | + |
|
| 1867 | + |
|
| 1868 | + /** |
|
| 1869 | + * Gets the model instance (eg instance of EEM_Attendee) given its classname (eg EE_Attendee) |
|
| 1870 | + * |
|
| 1871 | + * @param string $model_classname |
|
| 1872 | + * @param null $timezone |
|
| 1873 | + * @return EEM_Base |
|
| 1874 | + */ |
|
| 1875 | + protected static function _get_model_instance_with_name($model_classname, $timezone = null) |
|
| 1876 | + { |
|
| 1877 | + $model_classname = str_replace('EEM_', '', $model_classname); |
|
| 1878 | + $model = EE_Registry::instance()->load_model($model_classname); |
|
| 1879 | + $model->set_timezone($timezone); |
|
| 1880 | + return $model; |
|
| 1881 | + } |
|
| 1882 | + |
|
| 1883 | + |
|
| 1884 | + |
|
| 1885 | + /** |
|
| 1886 | + * If a model name is provided (eg Registration), gets the model classname for that model. |
|
| 1887 | + * Also works if a model class's classname is provided (eg EE_Registration). |
|
| 1888 | + * |
|
| 1889 | + * @param null $model_name |
|
| 1890 | + * @return string like EEM_Attendee |
|
| 1891 | + */ |
|
| 1892 | + private static function _get_model_classname($model_name = null) |
|
| 1893 | + { |
|
| 1894 | + if (strpos($model_name, "EE_") === 0) { |
|
| 1895 | + $model_classname = str_replace("EE_", "EEM_", $model_name); |
|
| 1896 | + } else { |
|
| 1897 | + $model_classname = "EEM_" . $model_name; |
|
| 1898 | + } |
|
| 1899 | + return $model_classname; |
|
| 1900 | + } |
|
| 1901 | + |
|
| 1902 | + |
|
| 1903 | + |
|
| 1904 | + /** |
|
| 1905 | + * returns the name of the primary key attribute |
|
| 1906 | + * |
|
| 1907 | + * @param null $classname |
|
| 1908 | + * @throws EE_Error |
|
| 1909 | + * @return string |
|
| 1910 | + */ |
|
| 1911 | + protected static function _get_primary_key_name($classname = null) |
|
| 1912 | + { |
|
| 1913 | + if ( ! $classname) { |
|
| 1914 | + throw new EE_Error( |
|
| 1915 | + sprintf( |
|
| 1916 | + __("What were you thinking calling _get_primary_key_name(%s)", "event_espresso"), |
|
| 1917 | + $classname |
|
| 1918 | + ) |
|
| 1919 | + ); |
|
| 1920 | + } |
|
| 1921 | + return self::_get_model($classname)->get_primary_key_field()->get_name(); |
|
| 1922 | + } |
|
| 1923 | + |
|
| 1924 | + |
|
| 1925 | + |
|
| 1926 | + /** |
|
| 1927 | + * Gets the value of the primary key. |
|
| 1928 | + * If the object hasn't yet been saved, it should be whatever the model field's default was |
|
| 1929 | + * (eg, if this were the EE_Event class, look at the primary key field on EEM_Event and see what its default value |
|
| 1930 | + * is. Usually defaults for integer primary keys are 0; string primary keys are usually NULL). |
|
| 1931 | + * |
|
| 1932 | + * @return mixed, if the primary key is of type INT it'll be an int. Otherwise it could be a string |
|
| 1933 | + * @throws \EE_Error |
|
| 1934 | + */ |
|
| 1935 | + public function ID() |
|
| 1936 | + { |
|
| 1937 | + //now that we know the name of the variable, use a variable variable to get its value and return its |
|
| 1938 | + if ($this->get_model()->has_primary_key_field()) { |
|
| 1939 | + return $this->_fields[self::_get_primary_key_name(get_class($this))]; |
|
| 1940 | + } else { |
|
| 1941 | + return $this->get_model()->get_index_primary_key_string($this->_fields); |
|
| 1942 | + } |
|
| 1943 | + } |
|
| 1944 | + |
|
| 1945 | + |
|
| 1946 | + |
|
| 1947 | + /** |
|
| 1948 | + * Adds a relationship to the specified EE_Base_Class object, given the relationship's name. Eg, if the current |
|
| 1949 | + * model is related to a group of events, the $relationName should be 'Event', and should be a key in the EE |
|
| 1950 | + * Model's $_model_relations array. If this model object doesn't exist in the DB, just caches the related thing |
|
| 1951 | + * |
|
| 1952 | + * @param mixed $otherObjectModelObjectOrID EE_Base_Class or the ID of the other object |
|
| 1953 | + * @param string $relationName eg 'Events','Question',etc. |
|
| 1954 | + * an attendee to a group, you also want to specify which role they |
|
| 1955 | + * will have in that group. So you would use this parameter to |
|
| 1956 | + * specify array('role-column-name'=>'role-id') |
|
| 1957 | + * @param array $extra_join_model_fields_n_values You can optionally include an array of key=>value pairs that |
|
| 1958 | + * allow you to further constrict the relation to being added. |
|
| 1959 | + * However, keep in mind that the columns (keys) given must match a |
|
| 1960 | + * column on the JOIN table and currently only the HABTM models |
|
| 1961 | + * accept these additional conditions. Also remember that if an |
|
| 1962 | + * exact match isn't found for these extra cols/val pairs, then a |
|
| 1963 | + * NEW row is created in the join table. |
|
| 1964 | + * @param null $cache_id |
|
| 1965 | + * @throws EE_Error |
|
| 1966 | + * @return EE_Base_Class the object the relation was added to |
|
| 1967 | + */ |
|
| 1968 | + public function _add_relation_to( |
|
| 1969 | + $otherObjectModelObjectOrID, |
|
| 1970 | + $relationName, |
|
| 1971 | + $extra_join_model_fields_n_values = array(), |
|
| 1972 | + $cache_id = null |
|
| 1973 | + ) { |
|
| 1974 | + //if this thing exists in the DB, save the relation to the DB |
|
| 1975 | + if ($this->ID()) { |
|
| 1976 | + $otherObject = $this->get_model() |
|
| 1977 | + ->add_relationship_to($this, $otherObjectModelObjectOrID, $relationName, |
|
| 1978 | + $extra_join_model_fields_n_values); |
|
| 1979 | + //clear cache so future get_many_related and get_first_related() return new results. |
|
| 1980 | + $this->clear_cache($relationName, $otherObject, true); |
|
| 1981 | + if ($otherObject instanceof EE_Base_Class) { |
|
| 1982 | + $otherObject->clear_cache($this->get_model()->get_this_model_name(), $this); |
|
| 1983 | + } |
|
| 1984 | + } else { |
|
| 1985 | + //this thing doesn't exist in the DB, so just cache it |
|
| 1986 | + if ( ! $otherObjectModelObjectOrID instanceof EE_Base_Class) { |
|
| 1987 | + throw new EE_Error(sprintf( |
|
| 1988 | + __('Before a model object is saved to the database, calls to _add_relation_to must be passed an actual object, not just an ID. You provided %s as the model object to a %s', |
|
| 1989 | + 'event_espresso'), |
|
| 1990 | + $otherObjectModelObjectOrID, |
|
| 1991 | + get_class($this) |
|
| 1992 | + )); |
|
| 1993 | + } else { |
|
| 1994 | + $otherObject = $otherObjectModelObjectOrID; |
|
| 1995 | + } |
|
| 1996 | + $this->cache($relationName, $otherObjectModelObjectOrID, $cache_id); |
|
| 1997 | + } |
|
| 1998 | + if ($otherObject instanceof EE_Base_Class) { |
|
| 1999 | + //fix the reciprocal relation too |
|
| 2000 | + if ($otherObject->ID()) { |
|
| 2001 | + //its saved so assumed relations exist in the DB, so we can just |
|
| 2002 | + //clear the cache so future queries use the updated info in the DB |
|
| 2003 | + $otherObject->clear_cache($this->get_model()->get_this_model_name(), null, true); |
|
| 2004 | + } else { |
|
| 2005 | + //it's not saved, so it caches relations like this |
|
| 2006 | + $otherObject->cache($this->get_model()->get_this_model_name(), $this); |
|
| 2007 | + } |
|
| 2008 | + } |
|
| 2009 | + return $otherObject; |
|
| 2010 | + } |
|
| 2011 | + |
|
| 2012 | + |
|
| 2013 | + |
|
| 2014 | + /** |
|
| 2015 | + * Removes a relationship to the specified EE_Base_Class object, given the relationships' name. Eg, if the current |
|
| 2016 | + * model is related to a group of events, the $relationName should be 'Events', and should be a key in the EE |
|
| 2017 | + * Model's $_model_relations array. If this model object doesn't exist in the DB, just removes the related thing |
|
| 2018 | + * from the cache |
|
| 2019 | + * |
|
| 2020 | + * @param mixed $otherObjectModelObjectOrID |
|
| 2021 | + * EE_Base_Class or the ID of the other object, OR an array key into the cache if this isn't saved |
|
| 2022 | + * to the DB yet |
|
| 2023 | + * @param string $relationName |
|
| 2024 | + * @param array $where_query |
|
| 2025 | + * You can optionally include an array of key=>value pairs that allow you to further constrict the |
|
| 2026 | + * relation to being added. However, keep in mind that the columns (keys) given must match a column |
|
| 2027 | + * on the JOIN table and currently only the HABTM models accept these additional conditions. Also |
|
| 2028 | + * remember that if an exact match isn't found for these extra cols/val pairs, then a NEW row is |
|
| 2029 | + * created in the join table. |
|
| 2030 | + * @return EE_Base_Class the relation was removed from |
|
| 2031 | + * @throws \EE_Error |
|
| 2032 | + */ |
|
| 2033 | + public function _remove_relation_to($otherObjectModelObjectOrID, $relationName, $where_query = array()) |
|
| 2034 | + { |
|
| 2035 | + if ($this->ID()) { |
|
| 2036 | + //if this exists in the DB, save the relation change to the DB too |
|
| 2037 | + $otherObject = $this->get_model() |
|
| 2038 | + ->remove_relationship_to($this, $otherObjectModelObjectOrID, $relationName, |
|
| 2039 | + $where_query); |
|
| 2040 | + $this->clear_cache($relationName, $otherObject); |
|
| 2041 | + } else { |
|
| 2042 | + //this doesn't exist in the DB, just remove it from the cache |
|
| 2043 | + $otherObject = $this->clear_cache($relationName, $otherObjectModelObjectOrID); |
|
| 2044 | + } |
|
| 2045 | + if ($otherObject instanceof EE_Base_Class) { |
|
| 2046 | + $otherObject->clear_cache($this->get_model()->get_this_model_name(), $this); |
|
| 2047 | + } |
|
| 2048 | + return $otherObject; |
|
| 2049 | + } |
|
| 2050 | + |
|
| 2051 | + |
|
| 2052 | + |
|
| 2053 | + /** |
|
| 2054 | + * Removes ALL the related things for the $relationName. |
|
| 2055 | + * |
|
| 2056 | + * @param string $relationName |
|
| 2057 | + * @param array $where_query_params like EEM_Base::get_all's $query_params[0] (where conditions) |
|
| 2058 | + * @return EE_Base_Class |
|
| 2059 | + * @throws \EE_Error |
|
| 2060 | + */ |
|
| 2061 | + public function _remove_relations($relationName, $where_query_params = array()) |
|
| 2062 | + { |
|
| 2063 | + if ($this->ID()) { |
|
| 2064 | + //if this exists in the DB, save the relation change to the DB too |
|
| 2065 | + $otherObjects = $this->get_model()->remove_relations($this, $relationName, $where_query_params); |
|
| 2066 | + $this->clear_cache($relationName, null, true); |
|
| 2067 | + } else { |
|
| 2068 | + //this doesn't exist in the DB, just remove it from the cache |
|
| 2069 | + $otherObjects = $this->clear_cache($relationName, null, true); |
|
| 2070 | + } |
|
| 2071 | + if (is_array($otherObjects)) { |
|
| 2072 | + foreach ($otherObjects as $otherObject) { |
|
| 2073 | + $otherObject->clear_cache($this->get_model()->get_this_model_name(), $this); |
|
| 2074 | + } |
|
| 2075 | + } |
|
| 2076 | + return $otherObjects; |
|
| 2077 | + } |
|
| 2078 | + |
|
| 2079 | + |
|
| 2080 | + |
|
| 2081 | + /** |
|
| 2082 | + * Gets all the related model objects of the specified type. Eg, if the current class if |
|
| 2083 | + * EE_Event, you could call $this->get_many_related('Registration') to get an array of all the |
|
| 2084 | + * EE_Registration objects which related to this event. Note: by default, we remove the "default query params" |
|
| 2085 | + * because we want to get even deleted items etc. |
|
| 2086 | + * |
|
| 2087 | + * @param string $relationName key in the model's _model_relations array |
|
| 2088 | + * @param array $query_params like EEM_Base::get_all |
|
| 2089 | + * @return EE_Base_Class[] Results not necessarily indexed by IDs, because some results might not have primary keys |
|
| 2090 | + * @throws \EE_Error |
|
| 2091 | + * or might not be saved yet. Consider using EEM_Base::get_IDs() on these results if |
|
| 2092 | + * you want IDs |
|
| 2093 | + */ |
|
| 2094 | + public function get_many_related($relationName, $query_params = array()) |
|
| 2095 | + { |
|
| 2096 | + if ($this->ID()) { |
|
| 2097 | + //this exists in the DB, so get the related things from either the cache or the DB |
|
| 2098 | + //if there are query parameters, forget about caching the related model objects. |
|
| 2099 | + if ($query_params) { |
|
| 2100 | + $related_model_objects = $this->get_model()->get_all_related($this, $relationName, $query_params); |
|
| 2101 | + } else { |
|
| 2102 | + //did we already cache the result of this query? |
|
| 2103 | + $cached_results = $this->get_all_from_cache($relationName); |
|
| 2104 | + if ( ! $cached_results) { |
|
| 2105 | + $related_model_objects = $this->get_model()->get_all_related($this, $relationName, $query_params); |
|
| 2106 | + //if no query parameters were passed, then we got all the related model objects |
|
| 2107 | + //for that relation. We can cache them then. |
|
| 2108 | + foreach ($related_model_objects as $related_model_object) { |
|
| 2109 | + $this->cache($relationName, $related_model_object); |
|
| 2110 | + } |
|
| 2111 | + } else { |
|
| 2112 | + $related_model_objects = $cached_results; |
|
| 2113 | + } |
|
| 2114 | + } |
|
| 2115 | + } else { |
|
| 2116 | + //this doesn't exist in the DB, so just get the related things from the cache |
|
| 2117 | + $related_model_objects = $this->get_all_from_cache($relationName); |
|
| 2118 | + } |
|
| 2119 | + return $related_model_objects; |
|
| 2120 | + } |
|
| 2121 | + |
|
| 2122 | + |
|
| 2123 | + |
|
| 2124 | + /** |
|
| 2125 | + * Instead of getting the related model objects, simply counts them. Ignores default_where_conditions by default, |
|
| 2126 | + * unless otherwise specified in the $query_params |
|
| 2127 | + * |
|
| 2128 | + * @param string $relation_name model_name like 'Event', or 'Registration' |
|
| 2129 | + * @param array $query_params like EEM_Base::get_all's |
|
| 2130 | + * @param string $field_to_count name of field to count by. By default, uses primary key |
|
| 2131 | + * @param bool $distinct if we want to only count the distinct values for the column then you can trigger |
|
| 2132 | + * that by the setting $distinct to TRUE; |
|
| 2133 | + * @return int |
|
| 2134 | + */ |
|
| 2135 | + public function count_related($relation_name, $query_params = array(), $field_to_count = null, $distinct = false) |
|
| 2136 | + { |
|
| 2137 | + return $this->get_model()->count_related($this, $relation_name, $query_params, $field_to_count, $distinct); |
|
| 2138 | + } |
|
| 2139 | + |
|
| 2140 | + |
|
| 2141 | + |
|
| 2142 | + /** |
|
| 2143 | + * Instead of getting the related model objects, simply sums up the values of the specified field. |
|
| 2144 | + * Note: ignores default_where_conditions by default, unless otherwise specified in the $query_params |
|
| 2145 | + * |
|
| 2146 | + * @param string $relation_name model_name like 'Event', or 'Registration' |
|
| 2147 | + * @param array $query_params like EEM_Base::get_all's |
|
| 2148 | + * @param string $field_to_sum name of field to count by. |
|
| 2149 | + * By default, uses primary key (which doesn't make much sense, so you should probably |
|
| 2150 | + * change it) |
|
| 2151 | + * @return int |
|
| 2152 | + */ |
|
| 2153 | + public function sum_related($relation_name, $query_params = array(), $field_to_sum = null) |
|
| 2154 | + { |
|
| 2155 | + return $this->get_model()->sum_related($this, $relation_name, $query_params, $field_to_sum); |
|
| 2156 | + } |
|
| 2157 | + |
|
| 2158 | + |
|
| 2159 | + |
|
| 2160 | + /** |
|
| 2161 | + * Gets the first (ie, one) related model object of the specified type. |
|
| 2162 | + * |
|
| 2163 | + * @param string $relationName key in the model's _model_relations array |
|
| 2164 | + * @param array $query_params like EEM_Base::get_all |
|
| 2165 | + * @return EE_Base_Class (not an array, a single object) |
|
| 2166 | + * @throws \EE_Error |
|
| 2167 | + */ |
|
| 2168 | + public function get_first_related($relationName, $query_params = array()) |
|
| 2169 | + { |
|
| 2170 | + if ($this->ID()) {//this exists in the DB, get from the cache OR the DB |
|
| 2171 | + //if they've provided some query parameters, don't bother trying to cache the result |
|
| 2172 | + //also make sure we're not caching the result of get_first_related |
|
| 2173 | + //on a relation which should have an array of objects (because the cache might have an array of objects) |
|
| 2174 | + if ($query_params |
|
| 2175 | + || ! $this->get_model()->related_settings_for($relationName) |
|
| 2176 | + instanceof |
|
| 2177 | + EE_Belongs_To_Relation |
|
| 2178 | + ) { |
|
| 2179 | + $related_model_object = $this->get_model()->get_first_related($this, $relationName, $query_params); |
|
| 2180 | + } else { |
|
| 2181 | + //first, check if we've already cached the result of this query |
|
| 2182 | + $cached_result = $this->get_one_from_cache($relationName); |
|
| 2183 | + if ( ! $cached_result) { |
|
| 2184 | + $related_model_object = $this->get_model()->get_first_related($this, $relationName, $query_params); |
|
| 2185 | + $this->cache($relationName, $related_model_object); |
|
| 2186 | + } else { |
|
| 2187 | + $related_model_object = $cached_result; |
|
| 2188 | + } |
|
| 2189 | + } |
|
| 2190 | + } else { |
|
| 2191 | + $related_model_object = null; |
|
| 2192 | + //this doesn't exist in the Db, but maybe the relation is of type belongs to, and so the related thing might |
|
| 2193 | + if ($this->get_model()->related_settings_for($relationName) instanceof EE_Belongs_To_Relation) { |
|
| 2194 | + $related_model_object = $this->get_model()->get_first_related($this, $relationName, $query_params); |
|
| 2195 | + } |
|
| 2196 | + //this doesn't exist in the DB and apparently the thing it belongs to doesn't either, just get what's cached on this object |
|
| 2197 | + if ( ! $related_model_object) { |
|
| 2198 | + $related_model_object = $this->get_one_from_cache($relationName); |
|
| 2199 | + } |
|
| 2200 | + } |
|
| 2201 | + return $related_model_object; |
|
| 2202 | + } |
|
| 2203 | + |
|
| 2204 | + |
|
| 2205 | + |
|
| 2206 | + /** |
|
| 2207 | + * Does a delete on all related objects of type $relationName and removes |
|
| 2208 | + * the current model object's relation to them. If they can't be deleted (because |
|
| 2209 | + * of blocking related model objects) does nothing. If the related model objects are |
|
| 2210 | + * soft-deletable, they will be soft-deleted regardless of related blocking model objects. |
|
| 2211 | + * If this model object doesn't exist yet in the DB, just removes its related things |
|
| 2212 | + * |
|
| 2213 | + * @param string $relationName |
|
| 2214 | + * @param array $query_params like EEM_Base::get_all's |
|
| 2215 | + * @return int how many deleted |
|
| 2216 | + * @throws \EE_Error |
|
| 2217 | + */ |
|
| 2218 | + public function delete_related($relationName, $query_params = array()) |
|
| 2219 | + { |
|
| 2220 | + if ($this->ID()) { |
|
| 2221 | + $count = $this->get_model()->delete_related($this, $relationName, $query_params); |
|
| 2222 | + } else { |
|
| 2223 | + $count = count($this->get_all_from_cache($relationName)); |
|
| 2224 | + $this->clear_cache($relationName, null, true); |
|
| 2225 | + } |
|
| 2226 | + return $count; |
|
| 2227 | + } |
|
| 2228 | + |
|
| 2229 | + |
|
| 2230 | + |
|
| 2231 | + /** |
|
| 2232 | + * Does a hard delete (ie, removes the DB row) on all related objects of type $relationName and removes |
|
| 2233 | + * the current model object's relation to them. If they can't be deleted (because |
|
| 2234 | + * of blocking related model objects) just does a soft delete on it instead, if possible. |
|
| 2235 | + * If the related thing isn't a soft-deletable model object, this function is identical |
|
| 2236 | + * to delete_related(). If this model object doesn't exist in the DB, just remove its related things |
|
| 2237 | + * |
|
| 2238 | + * @param string $relationName |
|
| 2239 | + * @param array $query_params like EEM_Base::get_all's |
|
| 2240 | + * @return int how many deleted (including those soft deleted) |
|
| 2241 | + * @throws \EE_Error |
|
| 2242 | + */ |
|
| 2243 | + public function delete_related_permanently($relationName, $query_params = array()) |
|
| 2244 | + { |
|
| 2245 | + if ($this->ID()) { |
|
| 2246 | + $count = $this->get_model()->delete_related_permanently($this, $relationName, $query_params); |
|
| 2247 | + } else { |
|
| 2248 | + $count = count($this->get_all_from_cache($relationName)); |
|
| 2249 | + } |
|
| 2250 | + $this->clear_cache($relationName, null, true); |
|
| 2251 | + return $count; |
|
| 2252 | + } |
|
| 2253 | + |
|
| 2254 | + |
|
| 2255 | + |
|
| 2256 | + /** |
|
| 2257 | + * is_set |
|
| 2258 | + * Just a simple utility function children can use for checking if property exists |
|
| 2259 | + * |
|
| 2260 | + * @access public |
|
| 2261 | + * @param string $field_name property to check |
|
| 2262 | + * @return bool TRUE if existing,FALSE if not. |
|
| 2263 | + */ |
|
| 2264 | + public function is_set($field_name) |
|
| 2265 | + { |
|
| 2266 | + return isset($this->_fields[$field_name]); |
|
| 2267 | + } |
|
| 2268 | + |
|
| 2269 | + |
|
| 2270 | + |
|
| 2271 | + /** |
|
| 2272 | + * Just a simple utility function children can use for checking if property (or properties) exists and throwing an |
|
| 2273 | + * EE_Error exception if they don't |
|
| 2274 | + * |
|
| 2275 | + * @param mixed (string|array) $properties properties to check |
|
| 2276 | + * @throws EE_Error |
|
| 2277 | + * @return bool TRUE if existing, throw EE_Error if not. |
|
| 2278 | + */ |
|
| 2279 | + protected function _property_exists($properties) |
|
| 2280 | + { |
|
| 2281 | + foreach ((array)$properties as $property_name) { |
|
| 2282 | + //first make sure this property exists |
|
| 2283 | + if ( ! $this->_fields[$property_name]) { |
|
| 2284 | + throw new EE_Error( |
|
| 2285 | + sprintf( |
|
| 2286 | + __( |
|
| 2287 | + 'Trying to retrieve a non-existent property (%s). Double check the spelling please', |
|
| 2288 | + 'event_espresso' |
|
| 2289 | + ), |
|
| 2290 | + $property_name |
|
| 2291 | + ) |
|
| 2292 | + ); |
|
| 2293 | + } |
|
| 2294 | + } |
|
| 2295 | + return true; |
|
| 2296 | + } |
|
| 2297 | + |
|
| 2298 | + |
|
| 2299 | + |
|
| 2300 | + /** |
|
| 2301 | + * This simply returns an array of model fields for this object |
|
| 2302 | + * |
|
| 2303 | + * @return array |
|
| 2304 | + * @throws \EE_Error |
|
| 2305 | + */ |
|
| 2306 | + public function model_field_array() |
|
| 2307 | + { |
|
| 2308 | + $fields = $this->get_model()->field_settings(false); |
|
| 2309 | + $properties = array(); |
|
| 2310 | + //remove prepended underscore |
|
| 2311 | + foreach ($fields as $field_name => $settings) { |
|
| 2312 | + $properties[$field_name] = $this->get($field_name); |
|
| 2313 | + } |
|
| 2314 | + return $properties; |
|
| 2315 | + } |
|
| 2316 | + |
|
| 2317 | + |
|
| 2318 | + |
|
| 2319 | + /** |
|
| 2320 | + * Very handy general function to allow for plugins to extend any child of EE_Base_Class. |
|
| 2321 | + * If a method is called on a child of EE_Base_Class that doesn't exist, this function is called |
|
| 2322 | + * (http://www.garfieldtech.com/blog/php-magic-call) and passed the method's name and arguments. Instead of |
|
| 2323 | + * requiring a plugin to extend the EE_Base_Class (which works fine is there's only 1 plugin, but when will that |
|
| 2324 | + * happen?) they can add a hook onto 'filters_hook_espresso__{className}__{methodName}' (eg, |
|
| 2325 | + * filters_hook_espresso__EE_Answer__my_great_function) and accepts 2 arguments: the object on which the function |
|
| 2326 | + * was called, and an array of the original arguments passed to the function. Whatever their callback function |
|
| 2327 | + * returns will be returned by this function. Example: in functions.php (or in a plugin): |
|
| 2328 | + * add_filter('FHEE__EE_Answer__my_callback','my_callback',10,3); function |
|
| 2329 | + * my_callback($previousReturnValue,EE_Base_Class $object,$argsArray){ |
|
| 2330 | + * $returnString= "you called my_callback! and passed args:".implode(",",$argsArray); |
|
| 2331 | + * return $previousReturnValue.$returnString; |
|
| 2332 | + * } |
|
| 2333 | + * require('EE_Answer.class.php'); |
|
| 2334 | + * $answer= EE_Answer::new_instance(array('REG_ID' => 2,'QST_ID' => 3,'ANS_value' => The answer is 42')); |
|
| 2335 | + * echo $answer->my_callback('monkeys',100); |
|
| 2336 | + * //will output "you called my_callback! and passed args:monkeys,100" |
|
| 2337 | + * |
|
| 2338 | + * @param string $methodName name of method which was called on a child of EE_Base_Class, but which |
|
| 2339 | + * @param array $args array of original arguments passed to the function |
|
| 2340 | + * @throws EE_Error |
|
| 2341 | + * @return mixed whatever the plugin which calls add_filter decides |
|
| 2342 | + */ |
|
| 2343 | + public function __call($methodName, $args) |
|
| 2344 | + { |
|
| 2345 | + $className = get_class($this); |
|
| 2346 | + $tagName = "FHEE__{$className}__{$methodName}"; |
|
| 2347 | + if ( ! has_filter($tagName)) { |
|
| 2348 | + throw new EE_Error( |
|
| 2349 | + sprintf( |
|
| 2350 | + __( |
|
| 2351 | + "Method %s on class %s does not exist! You can create one with the following code in functions.php or in a plugin: add_filter('%s','my_callback',10,3);function my_callback(\$previousReturnValue,EE_Base_Class \$object, \$argsArray){/*function body*/return \$whatever;}", |
|
| 2352 | + "event_espresso" |
|
| 2353 | + ), |
|
| 2354 | + $methodName, |
|
| 2355 | + $className, |
|
| 2356 | + $tagName |
|
| 2357 | + ) |
|
| 2358 | + ); |
|
| 2359 | + } |
|
| 2360 | + return apply_filters($tagName, null, $this, $args); |
|
| 2361 | + } |
|
| 2362 | + |
|
| 2363 | + |
|
| 2364 | + |
|
| 2365 | + /** |
|
| 2366 | + * Similar to insert_post_meta, adds a record in the Extra_Meta model's table with the given key and value. |
|
| 2367 | + * A $previous_value can be specified in case there are many meta rows with the same key |
|
| 2368 | + * |
|
| 2369 | + * @param string $meta_key |
|
| 2370 | + * @param mixed $meta_value |
|
| 2371 | + * @param mixed $previous_value |
|
| 2372 | + * @return bool|int # of records updated (or BOOLEAN if we actually ended up inserting the extra meta row) |
|
| 2373 | + * @throws \EE_Error |
|
| 2374 | + * NOTE: if the values haven't changed, returns 0 |
|
| 2375 | + */ |
|
| 2376 | + public function update_extra_meta($meta_key, $meta_value, $previous_value = null) |
|
| 2377 | + { |
|
| 2378 | + $query_params = array( |
|
| 2379 | + array( |
|
| 2380 | + 'EXM_key' => $meta_key, |
|
| 2381 | + 'OBJ_ID' => $this->ID(), |
|
| 2382 | + 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2383 | + ), |
|
| 2384 | + ); |
|
| 2385 | + if ($previous_value !== null) { |
|
| 2386 | + $query_params[0]['EXM_value'] = $meta_value; |
|
| 2387 | + } |
|
| 2388 | + $existing_rows_like_that = EEM_Extra_Meta::instance()->get_all($query_params); |
|
| 2389 | + if ( ! $existing_rows_like_that) { |
|
| 2390 | + return $this->add_extra_meta($meta_key, $meta_value); |
|
| 2391 | + } |
|
| 2392 | + foreach ($existing_rows_like_that as $existing_row) { |
|
| 2393 | + $existing_row->save(array('EXM_value' => $meta_value)); |
|
| 2394 | + } |
|
| 2395 | + return count($existing_rows_like_that); |
|
| 2396 | + } |
|
| 2397 | + |
|
| 2398 | + |
|
| 2399 | + |
|
| 2400 | + /** |
|
| 2401 | + * Adds a new extra meta record. If $unique is set to TRUE, we'll first double-check |
|
| 2402 | + * no other extra meta for this model object have the same key. Returns TRUE if the |
|
| 2403 | + * extra meta row was entered, false if not |
|
| 2404 | + * |
|
| 2405 | + * @param string $meta_key |
|
| 2406 | + * @param string $meta_value |
|
| 2407 | + * @param boolean $unique |
|
| 2408 | + * @return boolean |
|
| 2409 | + * @throws \EE_Error |
|
| 2410 | + */ |
|
| 2411 | + public function add_extra_meta($meta_key, $meta_value, $unique = false) |
|
| 2412 | + { |
|
| 2413 | + if ($unique) { |
|
| 2414 | + $existing_extra_meta = EEM_Extra_Meta::instance()->get_one( |
|
| 2415 | + array( |
|
| 2416 | + array( |
|
| 2417 | + 'EXM_key' => $meta_key, |
|
| 2418 | + 'OBJ_ID' => $this->ID(), |
|
| 2419 | + 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2420 | + ), |
|
| 2421 | + ) |
|
| 2422 | + ); |
|
| 2423 | + if ($existing_extra_meta) { |
|
| 2424 | + return false; |
|
| 2425 | + } |
|
| 2426 | + } |
|
| 2427 | + $new_extra_meta = EE_Extra_Meta::new_instance( |
|
| 2428 | + array( |
|
| 2429 | + 'EXM_key' => $meta_key, |
|
| 2430 | + 'EXM_value' => $meta_value, |
|
| 2431 | + 'OBJ_ID' => $this->ID(), |
|
| 2432 | + 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2433 | + ) |
|
| 2434 | + ); |
|
| 2435 | + $new_extra_meta->save(); |
|
| 2436 | + return true; |
|
| 2437 | + } |
|
| 2438 | + |
|
| 2439 | + |
|
| 2440 | + |
|
| 2441 | + /** |
|
| 2442 | + * Deletes all the extra meta rows for this record as specified by key. If $meta_value |
|
| 2443 | + * is specified, only deletes extra meta records with that value. |
|
| 2444 | + * |
|
| 2445 | + * @param string $meta_key |
|
| 2446 | + * @param string $meta_value |
|
| 2447 | + * @return int number of extra meta rows deleted |
|
| 2448 | + * @throws \EE_Error |
|
| 2449 | + */ |
|
| 2450 | + public function delete_extra_meta($meta_key, $meta_value = null) |
|
| 2451 | + { |
|
| 2452 | + $query_params = array( |
|
| 2453 | + array( |
|
| 2454 | + 'EXM_key' => $meta_key, |
|
| 2455 | + 'OBJ_ID' => $this->ID(), |
|
| 2456 | + 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2457 | + ), |
|
| 2458 | + ); |
|
| 2459 | + if ($meta_value !== null) { |
|
| 2460 | + $query_params[0]['EXM_value'] = $meta_value; |
|
| 2461 | + } |
|
| 2462 | + return EEM_Extra_Meta::instance()->delete($query_params); |
|
| 2463 | + } |
|
| 2464 | + |
|
| 2465 | + |
|
| 2466 | + |
|
| 2467 | + /** |
|
| 2468 | + * Gets the extra meta with the given meta key. If you specify "single" we just return 1, otherwise |
|
| 2469 | + * an array of everything found. Requires that this model actually have a relation of type EE_Has_Many_Any_Relation. |
|
| 2470 | + * You can specify $default is case you haven't found the extra meta |
|
| 2471 | + * |
|
| 2472 | + * @param string $meta_key |
|
| 2473 | + * @param boolean $single |
|
| 2474 | + * @param mixed $default if we don't find anything, what should we return? |
|
| 2475 | + * @return mixed single value if $single; array if ! $single |
|
| 2476 | + * @throws \EE_Error |
|
| 2477 | + */ |
|
| 2478 | + public function get_extra_meta($meta_key, $single = false, $default = null) |
|
| 2479 | + { |
|
| 2480 | + if ($single) { |
|
| 2481 | + $result = $this->get_first_related('Extra_Meta', array(array('EXM_key' => $meta_key))); |
|
| 2482 | + if ($result instanceof EE_Extra_Meta) { |
|
| 2483 | + return $result->value(); |
|
| 2484 | + } else { |
|
| 2485 | + return $default; |
|
| 2486 | + } |
|
| 2487 | + } else { |
|
| 2488 | + $results = $this->get_many_related('Extra_Meta', array(array('EXM_key' => $meta_key))); |
|
| 2489 | + if ($results) { |
|
| 2490 | + $values = array(); |
|
| 2491 | + foreach ($results as $result) { |
|
| 2492 | + if ($result instanceof EE_Extra_Meta) { |
|
| 2493 | + $values[$result->ID()] = $result->value(); |
|
| 2494 | + } |
|
| 2495 | + } |
|
| 2496 | + return $values; |
|
| 2497 | + } else { |
|
| 2498 | + return $default; |
|
| 2499 | + } |
|
| 2500 | + } |
|
| 2501 | + } |
|
| 2502 | + |
|
| 2503 | + |
|
| 2504 | + |
|
| 2505 | + /** |
|
| 2506 | + * Returns a simple array of all the extra meta associated with this model object. |
|
| 2507 | + * If $one_of_each_key is true (Default), it will be an array of simple key-value pairs, keys being the |
|
| 2508 | + * extra meta's key, and teh value being its value. However, if there are duplicate extra meta rows with |
|
| 2509 | + * the same key, only one will be used. (eg array('foo'=>'bar','monkey'=>123)) |
|
| 2510 | + * If $one_of_each_key is false, it will return an array with the top-level keys being |
|
| 2511 | + * the extra meta keys, but their values are also arrays, which have the extra-meta's ID as their sub-key, and |
|
| 2512 | + * finally the extra meta's value as each sub-value. (eg |
|
| 2513 | + * array('foo'=>array(1=>'bar',2=>'bill'),'monkey'=>array(3=>123))) |
|
| 2514 | + * |
|
| 2515 | + * @param boolean $one_of_each_key |
|
| 2516 | + * @return array |
|
| 2517 | + * @throws \EE_Error |
|
| 2518 | + */ |
|
| 2519 | + public function all_extra_meta_array($one_of_each_key = true) |
|
| 2520 | + { |
|
| 2521 | + $return_array = array(); |
|
| 2522 | + if ($one_of_each_key) { |
|
| 2523 | + $extra_meta_objs = $this->get_many_related('Extra_Meta', array('group_by' => 'EXM_key')); |
|
| 2524 | + foreach ($extra_meta_objs as $extra_meta_obj) { |
|
| 2525 | + if ($extra_meta_obj instanceof EE_Extra_Meta) { |
|
| 2526 | + $return_array[$extra_meta_obj->key()] = $extra_meta_obj->value(); |
|
| 2527 | + } |
|
| 2528 | + } |
|
| 2529 | + } else { |
|
| 2530 | + $extra_meta_objs = $this->get_many_related('Extra_Meta'); |
|
| 2531 | + foreach ($extra_meta_objs as $extra_meta_obj) { |
|
| 2532 | + if ($extra_meta_obj instanceof EE_Extra_Meta) { |
|
| 2533 | + if ( ! isset($return_array[$extra_meta_obj->key()])) { |
|
| 2534 | + $return_array[$extra_meta_obj->key()] = array(); |
|
| 2535 | + } |
|
| 2536 | + $return_array[$extra_meta_obj->key()][$extra_meta_obj->ID()] = $extra_meta_obj->value(); |
|
| 2537 | + } |
|
| 2538 | + } |
|
| 2539 | + } |
|
| 2540 | + return $return_array; |
|
| 2541 | + } |
|
| 2542 | + |
|
| 2543 | + |
|
| 2544 | + |
|
| 2545 | + /** |
|
| 2546 | + * Gets a pretty nice displayable nice for this model object. Often overridden |
|
| 2547 | + * |
|
| 2548 | + * @return string |
|
| 2549 | + * @throws \EE_Error |
|
| 2550 | + */ |
|
| 2551 | + public function name() |
|
| 2552 | + { |
|
| 2553 | + //find a field that's not a text field |
|
| 2554 | + $field_we_can_use = $this->get_model()->get_a_field_of_type('EE_Text_Field_Base'); |
|
| 2555 | + if ($field_we_can_use) { |
|
| 2556 | + return $this->get($field_we_can_use->get_name()); |
|
| 2557 | + } else { |
|
| 2558 | + $first_few_properties = $this->model_field_array(); |
|
| 2559 | + $first_few_properties = array_slice($first_few_properties, 0, 3); |
|
| 2560 | + $name_parts = array(); |
|
| 2561 | + foreach ($first_few_properties as $name => $value) { |
|
| 2562 | + $name_parts[] = "$name:$value"; |
|
| 2563 | + } |
|
| 2564 | + return implode(",", $name_parts); |
|
| 2565 | + } |
|
| 2566 | + } |
|
| 2567 | + |
|
| 2568 | + |
|
| 2569 | + |
|
| 2570 | + /** |
|
| 2571 | + * in_entity_map |
|
| 2572 | + * Checks if this model object has been proven to already be in the entity map |
|
| 2573 | + * |
|
| 2574 | + * @return boolean |
|
| 2575 | + * @throws \EE_Error |
|
| 2576 | + */ |
|
| 2577 | + public function in_entity_map() |
|
| 2578 | + { |
|
| 2579 | + if ($this->ID() && $this->get_model()->get_from_entity_map($this->ID()) === $this) { |
|
| 2580 | + //well, if we looked, did we find it in the entity map? |
|
| 2581 | + return true; |
|
| 2582 | + } else { |
|
| 2583 | + return false; |
|
| 2584 | + } |
|
| 2585 | + } |
|
| 2586 | + |
|
| 2587 | + |
|
| 2588 | + |
|
| 2589 | + /** |
|
| 2590 | + * refresh_from_db |
|
| 2591 | + * Makes sure the fields and values on this model object are in-sync with what's in the database. |
|
| 2592 | + * |
|
| 2593 | + * @throws EE_Error if this model object isn't in the entity mapper (because then you should |
|
| 2594 | + * just use what's in the entity mapper and refresh it) and WP_DEBUG is TRUE |
|
| 2595 | + */ |
|
| 2596 | + public function refresh_from_db() |
|
| 2597 | + { |
|
| 2598 | + if ($this->ID() && $this->in_entity_map()) { |
|
| 2599 | + $this->get_model()->refresh_entity_map_from_db($this->ID()); |
|
| 2600 | + } else { |
|
| 2601 | + //if it doesn't have ID, you shouldn't be asking to refresh it from teh database (because its not in the database) |
|
| 2602 | + //if it has an ID but it's not in the map, and you're asking me to refresh it |
|
| 2603 | + //that's kinda dangerous. You should just use what's in the entity map, or add this to the entity map if there's |
|
| 2604 | + //absolutely nothing in it for this ID |
|
| 2605 | + if (WP_DEBUG) { |
|
| 2606 | + throw new EE_Error( |
|
| 2607 | + sprintf( |
|
| 2608 | + __('Trying to refresh a model object with ID "%1$s" that\'s not in the entity map? First off: you should put it in the entity map by calling %2$s. Second off, if you want what\'s in the database right now, you should just call %3$s yourself and discard this model object.', |
|
| 2609 | + 'event_espresso'), |
|
| 2610 | + $this->ID(), |
|
| 2611 | + get_class($this->get_model()) . '::instance()->add_to_entity_map()', |
|
| 2612 | + get_class($this->get_model()) . '::instance()->refresh_entity_map()' |
|
| 2613 | + ) |
|
| 2614 | + ); |
|
| 2615 | + } |
|
| 2616 | + } |
|
| 2617 | + } |
|
| 2618 | + |
|
| 2619 | + |
|
| 2620 | + |
|
| 2621 | + /** |
|
| 2622 | + * Because some other plugins, like Advanced Cron Manager, expect all objects to have this method |
|
| 2623 | + * (probably a bad assumption they have made, oh well) |
|
| 2624 | + * |
|
| 2625 | + * @return string |
|
| 2626 | + */ |
|
| 2627 | + public function __toString() |
|
| 2628 | + { |
|
| 2629 | + try { |
|
| 2630 | + return sprintf('%s (%s)', $this->name(), $this->ID()); |
|
| 2631 | + } catch (Exception $e) { |
|
| 2632 | + EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__); |
|
| 2633 | + return ''; |
|
| 2634 | + } |
|
| 2635 | + } |
|
| 2636 | + |
|
| 2637 | + |
|
| 2638 | + |
|
| 2639 | + /** |
|
| 2640 | + * Clear related model objects if they're already in the DB, because otherwise when we |
|
| 2641 | + * UN-serialize this model object we'll need to be careful to add them to the entity map. |
|
| 2642 | + * This means if we have made changes to those related model objects, and want to unserialize |
|
| 2643 | + * the this model object on a subsequent request, changes to those related model objects will be lost. |
|
| 2644 | + * Instead, those related model objects should be directly serialized and stored. |
|
| 2645 | + * Eg, the following won't work: |
|
| 2646 | + * $reg = EEM_Registration::instance()->get_one_by_ID( 123 ); |
|
| 2647 | + * $att = $reg->attendee(); |
|
| 2648 | + * $att->set( 'ATT_fname', 'Dirk' ); |
|
| 2649 | + * update_option( 'my_option', serialize( $reg ) ); |
|
| 2650 | + * //END REQUEST |
|
| 2651 | + * //START NEXT REQUEST |
|
| 2652 | + * $reg = get_option( 'my_option' ); |
|
| 2653 | + * $reg->attendee()->save(); |
|
| 2654 | + * And would need to be replace with: |
|
| 2655 | + * $reg = EEM_Registration::instance()->get_one_by_ID( 123 ); |
|
| 2656 | + * $att = $reg->attendee(); |
|
| 2657 | + * $att->set( 'ATT_fname', 'Dirk' ); |
|
| 2658 | + * update_option( 'my_option', serialize( $reg ) ); |
|
| 2659 | + * //END REQUEST |
|
| 2660 | + * //START NEXT REQUEST |
|
| 2661 | + * $att = get_option( 'my_option' ); |
|
| 2662 | + * $att->save(); |
|
| 2663 | + * |
|
| 2664 | + * @return array |
|
| 2665 | + * @throws \EE_Error |
|
| 2666 | + */ |
|
| 2667 | + public function __sleep() |
|
| 2668 | + { |
|
| 2669 | + foreach ($this->get_model()->relation_settings() as $relation_name => $relation_obj) { |
|
| 2670 | + if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 2671 | + $classname = 'EE_' . $this->get_model()->get_this_model_name(); |
|
| 2672 | + if ( |
|
| 2673 | + $this->get_one_from_cache($relation_name) instanceof $classname |
|
| 2674 | + && $this->get_one_from_cache($relation_name)->ID() |
|
| 2675 | + ) { |
|
| 2676 | + $this->clear_cache($relation_name, $this->get_one_from_cache($relation_name)->ID()); |
|
| 2677 | + } |
|
| 2678 | + } |
|
| 2679 | + } |
|
| 2680 | + $this->_props_n_values_provided_in_constructor = array(); |
|
| 2681 | + return array_keys(get_object_vars($this)); |
|
| 2682 | + } |
|
| 2683 | + |
|
| 2684 | + |
|
| 2685 | + |
|
| 2686 | + /** |
|
| 2687 | + * restore _props_n_values_provided_in_constructor |
|
| 2688 | + * PLZ NOTE: this will reset the array to whatever fields values were present prior to serialization, |
|
| 2689 | + * and therefore should NOT be used to determine if state change has occurred since initial construction. |
|
| 2690 | + * At best, you would only be able to detect if state change has occurred during THIS request. |
|
| 2691 | + */ |
|
| 2692 | + public function __wakeup() |
|
| 2693 | + { |
|
| 2694 | + $this->_props_n_values_provided_in_constructor = $this->_fields; |
|
| 2695 | + } |
|
| 2696 | 2696 | |
| 2697 | 2697 | |
| 2698 | 2698 | |
@@ -150,8 +150,8 @@ discard block |
||
| 150 | 150 | list($this->_dt_frmt, $this->_tm_frmt) = $date_formats; |
| 151 | 151 | } else { |
| 152 | 152 | //set default formats for date and time |
| 153 | - $this->_dt_frmt = (string)get_option('date_format', 'Y-m-d'); |
|
| 154 | - $this->_tm_frmt = (string)get_option('time_format', 'g:i a'); |
|
| 153 | + $this->_dt_frmt = (string) get_option('date_format', 'Y-m-d'); |
|
| 154 | + $this->_tm_frmt = (string) get_option('time_format', 'g:i a'); |
|
| 155 | 155 | } |
| 156 | 156 | //if db model is instantiating |
| 157 | 157 | if ($bydb) { |
@@ -472,7 +472,7 @@ discard block |
||
| 472 | 472 | */ |
| 473 | 473 | public function get_format($full = true) |
| 474 | 474 | { |
| 475 | - return $full ? $this->_dt_frmt . ' ' . $this->_tm_frmt : array($this->_dt_frmt, $this->_tm_frmt); |
|
| 475 | + return $full ? $this->_dt_frmt.' '.$this->_tm_frmt : array($this->_dt_frmt, $this->_tm_frmt); |
|
| 476 | 476 | } |
| 477 | 477 | |
| 478 | 478 | |
@@ -580,7 +580,7 @@ discard block |
||
| 580 | 580 | //verify the field exists |
| 581 | 581 | $this->get_model()->field_settings_for($fieldname); |
| 582 | 582 | $cache_type = $pretty ? 'pretty' : 'standard'; |
| 583 | - $cache_type .= ! empty($extra_cache_ref) ? '_' . $extra_cache_ref : ''; |
|
| 583 | + $cache_type .= ! empty($extra_cache_ref) ? '_'.$extra_cache_ref : ''; |
|
| 584 | 584 | if (isset($this->_cached_properties[$fieldname][$cache_type])) { |
| 585 | 585 | return $this->_cached_properties[$fieldname][$cache_type]; |
| 586 | 586 | } |
@@ -795,7 +795,7 @@ discard block |
||
| 795 | 795 | $current_cache_id = '' |
| 796 | 796 | ) { |
| 797 | 797 | // verify that incoming object is of the correct type |
| 798 | - $obj_class = 'EE_' . $relationName; |
|
| 798 | + $obj_class = 'EE_'.$relationName; |
|
| 799 | 799 | if ($newly_saved_object instanceof $obj_class) { |
| 800 | 800 | /* @type EE_Base_Class $newly_saved_object */ |
| 801 | 801 | // now get the type of relation |
@@ -1277,7 +1277,7 @@ discard block |
||
| 1277 | 1277 | */ |
| 1278 | 1278 | public function get_i18n_datetime($field_name, $format = '') |
| 1279 | 1279 | { |
| 1280 | - $format = empty($format) ? $this->_dt_frmt . ' ' . $this->_tm_frmt : $format; |
|
| 1280 | + $format = empty($format) ? $this->_dt_frmt.' '.$this->_tm_frmt : $format; |
|
| 1281 | 1281 | return date_i18n( |
| 1282 | 1282 | $format, |
| 1283 | 1283 | EEH_DTT_Helper::get_timestamp_with_offset($this->get_raw($field_name), $this->_timezone) |
@@ -1415,8 +1415,8 @@ discard block |
||
| 1415 | 1415 | } |
| 1416 | 1416 | $original_timezone = $this->_timezone; |
| 1417 | 1417 | $this->set_timezone($timezone); |
| 1418 | - $fn = (array)$field_name; |
|
| 1419 | - $args = array_merge($fn, (array)$args); |
|
| 1418 | + $fn = (array) $field_name; |
|
| 1419 | + $args = array_merge($fn, (array) $args); |
|
| 1420 | 1420 | if ( ! method_exists($this, $callback)) { |
| 1421 | 1421 | throw new EE_Error( |
| 1422 | 1422 | sprintf( |
@@ -1428,8 +1428,8 @@ discard block |
||
| 1428 | 1428 | ) |
| 1429 | 1429 | ); |
| 1430 | 1430 | } |
| 1431 | - $args = (array)$args; |
|
| 1432 | - $return = $prepend . call_user_func_array(array($this, $callback), $args) . $append; |
|
| 1431 | + $args = (array) $args; |
|
| 1432 | + $return = $prepend.call_user_func_array(array($this, $callback), $args).$append; |
|
| 1433 | 1433 | $this->set_timezone($original_timezone); |
| 1434 | 1434 | return $return; |
| 1435 | 1435 | } |
@@ -1566,14 +1566,14 @@ discard block |
||
| 1566 | 1566 | * @param array $set_cols_n_values |
| 1567 | 1567 | * @param EE_Base_Class $model_object |
| 1568 | 1568 | */ |
| 1569 | - $set_cols_n_values = (array)apply_filters('FHEE__EE_Base_Class__save__set_cols_n_values', $set_cols_n_values, |
|
| 1569 | + $set_cols_n_values = (array) apply_filters('FHEE__EE_Base_Class__save__set_cols_n_values', $set_cols_n_values, |
|
| 1570 | 1570 | $this); |
| 1571 | 1571 | //set attributes as provided in $set_cols_n_values |
| 1572 | 1572 | foreach ($set_cols_n_values as $column => $value) { |
| 1573 | 1573 | $this->set($column, $value); |
| 1574 | 1574 | } |
| 1575 | 1575 | // no changes ? then don't do anything |
| 1576 | - if (! $this->_has_changes && $this->ID() && $this->get_model()->get_primary_key_field()->is_auto_increment()) { |
|
| 1576 | + if ( ! $this->_has_changes && $this->ID() && $this->get_model()->get_primary_key_field()->is_auto_increment()) { |
|
| 1577 | 1577 | return 0; |
| 1578 | 1578 | } |
| 1579 | 1579 | /** |
@@ -1625,8 +1625,8 @@ discard block |
||
| 1625 | 1625 | __('Using a model object %1$s that is NOT in the entity map, can lead to unexpected errors. You should either: %4$s 1. Put it in the entity mapper by calling %2$s %4$s 2. Discard this model object and use what is in the entity mapper %4$s 3. Fetch from the database using %3$s', |
| 1626 | 1626 | 'event_espresso'), |
| 1627 | 1627 | get_class($this), |
| 1628 | - get_class($this->get_model()) . '::instance()->add_to_entity_map()', |
|
| 1629 | - get_class($this->get_model()) . '::instance()->get_one_by_ID()', |
|
| 1628 | + get_class($this->get_model()).'::instance()->add_to_entity_map()', |
|
| 1629 | + get_class($this->get_model()).'::instance()->get_one_by_ID()', |
|
| 1630 | 1630 | '<br />' |
| 1631 | 1631 | ) |
| 1632 | 1632 | ); |
@@ -1894,7 +1894,7 @@ discard block |
||
| 1894 | 1894 | if (strpos($model_name, "EE_") === 0) { |
| 1895 | 1895 | $model_classname = str_replace("EE_", "EEM_", $model_name); |
| 1896 | 1896 | } else { |
| 1897 | - $model_classname = "EEM_" . $model_name; |
|
| 1897 | + $model_classname = "EEM_".$model_name; |
|
| 1898 | 1898 | } |
| 1899 | 1899 | return $model_classname; |
| 1900 | 1900 | } |
@@ -2278,7 +2278,7 @@ discard block |
||
| 2278 | 2278 | */ |
| 2279 | 2279 | protected function _property_exists($properties) |
| 2280 | 2280 | { |
| 2281 | - foreach ((array)$properties as $property_name) { |
|
| 2281 | + foreach ((array) $properties as $property_name) { |
|
| 2282 | 2282 | //first make sure this property exists |
| 2283 | 2283 | if ( ! $this->_fields[$property_name]) { |
| 2284 | 2284 | throw new EE_Error( |
@@ -2608,8 +2608,8 @@ discard block |
||
| 2608 | 2608 | __('Trying to refresh a model object with ID "%1$s" that\'s not in the entity map? First off: you should put it in the entity map by calling %2$s. Second off, if you want what\'s in the database right now, you should just call %3$s yourself and discard this model object.', |
| 2609 | 2609 | 'event_espresso'), |
| 2610 | 2610 | $this->ID(), |
| 2611 | - get_class($this->get_model()) . '::instance()->add_to_entity_map()', |
|
| 2612 | - get_class($this->get_model()) . '::instance()->refresh_entity_map()' |
|
| 2611 | + get_class($this->get_model()).'::instance()->add_to_entity_map()', |
|
| 2612 | + get_class($this->get_model()).'::instance()->refresh_entity_map()' |
|
| 2613 | 2613 | ) |
| 2614 | 2614 | ); |
| 2615 | 2615 | } |
@@ -2668,7 +2668,7 @@ discard block |
||
| 2668 | 2668 | { |
| 2669 | 2669 | foreach ($this->get_model()->relation_settings() as $relation_name => $relation_obj) { |
| 2670 | 2670 | if ($relation_obj instanceof EE_Belongs_To_Relation) { |
| 2671 | - $classname = 'EE_' . $this->get_model()->get_this_model_name(); |
|
| 2671 | + $classname = 'EE_'.$this->get_model()->get_this_model_name(); |
|
| 2672 | 2672 | if ( |
| 2673 | 2673 | $this->get_one_from_cache($relation_name) instanceof $classname |
| 2674 | 2674 | && $this->get_one_from_cache($relation_name)->ID() |