extra_field.lib.php 89 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Entity\ExtraField as EntityExtraField;
  4. /**
  5. * Class ExtraField
  6. */
  7. class ExtraField extends Model
  8. {
  9. public $columns = array(
  10. 'id',
  11. 'field_type',
  12. 'variable',
  13. 'display_text',
  14. 'default_value',
  15. 'field_order',
  16. 'visible',
  17. 'changeable',
  18. 'filter',
  19. 'extra_field_type',
  20. /* Enable this when field_loggeable is introduced as a table field (2.0)
  21. 'field_loggeable',
  22. */
  23. 'created_at'
  24. );
  25. public $ops = array(
  26. 'eq' => '=', //equal
  27. 'ne' => '<>', //not equal
  28. 'lt' => '<', //less than
  29. 'le' => '<=', //less than or equal
  30. 'gt' => '>', //greater than
  31. 'ge' => '>=', //greater than or equal
  32. 'bw' => 'LIKE', //begins with
  33. 'bn' => 'NOT LIKE', //doesn't begin with
  34. 'in' => 'LIKE', //is in
  35. 'ni' => 'NOT LIKE', //is not in
  36. 'ew' => 'LIKE', //ends with
  37. 'en' => 'NOT LIKE', //doesn't end with
  38. 'cn' => 'LIKE', //contains
  39. 'nc' => 'NOT LIKE' //doesn't contain
  40. );
  41. const FIELD_TYPE_TEXT = 1;
  42. const FIELD_TYPE_TEXTAREA = 2;
  43. const FIELD_TYPE_RADIO = 3;
  44. const FIELD_TYPE_SELECT = 4;
  45. const FIELD_TYPE_SELECT_MULTIPLE = 5;
  46. const FIELD_TYPE_DATE = 6;
  47. const FIELD_TYPE_DATETIME = 7;
  48. const FIELD_TYPE_DOUBLE_SELECT = 8;
  49. const FIELD_TYPE_DIVIDER = 9;
  50. const FIELD_TYPE_TAG = 10;
  51. const FIELD_TYPE_TIMEZONE = 11;
  52. const FIELD_TYPE_SOCIAL_PROFILE = 12;
  53. const FIELD_TYPE_CHECKBOX = 13;
  54. const FIELD_TYPE_MOBILE_PHONE_NUMBER = 14;
  55. const FIELD_TYPE_INTEGER = 15;
  56. const FIELD_TYPE_FILE_IMAGE = 16;
  57. const FIELD_TYPE_FLOAT = 17;
  58. const FIELD_TYPE_FILE = 18;
  59. const FIELD_TYPE_VIDEO_URL = 19;
  60. const FIELD_TYPE_LETTERS_ONLY = 20;
  61. const FIELD_TYPE_ALPHANUMERIC = 21;
  62. const FIELD_TYPE_LETTERS_SPACE = 22;
  63. const FIELD_TYPE_ALPHANUMERIC_SPACE = 23;
  64. public $type = 'user';
  65. public $pageName;
  66. public $pageUrl;
  67. public $extraFieldType = 0;
  68. /**
  69. * @param string $type
  70. */
  71. public function __construct($type)
  72. {
  73. $this->type = $type;
  74. $this->table = Database::get_main_table(TABLE_EXTRA_FIELD);
  75. $this->table_field_options = Database::get_main_table(TABLE_EXTRA_FIELD_OPTIONS);
  76. $this->table_field_values = Database::get_main_table(TABLE_EXTRA_FIELD_VALUES);
  77. switch ($this->type) {
  78. case 'calendar_event':
  79. $this->extraFieldType = EntityExtraField::CALENDAR_FIELD_TYPE;
  80. break;
  81. case 'course':
  82. $this->extraFieldType = EntityExtraField::COURSE_FIELD_TYPE;
  83. break;
  84. case 'user':
  85. $this->extraFieldType = EntityExtraField::USER_FIELD_TYPE;
  86. break;
  87. case 'session':
  88. $this->extraFieldType = EntityExtraField::SESSION_FIELD_TYPE;
  89. break;
  90. case 'question':
  91. $this->extraFieldType = EntityExtraField::QUESTION_FIELD_TYPE;
  92. break;
  93. case 'lp':
  94. $this->extraFieldType = EntityExtraField::LP_FIELD_TYPE;
  95. break;
  96. case 'lp_item':
  97. $this->extraFieldType = EntityExtraField::LP_ITEM_FIELD_TYPE;
  98. break;
  99. }
  100. $this->pageUrl = 'extra_fields.php?type='.$this->type;
  101. // Example QuestionFields
  102. $this->pageName = get_lang(ucwords($this->type).'Fields');
  103. }
  104. /**
  105. * @return int
  106. */
  107. public function getExtraFieldType()
  108. {
  109. return $this->extraFieldType;
  110. }
  111. /**
  112. * @return array
  113. */
  114. public static function getValidExtraFieldTypes()
  115. {
  116. return array(
  117. 'user',
  118. 'course',
  119. 'session',
  120. 'question',
  121. 'lp',
  122. 'calendar_event',
  123. 'lp_item'
  124. );
  125. }
  126. /**
  127. * @return int
  128. */
  129. public function get_count()
  130. {
  131. $em = Database::getManager();
  132. $query = $em->getRepository('ChamiloCoreBundle:ExtraField')->createQueryBuilder('e');
  133. $query->select('count(e.id)');
  134. $query->where('e.extraFieldType = :type');
  135. $query->setParameter('type', $this->getExtraFieldType());
  136. return $query->getQuery()->getScalarResult();
  137. }
  138. /**
  139. * @param string $sidx
  140. * @param string $sord
  141. * @param int $start
  142. * @param int $limit
  143. *
  144. * @return array
  145. */
  146. public function getAllGrid($sidx, $sord, $start, $limit)
  147. {
  148. switch ($sidx) {
  149. case 'field_order':
  150. $sidx = 'e.fieldOrder';
  151. break;
  152. case 'variable':
  153. $sidx = 'e.variable';
  154. break;
  155. case 'display_text':
  156. $sidx = 'e.displayText';
  157. break;
  158. case 'changeable':
  159. $sidx = 'e.changeable';
  160. break;
  161. case 'visible':
  162. $sidx = 'e.visible';
  163. break;
  164. case 'filter':
  165. $sidx = 'e.filter';
  166. break;
  167. case 'display_text':
  168. $sidx = 'e.fieldType';
  169. break;
  170. }
  171. $em = Database::getManager();
  172. $query = $em->getRepository('ChamiloCoreBundle:ExtraField')->createQueryBuilder('e');
  173. $query->select('e')
  174. ->where('e.extraFieldType = :type')
  175. ->setParameter('type', $this->getExtraFieldType())
  176. ->orderBy($sidx, $sord)
  177. ->setFirstResult($start)
  178. ->setMaxResults($limit);
  179. //echo $query->getQuery()->getSQL();
  180. return $query->getQuery()->getArrayResult();
  181. }
  182. /**
  183. * @param array $conditions
  184. * @param null $order_field_options_by
  185. *
  186. * @return array
  187. */
  188. public function get_all($conditions = array(), $order_field_options_by = null)
  189. {
  190. $conditions = Database::parse_conditions(array('where' => $conditions));
  191. if (empty($conditions)) {
  192. $conditions .= " WHERE extra_field_type = ".$this->extraFieldType;
  193. } else {
  194. $conditions .= " AND extra_field_type = ".$this->extraFieldType;
  195. }
  196. $sql = "SELECT * FROM $this->table
  197. $conditions
  198. ORDER BY field_order ASC
  199. ";
  200. $result = Database::query($sql);
  201. $extraFields = Database::store_result($result, 'ASSOC');
  202. $option = new ExtraFieldOption($this->type);
  203. if (!empty($extraFields)) {
  204. foreach ($extraFields as &$extraField) {
  205. $extraField['options'] = $option->get_field_options_by_field(
  206. $extraField['id'],
  207. false,
  208. $order_field_options_by
  209. );
  210. }
  211. }
  212. return $extraFields;
  213. }
  214. /**
  215. * @param string $variable
  216. *
  217. * @return array|bool
  218. */
  219. public function get_handler_field_info_by_field_variable($variable)
  220. {
  221. $variable = Database::escape_string($variable);
  222. $sql = "SELECT * FROM {$this->table}
  223. WHERE
  224. variable = '$variable' AND
  225. extra_field_type = $this->extraFieldType";
  226. $result = Database::query($sql);
  227. if (Database::num_rows($result)) {
  228. $row = Database::fetch_array($result, 'ASSOC');
  229. // All the options of the field
  230. $sql = "SELECT * FROM $this->table_field_options
  231. WHERE field_id='".intval($row['id'])."'
  232. ORDER BY option_order ASC";
  233. $result = Database::query($sql);
  234. while ($option = Database::fetch_array($result)) {
  235. $row['options'][$option['id']] = $option;
  236. }
  237. return $row;
  238. } else {
  239. return false;
  240. }
  241. }
  242. /**
  243. * @param int $fieldId
  244. *
  245. * @return array|bool
  246. */
  247. public function getFieldInfoByFieldId($fieldId)
  248. {
  249. $fieldId = intval($fieldId);
  250. $sql = "SELECT * FROM {$this->table}
  251. WHERE
  252. field_id = '$fieldId' AND
  253. extra_field_type = $this->extraFieldType";
  254. $result = Database::query($sql);
  255. if (Database::num_rows($result)) {
  256. $row = Database::fetch_array($result, 'ASSOC');
  257. // All the options of the field
  258. $sql = "SELECT * FROM $this->table_field_options
  259. WHERE field_id='".$fieldId."'
  260. ORDER BY option_order ASC";
  261. $result = Database::query($sql);
  262. while ($option = Database::fetch_array($result)) {
  263. $row['options'][$option['id']] = $option;
  264. }
  265. return $row;
  266. } else {
  267. return false;
  268. }
  269. }
  270. /**
  271. * @return int
  272. */
  273. public function get_max_field_order()
  274. {
  275. $sql = "SELECT MAX(field_order)
  276. FROM {$this->table}
  277. WHERE
  278. extra_field_type = '.$this->extraFieldType.'";
  279. $res = Database::query($sql);
  280. $order = 0;
  281. if (Database::num_rows($res) > 0) {
  282. $row = Database::fetch_row($res);
  283. $order = $row[0] + 1;
  284. }
  285. return $order;
  286. }
  287. /**
  288. * @param string $handler
  289. *
  290. * @return array
  291. */
  292. public static function get_extra_fields_by_handler($handler)
  293. {
  294. $types= array();
  295. $types[self::FIELD_TYPE_TEXT] = get_lang('FieldTypeText');
  296. $types[self::FIELD_TYPE_TEXTAREA] = get_lang('FieldTypeTextarea');
  297. $types[self::FIELD_TYPE_RADIO] = get_lang('FieldTypeRadio');
  298. $types[self::FIELD_TYPE_SELECT] = get_lang('FieldTypeSelect');
  299. $types[self::FIELD_TYPE_SELECT_MULTIPLE] = get_lang('FieldTypeSelectMultiple');
  300. $types[self::FIELD_TYPE_DATE] = get_lang('FieldTypeDate');
  301. $types[self::FIELD_TYPE_DATETIME] = get_lang('FieldTypeDatetime');
  302. $types[self::FIELD_TYPE_DOUBLE_SELECT] = get_lang('FieldTypeDoubleSelect');
  303. $types[self::FIELD_TYPE_DIVIDER] = get_lang('FieldTypeDivider');
  304. $types[self::FIELD_TYPE_TAG] = get_lang('FieldTypeTag');
  305. $types[self::FIELD_TYPE_TIMEZONE] = get_lang('FieldTypeTimezone');
  306. $types[self::FIELD_TYPE_SOCIAL_PROFILE] = get_lang('FieldTypeSocialProfile');
  307. $types[self::FIELD_TYPE_MOBILE_PHONE_NUMBER] = get_lang('FieldTypeMobilePhoneNumber');
  308. $types[self::FIELD_TYPE_CHECKBOX] = get_lang('FieldTypeCheckbox');
  309. $types[self::FIELD_TYPE_INTEGER] = get_lang('FieldTypeInteger');
  310. $types[self::FIELD_TYPE_FILE_IMAGE] = get_lang('FieldTypeFileImage');
  311. $types[self::FIELD_TYPE_FLOAT] = get_lang('FieldTypeFloat');
  312. $types[self::FIELD_TYPE_FILE] = get_lang('FieldTypeFile');
  313. $types[self::FIELD_TYPE_VIDEO_URL] = get_lang('FieldTypeVideoUrl');
  314. $types[self::FIELD_TYPE_LETTERS_ONLY] = get_lang('FieldTypeOnlyLetters');
  315. $types[self::FIELD_TYPE_ALPHANUMERIC] = get_lang('FieldTypeAlphanumeric');
  316. $types[self::FIELD_TYPE_LETTERS_SPACE] = get_lang(
  317. 'FieldTypeLettersSpaces'
  318. );
  319. $types[self::FIELD_TYPE_ALPHANUMERIC_SPACE] = get_lang(
  320. 'FieldTypeAlphanumericSpaces'
  321. );
  322. switch ($handler) {
  323. case 'course':
  324. case 'session':
  325. case 'user':
  326. break;
  327. }
  328. return $types;
  329. }
  330. /**
  331. * Add elements to a form
  332. *
  333. * @param FormValidator $form
  334. * @param int $itemId
  335. * @param array $exclude variables of extra field to exclude
  336. * @return array|bool
  337. */
  338. public function addElements($form, $itemId = 0, $exclude = [])
  339. {
  340. if (empty($form)) {
  341. return false;
  342. }
  343. $itemId = intval($itemId);
  344. $form->addHidden('item_id', $itemId);
  345. $extraData = false;
  346. if (!empty($itemId)) {
  347. $extraData = self::get_handler_extra_data($itemId);
  348. if ($form) {
  349. $form->setDefaults($extraData);
  350. }
  351. }
  352. $extraFields = $this->get_all(null, 'option_order');
  353. $extra = $this->set_extra_fields_in_form(
  354. $form,
  355. $extraData,
  356. false,
  357. $extraFields,
  358. $itemId,
  359. $exclude
  360. );
  361. return $extra;
  362. }
  363. /**
  364. * @param int $itemId (session_id, question_id, course id)
  365. *
  366. * @return array
  367. */
  368. public function get_handler_extra_data($itemId)
  369. {
  370. if (empty($itemId)) {
  371. return array();
  372. }
  373. $extra_data = array();
  374. $fields = self::get_all();
  375. $field_values = new ExtraFieldValue($this->type);
  376. if (!empty($fields) > 0) {
  377. foreach ($fields as $field) {
  378. $field_value = $field_values->get_values_by_handler_and_field_id(
  379. $itemId, $field['id']
  380. );
  381. if ($field['field_type'] == ExtraField::FIELD_TYPE_TAG) {
  382. $tags = UserManager::get_user_tags_to_string($itemId, $field['id'], false);
  383. $extra_data['extra_'.$field['variable']] = $tags;
  384. continue;
  385. }
  386. if ($field_value) {
  387. $field_value = $field_value['value'];
  388. switch ($field['field_type']) {
  389. case ExtraField::FIELD_TYPE_TAG:
  390. $tags = UserManager::get_user_tags_to_string($itemId, $field['id'], false);
  391. $extra_data['extra_'.$field['variable']] = $tags;
  392. break;
  393. case ExtraField::FIELD_TYPE_DOUBLE_SELECT:
  394. $selected_options = explode(
  395. '::',
  396. $field_value
  397. );
  398. $firstOption = isset($selected_options[0]) ? $selected_options[0] : '';
  399. $secondOption = isset($selected_options[1]) ? $selected_options[1] : '';
  400. $extra_data['extra_'.$field['variable']]['extra_'.$field['variable']] = $firstOption;
  401. $extra_data['extra_'.$field['variable']]['extra_'.$field['variable'].'_second'] = $secondOption;
  402. break;
  403. case ExtraField::FIELD_TYPE_SELECT_MULTIPLE:
  404. $field_value = explode(';', $field_value);
  405. $extra_data['extra_'.$field['variable']] = $field_value;
  406. break;
  407. case ExtraField::FIELD_TYPE_RADIO:
  408. $extra_data['extra_'.$field['variable']]['extra_'.$field['variable']] = $field_value;
  409. break;
  410. default:
  411. $extra_data['extra_'.$field['variable']] = $field_value;
  412. break;
  413. }
  414. } else {
  415. // Set default values
  416. if (isset($field['field_default_value']) && !empty($field['field_default_value'])) {
  417. $extra_data['extra_'.$field['variable']] = $field['field_default_value'];
  418. }
  419. }
  420. }
  421. }
  422. return $extra_data;
  423. }
  424. /**
  425. * @param string $field_type
  426. *
  427. * @return array
  428. */
  429. public function get_all_extra_field_by_type($field_type)
  430. {
  431. // all the information of the field
  432. $sql = "SELECT * FROM {$this->table}
  433. WHERE
  434. field_type = '".Database::escape_string($field_type)."' AND
  435. extra_field_type = $this->extraFieldType
  436. ";
  437. $result = Database::query($sql);
  438. $return = array();
  439. while ($row = Database::fetch_array($result)) {
  440. $return[] = $row['id'];
  441. }
  442. return $return;
  443. }
  444. /**
  445. * @return array
  446. */
  447. public function get_field_types()
  448. {
  449. return self::get_extra_fields_by_handler($this->type);
  450. }
  451. /**
  452. * @param int $id
  453. *
  454. * @return null
  455. */
  456. public function get_field_type_by_id($id)
  457. {
  458. $types = self::get_field_types();
  459. if (isset($types[$id])) {
  460. return $types[$id];
  461. }
  462. return null;
  463. }
  464. /**
  465. * Converts a string like this:
  466. * France:Paris;Bretagne;Marseilles;Lyon|Belgique:Bruxelles;Namur;Liège;Bruges|Peru:Lima;Piura;
  467. * into
  468. * array(
  469. * 'France' =>
  470. * array('Paris', 'Bregtane', 'Marseilles'),
  471. * 'Belgique' =>
  472. * array('Namur', 'Liège')
  473. * ), etc
  474. * @param string $string
  475. *
  476. * @return array
  477. */
  478. public static function extra_field_double_select_convert_string_to_array($string)
  479. {
  480. $options = explode('|', $string);
  481. $options_parsed = array();
  482. $id = 0;
  483. if (!empty($options)) {
  484. foreach ($options as $sub_options) {
  485. $options = explode(':', $sub_options);
  486. $sub_sub_options = explode(';', $options[1]);
  487. $options_parsed[$id] = array(
  488. 'label' => $options[0],
  489. 'options' => $sub_sub_options,
  490. );
  491. $id++;
  492. }
  493. }
  494. return $options_parsed;
  495. }
  496. /**
  497. * @param array $options
  498. *
  499. * @return array
  500. */
  501. public static function extra_field_double_select_convert_array_to_ordered_array($options)
  502. {
  503. $options_parsed = array();
  504. if (!empty($options)) {
  505. foreach ($options as $option) {
  506. if ($option['option_value'] == 0) {
  507. $options_parsed[$option['id']][] = $option;
  508. } else {
  509. $options_parsed[$option['option_value']][] = $option;
  510. }
  511. }
  512. }
  513. return $options_parsed;
  514. }
  515. /**
  516. * @param array $options the result of the get_field_options_by_field() array
  517. *
  518. * @return string
  519. */
  520. public static function extra_field_double_select_convert_array_to_string($options)
  521. {
  522. $string = null;
  523. $options_parsed = self::extra_field_double_select_convert_array_to_ordered_array($options);
  524. if (!empty($options_parsed)) {
  525. foreach ($options_parsed as $option) {
  526. foreach ($option as $key => $item) {
  527. $string .= $item['display_text'];
  528. if ($key == 0) {
  529. $string .= ':';
  530. } else {
  531. if (isset($option[$key + 1])) {
  532. $string .= ';';
  533. }
  534. }
  535. }
  536. $string .= '|';
  537. }
  538. }
  539. if (!empty($string)) {
  540. $string = substr($string, 0, strlen($string) - 1);
  541. }
  542. return $string;
  543. }
  544. /**
  545. * @param array $params
  546. *
  547. * @return array
  548. */
  549. public function clean_parameters($params)
  550. {
  551. if (!isset($params['variable']) || empty($params['variable'])) {
  552. $params['variable'] = $params['display_text'];
  553. }
  554. $params['variable'] = trim(strtolower(str_replace(" ", "_", $params['variable'])));
  555. if (!isset($params['field_order'])) {
  556. $max_order = self::get_max_field_order();
  557. $params['field_order'] = $max_order;
  558. }
  559. return $params;
  560. }
  561. /**
  562. * @param array $params
  563. * @param bool $show_query
  564. *
  565. * @return bool
  566. */
  567. public function save($params, $show_query = false)
  568. {
  569. $fieldInfo = self::get_handler_field_info_by_field_variable($params['variable']);
  570. $params = self::clean_parameters($params);
  571. $params['extra_field_type'] = $this->extraFieldType;
  572. if ($fieldInfo) {
  573. return $fieldInfo['id'];
  574. } else {
  575. $id = parent::save($params, $show_query);
  576. if ($id) {
  577. $session_field_option = new ExtraFieldOption($this->type);
  578. $params['field_id'] = $id;
  579. $session_field_option->save($params);
  580. }
  581. return $id;
  582. }
  583. }
  584. /**
  585. * @param array $params
  586. *
  587. * @return bool|void
  588. */
  589. public function update($params)
  590. {
  591. $params = self::clean_parameters($params);
  592. if (isset($params['id'])) {
  593. $field_option = new ExtraFieldOption($this->type);
  594. $params['field_id'] = $params['id'];
  595. $field_option->save($params);
  596. }
  597. parent::update($params);
  598. }
  599. /**
  600. * @param $id
  601. *
  602. * @return bool|void
  603. */
  604. public function delete($id)
  605. {
  606. $field_option = new ExtraFieldOption($this->type);
  607. $field_option->delete_all_options_by_field_id($id);
  608. $session_field_values = new ExtraFieldValue($this->type);
  609. $session_field_values->delete_all_values_by_field_id($id);
  610. parent::delete($id);
  611. }
  612. /**
  613. * @param FormValidator $form
  614. * @param array $extraData
  615. * @param bool $admin_permissions
  616. * @param int $user_id
  617. * @param array $extra
  618. * @param int $itemId
  619. * @param array $exclude variables of extra field to exclude
  620. * @return array
  621. */
  622. public function set_extra_fields_in_form(
  623. $form,
  624. $extraData,
  625. $admin_permissions = false,
  626. $extra = array(),
  627. $itemId = null,
  628. $exclude = []
  629. ) {
  630. $type = $this->type;
  631. $jquery_ready_content = null;
  632. if (!empty($extra)) {
  633. foreach ($extra as $field_details) {
  634. // Getting default value id if is set
  635. $defaultValueId = null;
  636. if (isset($field_details['options']) && !empty($field_details['options'])) {
  637. $valueToFind = null;
  638. if (isset($field_details['field_default_value'])) {
  639. $valueToFind = $field_details['field_default_value'];
  640. }
  641. // If a value is found we override the default value
  642. if (isset($extraData['extra_'.$field_details['variable']])) {
  643. $valueToFind = $extraData['extra_'.$field_details['variable']];
  644. }
  645. foreach ($field_details['options'] as $option) {
  646. if ($option['option_value'] == $valueToFind) {
  647. $defaultValueId = $option['id'];
  648. }
  649. }
  650. }
  651. if (!$admin_permissions) {
  652. if ($field_details['visible'] == 0) {
  653. continue;
  654. }
  655. if (in_array($field_details['variable'], $exclude)) {
  656. continue;
  657. }
  658. }
  659. switch ($field_details['field_type']) {
  660. case ExtraField::FIELD_TYPE_TEXT:
  661. $form->addElement(
  662. 'text',
  663. 'extra_'.$field_details['variable'],
  664. $field_details['display_text'],
  665. array()
  666. );
  667. $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes');
  668. $form->applyFilter('extra_'.$field_details['variable'], 'trim');
  669. if (!$admin_permissions) {
  670. if ($field_details['visible'] == 0) {
  671. $form->freeze(
  672. 'extra_'.$field_details['variable']
  673. );
  674. }
  675. }
  676. break;
  677. case ExtraField::FIELD_TYPE_TEXTAREA:
  678. $form->addHtmlEditor(
  679. 'extra_'.$field_details['variable'],
  680. $field_details['display_text'],
  681. false,
  682. false,
  683. array('ToolbarSet' => 'Profile', 'Width' => '100%', 'Height' => '130')
  684. );
  685. $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes');
  686. $form->applyFilter('extra_'.$field_details['variable'], 'trim');
  687. if (!$admin_permissions) {
  688. if ($field_details['visible'] == 0) {
  689. $form->freeze(
  690. 'extra_'.$field_details['variable']
  691. );
  692. }
  693. }
  694. break;
  695. case ExtraField::FIELD_TYPE_RADIO:
  696. $group = array();
  697. if (isset($field_details['options']) && !empty($field_details['options'])) {
  698. foreach ($field_details['options'] as $option_details) {
  699. $options[$option_details['option_value']] = $option_details['display_text'];
  700. $group[] = $form->createElement(
  701. 'radio',
  702. 'extra_'.$field_details['variable'],
  703. $option_details['option_value'],
  704. $option_details['display_text'].'<br />',
  705. $option_details['option_value']
  706. );
  707. }
  708. }
  709. $form->addGroup(
  710. $group,
  711. 'extra_'.$field_details['variable'],
  712. $field_details['display_text'],
  713. ''
  714. );
  715. if (!$admin_permissions) {
  716. if ($field_details['visible'] == 0) {
  717. $form->freeze(
  718. 'extra_'.$field_details['variable']
  719. );
  720. }
  721. }
  722. break;
  723. case ExtraField::FIELD_TYPE_CHECKBOX:
  724. $group = array();
  725. if (isset($field_details['options']) && !empty($field_details['options'])) {
  726. foreach ($field_details['options'] as $option_details) {
  727. $options[$option_details['option_value']] = $option_details['display_text'];
  728. $group[] = $form->createElement(
  729. 'checkbox',
  730. 'extra_'.$field_details['variable'],
  731. $option_details['option_value'],
  732. $option_details['display_text'].'<br />',
  733. $option_details['option_value']
  734. );
  735. }
  736. } else {
  737. $fieldVariable = "extra_{$field_details['variable']}";
  738. $checkboxAttributes = array();
  739. if (is_array($extraData) && array_key_exists($fieldVariable, $extraData)) {
  740. $checkboxAttributes['checked'] = true;
  741. }
  742. // We assume that is a switch on/off with 1 and 0 as values
  743. $group[] = $form->createElement(
  744. 'checkbox',
  745. 'extra_'.$field_details['variable'],
  746. null,
  747. //$field_details['display_text'].'<br />',
  748. get_lang('Yes'),
  749. $checkboxAttributes
  750. );
  751. }
  752. $form->addGroup(
  753. $group,
  754. 'extra_'.$field_details['variable'],
  755. $field_details['display_text'],
  756. ''
  757. );
  758. if (!$admin_permissions) {
  759. if ($field_details['visible'] == 0) {
  760. $form->freeze(
  761. 'extra_'.$field_details['variable']
  762. );
  763. }
  764. }
  765. break;
  766. case ExtraField::FIELD_TYPE_SELECT:
  767. $get_lang_variables = false;
  768. if (in_array(
  769. $field_details['variable'],
  770. array('mail_notify_message', 'mail_notify_invitation', 'mail_notify_group_message')
  771. )
  772. ) {
  773. $get_lang_variables = true;
  774. }
  775. // Get extra field workflow
  776. $userInfo = api_get_user_info();
  777. $addOptions = array();
  778. $optionsExists = false;
  779. global $app;
  780. // Check if exist $app['orm.em'] object
  781. if (isset($app['orm.em']) && is_object($app['orm.em'])) {
  782. $optionsExists = $app['orm.em']
  783. ->getRepository('ChamiloLMS\Entity\ExtraFieldOptionRelFieldOption')
  784. ->findOneBy(array('fieldId' => $field_details['id']));
  785. }
  786. if ($optionsExists) {
  787. if (isset($userInfo['status']) && !empty($userInfo['status'])) {
  788. $fieldWorkFlow = $app['orm.em']->getRepository('ChamiloLMS\Entity\ExtraFieldOptionRelFieldOption')
  789. ->findBy(
  790. array(
  791. 'fieldId' => $field_details['id'],
  792. 'relatedFieldOptionId' => $defaultValueId,
  793. 'roleId' => $userInfo['status']
  794. )
  795. );
  796. foreach ($fieldWorkFlow as $item) {
  797. $addOptions[] = $item->getFieldOptionId();
  798. }
  799. }
  800. }
  801. $options = array();
  802. if (empty($defaultValueId)) {
  803. $options[''] = get_lang('SelectAnOption');
  804. }
  805. $optionList = array();
  806. if (!empty($field_details['options'])) {
  807. foreach ($field_details['options'] as $option_details) {
  808. $optionList[$option_details['id']] = $option_details;
  809. if ($get_lang_variables) {
  810. $options[$option_details['option_value']] = get_lang($option_details['display_text']);
  811. } else {
  812. if ($optionsExists) {
  813. // Adding always the default value
  814. if ($option_details['id'] == $defaultValueId) {
  815. $options[$option_details['option_value']] = $option_details['display_text'];
  816. } else {
  817. if (isset($addOptions) && !empty($addOptions)) {
  818. // Parsing filters
  819. if (in_array($option_details['id'], $addOptions)) {
  820. $options[$option_details['option_value']] = $option_details['display_text'];
  821. }
  822. }
  823. }
  824. } else {
  825. // Normal behaviour
  826. $options[$option_details['option_value']] = $option_details['display_text'];
  827. }
  828. }
  829. }
  830. if (isset($optionList[$defaultValueId])) {
  831. if (isset($optionList[$defaultValueId]['option_value']) &&
  832. $optionList[$defaultValueId]['option_value'] == 'aprobada'
  833. ) {
  834. // @todo function don't exists api_is_question_manager
  835. /*if (api_is_question_manager() == false) {
  836. $form->freeze();
  837. }*/
  838. }
  839. }
  840. // Setting priority message
  841. if (isset($optionList[$defaultValueId]) &&
  842. isset($optionList[$defaultValueId]['priority'])
  843. ) {
  844. if (!empty($optionList[$defaultValueId]['priority'])) {
  845. $priorityId = $optionList[$defaultValueId]['priority'];
  846. $option = new ExtraFieldOption($this->type);
  847. $messageType = $option->getPriorityMessageType($priorityId);
  848. $form->addElement(
  849. 'label',
  850. null,
  851. Display::return_message(
  852. $optionList[$defaultValueId]['priority_message'],
  853. $messageType
  854. )
  855. );
  856. }
  857. }
  858. }
  859. if ($get_lang_variables) {
  860. $field_details['display_text'] = get_lang($field_details['display_text']);
  861. }
  862. // chzn-select doesn't work for sessions??
  863. $form->addElement(
  864. 'select',
  865. 'extra_' . $field_details['variable'],
  866. $field_details['display_text'],
  867. $options,
  868. array('id' => 'extra_'.$field_details['variable'])
  869. );
  870. /* Enable this when field_loggeable is introduced as a table field (2.0)
  871. if ($optionsExists && $field_details['field_loggeable'] && !empty($defaultValueId)) {
  872. $form->addElement(
  873. 'textarea',
  874. 'extra_' . $field_details['variable'] . '_comment',
  875. $field_details['display_text'] . ' ' . get_lang('Comment')
  876. );
  877. $extraFieldValue = new ExtraFieldValue($this->type);
  878. $repo = $app['orm.em']->getRepository($extraFieldValue->entityName);
  879. $repoLog = $app['orm.em']->getRepository('Gedmo\Loggable\Entity\LogEntry');
  880. $newEntity = $repo->findOneBy(
  881. array(
  882. $this->handlerEntityId => $itemId,
  883. 'fieldId' => $field_details['id']
  884. )
  885. );
  886. // @todo move this in a function inside the class
  887. if ($newEntity) {
  888. $logs = $repoLog->getLogEntries($newEntity);
  889. if (!empty($logs)) {
  890. $html = '<b>' . get_lang('LatestChanges') . '</b><br /><br />';
  891. $table = new HTML_Table(array('class' => 'data_table'));
  892. $table->setHeaderContents(0, 0, get_lang('Value'));
  893. $table->setHeaderContents(0, 1, get_lang('Comment'));
  894. $table->setHeaderContents(0, 2, get_lang('ModifyDate'));
  895. $table->setHeaderContents(0, 3, get_lang('Username'));
  896. $row = 1;
  897. foreach ($logs as $log) {
  898. $column = 0;
  899. $data = $log->getData();
  900. $fieldValue = isset($data['fieldValue']) ? $data['fieldValue'] : null;
  901. $comment = isset($data['comment']) ? $data['comment'] : null;
  902. $table->setCellContents($row, $column, $fieldValue);
  903. $column++;
  904. $table->setCellContents($row, $column, $comment);
  905. $column++;
  906. $table->setCellContents($row, $column, api_get_local_time($log->getLoggedAt()->format('Y-m-d H:i:s')));
  907. $column++;
  908. $table->setCellContents($row, $column, $log->getUsername());
  909. $row++;
  910. }
  911. $form->addElement('label', null, $html.$table->toHtml());
  912. }
  913. }
  914. }
  915. */
  916. if (!$admin_permissions) {
  917. if ($field_details['visible'] == 0) {
  918. $form->freeze('extra_' . $field_details['variable']);
  919. }
  920. }
  921. break;
  922. case ExtraField::FIELD_TYPE_SELECT_MULTIPLE:
  923. $options = array();
  924. foreach ($field_details['options'] as $option_id => $option_details) {
  925. $options[$option_details['option_value']] = $option_details['display_text'];
  926. }
  927. $form->addElement(
  928. 'select',
  929. 'extra_'.$field_details['variable'],
  930. $field_details['display_text'],
  931. $options,
  932. array('multiple' => 'multiple')
  933. );
  934. if (!$admin_permissions) {
  935. if ($field_details['visible'] == 0) {
  936. $form->freeze('extra_'.$field_details['variable']);
  937. }
  938. }
  939. break;
  940. case ExtraField::FIELD_TYPE_DATE:
  941. $form->addDatePicker('extra_'.$field_details['variable'], $field_details['display_text']);
  942. if (!$admin_permissions) {
  943. if ($field_details['visible'] == 0) {
  944. $form->freeze('extra_'.$field_details['variable']);
  945. }
  946. }
  947. $form->applyFilter('theme', 'trim');
  948. break;
  949. case ExtraField::FIELD_TYPE_DATETIME:
  950. $form->addDateTimePicker(
  951. 'extra_'.$field_details['variable'],
  952. $field_details['display_text']
  953. );
  954. $defaults['extra_'.$field_details['variable']] = api_get_local_time();
  955. if (!isset($form->_defaultValues['extra_'.$field_details['variable']])) {
  956. $form->setDefaults($defaults);
  957. }
  958. if (!$admin_permissions) {
  959. if ($field_details['visible'] == 0) {
  960. $form->freeze('extra_'.$field_details['variable']);
  961. }
  962. }
  963. $form->applyFilter('theme', 'trim');
  964. break;
  965. case ExtraField::FIELD_TYPE_DOUBLE_SELECT:
  966. $first_select_id = 'first_extra_'.$field_details['variable'];
  967. $url = api_get_path(WEB_AJAX_PATH).'extra_field.ajax.php?1=1';
  968. $jquery_ready_content .= '
  969. $("#'.$first_select_id.'").on("change", function() {
  970. var id = $(this).val();
  971. if (id) {
  972. $.ajax({
  973. url: "'.$url.'&a=get_second_select_options",
  974. dataType: "json",
  975. data: "type='.$type.'&field_id='.$field_details['id'].'&option_value_id="+id,
  976. success: function(data) {
  977. $("#second_extra_'.$field_details['variable'].'").empty();
  978. $.each(data, function(index, value) {
  979. $("#second_extra_'.$field_details['variable'].'").append($("<option/>", {
  980. value: index,
  981. text: value
  982. }));
  983. });
  984. },
  985. });
  986. } else {
  987. $("#second_extra_'.$field_details['variable'].'").empty();
  988. }
  989. });';
  990. $first_id = null;
  991. $second_id = null;
  992. if (!empty($extraData)) {
  993. $first_id = $extraData['extra_'.$field_details['variable']]['extra_'.$field_details['variable']];
  994. $second_id = $extraData['extra_'.$field_details['variable']]['extra_'.$field_details['variable'].'_second'];
  995. }
  996. $options = ExtraField::extra_field_double_select_convert_array_to_ordered_array(
  997. $field_details['options']
  998. );
  999. $values = array('' => get_lang('Select'));
  1000. $second_values = array();
  1001. if (!empty($options)) {
  1002. foreach ($options as $option) {
  1003. foreach ($option as $sub_option) {
  1004. if ($sub_option['option_value'] == '0') {
  1005. $values[$sub_option['id']] = $sub_option['display_text'];
  1006. } else {
  1007. if ($first_id === $sub_option['option_value']) {
  1008. $second_values[$sub_option['id']] = $sub_option['display_text'];
  1009. }
  1010. }
  1011. }
  1012. }
  1013. }
  1014. $group = array();
  1015. $group[] = $form->createElement(
  1016. 'select',
  1017. 'extra_'.$field_details['variable'],
  1018. null,
  1019. $values,
  1020. array('id' => $first_select_id)
  1021. );
  1022. $group[] = $form->createElement(
  1023. 'select',
  1024. 'extra_'.$field_details['variable'].'_second',
  1025. null,
  1026. $second_values,
  1027. array('id' => 'second_extra_'.$field_details['variable'])
  1028. );
  1029. $form->addGroup(
  1030. $group,
  1031. 'extra_'.$field_details['variable'],
  1032. $field_details['display_text'],
  1033. '&nbsp;'
  1034. );
  1035. if (!$admin_permissions) {
  1036. if ($field_details['visible'] == 0) {
  1037. $form->freeze('extra_'.$field_details['variable']);
  1038. }
  1039. }
  1040. break;
  1041. case ExtraField::FIELD_TYPE_DIVIDER:
  1042. $form->addElement(
  1043. 'static',
  1044. $field_details['variable'],
  1045. '<br /><strong>'.$field_details['display_text'].'</strong>'
  1046. );
  1047. break;
  1048. case ExtraField::FIELD_TYPE_TAG:
  1049. $variable = $field_details['variable'];
  1050. $field_id = $field_details['id'];
  1051. if ($this->type == 'user') {
  1052. /* //the magic should be here
  1053. $user_tags = UserManager::get_user_tags($user_id, $field_details[0]);
  1054. $tag_list = '';
  1055. if (is_array($user_tags) && count($user_tags) > 0) {
  1056. foreach ($user_tags as $tag) {
  1057. $tag_list .= '<option value="'.$tag['tag'].'" class="selected">'.$tag['tag'].'</option>';
  1058. }
  1059. }
  1060. $multi_select = '<select id="extra_'.$field_details[1].'" name="extra_'.$field_details[1].'">
  1061. '.$tag_list.'
  1062. </select>';
  1063. $form->addElement('label', $field_details[3], $multi_select);
  1064. $url = api_get_path(WEB_AJAX_PATH).'user_manager.ajax.php';
  1065. $complete_text = get_lang('StartToType');
  1066. //if cache is set to true the jquery will be called 1 time
  1067. $jquery_ready_content = <<<EOF
  1068. $("#extra_$field_details[1]").fcbkcomplete({
  1069. json_url: "$url?a=search_tags&field_id=$field_details[0]",
  1070. cache: false,
  1071. filter_case: true,
  1072. filter_hide: true,
  1073. complete_text:"$complete_text",
  1074. firstselected: true,
  1075. //onremove: "testme",
  1076. //onselect: "testme",
  1077. filter_selected: true,
  1078. newel: true
  1079. });
  1080. EOF;
  1081. break;*/
  1082. // The magic should be here
  1083. $user_tags = UserManager::get_user_tags($itemId, $field_details['id']);
  1084. $tag_list = '';
  1085. if (is_array($user_tags) && count($user_tags) > 0) {
  1086. foreach ($user_tags as $tag) {
  1087. $tag_list .= '<option value="'.$tag['tag'].'" class="selected">'.$tag['tag'].'</option>';
  1088. }
  1089. }
  1090. $url = api_get_path(WEB_AJAX_PATH).'user_manager.ajax.php';
  1091. } else {
  1092. $tag_list = '';
  1093. $em = Database::getManager();
  1094. $fieldTags = $em
  1095. ->getRepository('ChamiloCoreBundle:ExtraFieldRelTag')
  1096. ->findBy([
  1097. 'fieldId' => $field_id,
  1098. 'itemId' => $itemId
  1099. ]);
  1100. foreach ($fieldTags as $fieldTag) {
  1101. $tag = $em->find('ChamiloCoreBundle:Tag', $fieldTag->getTagId());
  1102. if (empty($tag)) {
  1103. continue;
  1104. }
  1105. $tag_list .= Display::tag(
  1106. 'option',
  1107. $tag->getTag(),
  1108. [
  1109. 'value' => $tag->getTag(),
  1110. 'class' => 'selected'
  1111. ]
  1112. );
  1113. }
  1114. $url = api_get_path(WEB_AJAX_PATH).'extra_field.ajax.php';
  1115. }
  1116. $form->addElement('hidden', 'extra_'.$field_details['variable'].'__persist__', 1);
  1117. $multiSelect = '<select id="extra_'.$field_details['variable'].'" name="extra_'.$field_details['variable'].'">
  1118. '.$tag_list.'
  1119. </select>';
  1120. $form->addElement('label', $field_details['display_text'], $multiSelect);
  1121. $complete_text = get_lang('StartToType');
  1122. //if cache is set to true the jquery will be called 1 time
  1123. $jquery_ready_content .= <<<EOF
  1124. $("#extra_$variable").fcbkcomplete({
  1125. json_url: "$url?a=search_tags&field_id=$field_id&type={$this->type}",
  1126. cache: false,
  1127. filter_case: true,
  1128. filter_hide: true,
  1129. complete_text:"$complete_text",
  1130. firstselected: false,
  1131. filter_selected: true,
  1132. newel: true
  1133. });
  1134. EOF;
  1135. break;
  1136. case ExtraField::FIELD_TYPE_TIMEZONE:
  1137. $form->addElement(
  1138. 'select',
  1139. 'extra_'.$field_details['variable'],
  1140. $field_details['display_text'],
  1141. api_get_timezones(),
  1142. ''
  1143. );
  1144. if ($field_details['visible'] == 0) {
  1145. $form->freeze(
  1146. 'extra_'.$field_details['variable']
  1147. );
  1148. }
  1149. break;
  1150. case ExtraField::FIELD_TYPE_SOCIAL_PROFILE:
  1151. // get the social network's favicon
  1152. $icon_path = UserManager::get_favicon_from_url(
  1153. $extraData['extra_'.$field_details['variable']],
  1154. $field_details['field_default_value']
  1155. );
  1156. // special hack for hi5
  1157. $leftpad = '1.7';
  1158. $top = '0.4';
  1159. $domain = parse_url($icon_path, PHP_URL_HOST);
  1160. if ($domain == 'www.hi5.com' or $domain == 'hi5.com') {
  1161. $leftpad = '3';
  1162. $top = '0';
  1163. }
  1164. // print the input field
  1165. $form->addElement(
  1166. 'text',
  1167. 'extra_'.$field_details['variable'],
  1168. $field_details['display_text'],
  1169. array(
  1170. 'size' => 60,
  1171. 'style' => 'background-image: url(\''.$icon_path.'\'); background-repeat: no-repeat; background-position: 0.4em '.$top.'em; padding-left: '.$leftpad.'em; '
  1172. )
  1173. );
  1174. $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes');
  1175. $form->applyFilter('extra_'.$field_details['variable'], 'trim');
  1176. if ($field_details['visible'] == 0) {
  1177. $form->freeze('extra_'.$field_details['variable']);
  1178. }
  1179. break;
  1180. case ExtraField::FIELD_TYPE_MOBILE_PHONE_NUMBER:
  1181. $form->addElement(
  1182. 'text',
  1183. 'extra_'.$field_details[1],
  1184. $field_details[3]." (".get_lang('CountryDialCode').")",
  1185. array('size' => 40, 'placeholder' => '(xx)xxxxxxxxx')
  1186. );
  1187. $form->applyFilter('extra_'.$field_details[1], 'stripslashes');
  1188. $form->applyFilter('extra_'.$field_details[1], 'trim');
  1189. $form->applyFilter('extra_'.$field_details[1], 'mobile_phone_number_filter');
  1190. $form->addRule(
  1191. 'extra_'.$field_details[1],
  1192. get_lang('MobilePhoneNumberWrong'),
  1193. 'mobile_phone_number'
  1194. );
  1195. if ($field_details['visible'] == 0) {
  1196. $form->freeze('extra_'.$field_details['variable']);
  1197. }
  1198. break;
  1199. case ExtraField::FIELD_TYPE_INTEGER:
  1200. $form->addElement(
  1201. 'number',
  1202. 'extra_'.$field_details['variable'],
  1203. $field_details['display_text'],
  1204. array('class' => 'span1', 'step' => 1)
  1205. );
  1206. $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes');
  1207. $form->applyFilter('extra_'.$field_details['variable'], 'trim');
  1208. $form->applyFilter('extra_'.$field_details['variable'], 'intval');
  1209. if (!$admin_permissions) {
  1210. if ($field_details['visible'] == 0) {
  1211. $form->freeze(
  1212. 'extra_'.$field_details['variable']
  1213. );
  1214. }
  1215. }
  1216. break;
  1217. case ExtraField::FIELD_TYPE_FILE_IMAGE:
  1218. $fieldVariable = "extra_{$field_details['variable']}";
  1219. $fieldTexts = array(
  1220. $field_details['display_text']
  1221. );
  1222. if (is_array($extraData) && array_key_exists($fieldVariable, $extraData)) {
  1223. if (file_exists(api_get_path(SYS_UPLOAD_PATH) . $extraData[$fieldVariable])) {
  1224. $fieldTexts[] = Display::img(
  1225. api_get_path(WEB_UPLOAD_PATH) . $extraData[$fieldVariable],
  1226. $field_details['display_text'],
  1227. array('width' => '300')
  1228. );
  1229. }
  1230. }
  1231. $form->addElement(
  1232. 'file',
  1233. $fieldVariable,
  1234. $fieldTexts,
  1235. array('accept' => 'image/*')
  1236. );
  1237. $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes');
  1238. $form->applyFilter('extra_'.$field_details['variable'], 'trim');
  1239. $allowed_picture_types = array ('jpg', 'jpeg', 'png', 'gif');
  1240. $form->addRule(
  1241. 'extra_'.$field_details['variable'],
  1242. get_lang('OnlyImagesAllowed') . ' ('.implode(',', $allowed_picture_types).')',
  1243. 'filetype',
  1244. $allowed_picture_types
  1245. );
  1246. if (!$admin_permissions) {
  1247. if ($field_details['visible'] == 0) {
  1248. $form->freeze(
  1249. 'extra_'.$field_details['variable']
  1250. );
  1251. }
  1252. }
  1253. break;
  1254. case ExtraField::FIELD_TYPE_FLOAT:
  1255. $form->addElement(
  1256. 'number',
  1257. 'extra_'.$field_details['variable'],
  1258. $field_details['display_text'],
  1259. array('class' => 'span1', 'step' => '0.01')
  1260. );
  1261. $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes');
  1262. $form->applyFilter('extra_'.$field_details['variable'], 'trim');
  1263. $form->applyFilter('extra_'.$field_details['variable'], 'floatval');
  1264. if (!$admin_permissions) {
  1265. if ($field_details['visible'] == 0) {
  1266. $form->freeze(
  1267. 'extra_'.$field_details['variable']
  1268. );
  1269. }
  1270. }
  1271. break;
  1272. case ExtraField::FIELD_TYPE_FILE:
  1273. $fieldVariable = "extra_{$field_details['variable']}";
  1274. $fieldTexts = array(
  1275. $field_details['display_text']
  1276. );
  1277. if (is_array($extraData) &&
  1278. array_key_exists($fieldVariable, $extraData)
  1279. ) {
  1280. if (file_exists(api_get_path(SYS_UPLOAD_PATH) . $extraData[$fieldVariable])) {
  1281. $fieldTexts[] = Display::url(
  1282. api_get_path(WEB_UPLOAD_PATH) . $extraData[$fieldVariable],
  1283. api_get_path(WEB_UPLOAD_PATH) . $extraData[$fieldVariable],
  1284. array(
  1285. 'title' => $field_details['display_text'],
  1286. 'target' => '_blank'
  1287. )
  1288. );
  1289. }
  1290. }
  1291. $form->addElement(
  1292. 'file',
  1293. $fieldVariable,
  1294. $fieldTexts,
  1295. array()
  1296. );
  1297. $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes');
  1298. $form->applyFilter('extra_'.$field_details['variable'], 'trim');
  1299. if (!$admin_permissions) {
  1300. if ($field_details['visible'] == 0) {
  1301. $form->freeze(
  1302. 'extra_'.$field_details['variable']
  1303. );
  1304. }
  1305. }
  1306. break;
  1307. case ExtraField::FIELD_TYPE_VIDEO_URL:
  1308. $form->addUrl(
  1309. "extra_{$field_details['variable']}",
  1310. $field_details['display_text'],
  1311. false,
  1312. ['placeholder' => 'https://']
  1313. );
  1314. break;
  1315. case ExtraField::FIELD_TYPE_LETTERS_ONLY:
  1316. $form->addTextLettersOnly(
  1317. "extra_{$field_details['variable']}",
  1318. $field_details['display_text']
  1319. );
  1320. $form->applyFilter('extra_' . $field_details['variable'], 'stripslashes');
  1321. if (!$admin_permissions) {
  1322. if ($field_details['visible'] == 0) {
  1323. $form->freeze(
  1324. 'extra_' . $field_details['variable']
  1325. );
  1326. }
  1327. }
  1328. break;
  1329. case ExtraField::FIELD_TYPE_ALPHANUMERIC:
  1330. $form->addTextAlphanumeric(
  1331. "extra_{$field_details['variable']}",
  1332. $field_details['display_text']
  1333. );
  1334. $form->applyFilter(
  1335. 'extra_' . $field_details['variable'],
  1336. 'stripslashes'
  1337. );
  1338. if (!$admin_permissions) {
  1339. if ($field_details['visible'] == 0) {
  1340. $form->freeze(
  1341. 'extra_' . $field_details['variable']
  1342. );
  1343. }
  1344. }
  1345. break;
  1346. case ExtraField::FIELD_TYPE_LETTERS_SPACE:
  1347. $form->addTextLettersAndSpaces(
  1348. "extra_{$field_details['variable']}",
  1349. $field_details['display_text']
  1350. );
  1351. $form->applyFilter('extra_' . $field_details['variable'], 'stripslashes');
  1352. if (!$admin_permissions) {
  1353. if ($field_details['visible'] == 0) {
  1354. $form->freeze(
  1355. 'extra_' . $field_details['variable']
  1356. );
  1357. }
  1358. }
  1359. break;
  1360. case ExtraField::FIELD_TYPE_ALPHANUMERIC_SPACE:
  1361. $form->addTextAlphanumericAndSpaces(
  1362. "extra_{$field_details['variable']}",
  1363. $field_details['display_text']
  1364. );
  1365. $form->applyFilter(
  1366. 'extra_' . $field_details['variable'],
  1367. 'stripslashes'
  1368. );
  1369. if (!$admin_permissions) {
  1370. if ($field_details['visible'] == 0) {
  1371. $form->freeze(
  1372. 'extra_' . $field_details['variable']
  1373. );
  1374. }
  1375. }
  1376. break;
  1377. }
  1378. }
  1379. }
  1380. $return = array();
  1381. $return['jquery_ready_content'] = $jquery_ready_content;
  1382. return $return;
  1383. }
  1384. function setupBreadcrumb(&$breadcrumb, $action)
  1385. {
  1386. if ($action == 'add') {
  1387. $breadcrumb[] = array('url' => $this->pageUrl, 'name' => $this->pageName);
  1388. $breadcrumb[] = array('url' => '#', 'name' => get_lang('Add'));
  1389. } elseif ($action == 'edit') {
  1390. $breadcrumb[] = array('url' => $this->pageUrl, 'name' => $this->pageName);
  1391. $breadcrumb[] = array('url' => '#', 'name' => get_lang('Edit'));
  1392. } else {
  1393. $breadcrumb[] = array('url' => '#', 'name' => $this->pageName);
  1394. }
  1395. }
  1396. /**
  1397. * Displays the title + grid
  1398. */
  1399. public function display()
  1400. {
  1401. // action links
  1402. echo '<div class="actions">';
  1403. echo '<a href="../admin/index.php">'.Display::return_icon(
  1404. 'back.png',
  1405. get_lang('BackTo').' '.get_lang('PlatformAdmin'),
  1406. '',
  1407. ICON_SIZE_MEDIUM
  1408. ).'</a>';
  1409. echo '<a href="'.api_get_self().'?action=add&type='.$this->type.'">'.Display::return_icon(
  1410. 'add_user_fields.png',
  1411. get_lang('Add'),
  1412. '',
  1413. ICON_SIZE_MEDIUM
  1414. ).'</a>';
  1415. echo '</div>';
  1416. echo Display::grid_html($this->type.'_fields');
  1417. }
  1418. /**
  1419. * @return array
  1420. */
  1421. public function getJqgridColumnNames()
  1422. {
  1423. return array(
  1424. get_lang('Name'),
  1425. get_lang('FieldLabel'),
  1426. get_lang('Type'),
  1427. get_lang('FieldChangeability'),
  1428. get_lang('Visibility'),
  1429. get_lang('Filter'),
  1430. get_lang('FieldOrder'),
  1431. get_lang('Actions')
  1432. );
  1433. }
  1434. /**
  1435. * @return array
  1436. */
  1437. public function getJqgridColumnModel()
  1438. {
  1439. return array(
  1440. array(
  1441. 'name' => 'display_text',
  1442. 'index' => 'display_text',
  1443. 'width' => '180',
  1444. 'align' => 'left',
  1445. ),
  1446. array(
  1447. 'name' => 'variable',
  1448. 'index' => 'variable',
  1449. 'width' => '',
  1450. 'align' => 'left',
  1451. 'sortable' => 'true'
  1452. ),
  1453. array(
  1454. 'name' => 'field_type',
  1455. 'index' => 'field_type',
  1456. 'width' => '',
  1457. 'align' => 'left',
  1458. 'sortable' => 'true'
  1459. ),
  1460. array(
  1461. 'name' => 'changeable',
  1462. 'index' => 'changeable',
  1463. 'width' => '50',
  1464. 'align' => 'left',
  1465. 'sortable' => 'true'
  1466. ),
  1467. array(
  1468. 'name' => 'visible',
  1469. 'index' => 'visible',
  1470. 'width' => '40',
  1471. 'align' => 'left',
  1472. 'sortable' => 'true'
  1473. ),
  1474. array(
  1475. 'name' => 'filter',
  1476. 'index' => 'filter',
  1477. 'width' => '30',
  1478. 'align' => 'left',
  1479. 'sortable' => 'true'
  1480. ),
  1481. array(
  1482. 'name' => 'field_order',
  1483. 'index' => 'field_order',
  1484. 'width' => '40',
  1485. 'align' => 'left',
  1486. 'sortable' => 'true'
  1487. ),
  1488. array(
  1489. 'name' => 'actions',
  1490. 'index' => 'actions',
  1491. 'width' => '100',
  1492. 'align' => 'left',
  1493. 'formatter' => 'action_formatter',
  1494. 'sortable' => 'false'
  1495. )
  1496. );
  1497. }
  1498. /**
  1499. * @param string $url
  1500. * @param string $action
  1501. * @return FormValidator
  1502. */
  1503. public function return_form($url, $action)
  1504. {
  1505. $form = new FormValidator($this->type.'_field', 'post', $url);
  1506. $form->addElement('hidden', 'type', $this->type);
  1507. $id = isset($_GET['id']) ? intval($_GET['id']) : null;
  1508. $form->addElement('hidden', 'id', $id);
  1509. // Setting the form elements
  1510. $header = get_lang('Add');
  1511. $defaults = array();
  1512. if ($action == 'edit') {
  1513. $header = get_lang('Modify');
  1514. // Setting the defaults
  1515. $defaults = $this->get($id);
  1516. }
  1517. $form->addElement('header', $header);
  1518. $form->addElement('text', 'display_text', get_lang('Name'), array('class' => 'span5'));
  1519. // Field type
  1520. $types = self::get_field_types();
  1521. $form->addElement(
  1522. 'select',
  1523. 'field_type',
  1524. get_lang('FieldType'),
  1525. $types,
  1526. array('id' => 'field_type', 'class' => 'chzn-select', 'data-placeholder' => get_lang('Select'))
  1527. );
  1528. $form->addElement('label', get_lang('Example'), '<div id="example">-</div>');
  1529. $form->addElement('text', 'variable', get_lang('FieldLabel'), array('class' => 'span5'));
  1530. $form->addElement(
  1531. 'text',
  1532. 'field_options',
  1533. get_lang('FieldPossibleValues'),
  1534. array('id' => 'field_options', 'class' => 'span6')
  1535. );
  1536. $fieldWithOptions = array(
  1537. ExtraField::FIELD_TYPE_RADIO,
  1538. ExtraField::FIELD_TYPE_SELECT_MULTIPLE,
  1539. ExtraField::FIELD_TYPE_SELECT,
  1540. ExtraField::FIELD_TYPE_TAG,
  1541. ExtraField::FIELD_TYPE_DOUBLE_SELECT,
  1542. );
  1543. if ($action == 'edit') {
  1544. if (in_array($defaults['field_type'], $fieldWithOptions)) {
  1545. $url = Display::url(
  1546. get_lang('EditExtraFieldOptions'),
  1547. 'extra_field_options.php?type='.$this->type.'&field_id='.$id
  1548. );
  1549. $form->addElement('label', null, $url);
  1550. if ($defaults['field_type'] == ExtraField::FIELD_TYPE_SELECT) {
  1551. $urlWorkFlow = Display::url(
  1552. get_lang('EditExtraFieldWorkFlow'),
  1553. 'extra_field_workflow.php?type='.$this->type.'&field_id='.$id
  1554. );
  1555. $form->addElement('label', null, $urlWorkFlow);
  1556. }
  1557. $form->freeze('field_options');
  1558. }
  1559. }
  1560. $form->addElement(
  1561. 'text',
  1562. 'default_value',
  1563. get_lang('FieldDefaultValue'),
  1564. array('id' => 'default_value')
  1565. );
  1566. $group = array();
  1567. $group[] = $form->createElement('radio', 'visible', null, get_lang('Yes'), 1);
  1568. $group[] = $form->createElement('radio', 'visible', null, get_lang('No'), 0);
  1569. $form->addGroup($group, '', get_lang('Visible'), '', false);
  1570. $group = array();
  1571. $group[] = $form->createElement('radio', 'changeable', null, get_lang('Yes'), 1);
  1572. $group[] = $form->createElement('radio', 'changeable', null, get_lang('No'), 0);
  1573. $form->addGroup($group, '', get_lang('FieldChangeability'), '', false);
  1574. $group = array();
  1575. $group[] = $form->createElement('radio', 'filter', null, get_lang('Yes'), 1);
  1576. $group[] = $form->createElement('radio', 'filter', null, get_lang('No'), 0);
  1577. $form->addGroup($group, '', get_lang('FieldFilter'), '', false);
  1578. /* Enable this when field_loggeable is introduced as a table field (2.0)
  1579. $group = array();
  1580. $group[] = $form->createElement('radio', 'field_loggeable', null, get_lang('Yes'), 1);
  1581. $group[] = $form->createElement('radio', 'field_loggeable', null, get_lang('No'), 0);
  1582. $form->addGroup($group, '', get_lang('FieldLoggeable'), '', false);
  1583. */
  1584. $form->addElement('text', 'field_order', get_lang('FieldOrder'), array('class' => 'span1'));
  1585. if ($action == 'edit') {
  1586. $option = new ExtraFieldOption($this->type);
  1587. if ($defaults['field_type'] == ExtraField::FIELD_TYPE_DOUBLE_SELECT) {
  1588. $form->freeze('field_options');
  1589. }
  1590. $defaults['field_options'] = $option->get_field_options_by_field_to_string($id);
  1591. $form->addButtonUpdate(get_lang('Modify'));
  1592. } else {
  1593. $defaults['visible'] = 0;
  1594. $defaults['changeable'] = 0;
  1595. $defaults['filter'] = 0;
  1596. $form->addButtonCreate(get_lang('Add'));
  1597. }
  1598. /*if (!empty($defaults['created_at'])) {
  1599. $defaults['created_at'] = api_convert_and_format_date($defaults['created_at']);
  1600. }
  1601. if (!empty($defaults['updated_at'])) {
  1602. $defaults['updated_at'] = api_convert_and_format_date($defaults['updated_at']);
  1603. }*/
  1604. $form->setDefaults($defaults);
  1605. // Setting the rules
  1606. $form->addRule('display_text', get_lang('ThisFieldIsRequired'), 'required');
  1607. $form->addRule('field_type', get_lang('ThisFieldIsRequired'), 'required');
  1608. return $form;
  1609. }
  1610. /**
  1611. * @param $token
  1612. * @return string
  1613. */
  1614. public function getJqgridActionLinks($token)
  1615. {
  1616. //With this function we can add actions to the jgrid (edit, delete, etc)
  1617. return 'function action_formatter(cellvalue, options, rowObject) {
  1618. return \'<a href="?action=edit&type='.$this->type.'&id=\'+options.rowId+\'">'.Display::return_icon(
  1619. 'edit.png',
  1620. get_lang('Edit'),
  1621. '',
  1622. ICON_SIZE_SMALL
  1623. ).'</a>'.
  1624. '&nbsp;<a onclick="javascript:if(!confirm('."\'".addslashes(
  1625. api_htmlentities(get_lang("ConfirmYourChoice"), ENT_QUOTES)
  1626. )."\'".')) return false;" href="?sec_token='.$token.'&type='.$this->type.'&action=delete&id=\'+options.rowId+\'">'.Display::return_icon(
  1627. 'delete.png',
  1628. get_lang('Delete'),
  1629. '',
  1630. ICON_SIZE_SMALL
  1631. ).'</a>'.
  1632. '\';
  1633. }';
  1634. }
  1635. /**
  1636. * @param array $columns
  1637. * @param array $column_model
  1638. * @param array $extraFields
  1639. * @return array
  1640. */
  1641. public function getRules(&$columns, &$column_model, $extraFields = array(), $checkExtraFieldExistence = false)
  1642. {
  1643. $fields = $this->get_all(
  1644. array(
  1645. 'visible = ? AND filter = ?' => array(1, 1)
  1646. ),
  1647. 'display_text'
  1648. );
  1649. $extraFieldOption = new ExtraFieldOption($this->type);
  1650. $rules = array();
  1651. if (!empty($fields)) {
  1652. foreach ($fields as $field) {
  1653. $search_options = array();
  1654. $type = 'text';
  1655. if (in_array($field['field_type'], array(self::FIELD_TYPE_SELECT, self::FIELD_TYPE_DOUBLE_SELECT))) {
  1656. $type = 'select';
  1657. $search_options['sopt'] = array('eq', 'ne'); //equal not equal
  1658. } else {
  1659. $search_options['sopt'] = array('cn', 'nc'); //contains not contains
  1660. }
  1661. $search_options['searchhidden'] = 'true';
  1662. $search_options['defaultValue'] = isset($search_options['field_default_value']) ? $search_options['field_default_value'] : null;
  1663. if ($field['field_type'] == self::FIELD_TYPE_DOUBLE_SELECT) {
  1664. //Add 2 selects
  1665. $options = $extraFieldOption->get_field_options_by_field($field['id']);
  1666. $options = self::extra_field_double_select_convert_array_to_ordered_array($options);
  1667. $first_options = array();
  1668. if (!empty($options)) {
  1669. foreach ($options as $option) {
  1670. foreach ($option as $sub_option) {
  1671. if ($sub_option['option_value'] == 0) {
  1672. $first_options[] = $sub_option['field_id'].'#'.$sub_option['id'].':'.$sub_option['display_text'];
  1673. }
  1674. }
  1675. }
  1676. }
  1677. $search_options['value'] = implode(';', $first_options);
  1678. $search_options['dataInit'] = 'fill_second_select';
  1679. // First
  1680. $column_model[] = array(
  1681. 'name' => 'extra_'.$field['variable'],
  1682. 'index' => 'extra_'.$field['variable'],
  1683. 'width' => '100',
  1684. 'hidden' => 'true',
  1685. 'search' => 'true',
  1686. 'stype' => 'select',
  1687. 'searchoptions' => $search_options,
  1688. );
  1689. $columns[] = $field['display_text'].' (1)';
  1690. $rules[] = array(
  1691. 'field' => 'extra_'.$field['variable'],
  1692. 'op' => 'cn',
  1693. );
  1694. //Second
  1695. $search_options['value'] = $field['id'].':';
  1696. $search_options['dataInit'] = 'register_second_select';
  1697. $column_model[] = array(
  1698. 'name' => 'extra_'.$field['variable'].'_second',
  1699. 'index' => 'extra_'.$field['variable'].'_second',
  1700. 'width' => '100',
  1701. 'hidden' => 'true',
  1702. 'search' => 'true',
  1703. 'stype' => 'select',
  1704. 'searchoptions' => $search_options,
  1705. );
  1706. $columns[] = $field['display_text'].' (2)';
  1707. $rules[] = array('field' => 'extra_'.$field['variable'].'_second', 'op' => 'cn');
  1708. continue;
  1709. } else {
  1710. $search_options['value'] = $extraFieldOption->getFieldOptionsToString(
  1711. $field['id'],
  1712. false,
  1713. 'display_text'
  1714. );
  1715. }
  1716. $column_model[] = array(
  1717. 'name' => 'extra_'.$field['variable'],
  1718. 'index' => 'extra_'.$field['variable'],
  1719. 'width' => '100',
  1720. 'hidden' => 'true',
  1721. 'search' => 'true',
  1722. 'stype' => $type,
  1723. 'searchoptions' => $search_options
  1724. );
  1725. $columns[] = $field['display_text'];
  1726. $rules[] = array('field' => 'extra_'.$field['variable'], 'op' => 'cn');
  1727. }
  1728. }
  1729. return $rules;
  1730. }
  1731. /**
  1732. * @param array $options
  1733. * @return array
  1734. */
  1735. public function parseConditions($options)
  1736. {
  1737. $inject_extra_fields = null;
  1738. $extraFieldOption = new ExtraFieldOption($this->type);
  1739. $double_fields = array();
  1740. if (isset($options['extra'])) {
  1741. $extra_fields = $options['extra'];
  1742. if (!empty($extra_fields)) {
  1743. $counter = 1;
  1744. foreach ($extra_fields as &$extra) {
  1745. $extra_field_obj = new ExtraField($this->type);
  1746. $extra_field_info = $extra_field_obj->get($extra['id']);
  1747. $extra['extra_field_info'] = $extra_field_info;
  1748. if (isset($extra_field_info['field_type']) && in_array(
  1749. $extra_field_info['field_type'],
  1750. array(
  1751. ExtraField::FIELD_TYPE_SELECT,
  1752. ExtraField::FIELD_TYPE_SELECT,
  1753. ExtraField::FIELD_TYPE_DOUBLE_SELECT
  1754. )
  1755. )
  1756. ) {
  1757. $inject_extra_fields .= " fvo$counter.display_text as {$extra['field']}, ";
  1758. } else {
  1759. $inject_extra_fields .= " fv$counter.value as {$extra['field']}, ";
  1760. }
  1761. if (isset($extra_fields_info[$extra['id']])) {
  1762. $info = $extra_fields_info[$extra['id']];
  1763. } else {
  1764. $info = $this->get($extra['id']);
  1765. $extra_fields_info[$extra['id']] = $info;
  1766. }
  1767. if (isset($info['field_type']) && $info['field_type'] == ExtraField::FIELD_TYPE_DOUBLE_SELECT) {
  1768. $double_fields[$info['id']] = $info;
  1769. }
  1770. $counter++;
  1771. }
  1772. }
  1773. }
  1774. $options_by_double = array();
  1775. foreach ($double_fields as $double) {
  1776. $my_options = $extraFieldOption->get_field_options_by_field(
  1777. $double['id'],
  1778. true
  1779. );
  1780. $options_by_double['extra_'.$double['variable']] = $my_options;
  1781. }
  1782. $field_value_to_join = array();
  1783. //filter can be all/any = and/or
  1784. $inject_joins = null;
  1785. $inject_where = null;
  1786. $where = null;
  1787. if (!empty($options['where'])) {
  1788. if (!empty($options['extra'])) {
  1789. // Removing double 1=1
  1790. $options['where'] = str_replace(' 1 = 1 AND', '', $options['where']);
  1791. // Always OR
  1792. $counter = 1;
  1793. foreach ($extra_fields as $extra_info) {
  1794. $extra_field_info = $extra_info['extra_field_info'];
  1795. $inject_joins .= " INNER JOIN $this->table_field_values fv$counter
  1796. ON (s.".$this->primaryKey." = fv$counter.".$this->handler_id.") ";
  1797. // Add options
  1798. if (isset($extra_field_info['field_type']) && in_array(
  1799. $extra_field_info['field_type'],
  1800. array(
  1801. ExtraField::FIELD_TYPE_SELECT,
  1802. ExtraField::FIELD_TYPE_SELECT,
  1803. ExtraField::FIELD_TYPE_DOUBLE_SELECT
  1804. )
  1805. )
  1806. ) {
  1807. $options['where'] = str_replace(
  1808. $extra_info['field'],
  1809. 'fv'.$counter.'.field_id = '.$extra_info['id'].' AND fvo'.$counter.'.option_value',
  1810. $options['where']
  1811. );
  1812. $inject_joins .= " INNER JOIN $this->table_field_options fvo$counter ".
  1813. " ON (fv$counter.field_id = fvo$counter.field_id AND fv$counter.value = fvo$counter.option_value) ";
  1814. } else {
  1815. //text, textarea, etc
  1816. $options['where'] = str_replace(
  1817. $extra_info['field'],
  1818. 'fv'.$counter.'.field_id = '.$extra_info['id'].' AND fv'.$counter.'.value',
  1819. $options['where']
  1820. );
  1821. }
  1822. $field_value_to_join[] = " fv$counter.$this->handler_id ";
  1823. $counter++;
  1824. }
  1825. if (!empty($field_value_to_join)) {
  1826. //$inject_where .= " AND s.id = ".implode(' = ', $field_value_to_join);
  1827. }
  1828. }
  1829. $where .= ' AND '.$options['where'];
  1830. }
  1831. $order = null;
  1832. if (!empty($options['order'])) {
  1833. $order = " ORDER BY ".$options['order'];
  1834. }
  1835. $limit = null;
  1836. if (!empty($options['limit'])) {
  1837. $limit = " LIMIT ".$options['limit'];
  1838. }
  1839. return array(
  1840. 'order' => $order,
  1841. 'limit' => $limit,
  1842. 'where' => $where,
  1843. 'inject_where' => $inject_where,
  1844. 'inject_joins' => $inject_joins,
  1845. 'field_value_to_join' => $field_value_to_join,
  1846. 'inject_extra_fields' => $inject_extra_fields,
  1847. );
  1848. }
  1849. //@todo move this in the display_class or somewhere else
  1850. /**
  1851. * @param $col
  1852. * @param $oper
  1853. * @param $val
  1854. * @return string
  1855. */
  1856. public function get_where_clause($col, $oper, $val)
  1857. {
  1858. if (empty($col)) {
  1859. return '';
  1860. }
  1861. if ($oper == 'bw' || $oper == 'bn') {
  1862. $val .= '%';
  1863. }
  1864. if ($oper == 'ew' || $oper == 'en') {
  1865. $val = '%'.$val;
  1866. }
  1867. if ($oper == 'cn' || $oper == 'nc' || $oper == 'in' || $oper == 'ni') {
  1868. $val = '%'.$val.'%';
  1869. }
  1870. $val = \Database::escape_string($val);
  1871. return " $col {$this->ops[$oper]} '$val' ";
  1872. }
  1873. /**
  1874. * @param $filters
  1875. * @param string $stringToSearch
  1876. * @return array
  1877. */
  1878. public function getExtraFieldRules($filters, $stringToSearch = 'extra_')
  1879. {
  1880. $extra_fields = array();
  1881. // Getting double select if exists
  1882. $double_select = array();
  1883. foreach ($filters->rules as $rule) {
  1884. if (strpos($rule->field, '_second') === false) {
  1885. } else {
  1886. $my_field = str_replace('_second', '', $rule->field);
  1887. $double_select[$my_field] = $rule->data;
  1888. }
  1889. }
  1890. $condition_array = array();
  1891. foreach ($filters->rules as $rule) {
  1892. if (strpos($rule->field, $stringToSearch) === false) {
  1893. //normal fields
  1894. $field = $rule->field;
  1895. if (isset($rule->data) && $rule->data != -1) {
  1896. $condition_array[] = $this->get_where_clause($field, $rule->op, $rule->data);
  1897. }
  1898. } else {
  1899. // Extra fields
  1900. if (strpos($rule->field, '_second') === false) {
  1901. //No _second
  1902. $original_field = str_replace($stringToSearch, '', $rule->field);
  1903. $field_option = $this->get_handler_field_info_by_variable($original_field);
  1904. if ($field_option['field_type'] == ExtraField::FIELD_TYPE_DOUBLE_SELECT) {
  1905. if (isset($double_select[$rule->field])) {
  1906. $data = explode('#', $rule->data);
  1907. $rule->data = $data[1].'::'.$double_select[$rule->field];
  1908. } else {
  1909. // only was sent 1 select
  1910. $data = explode('#', $rule->data);
  1911. $rule->data = $data[1];
  1912. }
  1913. if (!isset($rule->data)) {
  1914. $condition_array[] = ' ('.$this->get_where_clause($rule->field, $rule->op, $rule->data).') ';
  1915. $extra_fields[] = array('field' => $rule->field, 'id' => $field_option['id']);
  1916. }
  1917. } else {
  1918. if (isset($rule->data)) {
  1919. if ($rule->data == -1) {
  1920. continue;
  1921. }
  1922. $condition_array[] = ' ('.$this->get_where_clause($rule->field, $rule->op, $rule->data).') ';
  1923. $extra_fields[] = array(
  1924. 'field' => $rule->field,
  1925. 'id' => $field_option['id'],
  1926. 'data' => $rule->data
  1927. );
  1928. }
  1929. }
  1930. } else {
  1931. $my_field = str_replace('_second', '', $rule->field);
  1932. $original_field = str_replace($stringToSearch, '', $my_field);
  1933. $field_option = $this->get_handler_field_info_by_field_variable($original_field);
  1934. $extra_fields[] = array(
  1935. 'field' => $rule->field,
  1936. 'id' => $field_option['id']
  1937. );
  1938. }
  1939. }
  1940. }
  1941. return array(
  1942. 'extra_fields' => $extra_fields,
  1943. 'condition_array' => $condition_array
  1944. );
  1945. }
  1946. /**
  1947. * Get the extra fields and their formatted values
  1948. * @param int|string $itemId The item ID (It could be a session_id, course_id or user_id)
  1949. * @return array The extra fields data
  1950. */
  1951. public function getDataAndFormattedValues($itemId)
  1952. {
  1953. $valuesData = array();
  1954. $fields = $this->get_all();
  1955. foreach ($fields as $field) {
  1956. if ($field['visible'] != '1') {
  1957. continue;
  1958. }
  1959. $fieldValue = new ExtraFieldValue($this->type);
  1960. $valueData = $fieldValue->get_values_by_handler_and_field_id($itemId, $field['id'], true);
  1961. if (!$valueData) {
  1962. continue;
  1963. }
  1964. $displayedValue = get_lang('None');
  1965. switch ($field['field_type']) {
  1966. case ExtraField::FIELD_TYPE_CHECKBOX:
  1967. if ($valueData !== false && $valueData['value'] == '1') {
  1968. $displayedValue = get_lang('Yes');
  1969. } else {
  1970. $displayedValue = get_lang('No');
  1971. }
  1972. break;
  1973. case ExtraField::FIELD_TYPE_DATE:
  1974. if ($valueData !== false && !empty($valueData['value'])) {
  1975. $displayedValue = api_format_date($valueData['value'], DATE_FORMAT_LONG_NO_DAY);
  1976. }
  1977. break;
  1978. case ExtraField::FIELD_TYPE_FILE_IMAGE:
  1979. if ($valueData === false || empty($valueData['value'])) {
  1980. break;
  1981. }
  1982. if (!file_exists(api_get_path(SYS_UPLOAD_PATH) . $valueData['value'])) {
  1983. break;
  1984. }
  1985. $image = Display::img(
  1986. api_get_path(WEB_UPLOAD_PATH) . $valueData['value'],
  1987. $field['display_text'],
  1988. array('width' => '300')
  1989. );
  1990. $displayedValue = Display::url(
  1991. $image,
  1992. api_get_path(WEB_UPLOAD_PATH) . $valueData['value'],
  1993. array('target' => '_blank')
  1994. );
  1995. break;
  1996. case ExtraField::FIELD_TYPE_FILE:
  1997. if ($valueData === false || empty($valueData['value'])) {
  1998. break;
  1999. }
  2000. if (!file_exists(api_get_path(SYS_UPLOAD_PATH) . $valueData['value'])) {
  2001. break;
  2002. }
  2003. $displayedValue = Display::url(
  2004. get_lang('Download'),
  2005. api_get_path(WEB_UPLOAD_PATH) . $valueData['value'],
  2006. array(
  2007. 'title' => $field['display_text'],
  2008. 'target' => '_blank'
  2009. )
  2010. );
  2011. break;
  2012. default:
  2013. $displayedValue = $valueData['value'];
  2014. break;
  2015. }
  2016. $valuesData[] = array(
  2017. 'text' => $field['display_text'],
  2018. 'value' => $displayedValue
  2019. );
  2020. }
  2021. return $valuesData;
  2022. }
  2023. }