コード例 #1
0
 function loadReplacements()
 {
     $congs = self::getCongregations();
     $this->_keywords = $this->_cong_keywords = array();
     if (is_file($this->_filename)) {
         $this->_keywords = ODF_Tools::getKeywords($this->_filename);
     } else {
         if (is_dir($this->_filename)) {
             foreach (self::getCongregations() as $congid => $cong) {
                 foreach (explode(',', self::EXTENSIONS) as $extn) {
                     $filename = $this->_filename . '/' . $cong['meeting_time'] . '.' . $extn;
                     if (file_exists($filename)) {
                         $this->_cong_keywords[$congid] = array_merge(array_get($this->_cong_keywords, $congid, array()), ODF_Tools::getKeywords($filename));
                         $this->_keywords = array_merge($this->_keywords, $this->_cong_keywords[$congid]);
                     }
                 }
             }
         } else {
             add_message("Could not find file " . $this->_filename, 'error');
         }
     }
     $this->_keywords = array_unique($this->_keywords);
     $congs = self::getCongregations();
     foreach ($congs as $congid => $cong) {
         $service = Service::findByDateAndCong($this->_service_date, $congid);
         if (empty($service)) {
             add_message("Could not find service for " . $cong['name'] . " on " . $this->_service_date, 'failure');
             unset($congs[$congid]);
             continue;
         }
         $next_service = Service::findByDateAndCong(date('Y-m-d', strtotime($this->_service_date . ' +1 week')), $congid);
         $list = is_file($this->_filename) ? $this->_keywords : array_get($this->_cong_keywords, $congid, array());
         foreach ($list as $keyword) {
             $keyword = strtoupper($keyword);
             if (0 === strpos($keyword, 'NAME_OF_')) {
                 $role_title = substr($keyword, strlen('NAME_OF_'));
                 $this->_replacements[$congid][$keyword] = $service->getPersonnelByRoleTitle($role_title);
             } else {
                 if (0 === strpos($keyword, 'SERVICE_')) {
                     $service_field = strtolower(substr($keyword, strlen('SERVICE_')));
                     $this->_replacements[$congid][$keyword] = $service->getValue($service_field);
                     if (null === $this->_replacements[$congid][$keyword]) {
                         $this->_replacements[$congid][$keyword] = '';
                     }
                     if ($service_field == 'date') {
                         // make a friendly date
                         $this->_replacements[$congid][$keyword] = date('j F Y', strtotime($this->_replacements[$congid][$keyword]));
                     }
                 } else {
                     if (0 === strpos($keyword, 'NEXT_SERVICE_')) {
                         if (!empty($next_service)) {
                             $service_field = strtolower(substr($keyword, strlen('NEXT_SERVICE_')));
                             $this->_replacements[$congid][$keyword] = $next_service->getValue($service_field);
                             if ($service_field == 'date') {
                                 // make a short friendly date
                                 $this->_replacements[$congid][$keyword] = date('j M', strtotime($this->_replacements[$congid][$keyword]));
                             }
                         } else {
                             $this->_replacements[$congid][$keyword] = '';
                             add_message('NEXT_SERVICE keyword could not be replaced because no next service was found for ' . $cong['name'], 'warning');
                         }
                     } else {
                         if (0 === strpos($keyword, 'CONGREGATION_')) {
                             $cong_field = strtolower(substr($keyword, strlen('CONGREGATION_')));
                             $this->_replacements[$congid][$keyword] = $cong[$cong_field];
                         } else {
                             $this->_replacements[$congid][$keyword] = '';
                         }
                     }
                 }
             }
         }
     }
 }
 function processExpand()
 {
     if ($this->_service_date) {
         $found_files = array();
         foreach ($this->_dirs['expand'] as $dir) {
             $di = new DirectoryIterator($dir);
             foreach ($di as $fileinfo) {
                 if (!$fileinfo->isFile()) {
                     continue;
                 }
                 $pathinfo = pathinfo($fileinfo->getFilename());
                 if (in_array($pathinfo['extension'], array('odt', 'odp'))) {
                     $found_files[] = $fileinfo->getFilename();
                     $this->_keywords = array_merge($this->_keywords, ODF_Tools::getKeywords($fileinfo->getPathname()));
                     if (!empty($_POST['replacements'])) {
                         // make copies and replace the keywords
                         $this->expandFile($fileinfo->getPathname());
                     }
                 }
             }
         }
         if (empty($_POST['replacements'])) {
             if (empty($found_files)) {
                 add_message("Could not expand - no ODT or ODP files were found in " . implode(', ', $this->_dirs['expand']), 'failure');
             } else {
                 add_message("Files to be expanded: <br />" . implode('<br />', $found_files), 'success', true);
             }
             $this->loadReplacements();
         } else {
             $this->_addGeneratedFilesMessage();
         }
     }
 }