Advertisement
Greenice

Zend Framework Controller Example

Jan 31st, 2019
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 19.76 KB | None | 0 0
  1. <?php
  2.  
  3.     class BusinessesController extends Nocowboys_Controller_Action_V3_Default
  4.     {
  5.         const COUNT_REVIEWS_WITH_MICRODATA = 80;
  6.  
  7.         /**
  8.          * Manages showing a business page
  9.          * @return boolean
  10.          */
  11.         public function indexAction()
  12.         {
  13.             $config = $this->getConfig();
  14.  
  15.             $businessUri = $this->_request->getParam('businessUri');
  16.  
  17.             // Load the business if it exists
  18.             $businessTable = new Model_DbTable_Business();
  19.             $business = $businessTable->findBusinessByUri($businessUri);
  20.  
  21.             $allowRating = $this->_request->getParam('allow-rating');
  22.  
  23.             // If there's no business, then throw a 404
  24.             if (is_null($business))
  25.             {
  26.                 $this->_throw404();
  27.             }
  28.  
  29.             if ((!$business->isOnline()) and ( is_null($allowRating)))
  30.             {
  31.                 // Throw a 410 - page gone. The business exists but has been
  32.                 //  removed from the system. We can throw a 404, but with a 410
  33.                 //  we can choose not to report it as an error
  34.                 throw new Zend_Controller_Action_Exception('This business has been removed from NoCowboys', 410);
  35.             }
  36.  
  37.             $business->profilePageViewed();
  38.             $this->_normaliseUrl($business, true);
  39.  
  40.             $businessRegistered = $business->isRegistered();
  41.             $this->view->registered = $businessRegistered;
  42.  
  43.             // Load the images
  44.             $images = $business->getImages();
  45.             $this->view->images = $images;
  46.             $this->view->countImages = count($images);
  47.             $this->view->associations = $business->getAssociations();
  48.  
  49.             $publicMapCoordinates = $business->getPublicMapCoordinates();
  50.             $mapCoordinates = $business->getMapCoordinates(true);
  51.             $businessHiddenMap = $business->mapHidden();
  52.  
  53.             $this->view->businessObject = $business;
  54.             $this->view->businessLocation = $business->getLocationArea();
  55.             $this->view->mapCoordinates = $publicMapCoordinates;
  56.             $this->view->showMap = (($businessRegistered) and ( !is_null($publicMapCoordinates)));
  57.             $this->view->useMapMarker = (($businessHiddenMap == false) and ( !is_null($mapCoordinates)));
  58.  
  59.             // Load the ratings
  60.             $this->view->ratings = $business->getAuthenticatedRatings(false, false);
  61.             $this->view->nonCommentRatings = $business->getAuthenticatedRatings(false);
  62.             $this->view->unauthenticatedRatings = $business->getUnauthenticatedRatings(false, mktime(0, 0, 0, 8, 1, 2011));
  63.             $this->view->video = $business->getVideoKey();
  64.             $this->view->mapKey = $config->google->maps->key;
  65.  
  66.             count($this->view->ratings) > self::COUNT_REVIEWS_WITH_MICRODATA ? $showMicrodata = false : $showMicrodata = true;
  67.             $this->view->showMicrodata = $showMicrodata;
  68.  
  69.             $video = $business->getVideoKey();
  70.             $this->view->video = $video;
  71.             $this->view->countVideo = !is_null($video) ? 1 : 0;
  72.  
  73.             // Media
  74.             $this->view->countMedia = $this->view->countImages + $this->view->countVideo;
  75.  
  76.             // Tell Google not to index this page if we don't have enough
  77.             //  ratings
  78.             $totalRatings = count($this->view->ratings) + count($this->view->nonCommentRatings) + count($this->view->unauthenticatedRatings);
  79.             if (($totalRatings < $config->seo->business_minimum_rating_count) and !$businessRegistered)
  80.             {
  81.                 $this->_setNoIndex();
  82.             } elseif (!$businessRegistered and $business->isHideFromSearchEngines()) {
  83.                 $this->_setNoIndex();
  84.             }
  85.  
  86.             $pageTitle = $business->CompanyName;
  87.             $this->view->title = $pageTitle;
  88.             $this->view->headerTitle = $business->getTitle();
  89.  
  90.             if (!is_null($this->view->businessLocation))
  91.             {
  92.                 $areaUrl = $this->view->businessLocation->URLName;
  93.             }
  94.             else
  95.             {
  96.                 $areaUrl = 'new-zealand';
  97.             }
  98.  
  99.             $breadcrumbArray = $business->getBreadcrumbArray();
  100.             $this->view->breadcrumb = $breadcrumbArray;
  101.  
  102.             // Set up the meta tags
  103.             $metaDescription = $business->getMetaDescription();
  104.             $url = $business->getURI();
  105.  
  106.             // Show the meta tags for the page
  107.             $this->view->headMeta()->setName('description', $metaDescription);
  108.             $this->view->headMeta()->setName('publisher', 'https://plus.google.com/+nocowboys');
  109.  
  110.             // Opengraph tags
  111.             $this->view->HeadMetaProperty()->appendProperty('og:title', $pageTitle);
  112.             $this->view->HeadMetaProperty()->appendProperty('og:type', 'website');
  113.             $this->view->HeadMetaProperty()->appendProperty('og:url', $config->domain.$url);
  114.             $this->view->HeadMetaProperty()->appendProperty('og:description', $metaDescription);
  115.             $this->view->HeadMetaProperty()->appendProperty('fb:admins', $config->facebook->admins);
  116.             $this->view->HeadMetaProperty()->appendProperty('fb:app_id', $config->facebook->appId);
  117.  
  118.             $logo = $business->getLogo();
  119.  
  120.             if ($logo !== false)
  121.             {
  122.                 $logoUrl = $this->view->escape($config->domain.'/images/business-logos/300x300/100/'.$logo);
  123.                 $logoMicrodata = $logoUrl;
  124.             }
  125.             else
  126.             {
  127.                 $logoUrl = $config->domain.'/images/v3/logo-social.png';
  128.                 $logoMicrodata = $config->domain.'/images/v3/no-image-available.png';
  129.             }
  130.  
  131.             $this->view->logoMicrodataUrl = $logoMicrodata;
  132.  
  133.             $this->view->HeadMetaProperty()->appendProperty('og:image', $logoUrl);
  134.  
  135.             // Twitter cards
  136.             $this->view->headMeta()->setName('twitter:card', 'summary');
  137.             $this->view->headMeta()->setName('twitter:title', $pageTitle);
  138.             $this->view->headMeta()->setName('twitter:url', $url);
  139.             $this->view->headMeta()->setName('twitter:description', $metaDescription);
  140.             $this->view->headMeta()->setName('twitter:image', $logoUrl);
  141.  
  142.             // URL hardcoded because the authoritive source is the live website
  143.             $this->view->headMeta()->setName('canonical', $config->domain.$url);
  144.  
  145.             // Switch off auto-detect for phone links
  146.             $this->view->headMeta()->setName('format-detection', 'telephone=no');
  147.  
  148.             // Get competitors if the business is not registered
  149.             if (!$businessRegistered)
  150.             {
  151.                 $this->view->competitors = $business->getCompetitors();
  152.             }
  153.  
  154.             // Check if we're restricting the robots
  155.             if ((isset($this->metaRobots)) and ( trim($this->metaRobots)))
  156.             {
  157.                 $this->view->headMeta()->setName('robots', $this->metaRobots);
  158.             }
  159.  
  160.             $form = new Form_Business_Rate($business);
  161.  
  162.             if (($this->_request->isPost()) and ( $this->checkFormAction(Form_Business_Rate::FORM_ACTION)))
  163.             {
  164.                 $formData = $this->_request->getPost();
  165.  
  166.                 if ($form->isValid($formData))
  167.                 {
  168.                     $formData = $form->getValues();
  169.                     $this->_rateBusiness($business, $formData, $allowRating);
  170.                 }
  171.                 else
  172.                 {
  173.                     $this->showError('Looks like there was a problem with your rating. Look below for the issues, and try again.');
  174.                     $form->populate($formData);
  175.                 }
  176.             }
  177.             else
  178.             {
  179.                 if (!$business->isVerified())
  180.                 {
  181.                     $this->showWarning('This business has not yet been checked and
  182.                         verified for name and contact details. You may not be able to find it
  183.                         in any of the search results until we validate and confirm it.');
  184.                 }
  185.  
  186.                 // If there's a recent rating, set it up
  187.                 $mostRecentRatingId = $this->_request->getParam('most-recent-rating');
  188.                 $ratingJustAuthenticated = $this->_request->getParam('rating-authenticated');
  189.  
  190.                 if (!is_null($mostRecentRatingId))
  191.                 {
  192.                     $ratingTable = new Model_DbTable_Rating();
  193.                     $this->view->mostRecentRating = $ratingTable->fetchRowByValue('ID', $mostRecentRatingId);
  194.                 }
  195.                 else
  196.                 if (!is_null($ratingJustAuthenticated))
  197.                 {
  198.                     $ratingTable = new Model_DbTable_Rating();
  199.                     $this->view->ratingJustAuthenticated = $ratingTable->fetchRowByValue('ID', $ratingJustAuthenticated);
  200.                 }
  201.             }
  202.  
  203.             $this->view->form = $form;
  204.  
  205.             $this->_helper->BusinessSendEmail($business);
  206.             $this->_helper->BusinessSendTextMessage($business);
  207.             $this->_helper->layout->setLayout('v3/business-profile');
  208.  
  209.             // Set up the headers
  210.             $this->setHeaderLastModified(strtotime($business->lastUpdated));
  211.             $this->_enableGoogleMaps();
  212.             $this->_enableBusinessPageJs();
  213.  
  214.             $this->view->canonical = $config->domain.$url;
  215.  
  216.             if (!$business->isTrading())
  217.             {
  218.                 $contactLink = $this->view->url([], 'contact-send-message');
  219.                 $this->showWarning($business->CompanyName.' have notified NoCowboys they are no longer trading. If you do suspect they continue to trade (naughty, naughty!), <a href="'.$contactLink.'">let us know</a>!');
  220.             }
  221.  
  222.             // Add to analytics
  223.             $analytics = Model_DbTable_Analytic::createRowStatic();
  224.             $analytics->uri = $url;
  225.             $analytics->businessId = $business->ID;
  226.             $analytics->event = Model_DbTable_Analytic::PROFILE_VIEW;
  227.             $analytics->save();
  228.         }
  229.  
  230.         /**
  231.          * Figures out if the URL is an older one for the business and redirects if so
  232.          */
  233.         private function _normaliseUrl($businessObject, $isBusinessPage = false)
  234.         {
  235.             if ($isBusinessPage)
  236.             {
  237.                 $businessUri = $this->_request->getParam('businessUri');
  238.  
  239.                 if (strcasecmp(trim($businessUri), trim($businessObject->URLName)) != 0)
  240.                 {
  241.                     $params = array('business' => $businessObject->URLName);
  242.                     $this->_helper->redirector->setCode(301);
  243.                     $this->_helper->redirector->gotoUrl($businessObject->getURI());
  244.                 }
  245.             }
  246.             else
  247.             {
  248.                 $businessUri = $this->_request->getParam('business');
  249.  
  250.                 if (strcasecmp(trim($businessUri), trim($businessObject->URLName)) != 0)
  251.                 {
  252.                     $params = array('business' => $businessObject->URLName);
  253.                     $this->_helper->redirector->setCode(301);
  254.                     $this->_helper->redirector->gotoSimple($this->getRequest()->getActionName(), $this->getRequest()->getControllerName(), 'default', $params);
  255.                 }
  256.             }
  257.         }
  258.  
  259.         /**
  260.          * Handles any weird legacy business links that may pop up from time to time
  261.          *
  262.          */
  263.         public function legacyUrlAction()
  264.         {
  265.             $slug = $this->getRequest()->getParam('slug');
  266.             $redirectType = $this->getRequest()->getParam('type');
  267.  
  268.             // e.g. viewtradesman/Mechanics/CarTune-Service-Centre-61941
  269.             if ($redirectType == 1)
  270.             {
  271.                 // Split the string to get the business ID and redirect
  272.                 $uriArray = explode('-', $slug);
  273.  
  274.                 // The last item is the ID
  275.                 $businessId = $uriArray[count($uriArray) - 1];
  276.             }
  277.             else
  278.             // e.g. pages/viewtradesman.php?tradesman=65804
  279.             if ($redirectType == 2)
  280.             {
  281.                 // The slug is the business ID. Load and permanently redirect
  282.                 //  to that page
  283.                 $businessId = $slug;
  284.             }
  285.  
  286.             $businessTable = new Model_DbTable_Business();
  287.             $businessToLoad = $businessTable->fetchRowByValue('ID', $businessId);
  288.  
  289.             if (is_null($businessToLoad))
  290.             {
  291.                 // Send them to this phantom page so they get a 404
  292.                 header('location: /businesses/'.$slug);
  293.             }
  294.             else
  295.             {
  296.                 header('location: '.$businessToLoad->getUri());
  297.             }
  298.  
  299.             $this->_helper->layout()->disableLayout();
  300.             $this->_helper->viewRenderer->setNoRender(true);
  301.         }
  302.  
  303.         /**
  304.          * Confirms the given email ID is not spam, and sends it to the business
  305.          *  it was intended for
  306.          *
  307.          */
  308.         public function confirmSpamEmailAction()
  309.         {
  310.             $correspondenceId = $this->getRequest()->getParam('correspondence');
  311.  
  312.             if (is_null($correspondenceId))
  313.             {
  314.                 throw new Exception('No email ID given');
  315.             }
  316.  
  317.             $correspondence = Model_DbTable_BusinessCorrespondence::fetchRowByValueStatic('ID', $correspondenceId);
  318.  
  319.             if (is_null($correspondence))
  320.             {
  321.                 throw new Exception('No email available with that ID');
  322.             }
  323.  
  324.             $this->view->correspondence = $correspondence;
  325.             $this->view->isSpam = $correspondence->isSpam; // Set this explicitly - the correspondence object will change this shortly
  326.  
  327.             if ($correspondence->isSpam == 1)
  328.             {
  329.                 $this->view->title = 'Message sent successfully';
  330.                 $this->view->breadcrumb = 'Message sent successfully';
  331.  
  332.                 // Mark the correspondence as not spam. This will send it again
  333.                 //  also
  334.                 $correspondence->markNotSpam();
  335.             }
  336.             else
  337.             {
  338.                 $this->view->title = 'Email has already been sent';
  339.                 $this->view->breadcrumb = 'Email has already been sent';
  340.             }
  341.         }
  342.  
  343.         /**
  344.          * This is the landing page for businesses
  345.          *
  346.          */
  347.         public function salesAction()
  348.         {
  349.             $config = Zend_Registry::get('config');
  350.  
  351.             $defaultProductId = $config->website->defaultProductId;
  352.             $defaultMonthlyProductId = $config->website->defaultMonthlyProductId;
  353.  
  354.             $this->view->defaultProduct = Model_DbTable_Product::fetchRowByValueStatic('ID', $defaultProductId);
  355.             $this->view->defaultMonthlyProduct = Model_DbTable_Product::fetchRowByValueStatic('ID', $defaultMonthlyProductId);
  356.  
  357.             $newBusinessSession = new Zend_Session_Namespace(Model_DbTableRow_Business::SESSION_NAMESPACE_NEW_BUSINESS);
  358.             $newBusinessSession->isFacebookLead = false;
  359.  
  360.             $this->view->title = 'Kiwi businesses use NoCowboys to track our customers, '
  361.                 . 'build reputations, and find new work.';
  362.             $this->view->headMeta()->setName('description', 'NoCowboys is the trusted place to safeguard your '
  363.                 . 'business\'s reputation. See what we can do for your company');
  364.             $this->view->headMeta()->setName('keywords', 'Why (use NC), Reputation, Work, Business, Management, '
  365.                 . 'Marketing, Registration, Growth, Benefits, Advantages, Profile, Customers, Networking, Exposure');
  366.         }
  367.  
  368.         /**
  369.          * Action for sending an email to a business, mainly used by mobile
  370.          *
  371.          */
  372.         public function emailAction()
  373.         {
  374.             $business = $this->_getBusinessFromUri();
  375.             $this->_redirectToBusinessHomePage($business);
  376.  
  377.             $this->view->title = 'Send an email to '.$business->CompanyName;
  378.             $this->_helper->BusinessSendEmail($business);
  379.             $this->_helper->layout->setLayout('v3/layout');
  380.  
  381.             $breadcrumbArray = $business->getBreadcrumbArray('Email');
  382.             $this->view->breadcrumb = $breadcrumbArray;
  383.  
  384.             $this->view->headMeta()->setName('description', 'Need to get in touch with '.$business->CompanyName.'? Send them an email using our easy email sender');
  385.         }
  386.  
  387.         /**
  388.          * Action for sending an SMS to a business, mainly used by mobile
  389.          *
  390.          */
  391.         public function smsAction()
  392.         {
  393.             $business = $this->_getBusinessFromUri();
  394.             $this->_redirectToBusinessHomePage($business);
  395.  
  396.             $this->view->title = 'Send an SMS to '.$business->CompanyName;
  397.             $this->_helper->BusinessSendTextMessage($business);
  398.             $this->_helper->layout->setLayout('v3/layout');
  399.  
  400.             $breadcrumbArray = $business->getBreadcrumbArray('SMS');
  401.             $this->view->breadcrumb = $breadcrumbArray;
  402.  
  403.             $this->_enableBusinessPageJs();
  404.  
  405.             $this->view->headMeta()->setName('description', 'Need to get in touch with '.$business->CompanyName.'? Send them an SMS message using our easy SMS sender');
  406.         }
  407.  
  408.         /**
  409.          * Action for rating a business, mainly used by mobile
  410.          *
  411.          */
  412.         public function rateAction()
  413.         {
  414.             // Don't index this page
  415.             $this->_setNoIndex();
  416.             $business = $this->_getBusinessFromUri();
  417.  
  418.             $breadcrumbArray = $business->getBreadcrumbArray('Rate');
  419.             $this->view->breadcrumb = $breadcrumbArray;
  420.  
  421.             $this->view->title = 'Rate '.$business->CompanyName;
  422.  
  423.             $form = new Form_Business_Rate();
  424.  
  425.             if ($this->getRequest()->isPost())
  426.             {
  427.                 $formData = $this->getRequest()->getPost();
  428.                 if ($form->isValid($formData))
  429.                 {
  430.                     $formData = $form->getValues();
  431.                     $this->_rateBusiness($business, $formData);
  432.                 }
  433.                 else
  434.                 {
  435.                     $form->populate($formData);
  436.                 }
  437.             }
  438.  
  439.             $this->view->form = $form;
  440.             $this->_helper->layout->setLayout('v3/layout');
  441.  
  442.             $this->_enableBusinessPageJs();
  443.         }
  444.  
  445.         /**
  446.          * Redirects to a business' website. This is legacy, but some things
  447.          *  still link to it
  448.          *
  449.          * @deprecated since version 3.0
  450.          */
  451.         public function websiteAction()
  452.         {
  453.             $business = $this->_getBusinessFromUri();
  454.  
  455.             if ((is_null($business)) or ( trim($business->Website) == ''))
  456.             {
  457.                 $this->_throw404();
  458.             }
  459.  
  460.             $this->redirect($business->Website);
  461.             exit();
  462.         }
  463.  
  464.         /**
  465.          *
  466.          * @param type $business
  467.          * @param type $formData
  468.          * @param type $allowRating
  469.          */
  470.         protected function _rateBusiness($business, $formData, $allowRating = NULL)
  471.         {
  472.             // Save the user to the newsletter
  473.             Model_DbTable_MailingList::addUser($formData['emailAddress'], $formData['firstName'], isset($formData['newsletter']) and ( $formData['newsletter'] == 1));
  474.  
  475.             // Rating is good to go - create it and save it
  476.             $ratingTable = new Model_DbTable_Rating();
  477.             /** @var Model_DbTableRow_Rating $newRating */
  478.             $newRating = $ratingTable->createRow($formData);
  479.             $newRating->BusinessID = $business->ID;
  480.             $newRating->need_approve = Model_DbTable_Rating::RATING_STATUS_UNAPPROVED;
  481.             $newRating->save([
  482.                 'history' => true
  483.             ]);
  484.  
  485.             // Check the rating isn't spam and get any similar ratings
  486.             $isSpam = $newRating->isSpam();
  487.             $isBlacklisted = $newRating->isSenderBlacklisted();
  488.             $isWhitelisted = $newRating->isSenderWhitelisted();
  489.             $similarRatings = $newRating->getSimilarRatings();
  490.  
  491.             $success = true;
  492.             if(!$isWhitelisted) {
  493.                 $success = false;
  494.                 if ($isBlacklisted)
  495.                 {
  496.                     $this->showError(Model_DbTable_Blacklist::MESSAGE_BLACKLISTED);
  497.                 } elseif ($isSpam) {
  498.                         $newRating->sendSpamEmail();
  499.  
  500.                         $this->showError('Looks like there may be a problem with your rating. We\'ve sent you an email
  501.                        explaining the problem, and it also contains instructions for how to help us fix it for you. Check your
  502.                        inbox and follow the instructions and we\'ll put it right as soon as possible.');
  503.                 } elseif (count($similarRatings) > 0) {
  504.                             $newRating->sendDuplicateEmail();
  505.  
  506.                             $newRating->Locked = 1;
  507.                             $newRating->Hidden = 1;
  508.                             $newRating->save();
  509.  
  510.                             $this->showWarning('It looks like you may have rated this business before. This sometimes happens
  511.                            when you use the same company more than once, or post a rating on the same network as someone
  512.                            else using NoCowboys. Don\'t worry though, we\'ve sent you an email now with instructions for
  513.                            how to activate your rating. Check that, and we\'ll put it right as soon as possible.');
  514.                 } else {
  515.                     $success = true;
  516.                 }
  517.             }
  518.  
  519.             if($success){
  520.                 Nocowboys_Tools::setDetectionData(Model_DbTableRow_DetectionData::TYPE_RATING, $formData + [
  521.                     'Rate url' => $this->getCurrentUrl(),
  522.                     'Business name' => $business->CompanyName,
  523.                 ]);
  524.  
  525.                 $newRating->sendActivationEmail();
  526.  
  527.                 $businessUrl = $this->view->url(['businessUri' => $business->URLName], 'view-business').'?most-recent-rating='.$newRating->ID.'&allow-rating=1';
  528.                 $this->redirect($businessUrl);
  529.             }
  530.  
  531.             // Redirect back to the business page
  532.             if (!is_null($allowRating))
  533.             {
  534.                 header('location: /businesses/'.$business->URLName.'?allow-rating=1');
  535.             }
  536.             else
  537.             {
  538.                 header('location: /businesses/'.$business->URLName);
  539.             }
  540.             exit();
  541.         }
  542.  
  543.         /**
  544.          * Gets the business from the URL parameter
  545.          *
  546.          * @return Model_DbTableRow_Business
  547.          */
  548.         protected function _getBusinessFromUri()
  549.         {
  550.             $businessUri = $this->_request->getParam('businessUri');
  551.  
  552.             // Load the business if it exists
  553.             $businessTable = new Model_DbTable_Business();
  554.             $business = $businessTable->findBusinessByUri($businessUri);
  555.  
  556.             // If there's no business, then throw a 404
  557.             if (is_null($business))
  558.             {
  559.                 $this->_throw404();
  560.             }
  561.  
  562.             return $business;
  563.         }
  564.  
  565.         /**
  566.          * Redirect customer to business homepage if business is unregistered.
  567.          * @param Model_DbTableRow_Business $business
  568.          */
  569.         protected function _redirectToBusinessHomePage(Model_DbTableRow_Business $business )
  570.         {
  571.             if (!$business->isRegistered()) {
  572.                 $this->_helper->redirector->gotoRouteAndExit(['businessUri' => $business->URLName], 'view-business');
  573.             }
  574.         }
  575.  
  576.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement